2022-06-19 00:25:21 +00:00
|
|
|
import type {Database} from "sqlite3";
|
|
|
|
|
|
|
|
import log from "../../log";
|
|
|
|
import path from "path";
|
|
|
|
import fs from "fs";
|
|
|
|
import Config from "../../config";
|
|
|
|
import Msg, {Message} from "../../models/msg";
|
|
|
|
import Client from "../../client";
|
|
|
|
import Chan, {Channel} from "../../models/chan";
|
|
|
|
import type {
|
|
|
|
SearchResponse,
|
|
|
|
SearchQuery,
|
|
|
|
SqliteMessageStorage as ISqliteMessageStorage,
|
|
|
|
} from "./types";
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-02 08:42:12 +00:00
|
|
|
const currentSchemaVersion = 1520239200;
|
2017-11-28 17:56:53 +00:00
|
|
|
|
|
|
|
const schema = [
|
|
|
|
// Schema version #1
|
|
|
|
"CREATE TABLE IF NOT EXISTS options (name TEXT, value TEXT, CONSTRAINT name_unique UNIQUE (name))",
|
|
|
|
"CREATE TABLE IF NOT EXISTS messages (network TEXT, channel TEXT, time INTEGER, type TEXT, msg TEXT)",
|
|
|
|
"CREATE INDEX IF NOT EXISTS network_channel ON messages (network, channel)",
|
|
|
|
"CREATE INDEX IF NOT EXISTS time ON messages (time)",
|
|
|
|
];
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
class SqliteMessageStorage implements ISqliteMessageStorage {
|
|
|
|
client: Client;
|
|
|
|
isEnabled: boolean;
|
|
|
|
database!: Database;
|
|
|
|
|
|
|
|
constructor(client: Client) {
|
2018-04-27 10:16:23 +00:00
|
|
|
this.client = client;
|
2017-11-28 17:56:53 +00:00
|
|
|
this.isEnabled = false;
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:06:08 +00:00
|
|
|
enable() {
|
2022-05-01 19:12:39 +00:00
|
|
|
const logsPath = Config.getUserLogsPath();
|
2018-04-17 08:06:08 +00:00
|
|
|
const sqlitePath = path.join(logsPath, `${this.client.name}.sqlite3`);
|
2017-11-28 17:56:53 +00:00
|
|
|
|
|
|
|
try {
|
2020-03-16 11:53:48 +00:00
|
|
|
fs.mkdirSync(logsPath, {recursive: true});
|
2022-06-19 00:25:21 +00:00
|
|
|
} catch (e: any) {
|
|
|
|
log.error("Unable to create logs directory", String(e));
|
2017-11-28 17:56:53 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isEnabled = true;
|
|
|
|
|
2018-03-10 16:49:16 +00:00
|
|
|
this.database = new sqlite3.Database(sqlitePath);
|
2022-08-29 05:47:33 +00:00
|
|
|
|
|
|
|
this.run_migrations()
|
|
|
|
}
|
|
|
|
|
|
|
|
private run_migrations() {
|
2017-11-28 17:56:53 +00:00
|
|
|
this.database.serialize(() => {
|
2022-08-27 11:41:52 +00:00
|
|
|
schema.forEach((line) => this.run(line));
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
this.database.get(
|
|
|
|
"SELECT value FROM options WHERE name = 'schema_version'",
|
|
|
|
(err, row) => {
|
|
|
|
if (err) {
|
2022-06-19 00:25:21 +00:00
|
|
|
return log.error(`Failed to retrieve schema version: ${err.toString()}`);
|
2019-07-17 09:33:59 +00:00
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
// New table
|
|
|
|
if (row === undefined) {
|
2022-08-27 11:41:52 +00:00
|
|
|
this.run(
|
|
|
|
"INSERT INTO options (name, value) VALUES ('schema_version', ?)",
|
|
|
|
currentSchemaVersion
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
const storedSchemaVersion = parseInt(row.value, 10);
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (storedSchemaVersion === currentSchemaVersion) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (storedSchemaVersion > currentSchemaVersion) {
|
|
|
|
return log.error(
|
|
|
|
`sqlite messages schema version is higher than expected (${storedSchemaVersion} > ${currentSchemaVersion}). Is The Lounge out of date?`
|
|
|
|
);
|
|
|
|
}
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
log.info(
|
|
|
|
`sqlite messages schema version is out of date (${storedSchemaVersion} < ${currentSchemaVersion}). Running migrations if any.`
|
|
|
|
);
|
2017-11-28 17:56:53 +00:00
|
|
|
|
2022-08-27 11:41:52 +00:00
|
|
|
this.run(
|
|
|
|
"UPDATE options SET value = ? WHERE name = 'schema_version'",
|
|
|
|
currentSchemaVersion
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2017-11-28 17:56:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
close(callback?: (error?: Error | null) => void) {
|
2018-03-10 16:49:16 +00:00
|
|
|
if (!this.isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-14 06:45:52 +00:00
|
|
|
this.isEnabled = false;
|
|
|
|
|
2018-03-10 16:49:16 +00:00
|
|
|
this.database.close((err) => {
|
|
|
|
if (err) {
|
2022-06-19 00:25:21 +00:00
|
|
|
log.error(`Failed to close sqlite database: ${err.message}`);
|
2018-03-10 16:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
index(network: Network, channel: Chan, msg: Msg) {
|
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-08-27 11:41:52 +00:00
|
|
|
this.run(
|
|
|
|
"INSERT INTO messages(network, channel, time, type, msg) VALUES(?, ?, ?, ?, ?)",
|
|
|
|
network.uuid,
|
|
|
|
channel.name.toLowerCase(),
|
|
|
|
msg.time.getTime(),
|
|
|
|
msg.type,
|
|
|
|
JSON.stringify(clonedMsg)
|
2019-07-17 09:33:59 +00:00
|
|
|
);
|
2017-11-28 17:56:53 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
deleteChannel(network: Network, channel: Channel) {
|
2020-01-30 08:52:29 +00:00
|
|
|
if (!this.isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-27 11:41:52 +00:00
|
|
|
this.run(
|
|
|
|
"DELETE FROM messages WHERE network = ? AND channel = ?",
|
|
|
|
network.uuid,
|
|
|
|
channel.name.toLowerCase()
|
2020-01-30 08:52:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-28 17:56:53 +00:00
|
|
|
/**
|
|
|
|
* Load messages for given channel on a given network and resolve a promise with loaded messages.
|
|
|
|
*
|
2022-08-27 09:16:52 +00:00
|
|
|
* @param network Network - Network object where the channel is
|
|
|
|
* @param channel Channel - Channel object for which to load messages for
|
2017-11-28 17:56:53 +00:00
|
|
|
*/
|
2022-08-27 12:55:35 +00:00
|
|
|
async getMessages(network: Network, channel: Channel): Promise<Message[]> {
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!this.isEnabled || Config.values.maxHistory === 0) {
|
2017-11-28 17:56:53 +00:00
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
return rows.reverse().map((row: any) => {
|
|
|
|
const msg = JSON.parse(row.msg);
|
|
|
|
msg.time = row.time;
|
|
|
|
msg.type = row.type;
|
|
|
|
|
|
|
|
const newMsg = new Msg(msg);
|
|
|
|
newMsg.id = this.client.idMsg++;
|
|
|
|
|
|
|
|
return newMsg;
|
|
|
|
}) as Message[];
|
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> {
|
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 \'@\'';
|
|
|
|
const params = [`%${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-06-19 00:25:21 +00:00
|
|
|
params.push(maxResults.toString());
|
|
|
|
query.offset = parseInt(query.offset as string, 10) || 0;
|
|
|
|
params.push(String(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);
|
|
|
|
const response: SearchResponse = {
|
|
|
|
searchTerm: query.searchTerm,
|
|
|
|
target: query.channelName,
|
|
|
|
networkUuid: query.networkUuid,
|
|
|
|
offset: query.offset as number,
|
|
|
|
results: parseSearchRowsToMessages(query.offset as number, rows).reverse(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return response;
|
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 run(stmt: string, ...params: any[]) {
|
|
|
|
this.serialize_run(stmt, params).catch((err) =>
|
|
|
|
log.error(`failed to run ${stmt}`, String(err))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private serialize_run(stmt: string, params: any[]): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.database.serialize(() => {
|
|
|
|
this.database.run(stmt, params, (err) => {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2022-08-27 12:21:29 +00:00
|
|
|
|
|
|
|
private serialize_fetchall(stmt: string, ...params: any[]): Promise<any> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.database.serialize(() => {
|
|
|
|
this.database.all(stmt, params, (err, rows) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(rows);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2022-10-10 19:43:05 +00:00
|
|
|
|
|
|
|
private serialize_get(stmt: string, ...params: any[]): Promise<any> {
|
|
|
|
const log_id = this.stmt_id();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.database.serialize(() => {
|
|
|
|
this.database.get(stmt, params, (err, row) => {
|
|
|
|
log.debug(log_id, "callback", stmt);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(row);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-11-28 17:56:53 +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
|
|
|
|
|
|
|
export default SqliteMessageStorage;
|