Merge in upstream fixes #1

Merged
hgw merged 7 commits from upstream-fixes-202312 into master 2023-12-03 02:34:33 +00:00
Showing only changes of commit e91585f9ab - Show all commits

View File

@ -1,13 +1,17 @@
import type {Database} from "sqlite3"; import type { Database } from "sqlite3";
import log from "../../log"; import log from "../../log";
import path from "path"; import path from "path";
import fs from "fs/promises"; import fs from "fs/promises";
import Config from "../../config"; import Config from "../../config";
import Msg, {Message} from "../../models/msg"; import Msg, { Message } from "../../models/msg";
import Chan, {Channel} from "../../models/chan"; import Chan, { Channel } from "../../models/chan";
import Helper from "../../helper"; import Helper from "../../helper";
import type {SearchResponse, SearchQuery, SearchableMessageStorage} from "./types"; import type {
SearchResponse,
SearchQuery,
SearchableMessageStorage,
} from "./types";
import Network from "../../models/network"; import Network from "../../models/network";
// TODO; type // TODO; type
@ -16,15 +20,21 @@ let sqlite3: any;
try { try {
sqlite3 = require("sqlite3"); sqlite3 = require("sqlite3");
} catch (e: any) { } catch (e: any) {
Config.values.messageStorage = Config.values.messageStorage.filter((item) => item !== "sqlite"); Config.values.messageStorage = Config.values.messageStorage.filter(
(item) => item !== "sqlite"
);
log.error( log.error(
"Unable to load sqlite3 module. See https://github.com/mapbox/node-sqlite3/wiki/Binaries" "Unable to load sqlite3 module. See https://github.com/mapbox/node-sqlite3/wiki/Binaries"
); );
} }
type Migration = {version: number; stmts: string[]}; type Migration = { version: number; stmts: string[] };
type Rollback = {version: number; rollback_forbidden?: boolean; stmts: string[]}; type Rollback = {
version: number;
rollback_forbidden?: boolean;
stmts: string[];
};
export const currentSchemaVersion = 1679743888000; // use `new Date().getTime()` export const currentSchemaVersion = 1679743888000; // use `new Date().getTime()`
@ -121,7 +131,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
const sqlitePath = path.join(logsPath, `${this.userName}.sqlite3`); const sqlitePath = path.join(logsPath, `${this.userName}.sqlite3`);
try { try {
await fs.mkdir(logsPath, {recursive: true}); await fs.mkdir(logsPath, { recursive: true });
} catch (e) { } catch (e) {
throw Helper.catch_to_error("Unable to create logs directory", e); throw Helper.catch_to_error("Unable to create logs directory", e);
} }
@ -152,9 +162,10 @@ class SqliteMessageStorage implements SearchableMessageStorage {
await this.serialize_run(stmt, []); await this.serialize_run(stmt, []);
} }
await this.serialize_run("INSERT INTO options (name, value) VALUES ('schema_version', ?)", [ await this.serialize_run(
currentSchemaVersion.toString(), "INSERT INTO options (name, value) VALUES ('schema_version', ?)",
]); [currentSchemaVersion.toString()]
);
} }
async current_version(): Promise<number> { async current_version(): Promise<number> {
@ -181,9 +192,10 @@ class SqliteMessageStorage implements SearchableMessageStorage {
} }
async update_version_in_db() { async update_version_in_db() {
return this.serialize_run("UPDATE options SET value = ? WHERE name = 'schema_version'", [ return this.serialize_run(
currentSchemaVersion.toString(), "UPDATE options SET value = ? WHERE name = 'schema_version'",
]); [currentSchemaVersion.toString()]
);
} }
async _run_migrations(dbVersion: number) { async _run_migrations(dbVersion: number) {
@ -274,7 +286,7 @@ class SqliteMessageStorage implements SearchableMessageStorage {
stmts: [raw.statement], stmts: [raw.statement],
}); });
} else { } else {
last.stmts.push(raw.statment); last.stmts.push(raw.statement);
} }
} }
@ -282,7 +294,10 @@ class SqliteMessageStorage implements SearchableMessageStorage {
} }
async delete_migrations_older_than(version: number) { async delete_migrations_older_than(version: number) {
return this.serialize_run("delete from migrations where migrations.version > ?", [version]); return this.serialize_run(
"delete from migrations where migrations.version > ?",
[version]
);
} }
async _downgrade_to(version: number) { async _downgrade_to(version: number) {
@ -372,7 +387,12 @@ class SqliteMessageStorage implements SearchableMessageStorage {
// id is regenerated when messages are retrieved // id is regenerated when messages are retrieved
// previews are not stored because storage is cleared on lounge restart // previews are not stored because storage is cleared on lounge restart
// type and time are stored in a separate column // type and time are stored in a separate column
if (prop !== "id" && prop !== "previews" && prop !== "type" && prop !== "time") { if (
prop !== "id" &&
prop !== "previews" &&
prop !== "type" &&
prop !== "time"
) {
newMsg[prop] = msg[prop]; newMsg[prop] = msg[prop];
} }
@ -398,10 +418,10 @@ class SqliteMessageStorage implements SearchableMessageStorage {
return; return;
} }
await this.serialize_run("DELETE FROM messages WHERE network = ? AND channel = ?", [ await this.serialize_run(
network.uuid, "DELETE FROM messages WHERE network = ? AND channel = ?",
channel.name.toLowerCase(), [network.uuid, channel.name.toLowerCase()]
]); );
} }
async getMessages( async getMessages(
@ -416,7 +436,8 @@ class SqliteMessageStorage implements SearchableMessageStorage {
} }
// If unlimited history is specified, load 100k messages // If unlimited history is specified, load 100k messages
const limit = Config.values.maxHistory < 0 ? 100000 : Config.values.maxHistory; const limit =
Config.values.maxHistory < 0 ? 100000 : Config.values.maxHistory;
const rows = await this.serialize_fetchall( const rows = await this.serialize_fetchall(
"SELECT msg, type, time FROM messages WHERE network = ? AND channel = ? ORDER BY time DESC LIMIT ?", "SELECT msg, type, time FROM messages WHERE network = ? AND channel = ? ORDER BY time DESC LIMIT ?",