@vex-chat/spire 0.7.4 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -26
- package/dist/ClientManager.d.ts +24 -25
- package/dist/ClientManager.js +232 -509
- package/dist/ClientManager.js.map +1 -0
- package/dist/Database.d.ts +49 -41
- package/dist/Database.js +698 -716
- package/dist/Database.js.map +1 -0
- package/dist/Spire.d.ts +23 -15
- package/dist/Spire.js +518 -218
- package/dist/Spire.js.map +1 -0
- package/dist/__tests__/Database.spec.js +113 -73
- package/dist/__tests__/Database.spec.js.map +1 -0
- package/dist/db/schema.d.ts +134 -0
- package/dist/db/schema.js +2 -0
- package/dist/db/schema.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -5
- package/dist/index.js.map +1 -0
- package/dist/middleware/validate.d.ts +12 -0
- package/dist/middleware/validate.js +35 -0
- package/dist/middleware/validate.js.map +1 -0
- package/dist/migrations/2026-04-06_initial-schema.d.ts +3 -0
- package/dist/migrations/2026-04-06_initial-schema.js +192 -0
- package/dist/migrations/2026-04-06_initial-schema.js.map +1 -0
- package/dist/run.js +26 -21
- package/dist/run.js.map +1 -0
- package/dist/server/avatar.d.ts +3 -4
- package/dist/server/avatar.js +85 -67
- package/dist/server/avatar.js.map +1 -0
- package/dist/server/errors.d.ts +59 -0
- package/dist/server/errors.js +94 -0
- package/dist/server/errors.js.map +1 -0
- package/dist/server/file.d.ts +3 -4
- package/dist/server/file.js +101 -61
- package/dist/server/file.js.map +1 -0
- package/dist/server/index.d.ts +9 -6
- package/dist/server/index.js +595 -70
- package/dist/server/index.js.map +1 -0
- package/dist/server/invite.d.ts +4 -5
- package/dist/server/invite.js +21 -103
- package/dist/server/invite.js.map +1 -0
- package/dist/server/openapi.d.ts +2 -0
- package/dist/server/openapi.js +40 -0
- package/dist/server/openapi.js.map +1 -0
- package/dist/server/permissions.d.ts +16 -0
- package/dist/server/permissions.js +22 -0
- package/dist/server/permissions.js.map +1 -0
- package/dist/server/rateLimit.d.ts +28 -0
- package/dist/server/rateLimit.js +58 -0
- package/dist/server/rateLimit.js.map +1 -0
- package/dist/server/user.d.ts +4 -7
- package/dist/server/user.js +66 -76
- package/dist/server/user.js.map +1 -0
- package/dist/server/utils.d.ts +35 -7
- package/dist/server/utils.js +50 -6
- package/dist/server/utils.js.map +1 -0
- package/dist/types/express.d.ts +20 -0
- package/dist/types/express.js +2 -0
- package/dist/types/express.js.map +1 -0
- package/dist/utils/createLogger.js +13 -19
- package/dist/utils/createLogger.js.map +1 -0
- package/dist/utils/createUint8UUID.js +6 -10
- package/dist/utils/createUint8UUID.js.map +1 -0
- package/dist/utils/jwtSecret.d.ts +7 -0
- package/dist/utils/jwtSecret.js +15 -0
- package/dist/utils/jwtSecret.js.map +1 -0
- package/dist/utils/loadEnv.js +7 -22
- package/dist/utils/loadEnv.js.map +1 -0
- package/dist/utils/msgpack.d.ts +2 -0
- package/dist/utils/msgpack.js +4 -0
- package/dist/utils/msgpack.js.map +1 -0
- package/package.json +91 -63
- package/src/ClientManager.ts +434 -0
- package/src/Database.ts +925 -0
- package/src/Spire.ts +878 -0
- package/src/__tests__/Database.spec.ts +167 -0
- package/src/ambient-modules.d.ts +1 -0
- package/src/db/schema.ts +165 -0
- package/src/index.ts +3 -0
- package/src/middleware/validate.ts +38 -0
- package/src/migrations/2026-04-06_initial-schema.ts +218 -0
- package/src/run.ts +37 -0
- package/src/server/avatar.ts +141 -0
- package/src/server/errors.ts +133 -0
- package/src/server/file.ts +172 -0
- package/src/server/index.ts +855 -0
- package/src/server/invite.ts +65 -0
- package/src/server/openapi.ts +51 -0
- package/src/server/permissions.ts +40 -0
- package/src/server/rateLimit.ts +86 -0
- package/src/server/user.ts +125 -0
- package/src/server/utils.ts +59 -0
- package/src/types/express.ts +23 -0
- package/src/utils/createLogger.ts +47 -0
- package/src/utils/createUint8UUID.ts +9 -0
- package/src/utils/jwtSecret.ts +16 -0
- package/src/utils/loadEnv.ts +15 -0
- package/src/utils/msgpack.ts +4 -0
- package/avatars/169d76cb-6e7c-4e24-8224-017673eed8ff +0 -0
- package/avatars/1cae8d0b-0c6b-4c73-b25c-2d2349a57122 +0 -0
- package/avatars/1d87bc2b-71fc-4818-8004-40d04093e5fc +0 -0
- package/avatars/1f2c3d62-8b4d-465a-9895-51a24caef00d +0 -0
- package/avatars/245ee7fc-1004-41ab-adab-a04c9ceb9d7a +0 -0
- package/avatars/2465c28c-bdaf-4fa2-b42a-d054f04dc39b +0 -0
- package/avatars/3900a674-a2dd-4996-a61d-8c29b3270f41 +0 -0
- package/avatars/3c3b9c77-ea50-45e7-bb25-65d6f3d2a219 +0 -0
- package/avatars/414a2ff4-ad2f-4a7d-aa27-3b09ad3522b2 +0 -0
- package/avatars/522fe504-f0ad-4ed4-9dc6-e5dc2338e531 +0 -0
- package/avatars/53e5eb29-e7d1-4d58-add9-d44f39f0cfb7 +0 -0
- package/avatars/54d3f757-1038-41c8-bfb9-efd37b6e8ebe +0 -0
- package/avatars/623e86d7-c49c-46f6-9b76-ca70c9dbc43b +0 -0
- package/avatars/66e2abae-60f5-4dd1-b9fb-297a4bedfeb0 +0 -0
- package/avatars/6f37980e-f6fa-4d6d-9206-24050b403f45 +0 -0
- package/avatars/80138ece-eb5c-4b20-817b-903d6b0f54ae +0 -0
- package/avatars/841c77d3-37c4-431b-be22-672888062874 +0 -0
- package/avatars/88051a61-2bda-4750-95a3-5fb0b4918149 +0 -0
- package/avatars/89540973-c421-4bf1-8b89-9f1eaa51c1b5 +0 -0
- package/avatars/8a802fad-8c99-4942-8f80-47cec600149c +0 -0
- package/avatars/90531d8a-907a-4a1a-ac45-c85e4acb0df9 +0 -0
- package/avatars/9b7d0da9-b8d6-4801-b128-9993f79f464a +0 -0
- package/avatars/9bc456f1-c4c4-48a1-b9e6-fd44dd744a72 +0 -0
- package/avatars/9cf878bf-7430-49ec-a47a-78f3c93793dd +0 -0
- package/avatars/9ee82847-6ad3-45e5-92b9-f474a6c54d96 +0 -0
- package/avatars/ab44c857-d81d-4c88-85db-32f9532e5376 +0 -0
- package/avatars/b396a8d2-dc14-48d2-aac4-3755dc637051 +0 -0
- package/avatars/b6ac11c5-a8b2-4e0a-995a-9c87f1e58787 +0 -0
- package/avatars/b79d6855-b738-434c-be32-809637e62b9b +0 -0
- package/avatars/bbcd0188-d6a5-48ae-90fb-be5ff30599ab +0 -0
- package/avatars/bc7a9e0e-4720-4a6e-a90d-c11fec94d380 +0 -0
- package/avatars/c1c4889f-8383-4041-8bdd-9fded4046f37 +0 -0
- package/avatars/c4c7203c-d93a-4749-ade2-17053acf1d2a +0 -0
- package/avatars/ca974a70-0a23-4668-8b80-c4304dc7f793 +0 -0
- package/avatars/cf119a0d-eb3f-4bed-905b-f14a876c3535 +0 -0
- package/avatars/d464b03d-30c2-49e3-a666-80aefa8a1b35 +0 -0
- package/avatars/da0eee89-82f0-4d45-ab48-7d2786b634c5 +0 -0
- package/avatars/de4c77a5-68e9-4bb2-b40d-d964bf377d61 +0 -0
- package/avatars/dea95395-7d0b-42aa-a9ed-40c7d4fb4c48 +0 -0
- package/avatars/edb30749-59ba-4aa2-9c52-0fb22048f4cf +0 -0
- package/avatars/f17c245a-af7d-445b-9365-49f7f54b1eeb +0 -0
- package/avatars/f1ee6a35-b262-4dbf-99f5-3d011e3b98ec +0 -0
- package/avatars/f802bdd0-345d-41f6-9184-0f30e1258fb3 +0 -0
- package/dist/migrations/20210103192527_users.d.ts +0 -3
- package/dist/migrations/20210103192527_users.js +0 -30
- package/dist/migrations/20210103193502_mail.d.ts +0 -3
- package/dist/migrations/20210103193502_mail.js +0 -35
- package/dist/migrations/20210103193525_preKeys.d.ts +0 -3
- package/dist/migrations/20210103193525_preKeys.js +0 -30
- package/dist/migrations/20210103193553_oneTimeKeys.d.ts +0 -3
- package/dist/migrations/20210103193553_oneTimeKeys.js +0 -30
- package/dist/migrations/20210103193615_servers.d.ts +0 -3
- package/dist/migrations/20210103193615_servers.js +0 -28
- package/dist/migrations/20210103193729_channels.d.ts +0 -3
- package/dist/migrations/20210103193729_channels.js +0 -28
- package/dist/migrations/20210103193749_permissions.d.ts +0 -3
- package/dist/migrations/20210103193749_permissions.js +0 -30
- package/dist/migrations/20210103193801_files.d.ts +0 -3
- package/dist/migrations/20210103193801_files.js +0 -28
- package/files/00ea7368-45a7-4f6a-a199-974da14be1a0 +0 -0
- package/files/039503d2-a170-4962-b921-c97994ba64ff +0 -0
- package/files/14b6fa02-4cbb-40df-be4f-a07187cb619e +0 -0
- package/files/15c04cb1-dc6a-4f19-aa6f-4a3b92d05bf7 +0 -0
- package/files/2a8d411c-8b92-4532-b84d-d64c638d6293 +0 -0
- package/files/37de2cd3-08a8-4044-9c37-e13386765f3d +0 -0
- package/files/42452029-284e-4c81-9f18-feb6ce309eed +0 -0
- package/files/43d09f2f-29c8-415f-8c4a-23f2a32eb79e +0 -0
- package/files/52992923-33ab-44a1-8118-605e9b4856a7 +0 -0
- package/files/53180681-36e2-49c0-8382-94dca0da09bf +0 -0
- package/files/5a56cd7b-1d04-4619-b60f-1e5515b9164a +0 -0
- package/files/5ced7676-20c4-4219-a4f2-70a25eb7eea8 +0 -0
- package/files/60e787ff-8ec5-444d-b963-0aaf5313b53e +0 -0
- package/files/67a17729-fcb7-4339-9499-1fc08fea72ca +0 -0
- package/files/68d09565-908d-4a67-8f09-e183f8708eb4 +0 -0
- package/files/70e587c5-56e8-47f0-bd36-691efcb0cc2e +0 -4
- package/files/7a227619-b715-4e2c-a79d-b2158d56a799 +0 -0
- package/files/7e3cc3ea-b706-4835-994d-65d33acaf369 +0 -0
- package/files/816f72a0-65dc-40c1-8862-1d22065cca3c +0 -0
- package/files/8bf84972-5086-4631-a752-093d7b1a098b +0 -0
- package/files/8c46e3bc-3f2e-441d-b8cc-17428fc3d219 +0 -0
- package/files/8eb83364-8826-4eee-895a-ba5cd3ab85e9 +0 -0
- package/files/8faefaea-14e3-49e4-ac74-9710622bfae9 +0 -0
- package/files/91af08c2-9dca-41f4-b6c6-b7bf33505df9 +0 -0
- package/files/9349dffa-35dd-49a0-a651-10b438038f95 +0 -0
- package/files/b0a12a41-6283-4f27-b2c8-66774991d96c +0 -0
- package/files/b28afb08-d18b-48a8-93c3-6a59c66d8fee +0 -0
- package/files/be60fe01-1578-4363-a908-41500092b77d +0 -0
- package/files/cf7b90e3-734a-4453-9ff3-9a331404b5ef +0 -0
- package/files/d1be30aa-8ff4-41c1-b360-f65922029047 +0 -0
- package/files/d2d31a57-a413-443c-9b97-fa9ca394ef0b +0 -0
- package/files/d357f223-b786-478d-a113-b53cb62acdab +0 -0
- package/files/d4a69ee0-05c4-4ff0-8753-624cdd0f24e9 +0 -0
- package/files/d57f411b-1874-4f00-a6b0-2f86de6b229d +0 -0
- package/files/d85aaa33-7f70-4a70-b3d2-cad667beb38c +0 -0
- package/files/db2fc236-dbe0-4d24-bfaf-884829fd090a +0 -0
- package/files/df2e20ec-6dcc-4402-94ee-7d1163f84197 +0 -0
- package/files/e0880ab5-6d49-4dc0-a5ec-0d3793a8a7b8 +0 -0
- package/files/e59ee9c6-5aea-48ea-b3d9-94a324dc2260 +0 -0
- package/files/e6d7aad6-4220-4ce7-85f8-89d0b1f0cc89 +0 -0
- package/files/f0bfc98c-3534-459d-931a-7c6e77bde153 +0 -0
- package/files/f8443740-050c-4714-9bf6-334783ae6ffd +0 -0
- package/files/fc405ae7-f9fb-455d-ac8c-0ca0d88d4115 +0 -0
- package/jest.config.js +0 -13
- package/spire.sqlite +0 -0
package/dist/Database.js
CHANGED
|
@@ -1,372 +1,424 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
})
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
32
8
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
9
|
+
import { EventEmitter } from "events";
|
|
10
|
+
import { pbkdf2Sync } from "node:crypto";
|
|
11
|
+
import * as fs from "node:fs/promises";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
14
|
+
import { xMakeNonce, XUtils } from "@vex-chat/crypto";
|
|
15
|
+
import { MailType } from "@vex-chat/types";
|
|
16
|
+
/**
|
|
17
|
+
* Narrow a plain integer from the `mailType` SQL column to the
|
|
18
|
+
* `MailType` union (0 = initial, 1 = subsequent). Throws if the
|
|
19
|
+
* database contains an unexpected value, catching row corruption
|
|
20
|
+
* at read time instead of propagating an invalid literal into
|
|
21
|
+
* application code.
|
|
22
|
+
*/
|
|
23
|
+
function parseMailType(n) {
|
|
24
|
+
if (n === MailType.initial)
|
|
25
|
+
return MailType.initial;
|
|
26
|
+
if (n === MailType.subsequent)
|
|
27
|
+
return MailType.subsequent;
|
|
28
|
+
throw new Error(`Invalid mailType in database row: ${String(n)}`);
|
|
29
|
+
}
|
|
30
|
+
import BetterSqlite3 from "better-sqlite3";
|
|
31
|
+
import { Kysely, Migrator, sql, SqliteDialect } from "kysely";
|
|
32
|
+
import { stringify as uuidStringify, validate as uuidValidate } from "uuid";
|
|
33
|
+
import { createLogger } from "./utils/createLogger.js";
|
|
34
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
35
|
+
const migrationFolder = path.join(__dirname, "migrations");
|
|
36
|
+
/**
|
|
37
|
+
* Cross-platform Kysely migration provider.
|
|
38
|
+
*
|
|
39
|
+
* Replaces Kysely's built-in `FileMigrationProvider`, which on Windows
|
|
40
|
+
* fails with `ERR_UNSUPPORTED_ESM_URL_SCHEME` because it does
|
|
41
|
+
* `await import(joinedPath)` where `joinedPath` is a Windows absolute
|
|
42
|
+
* path like `D:\\spire\\src\\migrations\\schema.ts`. Node's ESM loader
|
|
43
|
+
* requires `file://` URLs for absolute paths on Windows.
|
|
44
|
+
*
|
|
45
|
+
* This implementation uses `pathToFileURL` to convert each migration
|
|
46
|
+
* file's absolute path to a `file://` URL before passing it to
|
|
47
|
+
* `import()`. Works on Linux, macOS, and Windows. Filters out `.d.ts`
|
|
48
|
+
* declaration files and accepts both `.ts` and `.js` source files for
|
|
49
|
+
* spire's `--experimental-strip-types` runtime.
|
|
50
|
+
*/
|
|
51
|
+
class CrossPlatformMigrationProvider {
|
|
52
|
+
folder;
|
|
53
|
+
constructor(folder) {
|
|
54
|
+
this.folder = folder;
|
|
55
|
+
}
|
|
56
|
+
async getMigrations() {
|
|
57
|
+
const files = await fs.readdir(this.folder);
|
|
58
|
+
const migrations = {};
|
|
59
|
+
for (const file of files.sort()) {
|
|
60
|
+
if (file.endsWith(".d.ts"))
|
|
61
|
+
continue;
|
|
62
|
+
if (!file.endsWith(".ts") && !file.endsWith(".js"))
|
|
63
|
+
continue;
|
|
64
|
+
const fullPath = path.join(this.folder, file);
|
|
65
|
+
const fileUrl = pathToFileURL(fullPath).href;
|
|
66
|
+
const mod = await import(__rewriteRelativeImportExtension(fileUrl));
|
|
67
|
+
if (!isMigration(mod)) {
|
|
68
|
+
throw new Error(`Invalid migration ${file}: expected an exported \`up\` function`);
|
|
69
|
+
}
|
|
70
|
+
const name = file.replace(/\.(ts|js)$/, "");
|
|
71
|
+
migrations[name] = mod;
|
|
72
|
+
}
|
|
73
|
+
return migrations;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function isMigration(mod) {
|
|
77
|
+
return (typeof mod === "object" &&
|
|
78
|
+
mod !== null &&
|
|
79
|
+
"up" in mod &&
|
|
80
|
+
typeof mod.up === "function");
|
|
81
|
+
}
|
|
41
82
|
const pubkeyRegex = /[0-9a-f]{64}/;
|
|
42
|
-
|
|
43
|
-
|
|
83
|
+
export const ITERATIONS = 1000;
|
|
84
|
+
// ── Row-to-interface converters ─────────────────────────────────────────
|
|
85
|
+
// SQLite stores booleans as integers and dates as strings, but the
|
|
86
|
+
// @vex-chat/types interfaces expect boolean / Date.
|
|
87
|
+
export class Database extends EventEmitter {
|
|
88
|
+
db;
|
|
89
|
+
log;
|
|
44
90
|
constructor(options) {
|
|
45
91
|
super();
|
|
46
|
-
this.log =
|
|
47
|
-
|
|
92
|
+
this.log = createLogger("spire-db", options?.logLevel || "error");
|
|
93
|
+
const dbType = options?.dbType || "sqlite3";
|
|
94
|
+
let filename;
|
|
95
|
+
switch (dbType) {
|
|
96
|
+
case "sqlite":
|
|
48
97
|
case "sqlite3":
|
|
49
|
-
|
|
50
|
-
client: "sqlite3",
|
|
51
|
-
connection: {
|
|
52
|
-
filename: "spire.sqlite",
|
|
53
|
-
},
|
|
54
|
-
useNullAsDefault: true,
|
|
55
|
-
});
|
|
98
|
+
filename = "spire.sqlite";
|
|
56
99
|
break;
|
|
57
100
|
case "sqlite3mem":
|
|
58
|
-
|
|
59
|
-
client: "sqlite3",
|
|
60
|
-
connection: {
|
|
61
|
-
filename: ":memory:",
|
|
62
|
-
},
|
|
63
|
-
useNullAsDefault: true,
|
|
64
|
-
});
|
|
101
|
+
filename = ":memory:";
|
|
65
102
|
break;
|
|
66
|
-
case "mysql":
|
|
67
103
|
default:
|
|
68
|
-
|
|
69
|
-
client: "mysql",
|
|
70
|
-
connection: {
|
|
71
|
-
host: process.env.SQL_HOST,
|
|
72
|
-
user: process.env.SQL_USER,
|
|
73
|
-
password: process.env.SQL_PASSWORD,
|
|
74
|
-
database: process.env.SQL_DB_NAME,
|
|
75
|
-
},
|
|
76
|
-
});
|
|
104
|
+
filename = "spire.sqlite";
|
|
77
105
|
break;
|
|
78
106
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
107
|
+
const sqliteDb = new BetterSqlite3(filename);
|
|
108
|
+
sqliteDb.pragma("journal_mode = WAL");
|
|
109
|
+
sqliteDb.pragma("synchronous = NORMAL");
|
|
110
|
+
sqliteDb.pragma("busy_timeout = 5000");
|
|
111
|
+
sqliteDb.pragma("cache_size = -64000");
|
|
112
|
+
sqliteDb.pragma("temp_store = memory");
|
|
113
|
+
sqliteDb.pragma("foreign_keys = ON");
|
|
114
|
+
this.db = new Kysely({
|
|
115
|
+
dialect: new SqliteDialect({ database: sqliteDb }),
|
|
116
|
+
});
|
|
117
|
+
void this.init();
|
|
118
|
+
}
|
|
119
|
+
async close() {
|
|
120
|
+
this.log.info("Closing database.");
|
|
121
|
+
await this.db.destroy();
|
|
122
|
+
}
|
|
123
|
+
async createChannel(name, serverID) {
|
|
124
|
+
const channel = {
|
|
125
|
+
channelID: crypto.randomUUID(),
|
|
126
|
+
name,
|
|
127
|
+
serverID,
|
|
128
|
+
};
|
|
129
|
+
await this.db.insertInto("channels").values(channel).execute();
|
|
130
|
+
return channel;
|
|
131
|
+
}
|
|
132
|
+
async createDevice(owner, payload) {
|
|
133
|
+
const device = {
|
|
134
|
+
deleted: 0,
|
|
135
|
+
deviceID: crypto.randomUUID(),
|
|
136
|
+
lastLogin: new Date().toISOString(),
|
|
137
|
+
name: payload.deviceName,
|
|
138
|
+
owner,
|
|
139
|
+
signKey: payload.signKey,
|
|
140
|
+
};
|
|
141
|
+
await this.db.insertInto("devices").values(device).execute();
|
|
142
|
+
const medPreKeys = {
|
|
143
|
+
deviceID: device.deviceID,
|
|
144
|
+
index: payload.preKeyIndex,
|
|
145
|
+
keyID: crypto.randomUUID(),
|
|
146
|
+
publicKey: payload.preKey,
|
|
147
|
+
signature: payload.preKeySignature,
|
|
148
|
+
userID: owner,
|
|
149
|
+
};
|
|
150
|
+
await this.db.insertInto("preKeys").values(medPreKeys).execute();
|
|
151
|
+
return toDevice(device);
|
|
152
|
+
}
|
|
153
|
+
async createEmoji(emoji) {
|
|
154
|
+
await this.db.insertInto("emojis").values(emoji).execute();
|
|
155
|
+
}
|
|
156
|
+
async createFile(file) {
|
|
157
|
+
await this.db.insertInto("files").values(file).execute();
|
|
158
|
+
}
|
|
159
|
+
async createInvite(inviteID, serverID, ownerID, expiration) {
|
|
160
|
+
const invite = {
|
|
161
|
+
expiration,
|
|
162
|
+
inviteID,
|
|
163
|
+
owner: ownerID,
|
|
164
|
+
serverID,
|
|
165
|
+
};
|
|
166
|
+
await this.db.insertInto("invites").values(invite).execute();
|
|
167
|
+
return invite;
|
|
168
|
+
}
|
|
169
|
+
async createPermission(userID, resourceType, resourceID, powerLevel) {
|
|
170
|
+
const permissionID = crypto.randomUUID();
|
|
171
|
+
// check if it already exists
|
|
172
|
+
const checkPermission = await this.db
|
|
173
|
+
.selectFrom("permissions")
|
|
174
|
+
.selectAll()
|
|
175
|
+
.where("userID", "=", userID)
|
|
176
|
+
.where("resourceID", "=", resourceID)
|
|
177
|
+
.execute();
|
|
178
|
+
const existing = checkPermission[0];
|
|
179
|
+
if (existing) {
|
|
180
|
+
return existing;
|
|
181
|
+
}
|
|
182
|
+
const permission = {
|
|
183
|
+
permissionID,
|
|
184
|
+
powerLevel,
|
|
185
|
+
resourceID,
|
|
186
|
+
resourceType,
|
|
187
|
+
userID,
|
|
188
|
+
};
|
|
189
|
+
await this.db.insertInto("permissions").values(permission).execute();
|
|
190
|
+
return permission;
|
|
191
|
+
}
|
|
192
|
+
async createServer(name, ownerID) {
|
|
193
|
+
// create the server
|
|
194
|
+
const server = {
|
|
195
|
+
name,
|
|
196
|
+
serverID: crypto.randomUUID(),
|
|
197
|
+
};
|
|
198
|
+
await this.db
|
|
199
|
+
.insertInto("servers")
|
|
200
|
+
.values({
|
|
201
|
+
icon: server.icon ?? null,
|
|
202
|
+
name: server.name,
|
|
203
|
+
serverID: server.serverID,
|
|
204
|
+
})
|
|
205
|
+
.execute();
|
|
206
|
+
// create the admin permission
|
|
207
|
+
await this.createPermission(ownerID, "server", server.serverID, 100);
|
|
208
|
+
// create the general channel
|
|
209
|
+
await this.createChannel("general", server.serverID);
|
|
210
|
+
return server;
|
|
211
|
+
}
|
|
212
|
+
async createUser(regKey, regPayload) {
|
|
213
|
+
try {
|
|
214
|
+
const salt = xMakeNonce();
|
|
215
|
+
const passwordHash = hashPassword(regPayload.password, salt);
|
|
216
|
+
const user = {
|
|
217
|
+
lastSeen: new Date().toISOString(),
|
|
218
|
+
passwordHash: passwordHash.toString("hex"),
|
|
219
|
+
passwordSalt: XUtils.encodeHex(salt),
|
|
220
|
+
userID: uuidStringify(regKey),
|
|
221
|
+
username: regPayload.username,
|
|
164
222
|
};
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
223
|
+
await this.db
|
|
224
|
+
.insertInto("users")
|
|
225
|
+
.values({
|
|
226
|
+
...user,
|
|
227
|
+
lastSeen: user.lastSeen,
|
|
228
|
+
})
|
|
229
|
+
.execute();
|
|
230
|
+
await this.createDevice(user.userID, regPayload);
|
|
231
|
+
return [user, null];
|
|
232
|
+
}
|
|
233
|
+
catch (err) {
|
|
234
|
+
return [null, err instanceof Error ? err : new Error(String(err))];
|
|
235
|
+
}
|
|
176
236
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
237
|
+
async deleteChannel(channelID) {
|
|
238
|
+
await this.deletePermissions(channelID);
|
|
239
|
+
await this.db
|
|
240
|
+
.deleteFrom("mail")
|
|
241
|
+
.where("group", "=", channelID)
|
|
242
|
+
.execute();
|
|
243
|
+
await this.db
|
|
244
|
+
.deleteFrom("channels")
|
|
245
|
+
.where("channelID", "=", channelID)
|
|
246
|
+
.execute();
|
|
247
|
+
}
|
|
248
|
+
async deleteDevice(deviceID) {
|
|
249
|
+
await this.db
|
|
250
|
+
.deleteFrom("preKeys")
|
|
251
|
+
.where("deviceID", "=", deviceID)
|
|
252
|
+
.execute();
|
|
253
|
+
await this.db
|
|
254
|
+
.deleteFrom("oneTimeKeys")
|
|
255
|
+
.where("deviceID", "=", deviceID)
|
|
256
|
+
.execute();
|
|
257
|
+
await this.db
|
|
258
|
+
.updateTable("devices")
|
|
259
|
+
.set({ deleted: 1 })
|
|
260
|
+
.where("deviceID", "=", deviceID)
|
|
261
|
+
.execute();
|
|
262
|
+
}
|
|
263
|
+
async deleteEmoji(emojiID) {
|
|
264
|
+
await this.db
|
|
265
|
+
.deleteFrom("emojis")
|
|
266
|
+
.where("emojiID", "=", emojiID)
|
|
267
|
+
.execute();
|
|
268
|
+
}
|
|
269
|
+
async deleteInvite(inviteID) {
|
|
270
|
+
await this.db
|
|
271
|
+
.deleteFrom("invites")
|
|
272
|
+
.where("inviteID", "=", inviteID)
|
|
273
|
+
.execute();
|
|
274
|
+
}
|
|
275
|
+
async deleteMail(nonce, userID) {
|
|
276
|
+
await this.db
|
|
277
|
+
.deleteFrom("mail")
|
|
278
|
+
.where("nonce", "=", XUtils.encodeHex(nonce))
|
|
279
|
+
.where("recipient", "=", userID)
|
|
280
|
+
.execute();
|
|
281
|
+
}
|
|
282
|
+
async deletePermission(permissionID) {
|
|
283
|
+
await this.db
|
|
284
|
+
.deleteFrom("permissions")
|
|
285
|
+
.where("permissionID", "=", permissionID)
|
|
286
|
+
.execute();
|
|
287
|
+
}
|
|
288
|
+
async deletePermissions(resourceID) {
|
|
289
|
+
await this.db
|
|
290
|
+
.deleteFrom("permissions")
|
|
291
|
+
.where("resourceID", "=", resourceID)
|
|
292
|
+
.execute();
|
|
293
|
+
}
|
|
294
|
+
async deleteServer(serverID) {
|
|
295
|
+
await this.deletePermissions(serverID);
|
|
296
|
+
const channels = await this.retrieveChannels(serverID);
|
|
297
|
+
for (const channel of channels) {
|
|
298
|
+
await this.deleteChannel(channel.channelID);
|
|
299
|
+
}
|
|
300
|
+
await this.db
|
|
301
|
+
.deleteFrom("servers")
|
|
302
|
+
.where("serverID", "=", serverID)
|
|
303
|
+
.execute();
|
|
304
|
+
}
|
|
305
|
+
async getKeyBundle(deviceID) {
|
|
306
|
+
const device = await this.retrieveDevice(deviceID);
|
|
307
|
+
if (!device) {
|
|
308
|
+
throw new Error("DeviceID not found.");
|
|
309
|
+
}
|
|
310
|
+
const otk = (await this.getOTK(deviceID)) || undefined;
|
|
311
|
+
const preKey = await this.getPreKeys(deviceID);
|
|
312
|
+
if (!preKey) {
|
|
313
|
+
throw new Error("Failed to get prekey.");
|
|
314
|
+
}
|
|
315
|
+
const keyBundle = {
|
|
316
|
+
otk,
|
|
317
|
+
preKey,
|
|
318
|
+
signKey: XUtils.decodeHex(device.signKey),
|
|
319
|
+
};
|
|
320
|
+
return keyBundle;
|
|
321
|
+
}
|
|
322
|
+
async getOTK(deviceID) {
|
|
323
|
+
const rows = await this.db
|
|
324
|
+
.selectFrom("oneTimeKeys")
|
|
325
|
+
.selectAll()
|
|
326
|
+
.where("deviceID", "=", deviceID)
|
|
327
|
+
.orderBy("index")
|
|
328
|
+
.limit(1)
|
|
329
|
+
.execute();
|
|
330
|
+
const otkInfo = rows[0];
|
|
331
|
+
if (!otkInfo) {
|
|
201
332
|
return null;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
createPermission(userID, resourceType, resourceID, powerLevel) {
|
|
258
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
259
|
-
const permissionID = uuid.v4();
|
|
260
|
-
// check if it already exists
|
|
261
|
-
const checkPermission = yield this.db
|
|
262
|
-
.from("permissions")
|
|
263
|
-
.select()
|
|
264
|
-
.where({ userID, resourceID });
|
|
265
|
-
if (checkPermission.length > 0) {
|
|
266
|
-
return checkPermission[0];
|
|
267
|
-
}
|
|
268
|
-
const permission = {
|
|
269
|
-
permissionID,
|
|
270
|
-
userID,
|
|
271
|
-
resourceType,
|
|
272
|
-
resourceID,
|
|
273
|
-
powerLevel,
|
|
274
|
-
};
|
|
275
|
-
yield this.db("permissions").insert(permission);
|
|
276
|
-
return permission;
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
retrieveInvite(inviteID) {
|
|
280
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
281
|
-
const rows = yield this.db
|
|
282
|
-
.from("invites")
|
|
283
|
-
.select()
|
|
284
|
-
.where({ inviteID });
|
|
285
|
-
if (rows.length === 0) {
|
|
286
|
-
return null;
|
|
287
|
-
}
|
|
288
|
-
return rows[0];
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
createInvite(inviteID, serverID, ownerID, expiration) {
|
|
292
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
293
|
-
const invite = {
|
|
294
|
-
inviteID,
|
|
295
|
-
serverID,
|
|
296
|
-
owner: ownerID,
|
|
297
|
-
expiration,
|
|
298
|
-
};
|
|
299
|
-
yield this.db("invites").insert(invite);
|
|
300
|
-
return invite;
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
retrieveGroupMembers(channelID) {
|
|
304
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
305
|
-
const channel = yield this.retrieveChannel(channelID);
|
|
306
|
-
if (!channel) {
|
|
307
|
-
return [];
|
|
308
|
-
}
|
|
309
|
-
const permissions = yield this.db
|
|
310
|
-
.from("permissions")
|
|
311
|
-
.select()
|
|
312
|
-
.where({ resourceID: channel.serverID });
|
|
313
|
-
const groupMembers = [];
|
|
314
|
-
for (const permission of permissions) {
|
|
315
|
-
const user = yield this.retrieveUser(permission.userID);
|
|
316
|
-
if (user) {
|
|
317
|
-
groupMembers.push(user);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
return groupMembers;
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
retrieveChannel(channelID) {
|
|
324
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
325
|
-
const channels = yield this.db
|
|
326
|
-
.from("channels")
|
|
327
|
-
.select()
|
|
328
|
-
.where({ channelID })
|
|
329
|
-
.limit(1);
|
|
330
|
-
if (channels.length === 0) {
|
|
331
|
-
return null;
|
|
332
|
-
}
|
|
333
|
-
return channels[0];
|
|
334
|
-
});
|
|
333
|
+
}
|
|
334
|
+
const otk = {
|
|
335
|
+
deviceID: otkInfo.deviceID,
|
|
336
|
+
index: otkInfo.index,
|
|
337
|
+
publicKey: XUtils.decodeHex(otkInfo.publicKey),
|
|
338
|
+
signature: XUtils.decodeHex(otkInfo.signature),
|
|
339
|
+
};
|
|
340
|
+
// delete the otk
|
|
341
|
+
await this.db
|
|
342
|
+
.deleteFrom("oneTimeKeys")
|
|
343
|
+
.where("deviceID", "=", deviceID)
|
|
344
|
+
.where("index", "=", otk.index)
|
|
345
|
+
.execute();
|
|
346
|
+
return otk;
|
|
347
|
+
}
|
|
348
|
+
async getOTKCount(deviceID) {
|
|
349
|
+
const result = await this.db
|
|
350
|
+
.selectFrom("oneTimeKeys")
|
|
351
|
+
.select((eb) => eb.fn.countAll().as("count"))
|
|
352
|
+
.where("deviceID", "=", deviceID)
|
|
353
|
+
.executeTakeFirst();
|
|
354
|
+
return Number(result?.count ?? 0);
|
|
355
|
+
}
|
|
356
|
+
async getPreKeys(deviceID) {
|
|
357
|
+
const rows = await this.db
|
|
358
|
+
.selectFrom("preKeys")
|
|
359
|
+
.selectAll()
|
|
360
|
+
.where("deviceID", "=", deviceID)
|
|
361
|
+
.execute();
|
|
362
|
+
const preKeyInfo = rows[0];
|
|
363
|
+
if (!preKeyInfo) {
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
const preKey = {
|
|
367
|
+
deviceID: preKeyInfo.deviceID,
|
|
368
|
+
index: preKeyInfo.index,
|
|
369
|
+
publicKey: XUtils.decodeHex(preKeyInfo.publicKey),
|
|
370
|
+
signature: XUtils.decodeHex(preKeyInfo.signature),
|
|
371
|
+
};
|
|
372
|
+
return preKey;
|
|
373
|
+
}
|
|
374
|
+
async getRequestsTotal() {
|
|
375
|
+
const row = await this.db
|
|
376
|
+
.selectFrom("service_metrics")
|
|
377
|
+
.select("metric_value")
|
|
378
|
+
.where("metric_key", "=", "requests_total")
|
|
379
|
+
.executeTakeFirst();
|
|
380
|
+
const raw = row?.metric_value;
|
|
381
|
+
const count = Number(raw);
|
|
382
|
+
if (!Number.isFinite(count) || count < 0) {
|
|
383
|
+
return 0;
|
|
384
|
+
}
|
|
385
|
+
return count;
|
|
335
386
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
387
|
+
async incrementRequestsTotal(by = 1) {
|
|
388
|
+
if (!Number.isFinite(by) || by <= 0) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
await this.db
|
|
392
|
+
.updateTable("service_metrics")
|
|
393
|
+
.set({
|
|
394
|
+
metric_value: sql `metric_value + ${Math.floor(by)}`,
|
|
395
|
+
})
|
|
396
|
+
.where("metric_key", "=", "requests_total")
|
|
397
|
+
.execute();
|
|
398
|
+
}
|
|
399
|
+
async isHealthy() {
|
|
400
|
+
try {
|
|
401
|
+
await sql `select 1 as ok`.execute(this.db);
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
catch (err) {
|
|
405
|
+
this.log.warn("Database health check failed: " + String(err));
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
344
408
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
};
|
|
352
|
-
yield this.db("channels").insert(channel);
|
|
353
|
-
return channel;
|
|
354
|
-
});
|
|
409
|
+
async markDeviceLogin(device) {
|
|
410
|
+
await this.db
|
|
411
|
+
.updateTable("devices")
|
|
412
|
+
.set({ lastLogin: new Date().toISOString() })
|
|
413
|
+
.where("deviceID", "=", device.deviceID)
|
|
414
|
+
.execute();
|
|
355
415
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
};
|
|
363
|
-
yield this.db("servers").insert(server);
|
|
364
|
-
// create the admin permission
|
|
365
|
-
yield this.createPermission(ownerID, "server", server.serverID, 100);
|
|
366
|
-
// create the general channel
|
|
367
|
-
yield this.createChannel("general", server.serverID);
|
|
368
|
-
return server;
|
|
369
|
-
});
|
|
416
|
+
async markUserSeen(user) {
|
|
417
|
+
await this.db
|
|
418
|
+
.updateTable("users")
|
|
419
|
+
.set({ lastSeen: new Date().toISOString() })
|
|
420
|
+
.where("userID", "=", user.userID)
|
|
421
|
+
.execute();
|
|
370
422
|
}
|
|
371
423
|
/**
|
|
372
424
|
* Retrives a list of users that should be notified when a specific resourceID
|
|
@@ -374,379 +426,309 @@ class Database extends events_1.EventEmitter {
|
|
|
374
426
|
*
|
|
375
427
|
* @param resourceID
|
|
376
428
|
*/
|
|
377
|
-
retrieveAffectedUsers(resourceID) {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
users.push(user);
|
|
385
|
-
}
|
|
429
|
+
async retrieveAffectedUsers(resourceID) {
|
|
430
|
+
const permissionList = await this.retrievePermissionsByResourceID(resourceID);
|
|
431
|
+
const users = [];
|
|
432
|
+
for (const permission of permissionList) {
|
|
433
|
+
const user = await this.retrieveUser(permission.userID);
|
|
434
|
+
if (user) {
|
|
435
|
+
users.push(user);
|
|
386
436
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
.
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
.
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
}
|
|
469
|
-
deleteServer(serverID) {
|
|
470
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
471
|
-
yield this.deletePermissions(serverID);
|
|
472
|
-
const channels = yield this.retrieveChannels(serverID);
|
|
473
|
-
for (const channel of channels) {
|
|
474
|
-
yield this.deleteChannel(channel.channelID);
|
|
475
|
-
}
|
|
476
|
-
yield this.db
|
|
477
|
-
.from("servers")
|
|
478
|
-
.where({ serverID })
|
|
479
|
-
.delete();
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
retrieveServers(userID) {
|
|
483
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
484
|
-
const serverPerms = yield this.retrievePermissions(userID, "server");
|
|
485
|
-
if (!serverPerms) {
|
|
486
|
-
return [];
|
|
487
|
-
}
|
|
488
|
-
const serverList = [];
|
|
489
|
-
for (const perm of serverPerms) {
|
|
490
|
-
const server = yield this.retrieveServer(perm.resourceID);
|
|
491
|
-
if (server) {
|
|
492
|
-
serverList.push(server);
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
return serverList;
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
createUser(regKey, regPayload) {
|
|
499
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
500
|
-
try {
|
|
501
|
-
const salt = crypto_1.xMakeNonce();
|
|
502
|
-
const passwordHash = exports.hashPassword(regPayload.password, salt);
|
|
503
|
-
const user = {
|
|
504
|
-
userID: uuid.stringify(regKey),
|
|
505
|
-
username: regPayload.username,
|
|
506
|
-
lastSeen: new Date(Date.now()),
|
|
507
|
-
passwordHash: passwordHash.toString("hex"),
|
|
508
|
-
passwordSalt: crypto_1.XUtils.encodeHex(salt),
|
|
509
|
-
};
|
|
510
|
-
yield this.db("users").insert(user);
|
|
511
|
-
const device = yield this.createDevice(user.userID, regPayload);
|
|
512
|
-
const medPreKeys = {
|
|
513
|
-
keyID: uuid.v4(),
|
|
514
|
-
userID: user.userID,
|
|
515
|
-
deviceID: device.deviceID,
|
|
516
|
-
publicKey: regPayload.preKey,
|
|
517
|
-
signature: regPayload.preKeySignature,
|
|
518
|
-
index: regPayload.preKeyIndex,
|
|
519
|
-
};
|
|
520
|
-
yield this.db("preKeys").insert(medPreKeys);
|
|
521
|
-
return [user, null];
|
|
522
|
-
}
|
|
523
|
-
catch (err) {
|
|
524
|
-
return [null, err];
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
}
|
|
528
|
-
createFile(file) {
|
|
529
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
530
|
-
return this.db("files").insert(file);
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
retrieveFile(fileID) {
|
|
534
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
535
|
-
const file = yield this.db
|
|
536
|
-
.from("files")
|
|
537
|
-
.select()
|
|
538
|
-
.where({ fileID });
|
|
539
|
-
if (file.length === 0) {
|
|
540
|
-
return null;
|
|
541
|
-
}
|
|
542
|
-
return file[0];
|
|
543
|
-
});
|
|
544
|
-
}
|
|
545
|
-
// the identifier can be username, public key, or userID
|
|
546
|
-
retrieveUser(userIdentifier) {
|
|
547
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
548
|
-
let rows = [];
|
|
549
|
-
if (uuid.validate(userIdentifier)) {
|
|
550
|
-
rows = yield this.db
|
|
551
|
-
.from("users")
|
|
552
|
-
.select()
|
|
553
|
-
.where({ userID: userIdentifier })
|
|
554
|
-
.limit(1);
|
|
555
|
-
}
|
|
556
|
-
else {
|
|
557
|
-
rows = yield this.db
|
|
558
|
-
.from("users")
|
|
559
|
-
.select()
|
|
560
|
-
.where({ username: userIdentifier })
|
|
561
|
-
.limit(1);
|
|
562
|
-
}
|
|
563
|
-
if (rows.length === 0) {
|
|
564
|
-
return null;
|
|
437
|
+
}
|
|
438
|
+
return users;
|
|
439
|
+
}
|
|
440
|
+
async retrieveChannel(channelID) {
|
|
441
|
+
const channels = await this.db
|
|
442
|
+
.selectFrom("channels")
|
|
443
|
+
.selectAll()
|
|
444
|
+
.where("channelID", "=", channelID)
|
|
445
|
+
.limit(1)
|
|
446
|
+
.execute();
|
|
447
|
+
return channels[0] ?? null;
|
|
448
|
+
}
|
|
449
|
+
async retrieveChannels(serverID) {
|
|
450
|
+
const channels = await this.db
|
|
451
|
+
.selectFrom("channels")
|
|
452
|
+
.selectAll()
|
|
453
|
+
.where("serverID", "=", serverID)
|
|
454
|
+
.execute();
|
|
455
|
+
return channels;
|
|
456
|
+
}
|
|
457
|
+
async retrieveDevice(deviceID) {
|
|
458
|
+
if (uuidValidate(deviceID)) {
|
|
459
|
+
const rows = await this.db
|
|
460
|
+
.selectFrom("devices")
|
|
461
|
+
.selectAll()
|
|
462
|
+
.where("deviceID", "=", deviceID)
|
|
463
|
+
.where("deleted", "=", 0)
|
|
464
|
+
.execute();
|
|
465
|
+
const device = rows[0];
|
|
466
|
+
return device ? toDevice(device) : null;
|
|
467
|
+
}
|
|
468
|
+
if (pubkeyRegex.test(deviceID)) {
|
|
469
|
+
const rows = await this.db
|
|
470
|
+
.selectFrom("devices")
|
|
471
|
+
.selectAll()
|
|
472
|
+
.where("signKey", "=", deviceID)
|
|
473
|
+
.where("deleted", "=", 0)
|
|
474
|
+
.execute();
|
|
475
|
+
const device = rows[0];
|
|
476
|
+
return device ? toDevice(device) : null;
|
|
477
|
+
}
|
|
478
|
+
return null;
|
|
479
|
+
}
|
|
480
|
+
async retrieveEmoji(emojiID) {
|
|
481
|
+
const rows = await this.db
|
|
482
|
+
.selectFrom("emojis")
|
|
483
|
+
.selectAll()
|
|
484
|
+
.where("emojiID", "=", emojiID)
|
|
485
|
+
.execute();
|
|
486
|
+
return rows[0] ?? null;
|
|
487
|
+
}
|
|
488
|
+
async retrieveEmojiList(userID) {
|
|
489
|
+
return this.db
|
|
490
|
+
.selectFrom("emojis")
|
|
491
|
+
.selectAll()
|
|
492
|
+
.where("owner", "=", userID)
|
|
493
|
+
.execute();
|
|
494
|
+
}
|
|
495
|
+
async retrieveFile(fileID) {
|
|
496
|
+
const file = await this.db
|
|
497
|
+
.selectFrom("files")
|
|
498
|
+
.selectAll()
|
|
499
|
+
.where("fileID", "=", fileID)
|
|
500
|
+
.execute();
|
|
501
|
+
return file[0] ?? null;
|
|
502
|
+
}
|
|
503
|
+
async retrieveGroupMembers(channelID) {
|
|
504
|
+
const channel = await this.retrieveChannel(channelID);
|
|
505
|
+
if (!channel) {
|
|
506
|
+
return [];
|
|
507
|
+
}
|
|
508
|
+
const permissions = await this.db
|
|
509
|
+
.selectFrom("permissions")
|
|
510
|
+
.selectAll()
|
|
511
|
+
.where("resourceID", "=", channel.serverID)
|
|
512
|
+
.execute();
|
|
513
|
+
const groupMembers = [];
|
|
514
|
+
for (const permission of permissions) {
|
|
515
|
+
const user = await this.retrieveUser(permission.userID);
|
|
516
|
+
if (user) {
|
|
517
|
+
groupMembers.push(user);
|
|
565
518
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
519
|
+
}
|
|
520
|
+
return groupMembers;
|
|
521
|
+
}
|
|
522
|
+
async retrieveInvite(inviteID) {
|
|
523
|
+
const rows = await this.db
|
|
524
|
+
.selectFrom("invites")
|
|
525
|
+
.selectAll()
|
|
526
|
+
.where("inviteID", "=", inviteID)
|
|
527
|
+
.execute();
|
|
528
|
+
return rows[0] ?? null;
|
|
529
|
+
}
|
|
530
|
+
async retrieveMail(deviceID) {
|
|
531
|
+
const rawRows = await this.db
|
|
532
|
+
.selectFrom("mail")
|
|
533
|
+
.selectAll()
|
|
534
|
+
.where("recipient", "=", deviceID)
|
|
535
|
+
.execute();
|
|
536
|
+
const rows = rawRows.map(toMailSQL);
|
|
537
|
+
const fixMail = (mail) => {
|
|
538
|
+
const msgb = {
|
|
539
|
+
authorID: mail.authorID,
|
|
540
|
+
cipher: XUtils.decodeHex(mail.cipher),
|
|
541
|
+
extra: XUtils.decodeHex(mail.extra),
|
|
542
|
+
forward: mail.forward,
|
|
543
|
+
group: mail.group ? XUtils.decodeHex(mail.group) : null,
|
|
573
544
|
mailID: mail.mailID,
|
|
574
545
|
mailType: mail.mailType,
|
|
575
|
-
|
|
576
|
-
sender: deviceID,
|
|
577
|
-
cipher: crypto_1.XUtils.encodeHex(mail.cipher),
|
|
578
|
-
nonce: crypto_1.XUtils.encodeHex(mail.nonce),
|
|
579
|
-
extra: crypto_1.XUtils.encodeHex(mail.extra),
|
|
580
|
-
header: crypto_1.XUtils.encodeHex(header),
|
|
581
|
-
time: new Date(Date.now()),
|
|
582
|
-
group: mail.group ? crypto_1.XUtils.encodeHex(mail.group) : null,
|
|
583
|
-
forward: mail.forward,
|
|
584
|
-
authorID: userID,
|
|
546
|
+
nonce: XUtils.decodeHex(mail.nonce),
|
|
585
547
|
readerID: mail.readerID,
|
|
548
|
+
recipient: mail.recipient,
|
|
549
|
+
sender: mail.sender,
|
|
586
550
|
};
|
|
587
|
-
|
|
588
|
-
|
|
551
|
+
const msgh = XUtils.decodeHex(mail.header);
|
|
552
|
+
return [msgh, msgb, mail.time];
|
|
553
|
+
};
|
|
554
|
+
const allMail = rows.map(fixMail);
|
|
555
|
+
return allMail;
|
|
556
|
+
}
|
|
557
|
+
async retrievePermission(permissionID) {
|
|
558
|
+
const rows = await this.db
|
|
559
|
+
.selectFrom("permissions")
|
|
560
|
+
.selectAll()
|
|
561
|
+
.where("permissionID", "=", permissionID)
|
|
562
|
+
.execute();
|
|
563
|
+
return rows[0] ?? null;
|
|
564
|
+
}
|
|
565
|
+
async retrievePermissions(userID, resourceType) {
|
|
566
|
+
if (resourceType === "all") {
|
|
567
|
+
const sList = await this.db
|
|
568
|
+
.selectFrom("permissions")
|
|
569
|
+
.selectAll()
|
|
570
|
+
.where("userID", "=", userID)
|
|
571
|
+
.execute();
|
|
572
|
+
return sList;
|
|
573
|
+
}
|
|
574
|
+
const serverList = await this.db
|
|
575
|
+
.selectFrom("permissions")
|
|
576
|
+
.selectAll()
|
|
577
|
+
.where("userID", "=", userID)
|
|
578
|
+
.where("resourceType", "=", resourceType)
|
|
579
|
+
.execute();
|
|
580
|
+
return serverList;
|
|
581
|
+
}
|
|
582
|
+
async retrievePermissionsByResourceID(resourceID) {
|
|
583
|
+
return this.db
|
|
584
|
+
.selectFrom("permissions")
|
|
585
|
+
.selectAll()
|
|
586
|
+
.where("resourceID", "=", resourceID)
|
|
587
|
+
.execute();
|
|
588
|
+
}
|
|
589
|
+
async retrieveServer(serverID) {
|
|
590
|
+
const rows = await this.db
|
|
591
|
+
.selectFrom("servers")
|
|
592
|
+
.selectAll()
|
|
593
|
+
.where("serverID", "=", serverID)
|
|
594
|
+
.limit(1)
|
|
595
|
+
.execute();
|
|
596
|
+
const row = rows[0];
|
|
597
|
+
return row ? toServer(row) : null;
|
|
598
|
+
}
|
|
599
|
+
async retrieveServerInvites(serverID) {
|
|
600
|
+
const rows = await this.db
|
|
601
|
+
.selectFrom("invites")
|
|
602
|
+
.selectAll()
|
|
603
|
+
.where("serverID", "=", serverID)
|
|
604
|
+
.execute();
|
|
605
|
+
return rows.filter((invite) => {
|
|
606
|
+
const valid = new Date(Date.now()).getTime() <
|
|
607
|
+
new Date(invite.expiration).getTime();
|
|
608
|
+
if (!valid) {
|
|
609
|
+
void this.deleteInvite(invite.inviteID);
|
|
610
|
+
}
|
|
611
|
+
return valid;
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
async retrieveServers(userID) {
|
|
615
|
+
const serverPerms = await this.retrievePermissions(userID, "server");
|
|
616
|
+
const serverList = [];
|
|
617
|
+
for (const perm of serverPerms) {
|
|
618
|
+
const server = await this.retrieveServer(perm.resourceID);
|
|
619
|
+
if (server) {
|
|
620
|
+
serverList.push(server);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return serverList;
|
|
589
624
|
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
.
|
|
596
|
-
.
|
|
597
|
-
.where(
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
625
|
+
// the identifier can be username, public key, or userID
|
|
626
|
+
async retrieveUser(userIdentifier) {
|
|
627
|
+
let rows;
|
|
628
|
+
if (uuidValidate(userIdentifier)) {
|
|
629
|
+
rows = await this.db
|
|
630
|
+
.selectFrom("users")
|
|
631
|
+
.selectAll()
|
|
632
|
+
.where("userID", "=", userIdentifier)
|
|
633
|
+
.limit(1)
|
|
634
|
+
.execute();
|
|
635
|
+
}
|
|
636
|
+
else {
|
|
637
|
+
rows = await this.db
|
|
638
|
+
.selectFrom("users")
|
|
639
|
+
.selectAll()
|
|
640
|
+
.where("username", "=", userIdentifier)
|
|
641
|
+
.limit(1)
|
|
642
|
+
.execute();
|
|
643
|
+
}
|
|
644
|
+
const row = rows[0];
|
|
645
|
+
return row ? toUserRecord(row) : null;
|
|
646
|
+
}
|
|
647
|
+
async retrieveUserDeviceList(userIDs) {
|
|
648
|
+
const rows = await this.db
|
|
649
|
+
.selectFrom("devices")
|
|
650
|
+
.selectAll()
|
|
651
|
+
.where("owner", "in", userIDs)
|
|
652
|
+
.where("deleted", "=", 0)
|
|
653
|
+
.execute();
|
|
654
|
+
return rows.map(toDevice);
|
|
655
|
+
}
|
|
656
|
+
async retrieveUsers() {
|
|
657
|
+
const rows = await this.db.selectFrom("users").selectAll().execute();
|
|
658
|
+
return rows.map(toUserRecord);
|
|
659
|
+
}
|
|
660
|
+
async saveMail(mail, header, deviceID, userID) {
|
|
661
|
+
const entry = {
|
|
662
|
+
authorID: userID,
|
|
663
|
+
cipher: XUtils.encodeHex(mail.cipher),
|
|
664
|
+
extra: XUtils.encodeHex(mail.extra),
|
|
665
|
+
forward: mail.forward,
|
|
666
|
+
group: mail.group ? XUtils.encodeHex(mail.group) : null,
|
|
667
|
+
header: XUtils.encodeHex(header),
|
|
668
|
+
mailID: mail.mailID,
|
|
669
|
+
mailType: mail.mailType,
|
|
670
|
+
nonce: XUtils.encodeHex(mail.nonce),
|
|
671
|
+
readerID: mail.readerID,
|
|
672
|
+
recipient: mail.recipient,
|
|
673
|
+
sender: deviceID,
|
|
674
|
+
time: new Date().toISOString(),
|
|
675
|
+
};
|
|
676
|
+
await this.db
|
|
677
|
+
.insertInto("mail")
|
|
678
|
+
.values({
|
|
679
|
+
...entry,
|
|
680
|
+
forward: entry.forward ? 1 : 0,
|
|
681
|
+
time: entry.time,
|
|
682
|
+
})
|
|
683
|
+
.execute();
|
|
684
|
+
}
|
|
685
|
+
async saveOTK(userID, deviceID, otks) {
|
|
686
|
+
for (const otk of otks) {
|
|
687
|
+
const newOTK = {
|
|
688
|
+
deviceID: otk.deviceID,
|
|
689
|
+
index: otk.index ?? 0,
|
|
690
|
+
keyID: crypto.randomUUID(),
|
|
691
|
+
publicKey: XUtils.encodeHex(otk.publicKey),
|
|
692
|
+
signature: XUtils.encodeHex(otk.signature),
|
|
693
|
+
userID,
|
|
614
694
|
};
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
});
|
|
618
|
-
}
|
|
619
|
-
deleteMail(nonce, userID) {
|
|
620
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
621
|
-
yield this.db
|
|
622
|
-
.from("mail")
|
|
623
|
-
.delete()
|
|
624
|
-
.where({ nonce: crypto_1.XUtils.encodeHex(nonce), recipient: userID });
|
|
625
|
-
});
|
|
626
|
-
}
|
|
627
|
-
markUserSeen(user) {
|
|
628
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
629
|
-
yield this.db("users")
|
|
630
|
-
.where({ userID: user.userID })
|
|
631
|
-
.update({
|
|
632
|
-
lastSeen: new Date(Date.now()),
|
|
633
|
-
});
|
|
634
|
-
});
|
|
635
|
-
}
|
|
636
|
-
markDeviceLogin(device) {
|
|
637
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
638
|
-
yield this.db("users")
|
|
639
|
-
.where({ deviceID: device.deviceID })
|
|
640
|
-
.update({
|
|
641
|
-
lastLogin: new Date(Date.now()),
|
|
642
|
-
});
|
|
643
|
-
});
|
|
695
|
+
await this.db.insertInto("oneTimeKeys").values(newOTK).execute();
|
|
696
|
+
}
|
|
644
697
|
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
this.
|
|
648
|
-
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
init() {
|
|
652
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
653
|
-
if (!(yield this.db.schema.hasTable("invites"))) {
|
|
654
|
-
yield this.db.schema.createTable("invites", (table) => {
|
|
655
|
-
table.string("inviteID").primary();
|
|
656
|
-
table.string("serverID").index();
|
|
657
|
-
table.string("owner");
|
|
658
|
-
table.string("expiration");
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
if (!(yield this.db.schema.hasTable("users"))) {
|
|
662
|
-
yield this.db.schema.createTable("users", (table) => {
|
|
663
|
-
table.string("userID").primary();
|
|
664
|
-
table.string("username").unique();
|
|
665
|
-
table.string("passwordHash");
|
|
666
|
-
table.string("passwordSalt");
|
|
667
|
-
table.dateTime("lastSeen");
|
|
668
|
-
});
|
|
669
|
-
}
|
|
670
|
-
if (!(yield this.db.schema.hasTable("devices"))) {
|
|
671
|
-
yield this.db.schema.createTable("devices", (table) => {
|
|
672
|
-
table.string("deviceID").primary();
|
|
673
|
-
table.string("signKey").unique();
|
|
674
|
-
table.string("owner");
|
|
675
|
-
table.string("name");
|
|
676
|
-
table.string("lastLogin");
|
|
677
|
-
});
|
|
678
|
-
}
|
|
679
|
-
if (!(yield this.db.schema.hasTable("mail"))) {
|
|
680
|
-
yield this.db.schema.createTable("mail", (table) => {
|
|
681
|
-
table.string("nonce").primary();
|
|
682
|
-
table.string("recipient").index();
|
|
683
|
-
table.string("mailID");
|
|
684
|
-
table.string("sender");
|
|
685
|
-
table.string("header");
|
|
686
|
-
table.text("cipher", "mediumtext");
|
|
687
|
-
table.string("group");
|
|
688
|
-
table.text("extra");
|
|
689
|
-
table.integer("mailType");
|
|
690
|
-
table.dateTime("time");
|
|
691
|
-
table.boolean("forward");
|
|
692
|
-
table.string("authorID");
|
|
693
|
-
table.string("readerID");
|
|
694
|
-
});
|
|
695
|
-
}
|
|
696
|
-
if (!(yield this.db.schema.hasTable("preKeys"))) {
|
|
697
|
-
yield this.db.schema.createTable("preKeys", (table) => {
|
|
698
|
-
table.string("keyID").primary();
|
|
699
|
-
table.string("userID").index();
|
|
700
|
-
table.string("deviceID").index();
|
|
701
|
-
table.string("publicKey");
|
|
702
|
-
table.string("signature");
|
|
703
|
-
table.integer("index");
|
|
704
|
-
});
|
|
705
|
-
}
|
|
706
|
-
if (!(yield this.db.schema.hasTable("oneTimeKeys"))) {
|
|
707
|
-
yield this.db.schema.createTable("oneTimeKeys", (table) => {
|
|
708
|
-
table.string("keyID").primary();
|
|
709
|
-
table.string("userID").index();
|
|
710
|
-
table.string("deviceID").index();
|
|
711
|
-
table.string("publicKey");
|
|
712
|
-
table.string("signature");
|
|
713
|
-
table.integer("index");
|
|
714
|
-
});
|
|
715
|
-
}
|
|
716
|
-
if (!(yield this.db.schema.hasTable("servers"))) {
|
|
717
|
-
yield this.db.schema.createTable("servers", (table) => {
|
|
718
|
-
table.string("serverID").primary();
|
|
719
|
-
table.string("name");
|
|
720
|
-
table.string("icon");
|
|
721
|
-
});
|
|
722
|
-
}
|
|
723
|
-
if (!(yield this.db.schema.hasTable("channels"))) {
|
|
724
|
-
yield this.db.schema.createTable("channels", (table) => {
|
|
725
|
-
table.string("channelID").primary();
|
|
726
|
-
table.string("serverID");
|
|
727
|
-
table.string("name");
|
|
728
|
-
});
|
|
729
|
-
}
|
|
730
|
-
if (!(yield this.db.schema.hasTable("permissions"))) {
|
|
731
|
-
yield this.db.schema.createTable("permissions", (table) => {
|
|
732
|
-
table.string("permissionID").primary();
|
|
733
|
-
table.string("userID").index();
|
|
734
|
-
table.string("resourceType");
|
|
735
|
-
table.string("resourceID").index();
|
|
736
|
-
table.integer("powerLevel");
|
|
737
|
-
});
|
|
738
|
-
}
|
|
739
|
-
if (!(yield this.db.schema.hasTable("files"))) {
|
|
740
|
-
yield this.db.schema.createTable("files", (table) => {
|
|
741
|
-
table.string("fileID").primary();
|
|
742
|
-
table.string("owner").index();
|
|
743
|
-
table.string("nonce");
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
this.emit("ready");
|
|
698
|
+
async init() {
|
|
699
|
+
const migrator = new Migrator({
|
|
700
|
+
db: this.db,
|
|
701
|
+
provider: new CrossPlatformMigrationProvider(migrationFolder),
|
|
747
702
|
});
|
|
703
|
+
const { error } = await migrator.migrateToLatest();
|
|
704
|
+
if (error) {
|
|
705
|
+
this.emit("error", error);
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
this.emit("ready");
|
|
748
709
|
}
|
|
749
710
|
}
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
711
|
+
function toDevice(row) {
|
|
712
|
+
return { ...row, deleted: Boolean(row.deleted) };
|
|
713
|
+
}
|
|
714
|
+
function toMailSQL(row) {
|
|
715
|
+
return {
|
|
716
|
+
...row,
|
|
717
|
+
extra: row.extra ?? "",
|
|
718
|
+
forward: Boolean(row.forward),
|
|
719
|
+
mailType: parseMailType(row.mailType),
|
|
720
|
+
time: row.time,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
function toServer(row) {
|
|
724
|
+
return {
|
|
725
|
+
icon: row.icon ?? undefined,
|
|
726
|
+
name: row.name,
|
|
727
|
+
serverID: row.serverID,
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
function toUserRecord(row) {
|
|
731
|
+
return { ...row };
|
|
732
|
+
}
|
|
733
|
+
export const hashPassword = (password, salt) => pbkdf2Sync(password, salt, ITERATIONS, 32, "sha512");
|
|
734
|
+
//# sourceMappingURL=Database.js.map
|