pokemon-showdown/server/private-messages/database.ts
Guangcong Luo 78439b4a02
Update to ESLint 9 (#10926)
ESLint has a whole new config format, so I figure it's a good time to
make the config system saner.

- First, we no longer have separate eslint-no-types configs. Lint
  performance shouldn't be enough of a problem to justify the
  relevant maintenance complexity.

- Second, our base config should work out-of-the-box now. `npx eslint`
  will work as expected, without any CLI flags. You should still use
  `npm run lint` which adds the `--cached` flag for performance.

- Third, whatever updates I did fixed style linting, which apparently
  has been bugged for quite some time, considering all the obvious
  mixed-tabs-and-spaces issues I found in the upgrade.

Also here are some changes to our style rules. In particular:

- Curly brackets (for objects etc) now have spaces inside them. Sorry
  for the huge change. ESLint doesn't support our old style, and most
  projects use Prettier style, so we might as well match them in this way.
  See https://github.com/eslint-stylistic/eslint-stylistic/issues/415

- String + number concatenation is no longer allowed. We now
  consistently use template strings for this.
2025-02-25 20:03:46 -08:00

92 lines
3.0 KiB
TypeScript

/**
* Storage handling for offline PMs.
* By Mia.
* @author mia-pi-git
*/
import { type SQL, FS } from '../../lib';
import { MAX_PENDING } from '.';
export const statements = {
send: 'INSERT INTO offline_pms (sender, receiver, message, time) VALUES (?, ?, ?, ?)',
clear: 'DELETE FROM offline_pms WHERE receiver = ?',
fetch: 'SELECT * FROM offline_pms WHERE receiver = ?',
fetchNew: 'SELECT * FROM offline_pms WHERE receiver = ? AND seen IS NULL',
clearDated: 'DELETE FROM offline_pms WHERE ? - time >= ?',
checkSentCount: 'SELECT count(*) as count FROM offline_pms WHERE sender = ? AND receiver = ?',
setSeen: 'UPDATE offline_pms SET seen = ? WHERE receiver = ? AND seen IS NULL',
clearSeen: 'DELETE FROM offline_pms WHERE ? - seen >= ?',
getSettings: 'SELECT * FROM pm_settings WHERE userid = ?',
setBlock: 'REPLACE INTO pm_settings (userid, view_only) VALUES (?, ?)',
deleteSettings: 'DELETE FROM pm_settings WHERE userid = ?',
} as const;
type Statement = keyof typeof statements;
class StatementMap {
env: SQL.TransactionEnvironment;
constructor(env: SQL.TransactionEnvironment) {
this.env = env;
}
run(name: Statement, args: any[] | AnyObject) {
return this.getStatement(name).run(args);
}
all(name: Statement, args: any[] | AnyObject) {
return this.getStatement(name).all(args);
}
get(name: Statement, args: any[] | AnyObject) {
return this.getStatement(name).get(args);
}
getStatement(name: Statement) {
const source = statements[name];
return this.env.statements.get(source)!;
}
}
export const transactions: {
[k: string]: (args: any[], env: SQL.TransactionEnvironment) => any,
} = {
send: (args, env) => {
const statementList = new StatementMap(env);
const [sender, receiver, message] = args;
const count = statementList.get('checkSentCount', [sender, receiver])?.count;
if (count && count > MAX_PENDING) {
return { error: `You have already sent the maximum ${MAX_PENDING} offline PMs to that user.` };
}
return statementList.run('send', [sender, receiver, message, Date.now()]);
},
listNew: (args, env) => {
const list = new StatementMap(env);
const [receiver] = args;
const pms = list.all('fetchNew', [receiver]);
list.run('setSeen', [Date.now(), receiver]);
return pms;
},
};
export function onDatabaseStart(database: import('better-sqlite3').Database) {
let version;
try {
version = database.prepare('SELECT * FROM db_info').get().version;
} catch {
const schemaContent = FS('databases/schemas/pms.sql').readSync();
database.exec(schemaContent);
}
const migrations = FS('databases/migrations/pms').readdirIfExistsSync();
if (version !== migrations.length) {
for (const migration of migrations) {
const num = /(\d+)\.sql$/.exec(migration)?.[1];
if (!num || version >= num) continue;
database.exec('BEGIN TRANSACTION');
try {
database.exec(FS(`databases/migrations/pms/${migration}`).readSync());
} catch (e: any) {
console.log(`Error in PM migration ${migration} - ${e.message}`);
console.log(e.stack);
database.exec('ROLLBACK');
continue;
}
database.exec('COMMIT');
}
}
}