chirkle 0.3.0 → 0.4.1
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 +99 -47
- package/bin/release/darwin-amd64/chirkle-server +4 -0
- package/bin/release/darwin-arm64/chirkle-server +0 -0
- package/bin/release/linux-amd64/chirkle-server +0 -0
- package/bin/release/linux-arm64/chirkle-server +0 -0
- package/package.json +9 -6
- package/pb_hooks/federation/inbox.js +2 -69
- package/pb_hooks/federation/messages.js +5 -3
- package/pb_hooks/federation/net.js +1 -0
- package/pb_hooks/federation/people.js +1 -0
- package/pb_hooks/federation.pb.js +1 -8
- package/pb_hooks/lib/access.js +33 -10
- package/pb_hooks/lib/account.js +542 -6
- package/pb_hooks/lib/anonymous.js +2 -129
- package/pb_hooks/lib/bulk.js +1 -2
- package/pb_hooks/lib/collections.js +4 -4
- package/pb_hooks/lib/dms.js +1 -1
- package/pb_hooks/lib/files.js +82 -120
- package/pb_hooks/lib/handles.js +2 -2
- package/pb_hooks/lib/inbox.js +40 -54
- package/pb_hooks/lib/invites.js +266 -0
- package/pb_hooks/lib/media.js +5 -43
- package/pb_hooks/lib/messages.js +72 -117
- package/pb_hooks/lib/moderation.js +2 -16
- package/pb_hooks/lib/reactions.js +28 -4
- package/pb_hooks/lib/retention.js +88 -0
- package/pb_hooks/lib/site.js +13 -2
- package/pb_hooks/lib/stats.js +6 -34
- package/pb_hooks/lib/users.js +88 -7
- package/pb_hooks/lib/worker.js +22 -1
- package/pb_hooks/main.pb.js +85 -94
- package/pb_hooks/telegram/api.js +64 -12
- package/pb_hooks/telegram/inbound.js +158 -31
- package/pb_hooks/telegram/links.js +4 -3
- package/pb_hooks/telegram/outbound.js +86 -45
- package/pb_hooks/telegram/people.js +3 -3
- package/pb_hooks/telegram/worker.js +59 -15
- package/pb_migrations/1760000045_password_reset.js +38 -0
- package/pb_migrations/1760000047_telegram_supergroup_ids.js +51 -0
- package/pb_migrations/1760000048_inbox_reactions_merged.js +24 -0
- package/pb_migrations/1760000049_inbox_room_index.js +22 -0
- package/pb_migrations/1760000050_messages_unread_index.js +22 -0
- package/pb_migrations/1760000051_users_kind.js +31 -0
- package/pb_migrations/1760000052_deleted_accounts.js +19 -0
- package/pb_migrations/1760000053_account_jobs.js +45 -0
- package/pb_migrations/1760000054_room_invites.js +39 -0
- package/pb_migrations/1760000055_invite_barred.js +16 -0
- package/pb_migrations/1760000056_telegram_fetch.js +21 -0
- package/pb_public/assets/{access-PB77EH4F.css → access-GPRLOZOO.css} +2 -2
- package/pb_public/assets/{access-PB77EH4F.css.map → access-GPRLOZOO.css.map} +2 -2
- package/pb_public/assets/access-UK3L54NR.js +2 -0
- package/pb_public/assets/access-UK3L54NR.js.map +7 -0
- package/pb_public/assets/account-CT2BVMAX.css +2 -0
- package/pb_public/assets/account-CT2BVMAX.css.map +7 -0
- package/pb_public/assets/account-api-J6A6IYVX.js +2 -0
- package/pb_public/assets/account-api-J6A6IYVX.js.map +7 -0
- package/pb_public/assets/app-HB3NZHQ2.js +3 -0
- package/pb_public/assets/app-HB3NZHQ2.js.map +7 -0
- package/pb_public/assets/app-XJ73EU5O.css +2 -0
- package/pb_public/assets/app-XJ73EU5O.css.map +7 -0
- package/pb_public/assets/chunk-56ACD3V5.js +2 -0
- package/pb_public/assets/chunk-56ACD3V5.js.map +7 -0
- package/pb_public/assets/chunk-AG73QNUN.js +2 -0
- package/pb_public/assets/chunk-AG73QNUN.js.map +7 -0
- package/pb_public/assets/chunk-DCLJRQ53.js +2 -0
- package/pb_public/assets/chunk-DCLJRQ53.js.map +7 -0
- package/pb_public/assets/chunk-DZC4SY4M.js +2 -0
- package/pb_public/assets/chunk-DZC4SY4M.js.map +7 -0
- package/pb_public/assets/chunk-N2OFGBCZ.js +2 -0
- package/pb_public/assets/chunk-N2OFGBCZ.js.map +7 -0
- package/pb_public/assets/chunk-OKIIBH7O.js +2 -0
- package/pb_public/assets/chunk-OKIIBH7O.js.map +7 -0
- package/pb_public/assets/chunk-RPCBA7VF.js +2 -0
- package/pb_public/assets/chunk-RPCBA7VF.js.map +7 -0
- package/pb_public/assets/chunk-UZDXAORM.js +2 -0
- package/pb_public/assets/chunk-UZDXAORM.js.map +7 -0
- package/pb_public/assets/chunk-VTOUI25E.js +4 -0
- package/pb_public/assets/chunk-VTOUI25E.js.map +7 -0
- package/pb_public/assets/chunk-WR25F7FU.js +2 -0
- package/pb_public/assets/chunk-WR25F7FU.js.map +7 -0
- package/pb_public/assets/{emoji-VA2WCEAC.css → emoji-WWNPWCF3.css} +2 -2
- package/pb_public/assets/{emoji-VA2WCEAC.css.map → emoji-WWNPWCF3.css.map} +2 -2
- package/pb_public/assets/{emoji-picker-65VVVGYW.js → emoji-picker-K6JTK2GV.js} +4 -4
- package/pb_public/assets/emoji-picker-K6JTK2GV.js.map +7 -0
- package/pb_public/assets/forward-267HRNPY.js +2 -0
- package/pb_public/assets/forward-267HRNPY.js.map +7 -0
- package/pb_public/assets/inbox-CLMYESNI.js +2 -0
- package/pb_public/assets/inbox-CLMYESNI.js.map +7 -0
- package/pb_public/assets/invite-OGT5E2MZ.js +2 -0
- package/pb_public/assets/invite-OGT5E2MZ.js.map +7 -0
- package/pb_public/assets/invite-api-7G7XPXQW.js +2 -0
- package/pb_public/assets/invite-api-7G7XPXQW.js.map +7 -0
- package/pb_public/assets/lazy-H3KIJVB5.js +2 -0
- package/pb_public/assets/lazy-H3KIJVB5.js.map +7 -0
- package/pb_public/assets/login-TK6K4WGB.js +2 -0
- package/pb_public/assets/login-TK6K4WGB.js.map +7 -0
- package/pb_public/assets/media-WZ4653L5.js +2 -0
- package/pb_public/assets/media-WZ4653L5.js.map +7 -0
- package/pb_public/assets/mention-suggest-ODJD2IC5.js +2 -0
- package/pb_public/assets/mention-suggest-ODJD2IC5.js.map +7 -0
- package/pb_public/assets/notify-EPDM772N.js +2 -0
- package/pb_public/assets/notify-EPDM772N.js.map +7 -0
- package/pb_public/assets/profile-3R6S5QYS.js +2 -0
- package/pb_public/assets/profile-3R6S5QYS.js.map +7 -0
- package/pb_public/assets/search-WCHOGAJQ.js +2 -0
- package/pb_public/assets/search-WCHOGAJQ.js.map +7 -0
- package/pb_public/assets/stats-GTO2H6PG.js +2 -0
- package/pb_public/assets/stats-GTO2H6PG.js.map +7 -0
- package/pb_public/assets/switcher-TFSO45KV.js +2 -0
- package/pb_public/assets/switcher-TFSO45KV.js.map +7 -0
- package/pb_public/assets/user-search-3HQOCQWL.js +2 -0
- package/pb_public/assets/user-search-3HQOCQWL.js.map +7 -0
- package/pb_public/assets/viewer-2Y5FJBW4.js +2 -0
- package/pb_public/assets/viewer-2Y5FJBW4.js.map +7 -0
- package/pb_public/index.html +10 -2
- package/pb_public/stats.html +1 -1
- package/scripts/service.mjs +201 -0
- package/scripts/start.mjs +21 -17
- package/pb_hooks/lib/enrich.js +0 -75
- package/pb_hooks/lib/lists.js +0 -179
- package/pb_hooks/lib/live.js +0 -137
- package/pb_hooks/lib/reads.js +0 -183
- package/pb_public/assets/access-GUDTDXIP.js +0 -2
- package/pb_public/assets/access-GUDTDXIP.js.map +0 -7
- package/pb_public/assets/app-3WX5JYL2.js +0 -3
- package/pb_public/assets/app-3WX5JYL2.js.map +0 -7
- package/pb_public/assets/app-WAFKVN2H.css +0 -2
- package/pb_public/assets/app-WAFKVN2H.css.map +0 -7
- package/pb_public/assets/emoji-picker-65VVVGYW.js.map +0 -7
- package/pb_public/assets/forward-66PELNWT.js +0 -2
- package/pb_public/assets/forward-66PELNWT.js.map +0 -7
- package/pb_public/assets/inbox-TZJWPRTG.js +0 -2
- package/pb_public/assets/inbox-TZJWPRTG.js.map +0 -7
- package/pb_public/assets/lazy-4CXRBHHM.js +0 -2
- package/pb_public/assets/lazy-4CXRBHHM.js.map +0 -7
- package/pb_public/assets/login-G7Y44CJ7.js +0 -2
- package/pb_public/assets/login-G7Y44CJ7.js.map +0 -7
- package/pb_public/assets/media-FPRU2QH5.js +0 -2
- package/pb_public/assets/media-FPRU2QH5.js.map +0 -7
- package/pb_public/assets/mention-suggest-R7MXUH6R.js +0 -2
- package/pb_public/assets/mention-suggest-R7MXUH6R.js.map +0 -7
- package/pb_public/assets/notify-RQ6ZLR46.js +0 -2
- package/pb_public/assets/notify-RQ6ZLR46.js.map +0 -7
- package/pb_public/assets/profile-2CNVT2KX.js +0 -2
- package/pb_public/assets/profile-2CNVT2KX.js.map +0 -7
- package/pb_public/assets/search-3UT4HMES.js +0 -2
- package/pb_public/assets/search-3UT4HMES.js.map +0 -7
- package/pb_public/assets/stats-JX52AQCO.js +0 -2
- package/pb_public/assets/stats-JX52AQCO.js.map +0 -7
- package/pb_public/assets/switcher-CEUKRLEY.js +0 -2
- package/pb_public/assets/switcher-CEUKRLEY.js.map +0 -7
- package/pb_public/assets/user-search-7GNEIXMN.js +0 -2
- package/pb_public/assets/user-search-7GNEIXMN.js.map +0 -7
- package/pb_public/assets/viewer-DXT7SKOU.js +0 -2
- package/pb_public/assets/viewer-DXT7SKOU.js.map +0 -7
- package/scripts/fetch-pocketbase.sh +0 -63
|
@@ -10,6 +10,12 @@ const { isService } = require(`${__hooks}/lib/messages.js`);
|
|
|
10
10
|
* @typedef {{ emoji: string, users: string[] }} Reaction
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Most different emoji on one message (QA BUG-258). One person may add as many of them as they like: their reactions
|
|
15
|
+
* to a message are one row in its author's Mentions & replies (lib/inbox.js), however many there are.
|
|
16
|
+
*/
|
|
17
|
+
const MAX_EMOJI = 20;
|
|
18
|
+
|
|
13
19
|
/**
|
|
14
20
|
* @param {string} field
|
|
15
21
|
* @param {string} message
|
|
@@ -54,6 +60,20 @@ function setReaction(list, emoji, user, on) {
|
|
|
54
60
|
return list.map((x) => (x === r ? { emoji, users: x.users.filter((u) => u !== user) } : x)).filter((x) => x.users.length > 0);
|
|
55
61
|
}
|
|
56
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Why `user`'s new `emoji` reaction can't be added to `list` (a message's), "" when it can: at most MAX_EMOJI
|
|
65
|
+
* different emoji (QA BUG-258). Taking one back is always fine.
|
|
66
|
+
* @param {Reaction[]} list
|
|
67
|
+
* @param {string} emoji
|
|
68
|
+
* @param {string} user
|
|
69
|
+
*/
|
|
70
|
+
function overLimit(list, emoji, user) {
|
|
71
|
+
if (list.some((r) => r.emoji === emoji && r.users.includes(user))) return "";
|
|
72
|
+
if (!list.some((r) => r.emoji === emoji) && list.length >= MAX_EMOJI)
|
|
73
|
+
return `A message can have at most ${MAX_EMOJI} different reactions.`;
|
|
74
|
+
return "";
|
|
75
|
+
}
|
|
76
|
+
|
|
57
77
|
/**
|
|
58
78
|
* POST /api/chat/reactions { message, emoji, on } → the message, as GET /api/collections/messages/records/:id
|
|
59
79
|
* answers it (the same ?expand and ?fields apply): the caller's `emoji` reaction on
|
|
@@ -62,8 +82,7 @@ function setReaction(list, emoji, user, on) {
|
|
|
62
82
|
* @param {core.RequestEvent} e
|
|
63
83
|
*/
|
|
64
84
|
function react(e) {
|
|
65
|
-
//
|
|
66
|
-
require(`${__hooks}/lib/anonymous.js`).refuseProbes(e);
|
|
85
|
+
// Its ?expand can't group messages by author: refused before this runs (server/anonymous.go bindProbes, §35).
|
|
67
86
|
const me = e.auth;
|
|
68
87
|
if (!me) throw new UnauthorizedError("The request requires a logged-in user.");
|
|
69
88
|
const info = e.requestInfo();
|
|
@@ -90,18 +109,23 @@ function react(e) {
|
|
|
90
109
|
// Read and write in one transaction: two people reacting at once both count. (Errors are thrown after it, so
|
|
91
110
|
// they keep their status.)
|
|
92
111
|
let problem = "";
|
|
112
|
+
let limit = "";
|
|
93
113
|
e.app.runInTransaction((tx) => {
|
|
94
114
|
const rec = tx.findRecordById("messages", id);
|
|
95
115
|
msg = rec;
|
|
96
116
|
if (rec.getBool("deleted")) problem = "That message was deleted.";
|
|
97
117
|
else if (tx.findRecordById("rooms", rec.getString("room")).getBool("archived")) problem = "This room is archived and read-only.";
|
|
98
118
|
if (problem) return;
|
|
99
|
-
const
|
|
119
|
+
const list = reactionsOf(rec);
|
|
120
|
+
const next = setReaction(list, /** @type {string} */ (emoji), me.id, on);
|
|
100
121
|
if (!next) return;
|
|
122
|
+
if (on) limit = overLimit(list, /** @type {string} */ (emoji), me.id);
|
|
123
|
+
if (limit) return;
|
|
101
124
|
rec.set("reactions", next);
|
|
102
125
|
tx.save(rec);
|
|
103
126
|
});
|
|
104
127
|
if (problem) throw badRequest("message", problem);
|
|
128
|
+
if (limit) throw badRequest("emoji", limit);
|
|
105
129
|
// The whole message, not just its reactions: a copy the client patched with a newer `updated` would hide
|
|
106
130
|
// changes it hasn't heard of yet (an edit while its realtime connection was down).
|
|
107
131
|
msg.set("reactions", reactionsOf(msg));
|
|
@@ -109,4 +133,4 @@ function react(e) {
|
|
|
109
133
|
return e.json(200, msg);
|
|
110
134
|
}
|
|
111
135
|
|
|
112
|
-
module.exports = { react, reactionsOf, setReaction };
|
|
136
|
+
module.exports = { overLimit, react, reactionsOf, setReaction };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/// <reference path="../../types/pocketbase.d.ts" />
|
|
2
|
+
// Retention (REFACTOR item 49, DESIGN.md §2): tables that only grew get a daily prune, as statistics (lib/stats.js)
|
|
3
|
+
// and the federation queues (federation/worker.js) already had.
|
|
4
|
+
//
|
|
5
|
+
// - `inbox` (§41): a person's newest INBOX_KEEP rows, none older than INBOX_DAYS. The list pages back no further than
|
|
6
|
+
// that, and the counts (server/unread.go) stop at 100 anyway.
|
|
7
|
+
// - Rows left behind by deletes that skipped the hooks (raw SQL, an old migration, a crash between two saves): inbox
|
|
8
|
+
// rows of a deleted (or soft-deleted) message, room, person or actor; room reads, inbox reads and hidden rooms of a
|
|
9
|
+
// person or room that is gone; media rows (§30) and link preview jobs (§28) of a soft-deleted or gone message (a
|
|
10
|
+
// forward's media rows go with its original, as lib/media.js drop() does). PocketBase's cascades only run for
|
|
11
|
+
// deletes it makes itself.
|
|
12
|
+
// - `room_invites` (DESIGN.md §44): links that expired a day or more ago.
|
|
13
|
+
//
|
|
14
|
+
// Messages are never pruned: history is kept (DESIGN.md §2). A retention period for them would be the owner's choice.
|
|
15
|
+
|
|
16
|
+
/** The inbox rows kept per person, newest first. */
|
|
17
|
+
const INBOX_KEEP = 500;
|
|
18
|
+
/** No inbox row older than this many days. */
|
|
19
|
+
const INBOX_DAYS = 180;
|
|
20
|
+
|
|
21
|
+
/** A live message: there and not soft-deleted. @param {string} col */
|
|
22
|
+
const live = (col) => `EXISTS (SELECT 1 FROM messages m WHERE m.id = ${col} AND m.deleted = FALSE)`;
|
|
23
|
+
/** @param {string} table @param {string} col */
|
|
24
|
+
const there = (table, col) => `EXISTS (SELECT 1 FROM ${table} t WHERE t.id = ${col})`;
|
|
25
|
+
|
|
26
|
+
/** What a prune deletes, by name: [label, SQL]. {:before} is INBOX_DAYS ago. */
|
|
27
|
+
const STEPS = [
|
|
28
|
+
["inbox_old", "DELETE FROM inbox WHERE created < {:before}"],
|
|
29
|
+
[
|
|
30
|
+
"inbox_over",
|
|
31
|
+
`DELETE FROM inbox WHERE id IN (
|
|
32
|
+
SELECT id FROM (SELECT id, ROW_NUMBER() OVER (PARTITION BY user ORDER BY created DESC, id DESC) AS n FROM inbox)
|
|
33
|
+
WHERE n > ${INBOX_KEEP})`,
|
|
34
|
+
],
|
|
35
|
+
[
|
|
36
|
+
"inbox_orphans",
|
|
37
|
+
`DELETE FROM inbox WHERE NOT ${live("inbox.message")} OR NOT ${there("rooms", "inbox.room")}
|
|
38
|
+
OR NOT ${there("users", "inbox.user")} OR NOT ${there("users", "inbox.actor")}`,
|
|
39
|
+
],
|
|
40
|
+
[
|
|
41
|
+
"room_reads_orphans",
|
|
42
|
+
`DELETE FROM room_reads WHERE NOT ${there("users", "room_reads.user")} OR NOT ${there("rooms", "room_reads.room")}`,
|
|
43
|
+
],
|
|
44
|
+
["inbox_reads_orphans", `DELETE FROM inbox_reads WHERE NOT ${there("users", "inbox_reads.user")}`],
|
|
45
|
+
[
|
|
46
|
+
"hidden_rooms_orphans",
|
|
47
|
+
`DELETE FROM hidden_rooms WHERE NOT ${there("users", "hidden_rooms.user")} OR NOT ${there("rooms", "hidden_rooms.room")}`,
|
|
48
|
+
],
|
|
49
|
+
["media_orphans", `DELETE FROM media WHERE NOT ${live("media.message")} OR (media.source != '' AND NOT ${live("media.source")})`],
|
|
50
|
+
["link_queue_orphans", `DELETE FROM link_queue WHERE NOT ${live("link_queue.message")}`],
|
|
51
|
+
// Invite links that ran out a day or more ago (DESIGN.md §44): they no longer work, and the room's managers see none.
|
|
52
|
+
["room_invites_expired", "DELETE FROM room_invites WHERE expires < strftime('%Y-%m-%d %H:%M:%fZ', 'now', '-1 day')"],
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
/** PocketBase's date text for `ms`. @param {number} ms */
|
|
56
|
+
const dbDate = (ms) => new Date(ms).toISOString().replace("T", " ");
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The daily prune. Returns how many rows each step deleted; logs them when there were any.
|
|
60
|
+
* @param {core.App} app
|
|
61
|
+
* @param {number} [now]
|
|
62
|
+
* @returns {Record<string, number>}
|
|
63
|
+
*/
|
|
64
|
+
function prune(app, now = Date.now()) {
|
|
65
|
+
/** @type {Record<string, number>} */
|
|
66
|
+
const deleted = {};
|
|
67
|
+
const before = dbDate(now - INBOX_DAYS * 24 * 3600_000);
|
|
68
|
+
for (const [label, sql] of STEPS) {
|
|
69
|
+
try {
|
|
70
|
+
const res = app.db().newQuery(sql).bind({ before }).execute();
|
|
71
|
+
deleted[label] = Number(res.rowsAffected());
|
|
72
|
+
} catch (err) {
|
|
73
|
+
deleted[label] = -1;
|
|
74
|
+
app.logger().error("retention: a prune step failed", "step", label, "error", String(err));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (Object.values(deleted).some((n) => n !== 0)) app.logger().info("retention: pruned", ...Object.entries(deleted).flat());
|
|
78
|
+
const failed = Object.entries(deleted).filter(([, n]) => n < 0);
|
|
79
|
+
require(`${__hooks}/lib/worker.js`).noted(app, "retention", failed.length ? `failed: ${failed.map(([k]) => k).join(", ")}` : undefined);
|
|
80
|
+
return deleted;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** POST /api/retention/tick (superusers, tests): a prune now → { deleted: { step: rows } }. @param {core.RequestEvent} e */
|
|
84
|
+
function tick(e) {
|
|
85
|
+
return e.json(200, { deleted: prune(e.app) });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = { INBOX_KEEP, INBOX_DAYS, prune, tick };
|
package/pb_hooks/lib/site.js
CHANGED
|
@@ -20,7 +20,7 @@ function checkInvite(e) {
|
|
|
20
20
|
const site = siteOf(e.app);
|
|
21
21
|
if (!site?.getBool("invite_only")) return;
|
|
22
22
|
const want = site.getString("invite_code").trim();
|
|
23
|
-
const body = /** @type {Record<string, unknown>} */ (e.requestInfo().body);
|
|
23
|
+
const body = /** @type {Record<string, unknown>} */ (e.requestInfo().body ?? {});
|
|
24
24
|
const got = typeof body.invite_code === "string" ? body.invite_code.trim() : "";
|
|
25
25
|
if (want !== "" && got !== "" && $security.equal(got, want)) return;
|
|
26
26
|
const [code, message] =
|
|
@@ -99,4 +99,15 @@ function assetLinks(e) {
|
|
|
99
99
|
]);
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
/**
|
|
103
|
+
* site enrich: `password_reset`, whether "Forgot password?" is offered (§39). Only once a superuser set up SMTP
|
|
104
|
+
* (*Settings → Mail settings*): without it, the reset emails would go nowhere while the app said they were sent.
|
|
105
|
+
* @param {core.RecordEnrichEvent} e
|
|
106
|
+
*/
|
|
107
|
+
function showPasswordReset(e) {
|
|
108
|
+
if (!e.record) return;
|
|
109
|
+
e.record.withCustomData(true);
|
|
110
|
+
e.record.set("password_reset", e.app.settings().smtp.enabled);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = { siteOf, checkInvite, checkOAuth2Signup, manifest, assetLinks, showPasswordReset };
|
package/pb_hooks/lib/stats.js
CHANGED
|
@@ -4,12 +4,13 @@
|
|
|
4
4
|
//
|
|
5
5
|
// Online is who has a realtime connection now: every open web tab and the Android app while it is in front hold one,
|
|
6
6
|
// with at least one subscription. A connection's app (web or Android) is noted when it subscribes, from its
|
|
7
|
-
// User-Agent (the app's OkHttp sends "okhttp/…")
|
|
8
|
-
// runtime for as long as the connection is open (docs/PERF.md §6).
|
|
7
|
+
// User-Agent (the app's OkHttp sends "okhttp/…"), by server/stats.go: in Go, so a subscription takes no JS runtime.
|
|
9
8
|
//
|
|
10
9
|
// Every 5 minutes `run` writes a sample of those counts and marks everyone online active today; a subscription marks
|
|
11
|
-
// its account too, at most once an hour (so a visit shorter than 5 minutes counts). `site.no_stats`
|
|
10
|
+
// its account too, at most once an hour (so a visit shorter than 5 minutes counts; server/stats.go). `site.no_stats`
|
|
11
|
+
// stops both.
|
|
12
12
|
|
|
13
|
+
/** The client store key of a connection's app (server/stats.go statsAppKey). */
|
|
13
14
|
const KEY_APP = "chirkle_app";
|
|
14
15
|
const SAMPLE_MINUTES = 5;
|
|
15
16
|
const ACTIVITY_DAYS = 90;
|
|
@@ -19,13 +20,6 @@ const DAY_MS = 86_400_000;
|
|
|
19
20
|
/** @param {Date} d @returns {string} as PocketBase stores dates */
|
|
20
21
|
const dbDate = (d) => d.toISOString().replace("T", " ");
|
|
21
22
|
|
|
22
|
-
/** @param {string} ua @returns {"web" | "android" | "other"} */
|
|
23
|
-
function appOf(ua) {
|
|
24
|
-
if (/^okhttp\//i.test(ua)) return "android";
|
|
25
|
-
if (/^Mozilla\//.test(ua)) return "web";
|
|
26
|
-
return "other";
|
|
27
|
-
}
|
|
28
|
-
|
|
29
23
|
/** @param {core.App} app */
|
|
30
24
|
function off(app) {
|
|
31
25
|
return !!require(`${__hooks}/lib/site.js`).siteOf(app)?.getBool("no_stats");
|
|
@@ -51,28 +45,6 @@ function touch(app, users, at) {
|
|
|
51
45
|
}
|
|
52
46
|
}
|
|
53
47
|
|
|
54
|
-
/**
|
|
55
|
-
* realtime subscribe request: note the connection's app, and mark its account active (once an hour). Never fails the
|
|
56
|
-
* subscription.
|
|
57
|
-
* @param {core.RealtimeSubscribeRequestEvent} e
|
|
58
|
-
*/
|
|
59
|
-
function subscribed(e) {
|
|
60
|
-
try {
|
|
61
|
-
// (This event's e.request is undefined in the JS runtime; requestInfo has the headers, lowercased.)
|
|
62
|
-
e.client.set(KEY_APP, appOf(String(e.requestInfo().headers.user_agent ?? "")));
|
|
63
|
-
const user = e.auth && !e.auth.isSuperuser() && e.auth.collection().name === "users" ? e.auth.id : "";
|
|
64
|
-
if (!user) return;
|
|
65
|
-
const now = new Date();
|
|
66
|
-
const hour = now.toISOString().slice(0, 13);
|
|
67
|
-
const key = `stats.touched.${user}`;
|
|
68
|
-
if (e.app.store().get(key) === hour) return;
|
|
69
|
-
e.app.store().set(key, hour);
|
|
70
|
-
if (!off(e.app)) touch(e.app, [user], now);
|
|
71
|
-
} catch (err) {
|
|
72
|
-
e.app.logger().warn("stats: subscribe", "error", String(err));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
48
|
/**
|
|
77
49
|
* Who is online now: distinct accounts (also by app; someone on both counts in each) and guests' connections.
|
|
78
50
|
* Superusers (the admin UI) and connections that haven't subscribed to anything yet don't count.
|
|
@@ -154,7 +126,7 @@ function tick(e) {
|
|
|
154
126
|
}
|
|
155
127
|
|
|
156
128
|
/** Accounts of this server's own: not a linked server's people (§33), nor the Telegram bridge's stand-ins (§26). */
|
|
157
|
-
const LOCAL_USER = "
|
|
129
|
+
const LOCAL_USER = "u.kind NOT IN ('telegram', 'peer')";
|
|
158
130
|
|
|
159
131
|
const DAYS_SQL = `
|
|
160
132
|
SELECT date(m.created, {:shift}) AS day, COUNT(*) AS messages, COUNT(DISTINCT m.author) AS people
|
|
@@ -249,4 +221,4 @@ function get(e) {
|
|
|
249
221
|
});
|
|
250
222
|
}
|
|
251
223
|
|
|
252
|
-
module.exports = {
|
|
224
|
+
module.exports = { online, run, prune, tick, get };
|
package/pb_hooks/lib/users.js
CHANGED
|
@@ -5,12 +5,58 @@
|
|
|
5
5
|
// of ASCII letters (QA BUG-55). `name_key` is hidden from clients, hence a route rather than a filter.
|
|
6
6
|
|
|
7
7
|
const { assertImagesFit } = require(`${__hooks}/lib/images.js`);
|
|
8
|
-
const { cleanName, originalNames, recordOf, setNameKey } = require(`${__hooks}/lib/records.js`);
|
|
8
|
+
const { cleanName, fieldError, originalNames, recordOf, setNameKey } = require(`${__hooks}/lib/records.js`);
|
|
9
9
|
const { nameKey } = require(`${__hooks}/lib/text.js`);
|
|
10
10
|
|
|
11
11
|
/** Most people one search returns. */
|
|
12
12
|
const LIMIT = 20;
|
|
13
13
|
|
|
14
|
+
/** Most bytes of a password: bcrypt reads 72, PocketBase takes 71. */
|
|
15
|
+
const PASSWORD_MAX_BYTES = 71;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Signup, password change, password reset: a password over PocketBase's limit is refused with the reason. Past 72 bytes
|
|
19
|
+
* PocketBase answered 400 with none ("Failed to create record.", "Invalid value."), which a password manager's long
|
|
20
|
+
* password ran into (QA BUG-267).
|
|
21
|
+
* @param {unknown} password
|
|
22
|
+
*/
|
|
23
|
+
function checkPasswordLength(password) {
|
|
24
|
+
if (typeof password !== "string") return;
|
|
25
|
+
let bytes = 0;
|
|
26
|
+
for (const c of password) {
|
|
27
|
+
const n = /** @type {number} */ (c.codePointAt(0));
|
|
28
|
+
bytes += n < 0x80 ? 1 : n < 0x800 ? 2 : n < 0x10000 ? 3 : 4;
|
|
29
|
+
}
|
|
30
|
+
if (bytes > PASSWORD_MAX_BYTES) {
|
|
31
|
+
throw fieldError(
|
|
32
|
+
"password",
|
|
33
|
+
"Use at most 71 characters (fewer if some aren't plain Latin letters, digits or punctuation).",
|
|
34
|
+
"validation_max_text_constraint",
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** What a deleted account is called (§43). Nobody else may take the name. */
|
|
40
|
+
const DELETED_NAME = "Deleted account";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* What kind of account `user` is (§5.1, `users.kind`): "person" (signs in here), "telegram" (a Telegram sender's
|
|
44
|
+
* stand-in, §26.3), "peer" (a linked instance's person, §33.4) or "deleted" (its person deleted it, §43). Never set: a
|
|
45
|
+
* person.
|
|
46
|
+
* @param {core.Record} user
|
|
47
|
+
* @returns {"person" | "telegram" | "peer" | "deleted"}
|
|
48
|
+
*/
|
|
49
|
+
function kindOf(user) {
|
|
50
|
+
const kind = user.getString("kind");
|
|
51
|
+
return kind === "telegram" || kind === "peer" || kind === "deleted" ? kind : "person";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A stand-in: an account nobody signs in to here (a Telegram sender's, another instance's person's, a deleted one).
|
|
56
|
+
* @param {core.Record} user
|
|
57
|
+
*/
|
|
58
|
+
const isStandIn = (user) => kindOf(user) !== "person";
|
|
59
|
+
|
|
14
60
|
/** @param {core.RequestEvent} e */
|
|
15
61
|
function searchUsers(e) {
|
|
16
62
|
const me = e.auth;
|
|
@@ -21,11 +67,19 @@ function searchUsers(e) {
|
|
|
21
67
|
// `~` treats a % in the value as a wildcard (and then stops adding its own): escape it (QA BUG-34). `_` is escaped.
|
|
22
68
|
// "@ann" finds Ann by her handle (§41) as well as by name.
|
|
23
69
|
const handle = key.replace(/^@/, "");
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
70
|
+
// Not a deleted account (§43): there is nobody to message.
|
|
71
|
+
const found = e.app.findRecordsByFilter(
|
|
72
|
+
"users",
|
|
73
|
+
"(name_key ~ {:q} || handle ~ {:h}) && id != {:me} && kind != 'deleted'",
|
|
74
|
+
"name",
|
|
75
|
+
LIMIT,
|
|
76
|
+
0,
|
|
77
|
+
{
|
|
78
|
+
q: key.replace(/%/g, "\\%"),
|
|
79
|
+
h: handle.replace(/%/g, "\\%"),
|
|
80
|
+
me: me.id,
|
|
81
|
+
},
|
|
82
|
+
);
|
|
29
83
|
// Only the public profile, never the email (§6).
|
|
30
84
|
return e.json(
|
|
31
85
|
200,
|
|
@@ -56,7 +110,22 @@ function beforeUserSave(e) {
|
|
|
56
110
|
const rec = recordOf(e);
|
|
57
111
|
const email = rec.getString("email");
|
|
58
112
|
if (rec.isNew() || email !== rec.original().getString("email")) rec.set("email", email.trim().toLowerCase());
|
|
113
|
+
// Signups and accounts a superuser makes are people; the bridges set their stand-ins' kind before saving.
|
|
114
|
+
if (rec.isNew() && !rec.getString("kind")) rec.set("kind", "person");
|
|
115
|
+
if (kindOf(rec) === "deleted") {
|
|
116
|
+
// A deleted account (§43, lib/account.js erase): one name for all of them, each with a key of its own (no name
|
|
117
|
+
// is cleaned to a control character, so no one else's key is ever this), and no handle.
|
|
118
|
+
rec.set("name", DELETED_NAME);
|
|
119
|
+
rec.set("name_key", `\u0001deleted ${rec.id}`);
|
|
120
|
+
rec.set("handle", "");
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
59
123
|
cleanName(rec);
|
|
124
|
+
// "Deleted account" is what deleted accounts are called (§43): taken, by them.
|
|
125
|
+
const renamed = rec.isNew() || rec.getString("name") !== rec.original().getString("name");
|
|
126
|
+
if (renamed && nameKey(rec.getString("name")) === nameKey(DELETED_NAME)) {
|
|
127
|
+
throw fieldError("name", "This display name is taken. Choose another one.", "validation_not_unique");
|
|
128
|
+
}
|
|
60
129
|
if (!rec.isNew() && rec.getString("name") === rec.original().getString("name")) {
|
|
61
130
|
// Name unchanged: keep its key. Accounts named before name_key existed may share a name (the migration made
|
|
62
131
|
// their keys unique), and must still be able to change their picture or password.
|
|
@@ -83,4 +152,16 @@ function findLoginByEmail(e) {
|
|
|
83
152
|
}
|
|
84
153
|
}
|
|
85
154
|
|
|
86
|
-
|
|
155
|
+
/**
|
|
156
|
+
* users request-password-reset (§39): no email to a stand-in (Telegram, another instance): its address is made
|
|
157
|
+
* up (`….invalid`) and nobody can log in as it (`kind`, §5.1). Answered like any other request, as PocketBase does for an unknown
|
|
158
|
+
* address, so it doesn't tell which addresses have accounts. Only called for an address that matched: PocketBase
|
|
159
|
+
* answers an unknown one before the hooks, so the clients send it in lower case, the way emails are stored (QA BUG-47).
|
|
160
|
+
* @param {core.RecordRequestPasswordResetRequestEvent} e
|
|
161
|
+
*/
|
|
162
|
+
function skipStandInReset(e) {
|
|
163
|
+
if (e.record && isStandIn(e.record)) return e.noContent(204);
|
|
164
|
+
return e.next();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
module.exports = { DELETED_NAME, beforeUserSave, checkPasswordLength, findLoginByEmail, isStandIn, kindOf, searchUsers, skipStandInReset };
|
package/pb_hooks/lib/worker.js
CHANGED
|
@@ -9,6 +9,9 @@
|
|
|
9
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
10
|
// looks for work only when a save wakes it (`wake`, after the save is committed) and every IDLE_MS, not every
|
|
11
11
|
// second (docs/PERF.md). The pass every IDLE_MS finds what was queued some other way (a migration, raw SQL).
|
|
12
|
+
//
|
|
13
|
+
// Each pass notes when it ran (`<name>.last`, epoch ms) and a failed one its error (`<name>.error`, "<ms> <error>"),
|
|
14
|
+
// in the app store: GET /api/chat/metrics (server/metrics.go) reports them, with the lock's last renew.
|
|
12
15
|
|
|
13
16
|
/** How long one run goes on: the next one starts on the next minute. */
|
|
14
17
|
const RUN_MS = 57_000;
|
|
@@ -68,6 +71,18 @@ function unlock(app, name, me) {
|
|
|
68
71
|
* @property {boolean} [wakes] its work is queued by saves that call `wake`: idle, it passes only when woken
|
|
69
72
|
*/
|
|
70
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Note a pass of worker `name`: when, and its error if it failed (REFACTOR item 51).
|
|
76
|
+
* @param {core.App} app
|
|
77
|
+
* @param {string} name
|
|
78
|
+
* @param {unknown} [err]
|
|
79
|
+
*/
|
|
80
|
+
function noted(app, name, err) {
|
|
81
|
+
const now = Date.now();
|
|
82
|
+
app.store().set(`${name}.last`, String(now));
|
|
83
|
+
if (err !== undefined) app.store().set(`${name}.error`, `${now} ${String(err).slice(0, 300)}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
71
86
|
/** The app store's key of worker `name`'s wake-up. @param {string} name */
|
|
72
87
|
const wakeKey = (name) => `${name}.wake`;
|
|
73
88
|
|
|
@@ -122,8 +137,10 @@ function run(app, w) {
|
|
|
122
137
|
busy = false;
|
|
123
138
|
try {
|
|
124
139
|
busy = !!w.pass(renew);
|
|
140
|
+
noted(app, w.name);
|
|
125
141
|
} catch (err) {
|
|
126
142
|
if (isLost(err)) return app.logger().warn(`${w.label}: worker took too long, another run took over`);
|
|
143
|
+
noted(app, w.name, err);
|
|
127
144
|
app.logger().error(`${w.label}: worker`, "error", String(err));
|
|
128
145
|
}
|
|
129
146
|
if (!busy) sleep(1000);
|
|
@@ -150,10 +167,14 @@ function once(app, w) {
|
|
|
150
167
|
w.pass(() => {
|
|
151
168
|
if (!beat(app, w.name, me)) throw new Error(LOST);
|
|
152
169
|
});
|
|
170
|
+
noted(app, w.name);
|
|
171
|
+
} catch (err) {
|
|
172
|
+
if (!isLost(err)) noted(app, w.name, err);
|
|
173
|
+
throw err;
|
|
153
174
|
} finally {
|
|
154
175
|
unlock(app, w.name, me);
|
|
155
176
|
}
|
|
156
177
|
return true;
|
|
157
178
|
}
|
|
158
179
|
|
|
159
|
-
module.exports = { run, once, wake, isLost, lock, beat, unlock };
|
|
180
|
+
module.exports = { run, once, wake, isLost, lock, beat, unlock, noted };
|