chirkle 0.0.0-stage → 0.1.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/LICENSE +674 -0
- package/README.md +310 -2
- package/package.json +75 -4
- package/pb_hooks/federation/config.js +276 -0
- package/pb_hooks/federation/inbox.js +316 -0
- package/pb_hooks/federation/messages.js +420 -0
- package/pb_hooks/federation/mirror.js +277 -0
- package/pb_hooks/federation/net.js +247 -0
- package/pb_hooks/federation/peers.js +490 -0
- package/pb_hooks/federation/people.js +376 -0
- package/pb_hooks/federation/rooms.js +552 -0
- package/pb_hooks/federation/worker.js +413 -0
- package/pb_hooks/federation.pb.js +141 -0
- package/pb_hooks/lib/access.js +102 -0
- package/pb_hooks/lib/account.js +31 -0
- package/pb_hooks/lib/anonymous.js +189 -0
- package/pb_hooks/lib/bulk.js +187 -0
- package/pb_hooks/lib/collections.js +587 -0
- package/pb_hooks/lib/dms.js +123 -0
- package/pb_hooks/lib/emoji.js +28 -0
- package/pb_hooks/lib/enrich.js +75 -0
- package/pb_hooks/lib/features.js +31 -0
- package/pb_hooks/lib/files.js +375 -0
- package/pb_hooks/lib/http.js +146 -0
- package/pb_hooks/lib/images.js +160 -0
- package/pb_hooks/lib/keepalive.js +31 -0
- package/pb_hooks/lib/lists.js +173 -0
- package/pb_hooks/lib/media.js +635 -0
- package/pb_hooks/lib/messages.js +519 -0
- package/pb_hooks/lib/moderation.js +128 -0
- package/pb_hooks/lib/outbound.js +116 -0
- package/pb_hooks/lib/pages.js +171 -0
- package/pb_hooks/lib/previews.js +346 -0
- package/pb_hooks/lib/reactions.js +110 -0
- package/pb_hooks/lib/reads.js +181 -0
- package/pb_hooks/lib/records.js +77 -0
- package/pb_hooks/lib/rooms.js +148 -0
- package/pb_hooks/lib/site.js +102 -0
- package/pb_hooks/lib/text.js +110 -0
- package/pb_hooks/lib/users.js +81 -0
- package/pb_hooks/lib/worker.js +159 -0
- package/pb_hooks/main.pb.js +324 -0
- package/pb_hooks/telegram/api.js +128 -0
- package/pb_hooks/telegram/inbound.js +548 -0
- package/pb_hooks/telegram/links.js +157 -0
- package/pb_hooks/telegram/outbound.js +323 -0
- package/pb_hooks/telegram/people.js +166 -0
- package/pb_hooks/telegram/rich.js +192 -0
- package/pb_hooks/telegram/worker.js +181 -0
- package/pb_hooks/telegram.pb.js +47 -0
- package/pb_migrations/1760000000_init.js +150 -0
- package/pb_migrations/1760000001_qa_fixes.js +69 -0
- package/pb_migrations/1760000002_avatars.js +39 -0
- package/pb_migrations/1760000003_direct_messages.js +142 -0
- package/pb_migrations/1760000004_attachments.js +28 -0
- package/pb_migrations/1760000005_file_names.js +16 -0
- package/pb_migrations/1760000006_room_reads.js +41 -0
- package/pb_migrations/1760000007_replies.js +23 -0
- package/pb_migrations/1760000008_lowercase_emails.js +24 -0
- package/pb_migrations/1760000009_reactions.js +32 -0
- package/pb_migrations/1760000010_fit_thumbs.js +18 -0
- package/pb_migrations/1760000011_forwarding.js +39 -0
- package/pb_migrations/1760000012_accounts.js +55 -0
- package/pb_migrations/1760000013_qa_pass5.js +19 -0
- package/pb_migrations/1760000014_room_access.js +96 -0
- package/pb_migrations/1760000015_site.js +46 -0
- package/pb_migrations/1760000016_forward_refs.js +43 -0
- package/pb_migrations/1760000017_no_dms.js +18 -0
- package/pb_migrations/1760000018_attachment_limits.js +24 -0
- package/pb_migrations/1760000019_telegram.js +109 -0
- package/pb_migrations/1760000020_service_messages.js +21 -0
- package/pb_migrations/1760000021_forward_view_index.js +41 -0
- package/pb_migrations/1760000022_link_previews.js +43 -0
- package/pb_migrations/1760000023_room_writers.js +49 -0
- package/pb_migrations/1760000024_message_search.js +51 -0
- package/pb_migrations/1760000025_search_dotted_i.js +16 -0
- package/pb_migrations/1760000026_push.js +67 -0
- package/pb_migrations/1760000027_push_tokens_per_user.js +31 -0
- package/pb_migrations/1760000028_media_grid_thumbs.js +18 -0
- package/pb_migrations/1760000029_media.js +71 -0
- package/pb_migrations/1760000030_collections.js +140 -0
- package/pb_migrations/1760000031_unread_limit.js +20 -0
- package/pb_migrations/1760000032_federation.js +227 -0
- package/pb_migrations/1760000033_federation_requests.js +44 -0
- package/pb_migrations/1760000034_telegram_parts.js +26 -0
- package/pb_migrations/1760000035_telegram_anonymous.js +16 -0
- package/pb_migrations/1760000036_chirkle.js +20 -0
- package/pb_migrations/1760000037_file_dims.js +35 -0
- package/pb_migrations/1760000038_drop_push.js +64 -0
- package/pb_migrations/1760000039_anonymous_rooms.js +40 -0
- package/pb_migrations/1760000040_moderators.js +42 -0
- package/pb_migrations/1760000041_shared_files.js +500 -0
- package/pb_migrations/1760000042_files_src.js +17 -0
- package/pb_public/assets/access-ALBW2MT2.js +2 -0
- package/pb_public/assets/access-ALBW2MT2.js.map +7 -0
- package/pb_public/assets/access-PB77EH4F.css +2 -0
- package/pb_public/assets/access-PB77EH4F.css.map +7 -0
- package/pb_public/assets/app-ECR34OBK.css +2 -0
- package/pb_public/assets/app-ECR34OBK.css.map +7 -0
- package/pb_public/assets/app-SILEBEXB.js +3 -0
- package/pb_public/assets/app-SILEBEXB.js.map +7 -0
- package/pb_public/assets/collections-VEOIOCAW.css +2 -0
- package/pb_public/assets/collections-VEOIOCAW.css.map +7 -0
- package/pb_public/assets/emoji-WSTVXAW5.css +2 -0
- package/pb_public/assets/emoji-WSTVXAW5.css.map +7 -0
- package/pb_public/assets/emoji-picker-QYRX6NYL.js +609 -0
- package/pb_public/assets/emoji-picker-QYRX6NYL.js.map +7 -0
- package/pb_public/assets/inter-cyrillic-JEOLYBOO.woff2 +0 -0
- package/pb_public/assets/inter-cyrillic-ext-IYF56FF6.woff2 +0 -0
- package/pb_public/assets/inter-greek-IRE366VL.woff2 +0 -0
- package/pb_public/assets/inter-latin-NRMW37G5.woff2 +0 -0
- package/pb_public/assets/inter-latin-ext-HA22NDSG.woff2 +0 -0
- package/pb_public/assets/lazy-SCCVSUZO.js +2 -0
- package/pb_public/assets/lazy-SCCVSUZO.js.map +7 -0
- package/pb_public/assets/media-DH7ECZSF.js +2 -0
- package/pb_public/assets/media-DH7ECZSF.js.map +7 -0
- package/pb_public/assets/media-L3EYTHKT.css +2 -0
- package/pb_public/assets/media-L3EYTHKT.css.map +7 -0
- package/pb_public/assets/notify-L6GERFNO.js +2 -0
- package/pb_public/assets/notify-L6GERFNO.js.map +7 -0
- package/pb_public/assets/search-3UT4HMES.js +2 -0
- package/pb_public/assets/search-3UT4HMES.js.map +7 -0
- package/pb_public/assets/switcher-CEUKRLEY.js +2 -0
- package/pb_public/assets/switcher-CEUKRLEY.js.map +7 -0
- package/pb_public/assets/viewer-DXT7SKOU.js +2 -0
- package/pb_public/assets/viewer-DXT7SKOU.js.map +7 -0
- package/pb_public/assets/viewer-P7GYCBYM.css +2 -0
- package/pb_public/assets/viewer-P7GYCBYM.css.map +7 -0
- package/pb_public/icons/apple-touch-icon.png +0 -0
- package/pb_public/icons/icon-192.png +0 -0
- package/pb_public/icons/icon-512.png +0 -0
- package/pb_public/icons/icon.svg +1 -0
- package/pb_public/icons/maskable-512.png +0 -0
- package/pb_public/index.html +21 -0
- package/pb_public/sw.js +1 -0
- package/scripts/fetch-pocketbase.sh +63 -0
- package/scripts/start.mjs +93 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/// <reference path="../../types/pocketbase.d.ts" />
|
|
2
|
+
// Cleaning and comparing user-written text (names, messages, file names). Pure string functions, no records.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Invisible format characters: zero-width spaces and joiners, bidi marks/overrides/isolates, word joiners,
|
|
6
|
+
* BOM, soft hyphen. `String.trim()` keeps them, so text made only of these would look empty.
|
|
7
|
+
*/
|
|
8
|
+
const INVISIBLE = /[----]/g;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Removed from names (users, rooms): never needed there, and used to spoof ("admin", "general").
|
|
12
|
+
* ZWNJ/ZWJ (U+200C/D) stay: some scripts and emoji sequences need them.
|
|
13
|
+
*/
|
|
14
|
+
const NAME_JUNK = /[---]/g;
|
|
15
|
+
|
|
16
|
+
/** Control characters (tabs, line breaks, C1) and line/paragraph separators: shown as spaces, if at all (QA BUG-38). */
|
|
17
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: finding control characters is the point
|
|
18
|
+
const CONTROL = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Longest original file name kept, in UTF-8 bytes. PocketBase stores the original name as file metadata cut at
|
|
22
|
+
* 255 bytes, which can split a character and then fails the whole upload (QA BUG-35).
|
|
23
|
+
*/
|
|
24
|
+
const MAX_FILE_NAME_BYTES = 200;
|
|
25
|
+
|
|
26
|
+
/** @param {string} s */
|
|
27
|
+
const isBlank = (s) => s.replace(INVISIBLE, "").trim() === "";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A name as it should be shown: no invisible/bidi characters, control characters as spaces, every run of
|
|
31
|
+
* whitespace as one space, trimmed. Names that look the same are then the same (QA BUG-38).
|
|
32
|
+
* @param {string} s
|
|
33
|
+
*/
|
|
34
|
+
const tidy = (s) => s.replace(NAME_JUNK, "").replace(CONTROL, " ").replace(/\s+/g, " ").trim();
|
|
35
|
+
|
|
36
|
+
/** @param {string} s */
|
|
37
|
+
const utf8Length = (s) => {
|
|
38
|
+
let n = 0;
|
|
39
|
+
for (const c of s) {
|
|
40
|
+
const cp = c.codePointAt(0) ?? 0;
|
|
41
|
+
n += cp < 0x80 ? 1 : cp < 0x800 ? 2 : cp < 0x10000 ? 3 : 4;
|
|
42
|
+
}
|
|
43
|
+
return n;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* `name` cut to at most `max` UTF-8 bytes on a character boundary, keeping its extension (QA BUG-36).
|
|
48
|
+
* @param {string} name
|
|
49
|
+
* @param {number} [max]
|
|
50
|
+
*/
|
|
51
|
+
function clipFileName(name, max = MAX_FILE_NAME_BYTES) {
|
|
52
|
+
if (utf8Length(name) <= max) return name;
|
|
53
|
+
const dot = name.lastIndexOf(".");
|
|
54
|
+
const ext = dot > 0 && name.length - dot <= 16 ? name.slice(dot) : "";
|
|
55
|
+
let stem = "";
|
|
56
|
+
let n = utf8Length(ext);
|
|
57
|
+
for (const c of name.slice(0, name.length - ext.length)) {
|
|
58
|
+
n += utf8Length(c);
|
|
59
|
+
if (n > max) break;
|
|
60
|
+
stem += c;
|
|
61
|
+
}
|
|
62
|
+
return stem.trimEnd() + ext;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Uniqueness key for room and display names: NFKC + full case folding (via upper→lower, so "ß" matches "SS" and "Ü"
|
|
67
|
+
* "ü"), without invisible characters. Keep in sync with pb_migrations/1760000001_qa_fixes.js and
|
|
68
|
+
* 1760000012_accounts.js. Message search folds text the same way, and a little more (`searchKey`).
|
|
69
|
+
* @param {string} name
|
|
70
|
+
*/
|
|
71
|
+
function nameKey(name) {
|
|
72
|
+
const s = name.replace(INVISIBLE, "");
|
|
73
|
+
return (typeof s.normalize === "function" ? s.normalize("NFKC") : s).toUpperCase().toLowerCase();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* What message search matches (§29, messages.text_key): the text folded as names are (`nameKey`), and "İ" as "i".
|
|
78
|
+
* Lowercased, "İ" is "i" and a combining dot, so "istanbul" as typed found no "İstanbul" (QA BUG-113). Keep in sync
|
|
79
|
+
* with foldKey() in web/src/views/room/search.ts and pb_migrations/1760000025_search_dotted_i.js.
|
|
80
|
+
* @param {string} text
|
|
81
|
+
*/
|
|
82
|
+
const searchKey = (text) => nameKey(text).replace(/i\u0307/g, "i");
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Leading "#"s of a room name: rooms are shown as "#name", so "#general" would read "##general", next to a
|
|
86
|
+
* different "general" (QA BUG-48).
|
|
87
|
+
* @param {string} name
|
|
88
|
+
*/
|
|
89
|
+
const stripHashes = (name) => name.replace(/^[#\s]+/, "");
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* An upload's name as the user's file is called. Browsers send `"` as "%22" (and line breaks as "%0D"/"%0A") in a
|
|
93
|
+
* multipart file name, per the HTML spec, so "Q&A "v2".txt" arrived as "Q&A %22v2%22.txt" (QA BUG-54).
|
|
94
|
+
* @param {string} name
|
|
95
|
+
*/
|
|
96
|
+
const unescapeUploadName = (name) => name.replace(/%22/g, '"').replace(/%0[AD]/gi, " ");
|
|
97
|
+
|
|
98
|
+
/** Control characters a message has no use for: all but tab and line feed (QA BUG-105). */
|
|
99
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: finding control characters is the point
|
|
100
|
+
const TEXT_CONTROL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/g;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Message text as it should be kept: line breaks as "\n" (CR LF, a lone CR and the line/paragraph separators), and
|
|
104
|
+
* no other control characters. Pasted terminal output brought NUL, bell and escape codes, which show as nothing or as
|
|
105
|
+
* boxes, and which Telegram refuses (QA BUG-105).
|
|
106
|
+
* @param {string} s
|
|
107
|
+
*/
|
|
108
|
+
const cleanText = (s) => s.replace(/\r\n?|[\u2028\u2029]/g, "\n").replace(TEXT_CONTROL, "");
|
|
109
|
+
|
|
110
|
+
module.exports = { isBlank, tidy, clipFileName, nameKey, searchKey, stripHashes, unescapeUploadName, cleanText };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/// <reference path="../../types/pocketbase.d.ts" />
|
|
2
|
+
// users (DESIGN.md §5.1, §22.1): the hooks that clean a saved user (display name, email) and find a login by email, and
|
|
3
|
+
// GET /api/chat/users?q= (§16.5): find people to message by name. Matched against `name_key` (case folded
|
|
4
|
+
// like names are compared, §22.1), so "иван" finds "Иван" and "strasse" "Straße": SQLite's LIKE only ignores the case
|
|
5
|
+
// of ASCII letters (QA BUG-55). `name_key` is hidden from clients, hence a route rather than a filter.
|
|
6
|
+
|
|
7
|
+
const { assertImagesFit } = require(`${__hooks}/lib/images.js`);
|
|
8
|
+
const { cleanName, originalNames, recordOf, setNameKey } = require(`${__hooks}/lib/records.js`);
|
|
9
|
+
const { nameKey } = require(`${__hooks}/lib/text.js`);
|
|
10
|
+
|
|
11
|
+
/** Most people one search returns. */
|
|
12
|
+
const LIMIT = 20;
|
|
13
|
+
|
|
14
|
+
/** @param {core.RequestEvent} e */
|
|
15
|
+
function searchUsers(e) {
|
|
16
|
+
const me = e.auth;
|
|
17
|
+
if (!me) throw new UnauthorizedError("The request requires a logged-in user.");
|
|
18
|
+
const raw = e.requestInfo().query.q;
|
|
19
|
+
const key = nameKey(typeof raw === "string" ? raw.trim() : "");
|
|
20
|
+
if (!key) return e.json(200, []);
|
|
21
|
+
// `~` treats a % in the value as a wildcard (and then stops adding its own): escape it (QA BUG-34). `_` is escaped.
|
|
22
|
+
const found = e.app.findRecordsByFilter("users", "name_key ~ {:q} && id != {:me}", "name", LIMIT, 0, {
|
|
23
|
+
q: key.replace(/%/g, "\\%"),
|
|
24
|
+
me: me.id,
|
|
25
|
+
});
|
|
26
|
+
// Only the public profile, never the email (§6).
|
|
27
|
+
return e.json(
|
|
28
|
+
200,
|
|
29
|
+
found.flatMap((u) =>
|
|
30
|
+
u
|
|
31
|
+
? [
|
|
32
|
+
{
|
|
33
|
+
id: u.id,
|
|
34
|
+
name: u.getString("name"),
|
|
35
|
+
avatar: u.getString("avatar"),
|
|
36
|
+
no_dms: u.getBool("no_dms"),
|
|
37
|
+
instance: u.getString("instance"),
|
|
38
|
+
},
|
|
39
|
+
]
|
|
40
|
+
: [],
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* users create/update: clean the display name (§5.1), which must be unique (§22.1). A new email is stored in lower case, so the same address can't
|
|
47
|
+
* make two accounts and logging in doesn't depend on how it was capitalized (QA BUG-47). Existing ones are left as they
|
|
48
|
+
* are on other updates (the migration lowered every one it could).
|
|
49
|
+
* @param {core.RecordEvent} e
|
|
50
|
+
*/
|
|
51
|
+
function beforeUserSave(e) {
|
|
52
|
+
const rec = recordOf(e);
|
|
53
|
+
const email = rec.getString("email");
|
|
54
|
+
if (rec.isNew() || email !== rec.original().getString("email")) rec.set("email", email.trim().toLowerCase());
|
|
55
|
+
cleanName(rec);
|
|
56
|
+
if (!rec.isNew() && rec.getString("name") === rec.original().getString("name")) {
|
|
57
|
+
// Name unchanged: keep its key. Accounts named before name_key existed may share a name (the migration made
|
|
58
|
+
// their keys unique), and must still be able to change their picture or password.
|
|
59
|
+
rec.set("name_key", rec.original().getString("name_key"));
|
|
60
|
+
} else {
|
|
61
|
+
setNameKey(e.app, "users", rec, "This display name is taken. Choose another one.");
|
|
62
|
+
}
|
|
63
|
+
assertImagesFit(rec, "avatar");
|
|
64
|
+
originalNames(rec, "avatar"); // only for the upload's own name (QA BUG-35)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* users auth-with-password: an email that matched nobody as typed is looked up in lower case, the way emails are
|
|
69
|
+
* stored (QA BUG-47). PocketBase then checks the password as usual.
|
|
70
|
+
* @param {core.RecordAuthWithPasswordRequestEvent} e
|
|
71
|
+
*/
|
|
72
|
+
function findLoginByEmail(e) {
|
|
73
|
+
if (e.record || (e.identityField && e.identityField !== "email") || !e.identity.includes("@")) return;
|
|
74
|
+
try {
|
|
75
|
+
e.record = e.app.findFirstRecordByFilter("users", "email = {:email}", { email: e.identity.trim().toLowerCase() });
|
|
76
|
+
} catch {
|
|
77
|
+
// no such user: PocketBase answers "Failed to authenticate." as for any wrong login
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = { beforeUserSave, findLoginByEmail, searchUsers };
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/// <reference path="../../types/pocketbase.d.ts" />
|
|
2
|
+
// The cron workers' run loop (link previews §28, media §30.1, Telegram §26.7, federation §33.8): a cron job starts a
|
|
3
|
+
// run every minute and it goes on for most of that minute, a pass at a time. One run at a time, by a lock in the app
|
|
4
|
+
// store holding the run's own handle and when it was last renewed. A lock not renewed for `staleMs` was left by a run
|
|
5
|
+
// that died (or is stuck) and is taken over; the old run then stops at its next renew, and its unlock leaves the new
|
|
6
|
+
// run's lock alone (QA BUG-147). Passes renew after every item they handle (`renew`), so a slow item can't make the
|
|
7
|
+
// lock look stale while it is still being worked on (QA BUG-111).
|
|
8
|
+
//
|
|
9
|
+
// A worker whose work is queued by this server's own saves can say so (`wakes`): with nothing to do, its run then
|
|
10
|
+
// looks for work only when a save wakes it (`wake`, after the save is committed) and every IDLE_MS, not every
|
|
11
|
+
// second (docs/PERF.md). The pass every IDLE_MS finds what was queued some other way (a migration, raw SQL).
|
|
12
|
+
|
|
13
|
+
/** How long one run goes on: the next one starts on the next minute. */
|
|
14
|
+
const RUN_MS = 57_000;
|
|
15
|
+
/** With nothing to do, how often a worker that `wakes` looks for work unwoken. */
|
|
16
|
+
const IDLE_MS = 30_000;
|
|
17
|
+
/** Thrown by `renew` once the lock is another run's: the pass stops there. */
|
|
18
|
+
const LOST = "worker: lock lost";
|
|
19
|
+
|
|
20
|
+
/** @param {unknown} err */
|
|
21
|
+
const isLost = (err) => String(err).includes(LOST);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Take lock `name`, unless a run holds it. Returns the run's handle ("" when not taken).
|
|
25
|
+
* @param {core.App} app
|
|
26
|
+
* @param {string} name
|
|
27
|
+
* @param {number} staleMs
|
|
28
|
+
*/
|
|
29
|
+
function lock(app, name, staleMs) {
|
|
30
|
+
const me = $security.randomString(12);
|
|
31
|
+
let mine = false;
|
|
32
|
+
app.store().setFunc(name, (old) => {
|
|
33
|
+
if (old && Date.now() - Number(String(old).split(" ")[1]) < staleMs) return old;
|
|
34
|
+
mine = true;
|
|
35
|
+
return `${me} ${Date.now()}`;
|
|
36
|
+
});
|
|
37
|
+
return mine ? me : "";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Renew run `me`'s lock. False when it isn't its lock any more.
|
|
42
|
+
* @param {core.App} app
|
|
43
|
+
* @param {string} name
|
|
44
|
+
* @param {string} me
|
|
45
|
+
*/
|
|
46
|
+
function beat(app, name, me) {
|
|
47
|
+
let mine = false;
|
|
48
|
+
app.store().setFunc(name, (old) => {
|
|
49
|
+
if (String(old || "").split(" ")[0] !== me) return old;
|
|
50
|
+
mine = true;
|
|
51
|
+
return `${me} ${Date.now()}`;
|
|
52
|
+
});
|
|
53
|
+
return mine;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** @param {core.App} app @param {string} name @param {string} me */
|
|
57
|
+
function unlock(app, name, me) {
|
|
58
|
+
app.store().setFunc(name, (old) => (String(old || "").split(" ")[0] === me ? undefined : old));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @typedef {object} Worker
|
|
63
|
+
* @property {string} name the lock's key in the app store, e.g. "media.worker"
|
|
64
|
+
* @property {string} label how its log lines start, e.g. "media"
|
|
65
|
+
* @property {number} staleMs
|
|
66
|
+
* @property {(renew: () => void) => unknown} pass one pass; call `renew` after each item. Truthy: there may be more
|
|
67
|
+
* to do now, so the next pass goes at once; otherwise the run waits a second first.
|
|
68
|
+
* @property {boolean} [wakes] its work is queued by saves that call `wake`: idle, it passes only when woken
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/** The app store's key of worker `name`'s wake-up. @param {string} name */
|
|
72
|
+
const wakeKey = (name) => `${name}.wake`;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Something was queued for worker `name` (its lock's key): its run passes within a second. Call it once the work is
|
|
76
|
+
* committed: a pass that starts before that wouldn't see it.
|
|
77
|
+
* @param {core.App} app
|
|
78
|
+
* @param {string} name
|
|
79
|
+
*/
|
|
80
|
+
function wake(app, name) {
|
|
81
|
+
app.store().set(wakeKey(name), true);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Was worker `name` woken? Then it isn't any more.
|
|
86
|
+
* @param {core.App} app
|
|
87
|
+
* @param {string} name
|
|
88
|
+
*/
|
|
89
|
+
function woken(app, name) {
|
|
90
|
+
let was = false;
|
|
91
|
+
app.store().setFunc(wakeKey(name), (old) => {
|
|
92
|
+
was = !!old;
|
|
93
|
+
return undefined;
|
|
94
|
+
});
|
|
95
|
+
return was;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The cron job: passes until the minute is nearly over.
|
|
100
|
+
* @param {core.App} app
|
|
101
|
+
* @param {Worker} w
|
|
102
|
+
*/
|
|
103
|
+
function run(app, w) {
|
|
104
|
+
const me = lock(app, w.name, w.staleMs);
|
|
105
|
+
if (!me) return;
|
|
106
|
+
const renew = () => {
|
|
107
|
+
if (!beat(app, w.name, me)) throw new Error(LOST);
|
|
108
|
+
};
|
|
109
|
+
try {
|
|
110
|
+
const until = Date.now() + RUN_MS;
|
|
111
|
+
let passed = 0;
|
|
112
|
+
let busy = true;
|
|
113
|
+
while (Date.now() < until) {
|
|
114
|
+
// Idle: a wakes worker passes when woken, or once IDLE_MS have gone by.
|
|
115
|
+
if (w.wakes && !busy && !woken(app, w.name) && Date.now() - passed < IDLE_MS) {
|
|
116
|
+
sleep(1000);
|
|
117
|
+
if (!beat(app, w.name, me)) return;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (w.wakes) woken(app, w.name); // what was queued before this pass, it sees
|
|
121
|
+
passed = Date.now();
|
|
122
|
+
busy = false;
|
|
123
|
+
try {
|
|
124
|
+
busy = !!w.pass(renew);
|
|
125
|
+
} catch (err) {
|
|
126
|
+
if (isLost(err)) return app.logger().warn(`${w.label}: worker took too long, another run took over`);
|
|
127
|
+
app.logger().error(`${w.label}: worker`, "error", String(err));
|
|
128
|
+
}
|
|
129
|
+
if (!busy) sleep(1000);
|
|
130
|
+
if (!beat(app, w.name, me)) return;
|
|
131
|
+
}
|
|
132
|
+
} finally {
|
|
133
|
+
unlock(app, w.name, me);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* One pass now, holding the lock (a tick route), unless a run holds it: it does the work within a second. Returns
|
|
139
|
+
* whether it ran.
|
|
140
|
+
* @param {core.App} app
|
|
141
|
+
* @param {Worker} w
|
|
142
|
+
*/
|
|
143
|
+
function once(app, w) {
|
|
144
|
+
const me = lock(app, w.name, w.staleMs);
|
|
145
|
+
if (!me) {
|
|
146
|
+
if (w.wakes) wake(app, w.name);
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
w.pass(() => {
|
|
151
|
+
if (!beat(app, w.name, me)) throw new Error(LOST);
|
|
152
|
+
});
|
|
153
|
+
} finally {
|
|
154
|
+
unlock(app, w.name, me);
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
module.exports = { run, once, wake, isLost, lock, beat, unlock };
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/// <reference path="../types/pocketbase.d.ts" />
|
|
2
|
+
// Server-side hooks (DESIGN.md §5.6, §16, §18, §20, §21, §22, §23, §24, §28, §30, §31, §36, §37; docs/ANDROID.md §9.4). Logic lives in lib/, one module per collection
|
|
3
|
+
// or route: PocketBase hook handlers can't see top-level variables, so each handler require()s its module.
|
|
4
|
+
|
|
5
|
+
onBootstrap((e) => {
|
|
6
|
+
e.next(); // the settings are loaded once bootstrapped
|
|
7
|
+
require(`${__hooks}/lib/http.js`).trustProxy(e.app);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
routerUse((e) => {
|
|
11
|
+
require(`${__hooks}/lib/http.js`).headers(e);
|
|
12
|
+
return e.next();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Bodies up to 10 attachments of 20 MB (§17.1): replaces PocketBase's default limit (same middleware id); lib/http.js
|
|
16
|
+
// keeps the default for everything but a message create.
|
|
17
|
+
routerUse($apis.bodyLimit(require(`${__hooks}/lib/http.js`).MESSAGE_BODY_LIMIT));
|
|
18
|
+
routerUse((e) => require(`${__hooks}/lib/http.js`).bodyLimit(e));
|
|
19
|
+
|
|
20
|
+
// Nobody but superusers finds out who wrote a message in an anonymous room by filtering, sorting or expanding (§35).
|
|
21
|
+
routerUse((e) => {
|
|
22
|
+
// Re-encoded: letters never are (%61uthor would pass a look at the raw query).
|
|
23
|
+
if (/author|forwarded_from/i.test(e.request?.url?.query().encode() ?? "")) require(`${__hooks}/lib/anonymous.js`).refuseProbes(e);
|
|
24
|
+
return e.next();
|
|
25
|
+
});
|
|
26
|
+
// A message's or collection item's file is its shared `files` row's (§37): served without a file token.
|
|
27
|
+
routerUse((e) => {
|
|
28
|
+
if (e.request?.url?.path.startsWith("/api/files/") && require(`${__hooks}/lib/files.js`).serve(e)) return;
|
|
29
|
+
return e.next();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
onRealtimeSubscribeRequest((e) => {
|
|
33
|
+
require(`${__hooks}/lib/anonymous.js`).refuseSubscriptionProbes(e);
|
|
34
|
+
e.next();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Not the app's index.html (PocketBase's fallback for any other path, §7.1) for a missing asset or API.
|
|
38
|
+
routerAdd("GET", "/assets/{path...}", (e) => require(`${__hooks}/lib/http.js`).asset(e));
|
|
39
|
+
routerAdd("GET", "/manifest.webmanifest", (e) => require(`${__hooks}/lib/site.js`).manifest(e));
|
|
40
|
+
routerAdd("GET", "/.well-known/assetlinks.json", (e) => require(`${__hooks}/lib/site.js`).assetLinks(e));
|
|
41
|
+
routerAdd("GET", "/api/{path...}", () => {
|
|
42
|
+
throw new NotFoundError();
|
|
43
|
+
});
|
|
44
|
+
routerAdd("POST", "/api/previews/tick", (e) => require(`${__hooks}/lib/previews.js`).tick(e), $apis.requireSuperuserAuth());
|
|
45
|
+
routerAdd("GET", "/api/chat/rooms", (e) => require(`${__hooks}/lib/lists.js`).rooms(e));
|
|
46
|
+
routerAdd("GET", "/api/chat/rooms/{room}/messages", (e) => require(`${__hooks}/lib/lists.js`).messages(e));
|
|
47
|
+
routerAdd("GET", "/api/chat/notifiable", (e) => require(`${__hooks}/lib/lists.js`).notifiable(e), $apis.requireAuth("users"));
|
|
48
|
+
routerAdd("POST", "/api/chat/dms", (e) => require(`${__hooks}/lib/dms.js`).openDm(e), $apis.requireAuth("users"));
|
|
49
|
+
routerAdd("GET", "/api/chat/unread", (e) => require(`${__hooks}/lib/reads.js`).unread(e), $apis.requireAuth("users"));
|
|
50
|
+
routerAdd("GET", "/api/chat/unread/{room}", (e) => require(`${__hooks}/lib/reads.js`).firstUnread(e), $apis.requireAuth("users"));
|
|
51
|
+
routerAdd("POST", "/api/chat/reads", (e) => require(`${__hooks}/lib/reads.js`).markRead(e), $apis.requireAuth("users"));
|
|
52
|
+
routerAdd("POST", "/api/chat/reactions", (e) => require(`${__hooks}/lib/reactions.js`).react(e), $apis.requireAuth("users"));
|
|
53
|
+
routerAdd("POST", "/api/chat/delete", (e) => require(`${__hooks}/lib/bulk.js`).deleteMessages(e), $apis.requireAuth("users"));
|
|
54
|
+
routerAdd("POST", "/api/chat/ban", (e) => require(`${__hooks}/lib/moderation.js`).ban(e), $apis.requireAuth("users"));
|
|
55
|
+
routerAdd("POST", "/api/chat/forward", (e) => require(`${__hooks}/lib/bulk.js`).forward(e), $apis.requireAuth("users"));
|
|
56
|
+
routerAdd("GET", "/api/chat/media/{room}", (e) => require(`${__hooks}/lib/media.js`).list(e));
|
|
57
|
+
routerAdd("GET", "/api/chat/posters/{message}/{file}", (e) => require(`${__hooks}/lib/media.js`).poster(e));
|
|
58
|
+
routerAdd("GET", "/api/chat/posters/items/{item}/{file}", (e) => require(`${__hooks}/lib/media.js`).itemPoster(e));
|
|
59
|
+
routerAdd("GET", "/api/chat/collections", (e) => require(`${__hooks}/lib/collections.js`).list(e), $apis.requireAuth("users"));
|
|
60
|
+
routerAdd("GET", "/api/chat/badges", (e) => require(`${__hooks}/lib/collections.js`).badges(e));
|
|
61
|
+
routerAdd("POST", "/api/chat/items", (e) => require(`${__hooks}/lib/collections.js`).saveItem(e), $apis.requireAuth("users"));
|
|
62
|
+
routerAdd("GET", "/api/chat/saved", (e) => require(`${__hooks}/lib/collections.js`).saved(e), $apis.requireAuth("users"));
|
|
63
|
+
routerAdd("DELETE", "/api/chat/items/{id}", (e) => require(`${__hooks}/lib/collections.js`).removeItem(e), $apis.requireAuth("users"));
|
|
64
|
+
routerAdd("POST", "/api/chat/subs", (e) => require(`${__hooks}/lib/collections.js`).subscribe(e), $apis.requireAuth("users"));
|
|
65
|
+
routerAdd("POST", "/api/chat/subs/seen", (e) => require(`${__hooks}/lib/collections.js`).markSeen(e), $apis.requireAuth("users"));
|
|
66
|
+
routerAdd("POST", "/api/keepalive/tick", (e) => require(`${__hooks}/lib/keepalive.js`).tick(e), $apis.requireSuperuserAuth());
|
|
67
|
+
routerAdd("POST", "/api/media/tick", (e) => require(`${__hooks}/lib/media.js`).tick(e), $apis.requireSuperuserAuth());
|
|
68
|
+
routerAdd("GET", "/api/chat/users", (e) => require(`${__hooks}/lib/users.js`).searchUsers(e), $apis.requireAuth("users"));
|
|
69
|
+
routerAdd("POST", "/api/chat/password", (e) => require(`${__hooks}/lib/account.js`).changePassword(e), $apis.requireAuth("users"));
|
|
70
|
+
|
|
71
|
+
// The core collections' hooks (messages, rooms, users) are all here, with the optional features' parts (lib/features.js)
|
|
72
|
+
// called in order: the federation's first, then the core's, then the Telegram bridge's; after the save, the other way
|
|
73
|
+
// round.
|
|
74
|
+
|
|
75
|
+
onRecordCreate((e) => {
|
|
76
|
+
require(`${__hooks}/lib/users.js`).beforeUserSave(e);
|
|
77
|
+
e.next();
|
|
78
|
+
}, "users");
|
|
79
|
+
|
|
80
|
+
onRecordUpdate((e) => {
|
|
81
|
+
const fed = require(`${__hooks}/lib/features.js`).federation(e.app);
|
|
82
|
+
// A changed profile goes to the peers that know it (§33).
|
|
83
|
+
const profile = fed && e.record ? fed.people.profileKey(e.record.original()) : "";
|
|
84
|
+
require(`${__hooks}/lib/users.js`).beforeUserSave(e);
|
|
85
|
+
// A ban ends their sessions (§36).
|
|
86
|
+
const moderation = require(`${__hooks}/lib/moderation.js`);
|
|
87
|
+
const banned = !!e.record && moderation.beforeBan(e.record);
|
|
88
|
+
e.next();
|
|
89
|
+
if (banned && e.record) moderation.afterBan(e.app, e.record.id);
|
|
90
|
+
if (fed && e.record) fed.people.userChanged(e.app, e.record, profile);
|
|
91
|
+
}, "users");
|
|
92
|
+
|
|
93
|
+
// Names ending in a peer's label are its people's (§33), and a Telegram stand-in's are the bridge's (§26).
|
|
94
|
+
onRecordUpdateRequest((e) => {
|
|
95
|
+
const features = require(`${__hooks}/lib/features.js`);
|
|
96
|
+
features.federation(e.app)?.people.refuseLabelName(e);
|
|
97
|
+
features.telegram()?.people.refuseStandInName(e);
|
|
98
|
+
e.next();
|
|
99
|
+
}, "users");
|
|
100
|
+
|
|
101
|
+
onRecordDelete((e) => {
|
|
102
|
+
require(`${__hooks}/lib/messages.js`).beforeUserDelete(e);
|
|
103
|
+
e.next();
|
|
104
|
+
}, "users");
|
|
105
|
+
|
|
106
|
+
onRecordCreateRequest((e) => {
|
|
107
|
+
const features = require(`${__hooks}/lib/features.js`);
|
|
108
|
+
features.federation(e.app)?.people.refuseLabelName(e);
|
|
109
|
+
require(`${__hooks}/lib/site.js`).checkInvite(e);
|
|
110
|
+
features.telegram()?.people.refuseStandInName(e);
|
|
111
|
+
e.next();
|
|
112
|
+
}, "users");
|
|
113
|
+
|
|
114
|
+
onRecordAuthWithOAuth2Request((e) => {
|
|
115
|
+
require(`${__hooks}/lib/site.js`).checkOAuth2Signup(e);
|
|
116
|
+
require(`${__hooks}/lib/moderation.js`).refuseBanned(e.record, null);
|
|
117
|
+
e.next();
|
|
118
|
+
}, "users");
|
|
119
|
+
|
|
120
|
+
onRecordAuthWithPasswordRequest((e) => {
|
|
121
|
+
require(`${__hooks}/lib/users.js`).findLoginByEmail(e);
|
|
122
|
+
// A banned account is told so (§36).
|
|
123
|
+
require(`${__hooks}/lib/moderation.js`).refuseBanned(e.record, e.password);
|
|
124
|
+
e.next();
|
|
125
|
+
}, "users");
|
|
126
|
+
|
|
127
|
+
onRecordCreateRequest((e) => {
|
|
128
|
+
require(`${__hooks}/lib/rooms.js`).beforeRoomCreateRequest(e);
|
|
129
|
+
e.next();
|
|
130
|
+
}, "rooms");
|
|
131
|
+
|
|
132
|
+
onRecordUpdateRequest((e) => {
|
|
133
|
+
// A shared room's members (§33.5): checked against what each side may change, and sent once saved.
|
|
134
|
+
const fed = require(`${__hooks}/lib/features.js`).federation(e.app);
|
|
135
|
+
if (fed) fed.rooms.checkHomeMembers(e);
|
|
136
|
+
const members = fed ? fed.mirror.checkMirrorEdit(e) : null;
|
|
137
|
+
require(`${__hooks}/lib/rooms.js`).beforeRoomUpdateRequest(e);
|
|
138
|
+
e.next();
|
|
139
|
+
if (fed && members && e.record) fed.mirror.sendMembers(e.app, e.record, members);
|
|
140
|
+
}, "rooms");
|
|
141
|
+
|
|
142
|
+
onRecordCreate((e) => {
|
|
143
|
+
const fed = require(`${__hooks}/lib/features.js`).federation(e.app);
|
|
144
|
+
require(`${__hooks}/lib/rooms.js`).beforeRoomCreate(e);
|
|
145
|
+
e.next();
|
|
146
|
+
// A DM opened with someone on a peer is shared with it (§33).
|
|
147
|
+
if (fed && e.record) fed.rooms.roomCreated(e.app, e.record);
|
|
148
|
+
}, "rooms");
|
|
149
|
+
|
|
150
|
+
onRecordUpdate((e) => {
|
|
151
|
+
const fed = require(`${__hooks}/lib/features.js`).federation(e.app);
|
|
152
|
+
const state = fed && e.record ? fed.rooms.stateOf(e.record.original()) : "";
|
|
153
|
+
require(`${__hooks}/lib/rooms.js`).beforeRoomUpdate(e);
|
|
154
|
+
const access = require(`${__hooks}/lib/access.js`);
|
|
155
|
+
const before = access.accessOf(e.record?.original() ?? e.record);
|
|
156
|
+
e.next();
|
|
157
|
+
// Saved: whoever can't see it any more is told (§23.4).
|
|
158
|
+
if (e.record) access.dropLostAccess(e.app, before, e.record);
|
|
159
|
+
// Its anonymous messages show its new name (§35).
|
|
160
|
+
if (e.record) require(`${__hooks}/lib/anonymous.js`).forget(e.app, e.record.id);
|
|
161
|
+
// A home room's changes go to its mirrors (§33).
|
|
162
|
+
if (fed && e.record) fed.rooms.roomChanged(e.app, e.record, state);
|
|
163
|
+
}, "rooms");
|
|
164
|
+
|
|
165
|
+
onRecordDelete((e) => {
|
|
166
|
+
const fed = require(`${__hooks}/lib/features.js`).federation(e.app);
|
|
167
|
+
const room = e.record;
|
|
168
|
+
const peers = fed && room ? fed.rooms.peersToTell(e.app, room) : [];
|
|
169
|
+
e.next();
|
|
170
|
+
if (room) require(`${__hooks}/lib/anonymous.js`).forget(e.app, room.id);
|
|
171
|
+
if (fed && room) fed.rooms.tellDeleted(e.app, room, peers);
|
|
172
|
+
}, "rooms");
|
|
173
|
+
|
|
174
|
+
onRecordCreateRequest((e) => {
|
|
175
|
+
const messages = require(`${__hooks}/lib/messages.js`);
|
|
176
|
+
messages.beforeMessageCreateRequest(e);
|
|
177
|
+
// One transaction for the attachments' `files` rows (uploads, and files attached from collections, §31, §37) and
|
|
178
|
+
// the message: refused, nothing is left stored.
|
|
179
|
+
e.app.runInTransaction((tx) => {
|
|
180
|
+
e.app = tx;
|
|
181
|
+
messages.setRequestFiles(e);
|
|
182
|
+
e.next();
|
|
183
|
+
});
|
|
184
|
+
}, "messages");
|
|
185
|
+
|
|
186
|
+
onRecordCreate((e) => {
|
|
187
|
+
require(`${__hooks}/lib/collections.js`).beforeCollectionSave(e);
|
|
188
|
+
e.next();
|
|
189
|
+
}, "collections");
|
|
190
|
+
|
|
191
|
+
onRecordUpdate((e) => {
|
|
192
|
+
const collections = require(`${__hooks}/lib/collections.js`);
|
|
193
|
+
collections.beforeCollectionSave(e);
|
|
194
|
+
const wasPublic = !!e.record && e.record.original().getBool("public");
|
|
195
|
+
e.next();
|
|
196
|
+
if (e.record) collections.afterCollectionUpdate(e.app, e.record, wasPublic);
|
|
197
|
+
}, "collections");
|
|
198
|
+
|
|
199
|
+
// What its files' badges say may have changed (§31). Once it is saved: told earlier, a client asking at once could
|
|
200
|
+
// still read it as it was.
|
|
201
|
+
onRecordAfterUpdateSuccess((e) => {
|
|
202
|
+
if (e.record) require(`${__hooks}/lib/collections.js`).afterCollectionSaved(e.app, e.record);
|
|
203
|
+
e.next();
|
|
204
|
+
}, "collections");
|
|
205
|
+
|
|
206
|
+
onRecordAfterDeleteSuccess((e) => {
|
|
207
|
+
if (e.record) require(`${__hooks}/lib/collections.js`).tellBadges(e.app, e.record.id);
|
|
208
|
+
e.next();
|
|
209
|
+
}, "collections");
|
|
210
|
+
|
|
211
|
+
// An item taken out (or its collection, or its owner, deleted): its file is named once less (§37).
|
|
212
|
+
onRecordDelete((e) => {
|
|
213
|
+
const files = require(`${__hooks}/lib/files.js`);
|
|
214
|
+
const names = e.record ? files.namedBy(e.app, "collection_items", e.record.id) : [];
|
|
215
|
+
e.next();
|
|
216
|
+
files.unlink(e.app, names);
|
|
217
|
+
}, "collection_items");
|
|
218
|
+
|
|
219
|
+
// A message deleted for good (a superuser, its room): its files are named once less (§37). A deleted one has none.
|
|
220
|
+
onRecordDelete((e) => {
|
|
221
|
+
const files = require(`${__hooks}/lib/files.js`);
|
|
222
|
+
const names = e.record ? files.namedBy(e.app, "messages", e.record.id) : [];
|
|
223
|
+
e.next();
|
|
224
|
+
files.unlink(e.app, names);
|
|
225
|
+
}, "messages");
|
|
226
|
+
|
|
227
|
+
onRecordCreate((e) => {
|
|
228
|
+
const messages = require(`${__hooks}/lib/messages.js`);
|
|
229
|
+
messages.beforeMessageCreate(e);
|
|
230
|
+
if (e.record) messages.setTextKey(e.record);
|
|
231
|
+
e.next();
|
|
232
|
+
const rec = e.record;
|
|
233
|
+
if (!rec) return;
|
|
234
|
+
// Saved: what each feature does with a new message is queued, in the save's transaction.
|
|
235
|
+
const features = require(`${__hooks}/lib/features.js`);
|
|
236
|
+
features.telegram()?.outbound.queueCreate(e.app, rec);
|
|
237
|
+
require(`${__hooks}/lib/previews.js`).queue(e.app, rec);
|
|
238
|
+
require(`${__hooks}/lib/media.js`).add(e.app, rec);
|
|
239
|
+
features.federation(e.app)?.messages.messageCreated(e.app, rec);
|
|
240
|
+
}, "messages");
|
|
241
|
+
|
|
242
|
+
onRecordAfterCreateSuccess((e) => {
|
|
243
|
+
require(`${__hooks}/lib/messages.js`).afterMessageCreate(e);
|
|
244
|
+
e.next();
|
|
245
|
+
}, "messages");
|
|
246
|
+
|
|
247
|
+
// Committed (with the message that queued it): the workers look at it now, not on their next look round.
|
|
248
|
+
onRecordAfterCreateSuccess((e) => {
|
|
249
|
+
e.next();
|
|
250
|
+
require(`${__hooks}/lib/previews.js`).wake(e.app);
|
|
251
|
+
}, "link_queue");
|
|
252
|
+
|
|
253
|
+
// A new file (an attachment, an upload into a collection) is committed: the media worker looks at it now
|
|
254
|
+
// (lib/worker.js wake).
|
|
255
|
+
onRecordAfterCreateSuccess((e) => {
|
|
256
|
+
e.next();
|
|
257
|
+
require(`${__hooks}/lib/media.js`).wake(e.app);
|
|
258
|
+
}, "files");
|
|
259
|
+
|
|
260
|
+
onRecordUpdate((e) => {
|
|
261
|
+
const rec = e.record;
|
|
262
|
+
if (!rec) return e.next();
|
|
263
|
+
const features = require(`${__hooks}/lib/features.js`);
|
|
264
|
+
// Messages in shared rooms (§33.5): what it was, and a mirror's message changed only as its home allows.
|
|
265
|
+
const fed = features.federation(e.app);
|
|
266
|
+
const was = fed ? fed.messages.before(rec) : null;
|
|
267
|
+
if (fed) fed.messages.checkMirrorChange(e.app, rec);
|
|
268
|
+
const messages = require(`${__hooks}/lib/messages.js`);
|
|
269
|
+
messages.beforeMessageUpdate(e);
|
|
270
|
+
messages.setTextKey(rec);
|
|
271
|
+
const touch = messages.changesForwards(rec);
|
|
272
|
+
const edited = rec.getString("text") !== rec.original().getString("text");
|
|
273
|
+
const deleted = rec.getBool("deleted") && !rec.original().getBool("deleted");
|
|
274
|
+
// As stored, not as loaded: another delete of it may have been committed since (QA BUG-223).
|
|
275
|
+
const files = deleted ? require(`${__hooks}/lib/files.js`).namedBy(e.app, "messages", rec.id) : [];
|
|
276
|
+
const tg = features.telegram();
|
|
277
|
+
const change = tg ? tg.outbound.changeOf(rec) : "";
|
|
278
|
+
e.next();
|
|
279
|
+
if (tg) tg.outbound.queueUpdate(e.app, rec, change);
|
|
280
|
+
// Saved: a deleted message's media leave the grid, its forwards' too (§30.1), and its files are named once less (§37).
|
|
281
|
+
if (deleted) require(`${__hooks}/lib/media.js`).drop(e.app, rec.id);
|
|
282
|
+
if (deleted) require(`${__hooks}/lib/files.js`).unlink(e.app, files);
|
|
283
|
+
// Saved: forwards of it tell their readers (§21.3).
|
|
284
|
+
if (touch) messages.touchForwards(e.app, rec);
|
|
285
|
+
if (edited) require(`${__hooks}/lib/previews.js`).queue(e.app, rec);
|
|
286
|
+
if (fed && was) fed.messages.messageChanged(e.app, rec, was);
|
|
287
|
+
}, "messages");
|
|
288
|
+
|
|
289
|
+
onRecordEnrich((e) => {
|
|
290
|
+
const enrich = require(`${__hooks}/lib/enrich.js`);
|
|
291
|
+
// lib/messages.js only for the messages that need it: loading it costs more than the rest (lib/enrich.js).
|
|
292
|
+
if (enrich.needsWork(e)) {
|
|
293
|
+
const messages = require(`${__hooks}/lib/messages.js`);
|
|
294
|
+
messages.keepToRoom(e);
|
|
295
|
+
messages.showOriginal(e);
|
|
296
|
+
}
|
|
297
|
+
e.next();
|
|
298
|
+
// An anonymous room's message shows the room, not its author (§35): once expanded (in next()), by the real author.
|
|
299
|
+
if (enrich.isAnonymous(e)) require(`${__hooks}/lib/anonymous.js`).hide(e);
|
|
300
|
+
}, "messages");
|
|
301
|
+
|
|
302
|
+
onRecordUpdateRequest((e) => {
|
|
303
|
+
require(`${__hooks}/lib/messages.js`).beforeMessageUpdateRequest(e);
|
|
304
|
+
// One transaction for the save, so beforeMessageUpdate reads the reactions it keeps right before writing (§20.2).
|
|
305
|
+
e.app.runInTransaction((tx) => {
|
|
306
|
+
e.app = tx;
|
|
307
|
+
e.next();
|
|
308
|
+
});
|
|
309
|
+
}, "messages");
|
|
310
|
+
|
|
311
|
+
// Link previews (§28): fetched by a worker, a minute's run at a time.
|
|
312
|
+
if (require(`${__hooks}/lib/previews.js`).enabled()) {
|
|
313
|
+
cronAdd("previews", "* * * * *", () => require(`${__hooks}/lib/previews.js`).run($app));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Quiet realtime connections kept open: PocketBase closes them after 5 minutes, and clients then load everything again
|
|
317
|
+
// (lib/keepalive.js).
|
|
318
|
+
cronAdd("keepalive", "*/3 * * * *", () => require(`${__hooks}/lib/keepalive.js`).run($app));
|
|
319
|
+
|
|
320
|
+
// The media grid's thumbnails and video posters (§30.1): made by a worker, a minute's run at a time. MEDIA_CRON=off
|
|
321
|
+
// leaves them to POST /api/media/tick (tests).
|
|
322
|
+
if ($os.getenv("MEDIA_CRON").trim().toLowerCase() !== "off") {
|
|
323
|
+
cronAdd("media", "* * * * *", () => require(`${__hooks}/lib/media.js`).run($app));
|
|
324
|
+
}
|