2022-06-19 00:25:21 +00:00
|
|
|
import type {Database} from "sqlite3";
|
|
|
|
|
|
|
|
import log from "../../log";
|
|
|
|
import path from "path";
|
2022-11-01 21:51:40 +00:00
|
|
|
import fs from "fs/promises";
|
2022-06-19 00:25:21 +00:00
|
|
|
import Config from "../../config";
|
|
|
|
import Msg, {Message} from "../../models/msg";
|
|
|
|
import Chan, {Channel} from "../../models/chan";
|
2022-11-01 21:51:40 +00:00
|
|
|
import Helper from "../../helper";
|
2022-12-30 15:06:46 +00:00
|
|
|
import type {SearchResponse, SearchQuery, SearchableMessageStorage} from "./types";
|
2022-06-19 00:25:21 +00:00
|
|
|
import Network from "../../models/network";
|
|
|
|
|
|
|
|
// TODO; type
|
|
|
|
let sqlite3: any;
|
2018-04-26 09:11:38 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
sqlite3 = require("sqlite3");
|
2022-06-19 00:25:21 +00:00
|
|
|
} catch (e: any) {
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.messageStorage = Config.values.messageStorage.filter((item) => item !== "sqlite");
|
2018-04-26 09:11:38 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
log.error(
|
2022-03-12 15:39:03 +00:00
|
|
|
"Unable to load sqlite3 module. See https://github.com/mapbox/node-sqlite3/wiki/Binaries"
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
2018-04-26 09:11:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 13:57:36 +00:00
|
|
|
type Migration = {version: number; stmts: string[]};
|
2023-03-25 17:27:50 +00:00
|
|
|
type Rollback = {version: number; rollback_forbidden?: boolean; stmts: string[]};
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2023-03-25 17:27:50 +00:00
|
|
|
export const currentSchemaVersion = 1679743888000; // use `new Date().getTime()`
|
2022-12-28 13:57:36 +00:00
|
|
|
|
|
|
|
// Desired schema, adapt to the newest version and add migrations to the array below
|
2017-11-28 17:56:53 +00:00
|
|
|
const schema = [
|
2023-03-25 17:27:50 +00:00
|
|
|
"CREATE TABLE options (name TEXT, value TEXT, CONSTRAINT name_unique UNIQUE (name))",
|
|
|
|
"CREATE TABLE messages (id INTEGER PRIMARY KEY AUTOINCREMENT, network TEXT, channel TEXT, time INTEGER, type TEXT, msg TEXT)",
|
|
|
|
`CREATE TABLE migrations (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
version INTEGER NOT NULL UNIQUE,
|
|
|
|
rollback_forbidden INTEGER DEFAULT 0 NOT NULL
|
|
|
|
)`,
|
|
|
|
`CREATE TABLE rollback_steps (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
migration_id INTEGER NOT NULL REFERENCES migrations ON DELETE CASCADE,
|
|
|
|
step INTEGER NOT NULL,
|
|
|
|
statement TEXT NOT NULL
|
|
|
|
)`,
|
|
|
|
"CREATE INDEX network_channel ON messages (network, channel)",
|
|
|
|
"CREATE INDEX time ON messages (time)",
|
2017-11-28 17:56:53 +00:00
|
|
|
];
|
|
|
|
|
2022-12-28 13:57:36 +00:00
|
|
|
// the migrations will be executed in an exclusive transaction as a whole
|
2022-12-29 13:35:27 +00:00
|
|
|
// add new migrations to the end, with the version being the new 'currentSchemaVersion'
|
2023-03-25 17:27:50 +00:00
|
|
|
// write a corresponding down migration into rollbacks
|
2022-12-29 13:35:27 +00:00
|
|
|
export const migrations: Migration[] = [
|
|
|
|
{
|
|
|
|
version: 1672236339873,
|
|
|
|
stmts: [
|
2023-03-25 17:27:50 +00:00
|
|
|
"CREATE TABLE messages_new (id INTEGER PRIMARY KEY AUTOINCREMENT, network TEXT, channel TEXT, time INTEGER, type TEXT, msg TEXT)",
|
|
|
|
"INSERT INTO messages_new(network, channel, time, type, msg) select network, channel, time, type, msg from messages order by time asc",
|
|
|
|
"DROP TABLE messages",
|
|
|
|
"ALTER TABLE messages_new RENAME TO messages",
|
|
|
|
"CREATE INDEX network_channel ON messages (network, channel)",
|
|
|
|
"CREATE INDEX time ON messages (time)",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
version: 1679743888000,
|
|
|
|
stmts: [
|
|
|
|
`CREATE TABLE IF NOT EXISTS migrations (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
version INTEGER NOT NULL UNIQUE,
|
|
|
|
rollback_forbidden INTEGER DEFAULT 0 NOT NULL
|
|
|
|
)`,
|
|
|
|
`CREATE TABLE IF NOT EXISTS rollback_steps (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
migration_id INTEGER NOT NULL REFERENCES migrations ON DELETE CASCADE,
|
|
|
|
step INTEGER NOT NULL,
|
|
|
|
statement TEXT NOT NULL
|
|
|
|
)`,
|
2022-12-29 13:35:27 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
2022-12-28 13:57:36 +00:00
|
|
|
|
2023-03-25 17:27:50 +00:00
|
|
|
// down migrations need to restore the state of the prior version.
|
|
|
|
// rollback can be disallowed by adding rollback_forbidden: true to it
|
|
|
|
export const rollbacks: Rollback[] = [
|
|
|
|
{
|
|
|
|
version: 1672236339873,
|
|
|
|
stmts: [], // changes aren't visible, left empty on purpose
|
|
|
|
},
|
|
|
|
{
|
|
|
|
version: 1679743888000,
|
|
|
|
stmts: [], // here we can't drop the tables, as we use them in the code, so just leave those in
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-11-26 12:52:27 +00:00
|
|
|
class Deferred {
|
|
|
|
resolve!: () => void;
|
|
|
|
promise: Promise<void>;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.promise = new Promise((resolve) => {
|
|
|
|
this.resolve = resolve;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 15:06:46 +00:00
|
|
|
class SqliteMessageStorage implements SearchableMessageStorage {
|
2022-06-19 00:25:21 +00:00
|
|
|
isEnabled: boolean;
|
|
|
|
database!: Database;
|
2022-11-26 12:52:27 +00:00
|
|
|
initDone: Deferred;
|
2022-12-30 15:40:12 +00:00
|
|
|
userName: string;
|
2022-06-19 00:25:21 +00:00
|
|
|
|
2022-12-30 15:40:12 +00:00
|
|
|
constructor(userName: string) {
|
|
|
|
this.userName = userName;
|
2017-11-28 17:56:53 +00:00
|
|
|
this.isEnabled = false;
|
2022-11-26 12:52:27 +00:00
|
|
|
this.initDone = new Deferred();
|
2017-11-28 17:56:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 12:52:27 +00:00
|
|
|
async _enable() {
|
2022-05-01 19:12:39 +00:00
|
|
|
const logsPath = Config.getUserLogsPath();
|
2022-12-30 15:40:12 +00:00
|
|
|
const sqlitePath = path.join(logsPath, `${this.userName}.sqlite3`);
|
2017-11-28 17:56:53 +00:00
|
|
|
|
|
|
|
try {
|
2022-11-01 21:51:40 +00:00
|
|
|
await fs.mkdir(logsPath, {recursive: true});
|
|
|
|
} catch (e) {
|
|
|
|
throw Helper.catch_to_error("Unable to create logs directory", e);
|
2017-11-28 17:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.isEnabled = true;
|
|
|
|
|
2018-03-10 16:49:16 +00:00
|
|
|
this.database = new sqlite3.Database(sqlitePath);
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
try {
|
2023-03-25 17:27:50 +00:00
|
|
|
await this.run_pragmas(); // must be done outside of a transaction
|
2022-11-01 21:51:40 +00:00
|
|
|
await this.run_migrations();
|
|
|
|
} catch (e) {
|
2022-10-10 19:43:30 +00:00
|
|
|
this.isEnabled = false;
|
2022-11-01 21:51:40 +00:00
|
|
|
throw Helper.catch_to_error("Migration failed", e);
|
|
|
|
}
|
2022-08-29 05:47:33 +00:00
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2022-11-26 12:52:27 +00:00
|
|
|
async enable() {
|
|
|
|
try {
|
|
|
|
await this._enable();
|
|
|
|
} finally {
|
|
|
|
this.initDone.resolve(); // unblock the instance methods
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 13:35:27 +00:00
|
|
|
async setup_new_db() {
|
2022-10-10 19:43:30 +00:00
|
|
|
for (const stmt of schema) {
|
|
|
|
await this.serialize_run(stmt, []);
|
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2022-12-29 13:35:27 +00:00
|
|
|
await this.serialize_run("INSERT INTO options (name, value) VALUES ('schema_version', ?)", [
|
|
|
|
currentSchemaVersion.toString(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async current_version(): Promise<number> {
|
|
|
|
const have_options = await this.serialize_get(
|
|
|
|
"select 1 from sqlite_master where type = 'table' and name = 'options'"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!have_options) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-10 19:43:30 +00:00
|
|
|
const version = await this.serialize_get(
|
|
|
|
"SELECT value FROM options WHERE name = 'schema_version'"
|
|
|
|
);
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2022-10-10 19:43:30 +00:00
|
|
|
if (version === undefined) {
|
2022-12-29 13:35:27 +00:00
|
|
|
// technically shouldn't happen, means something created a schema but didn't populate it
|
|
|
|
// we'll try our best to recover
|
|
|
|
return 0;
|
2022-10-10 19:43:30 +00:00
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2022-10-10 19:43:30 +00:00
|
|
|
const storedSchemaVersion = parseInt(version.value, 10);
|
2022-12-29 13:35:27 +00:00
|
|
|
return storedSchemaVersion;
|
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2023-03-25 17:27:50 +00:00
|
|
|
async update_version_in_db() {
|
|
|
|
return this.serialize_run("UPDATE options SET value = ? WHERE name = 'schema_version'", [
|
|
|
|
currentSchemaVersion.toString(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-12-29 13:35:27 +00:00
|
|
|
async _run_migrations(dbVersion: number) {
|
2022-10-10 19:43:30 +00:00
|
|
|
log.info(
|
2022-12-29 13:35:27 +00:00
|
|
|
`sqlite messages schema version is out of date (${dbVersion} < ${currentSchemaVersion}). Running migrations.`
|
2022-10-10 19:43:30 +00:00
|
|
|
);
|
|
|
|
|
2022-12-29 13:35:27 +00:00
|
|
|
const to_execute = necessaryMigrations(dbVersion);
|
|
|
|
|
|
|
|
for (const stmt of to_execute.map((m) => m.stmts).flat()) {
|
|
|
|
await this.serialize_run(stmt, []);
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:27:50 +00:00
|
|
|
await this.update_version_in_db();
|
|
|
|
}
|
|
|
|
|
|
|
|
async run_pragmas() {
|
|
|
|
await this.serialize_run("PRAGMA foreign_keys = ON;", []);
|
2017-11-28 17:56:53 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 13:35:27 +00:00
|
|
|
async run_migrations() {
|
|
|
|
const version = await this.current_version();
|
|
|
|
|
|
|
|
if (version > currentSchemaVersion) {
|
2023-10-09 10:28:14 +00:00
|
|
|
throw `sqlite messages schema version is higher than expected (${version} > ${currentSchemaVersion}). Is Hard Lounge out of date?`;
|
2022-12-29 13:35:27 +00:00
|
|
|
} else if (version === currentSchemaVersion) {
|
|
|
|
return; // nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.serialize_run("BEGIN EXCLUSIVE TRANSACTION", []);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (version === 0) {
|
|
|
|
await this.setup_new_db();
|
|
|
|
} else {
|
|
|
|
await this._run_migrations(version);
|
|
|
|
}
|
2023-03-25 17:27:50 +00:00
|
|
|
|
|
|
|
await this.insert_rollback_since(version);
|
2022-12-29 13:35:27 +00:00
|
|
|
} catch (err) {
|
|
|
|
await this.serialize_run("ROLLBACK", []);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.serialize_run("COMMIT", []);
|
|
|
|
await this.serialize_run("VACUUM", []);
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
async close() {
|
2018-03-10 16:49:16 +00:00
|
|
|
if (!this.isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-14 06:45:52 +00:00
|
|
|
this.isEnabled = false;
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
this.database.close((err) => {
|
|
|
|
if (err) {
|
|
|
|
reject(`Failed to close sqlite database: ${err.message}`);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-10 16:49:16 +00:00
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
resolve();
|
|
|
|
});
|
2018-03-10 16:49:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:27:50 +00:00
|
|
|
async fetch_rollbacks(since_version: number) {
|
|
|
|
const res = await this.serialize_fetchall(
|
|
|
|
`select version, rollback_forbidden, statement
|
|
|
|
from rollback_steps
|
|
|
|
join migrations on migrations.id=rollback_steps.migration_id
|
|
|
|
where version > ?
|
|
|
|
order by version desc, step asc`,
|
|
|
|
since_version
|
|
|
|
);
|
|
|
|
const result: Rollback[] = [];
|
|
|
|
|
|
|
|
// convert to Rollback[]
|
|
|
|
// requires ordering in the sql statement
|
|
|
|
for (const raw of res) {
|
|
|
|
const last = result.at(-1);
|
|
|
|
|
|
|
|
if (!last || raw.version !== last.version) {
|
|
|
|
result.push({
|
|
|
|
version: raw.version,
|
|
|
|
rollback_forbidden: Boolean(raw.rollback_forbidden),
|
|
|
|
stmts: [raw.statement],
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
last.stmts.push(raw.statment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete_migrations_older_than(version: number) {
|
|
|
|
return this.serialize_run("delete from migrations where migrations.version > ?", [version]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _downgrade_to(version: number) {
|
|
|
|
const _rollbacks = await this.fetch_rollbacks(version);
|
|
|
|
|
|
|
|
if (_rollbacks.length === 0) {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
const forbidden = _rollbacks.find((item) => item.rollback_forbidden);
|
|
|
|
|
|
|
|
if (forbidden) {
|
|
|
|
throw Error(`can't downgrade past ${forbidden.version}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const rollback of _rollbacks) {
|
|
|
|
for (const stmt of rollback.stmts) {
|
|
|
|
await this.serialize_run(stmt, []);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.delete_migrations_older_than(version);
|
|
|
|
await this.update_version_in_db();
|
|
|
|
|
|
|
|
return _rollbacks.at(-1)!.version; // assert valid due to length guard above
|
|
|
|
}
|
|
|
|
|
|
|
|
async downgrade_to(version: number) {
|
|
|
|
if (version <= 0) {
|
|
|
|
throw Error(`${version} is not a valid version to downgrade to`);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.serialize_run("BEGIN EXCLUSIVE TRANSACTION", []);
|
|
|
|
|
|
|
|
let new_version: number;
|
|
|
|
|
|
|
|
try {
|
|
|
|
new_version = await this._downgrade_to(version);
|
|
|
|
} catch (err) {
|
|
|
|
await this.serialize_run("ROLLBACK", []);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.serialize_run("COMMIT", []);
|
|
|
|
return new_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
async downgrade() {
|
|
|
|
const res = await this.downgrade_to(currentSchemaVersion);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
async insert_rollback_since(version: number) {
|
|
|
|
const missing = newRollbacks(version);
|
|
|
|
|
|
|
|
for (const rollback of missing) {
|
|
|
|
const migration = await this.serialize_get(
|
|
|
|
`insert into migrations
|
|
|
|
(version, rollback_forbidden)
|
|
|
|
values (?, ?)
|
|
|
|
returning id`,
|
|
|
|
rollback.version,
|
|
|
|
rollback.rollback_forbidden || 0
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const stmt of rollback.stmts) {
|
|
|
|
let step = 0;
|
|
|
|
await this.serialize_run(
|
|
|
|
`insert into rollback_steps
|
|
|
|
(migration_id, step, statement)
|
|
|
|
values (?, ?, ?)`,
|
|
|
|
[migration.id, step, stmt]
|
|
|
|
);
|
|
|
|
step++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
async index(network: Network, channel: Chan, msg: Msg) {
|
2022-11-26 12:52:27 +00:00
|
|
|
await this.initDone.promise;
|
|
|
|
|
2017-11-28 17:56:53 +00:00
|
|
|
if (!this.isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const clonedMsg = Object.keys(msg).reduce((newMsg, prop) => {
|
|
|
|
// id is regenerated when messages are retrieved
|
|
|
|
// previews are not stored because storage is cleared on lounge restart
|
|
|
|
// type and time are stored in a separate column
|
|
|
|
if (prop !== "id" && prop !== "previews" && prop !== "type" && prop !== "time") {
|
|
|
|
newMsg[prop] = msg[prop];
|
|
|
|
}
|
|
|
|
|
|
|
|
return newMsg;
|
|
|
|
}, {});
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
await this.serialize_run(
|
2022-08-27 11:41:52 +00:00
|
|
|
"INSERT INTO messages(network, channel, time, type, msg) VALUES(?, ?, ?, ?, ?)",
|
2022-11-01 21:51:40 +00:00
|
|
|
[
|
2019-07-17 09:33:59 +00:00
|
|
|
network.uuid,
|
|
|
|
channel.name.toLowerCase(),
|
|
|
|
msg.time.getTime(),
|
|
|
|
msg.type,
|
2022-11-01 21:51:40 +00:00
|
|
|
JSON.stringify(clonedMsg),
|
|
|
|
]
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
2017-11-28 17:56:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
async deleteChannel(network: Network, channel: Channel) {
|
2022-11-26 12:52:27 +00:00
|
|
|
await this.initDone.promise;
|
|
|
|
|
2020-01-30 08:52:29 +00:00
|
|
|
if (!this.isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
await this.serialize_run("DELETE FROM messages WHERE network = ? AND channel = ?", [
|
2022-08-27 11:41:52 +00:00
|
|
|
network.uuid,
|
2022-11-01 21:51:40 +00:00
|
|
|
channel.name.toLowerCase(),
|
|
|
|
]);
|
2020-01-30 08:52:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 15:40:12 +00:00
|
|
|
async getMessages(
|
|
|
|
network: Network,
|
|
|
|
channel: Channel,
|
|
|
|
nextID: () => number
|
|
|
|
): Promise<Message[]> {
|
2022-11-26 12:52:27 +00:00
|
|
|
await this.initDone.promise;
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!this.isEnabled || Config.values.maxHistory === 0) {
|
2022-11-01 21:51:40 +00:00
|
|
|
return [];
|
2017-11-28 17:56:53 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 11:08:05 +00:00
|
|
|
// If unlimited history is specified, load 100k messages
|
2022-05-01 19:12:39 +00:00
|
|
|
const limit = Config.values.maxHistory < 0 ? 100000 : Config.values.maxHistory;
|
2018-06-11 11:08:05 +00:00
|
|
|
|
2022-08-27 12:55:35 +00:00
|
|
|
const rows = await this.serialize_fetchall(
|
|
|
|
"SELECT msg, type, time FROM messages WHERE network = ? AND channel = ? ORDER BY time DESC LIMIT ?",
|
|
|
|
network.uuid,
|
|
|
|
channel.name.toLowerCase(),
|
|
|
|
limit
|
|
|
|
);
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
return rows.reverse().map((row: any): Message => {
|
2022-08-27 12:55:35 +00:00
|
|
|
const msg = JSON.parse(row.msg);
|
|
|
|
msg.time = row.time;
|
|
|
|
msg.type = row.type;
|
|
|
|
|
|
|
|
const newMsg = new Msg(msg);
|
2022-12-30 15:40:12 +00:00
|
|
|
newMsg.id = nextID();
|
2022-08-27 12:55:35 +00:00
|
|
|
|
|
|
|
return newMsg;
|
2022-11-01 21:51:40 +00:00
|
|
|
});
|
2017-11-28 17:56:53 +00:00
|
|
|
}
|
2018-04-17 08:06:08 +00:00
|
|
|
|
2022-08-27 13:18:22 +00:00
|
|
|
async search(query: SearchQuery): Promise<SearchResponse> {
|
2022-11-26 12:52:27 +00:00
|
|
|
await this.initDone.promise;
|
|
|
|
|
2019-12-31 16:21:34 +00:00
|
|
|
if (!this.isEnabled) {
|
2022-06-19 00:25:21 +00:00
|
|
|
// this should never be hit as messageProvider is checked in client.search()
|
2022-08-27 13:18:22 +00:00
|
|
|
throw new Error(
|
2022-08-27 09:03:49 +00:00
|
|
|
"search called but sqlite provider not enabled. This is a programming error"
|
|
|
|
);
|
2019-12-31 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 00:49:13 +00:00
|
|
|
// Using the '@' character to escape '%' and '_' in patterns.
|
|
|
|
const escapedSearchTerm = query.searchTerm.replace(/([%_@])/g, "@$1");
|
|
|
|
|
2019-12-31 16:21:34 +00:00
|
|
|
let select =
|
2022-04-12 00:49:13 +00:00
|
|
|
'SELECT msg, type, time, network, channel FROM messages WHERE type = "message" AND json_extract(msg, "$.text") LIKE ? ESCAPE \'@\'';
|
2022-11-12 21:50:56 +00:00
|
|
|
const params: any[] = [`%${escapedSearchTerm}%`];
|
2020-03-07 12:56:50 +00:00
|
|
|
|
|
|
|
if (query.networkUuid) {
|
|
|
|
select += " AND network = ? ";
|
|
|
|
params.push(query.networkUuid);
|
|
|
|
}
|
2019-12-31 16:21:34 +00:00
|
|
|
|
|
|
|
if (query.channelName) {
|
|
|
|
select += " AND channel = ? ";
|
2020-06-03 11:17:53 +00:00
|
|
|
params.push(query.channelName.toLowerCase());
|
2019-12-31 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 12:56:50 +00:00
|
|
|
const maxResults = 100;
|
|
|
|
|
|
|
|
select += " ORDER BY time DESC LIMIT ? OFFSET ? ";
|
2022-11-12 21:50:56 +00:00
|
|
|
params.push(maxResults);
|
|
|
|
params.push(query.offset);
|
2019-12-31 16:21:34 +00:00
|
|
|
|
2022-08-27 13:18:22 +00:00
|
|
|
const rows = await this.serialize_fetchall(select, ...params);
|
2022-11-26 16:14:09 +00:00
|
|
|
return {
|
|
|
|
...query,
|
2022-11-24 08:45:01 +00:00
|
|
|
results: parseSearchRowsToMessages(query.offset, rows).reverse(),
|
2022-08-27 13:18:22 +00:00
|
|
|
};
|
2019-12-31 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:06:08 +00:00
|
|
|
canProvideMessages() {
|
|
|
|
return this.isEnabled;
|
|
|
|
}
|
2022-08-27 11:41:52 +00:00
|
|
|
|
|
|
|
private serialize_run(stmt: string, params: any[]): Promise<void> {
|
2019-12-31 16:21:34 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-27 11:41:52 +00:00
|
|
|
this.database.serialize(() => {
|
|
|
|
this.database.run(stmt, params, (err) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
2019-12-31 16:21:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
private serialize_fetchall(stmt: string, ...params: any[]): Promise<any[]> {
|
2022-08-27 12:21:29 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.database.serialize(() => {
|
|
|
|
this.database.all(stmt, params, (err, rows) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(rows);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-04-17 08:06:08 +00:00
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2022-10-10 19:43:05 +00:00
|
|
|
private serialize_get(stmt: string, ...params: any[]): Promise<any> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.database.serialize(() => {
|
|
|
|
this.database.get(stmt, params, (err, row) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(row);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
}
|
2019-12-31 16:21:34 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
// TODO: type any
|
|
|
|
function parseSearchRowsToMessages(id: number, rows: any[]) {
|
|
|
|
const messages: Msg[] = [];
|
2020-03-07 12:56:50 +00:00
|
|
|
|
|
|
|
for (const row of rows) {
|
|
|
|
const msg = JSON.parse(row.msg);
|
|
|
|
msg.time = row.time;
|
|
|
|
msg.type = row.type;
|
2020-04-24 07:20:40 +00:00
|
|
|
msg.networkUuid = row.network;
|
|
|
|
msg.channelName = row.channel;
|
2020-03-07 12:56:50 +00:00
|
|
|
msg.id = id;
|
|
|
|
messages.push(new Msg(msg));
|
|
|
|
id += 1;
|
|
|
|
}
|
2019-12-31 16:21:34 +00:00
|
|
|
|
2020-03-07 12:56:50 +00:00
|
|
|
return messages;
|
2019-12-31 16:21:34 +00:00
|
|
|
}
|
2022-08-27 09:06:57 +00:00
|
|
|
|
2022-12-28 13:57:36 +00:00
|
|
|
export function necessaryMigrations(since: number): Migration[] {
|
|
|
|
return migrations.filter((m) => m.version > since);
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:27:50 +00:00
|
|
|
export function newRollbacks(since: number): Rollback[] {
|
|
|
|
return rollbacks.filter((r) => r.version > since);
|
|
|
|
}
|
|
|
|
|
2022-08-27 09:06:57 +00:00
|
|
|
export default SqliteMessageStorage;
|