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.
Files changed (137) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +310 -2
  3. package/package.json +75 -4
  4. package/pb_hooks/federation/config.js +276 -0
  5. package/pb_hooks/federation/inbox.js +316 -0
  6. package/pb_hooks/federation/messages.js +420 -0
  7. package/pb_hooks/federation/mirror.js +277 -0
  8. package/pb_hooks/federation/net.js +247 -0
  9. package/pb_hooks/federation/peers.js +490 -0
  10. package/pb_hooks/federation/people.js +376 -0
  11. package/pb_hooks/federation/rooms.js +552 -0
  12. package/pb_hooks/federation/worker.js +413 -0
  13. package/pb_hooks/federation.pb.js +141 -0
  14. package/pb_hooks/lib/access.js +102 -0
  15. package/pb_hooks/lib/account.js +31 -0
  16. package/pb_hooks/lib/anonymous.js +189 -0
  17. package/pb_hooks/lib/bulk.js +187 -0
  18. package/pb_hooks/lib/collections.js +587 -0
  19. package/pb_hooks/lib/dms.js +123 -0
  20. package/pb_hooks/lib/emoji.js +28 -0
  21. package/pb_hooks/lib/enrich.js +75 -0
  22. package/pb_hooks/lib/features.js +31 -0
  23. package/pb_hooks/lib/files.js +375 -0
  24. package/pb_hooks/lib/http.js +146 -0
  25. package/pb_hooks/lib/images.js +160 -0
  26. package/pb_hooks/lib/keepalive.js +31 -0
  27. package/pb_hooks/lib/lists.js +173 -0
  28. package/pb_hooks/lib/media.js +635 -0
  29. package/pb_hooks/lib/messages.js +519 -0
  30. package/pb_hooks/lib/moderation.js +128 -0
  31. package/pb_hooks/lib/outbound.js +116 -0
  32. package/pb_hooks/lib/pages.js +171 -0
  33. package/pb_hooks/lib/previews.js +346 -0
  34. package/pb_hooks/lib/reactions.js +110 -0
  35. package/pb_hooks/lib/reads.js +181 -0
  36. package/pb_hooks/lib/records.js +77 -0
  37. package/pb_hooks/lib/rooms.js +148 -0
  38. package/pb_hooks/lib/site.js +102 -0
  39. package/pb_hooks/lib/text.js +110 -0
  40. package/pb_hooks/lib/users.js +81 -0
  41. package/pb_hooks/lib/worker.js +159 -0
  42. package/pb_hooks/main.pb.js +324 -0
  43. package/pb_hooks/telegram/api.js +128 -0
  44. package/pb_hooks/telegram/inbound.js +548 -0
  45. package/pb_hooks/telegram/links.js +157 -0
  46. package/pb_hooks/telegram/outbound.js +323 -0
  47. package/pb_hooks/telegram/people.js +166 -0
  48. package/pb_hooks/telegram/rich.js +192 -0
  49. package/pb_hooks/telegram/worker.js +181 -0
  50. package/pb_hooks/telegram.pb.js +47 -0
  51. package/pb_migrations/1760000000_init.js +150 -0
  52. package/pb_migrations/1760000001_qa_fixes.js +69 -0
  53. package/pb_migrations/1760000002_avatars.js +39 -0
  54. package/pb_migrations/1760000003_direct_messages.js +142 -0
  55. package/pb_migrations/1760000004_attachments.js +28 -0
  56. package/pb_migrations/1760000005_file_names.js +16 -0
  57. package/pb_migrations/1760000006_room_reads.js +41 -0
  58. package/pb_migrations/1760000007_replies.js +23 -0
  59. package/pb_migrations/1760000008_lowercase_emails.js +24 -0
  60. package/pb_migrations/1760000009_reactions.js +32 -0
  61. package/pb_migrations/1760000010_fit_thumbs.js +18 -0
  62. package/pb_migrations/1760000011_forwarding.js +39 -0
  63. package/pb_migrations/1760000012_accounts.js +55 -0
  64. package/pb_migrations/1760000013_qa_pass5.js +19 -0
  65. package/pb_migrations/1760000014_room_access.js +96 -0
  66. package/pb_migrations/1760000015_site.js +46 -0
  67. package/pb_migrations/1760000016_forward_refs.js +43 -0
  68. package/pb_migrations/1760000017_no_dms.js +18 -0
  69. package/pb_migrations/1760000018_attachment_limits.js +24 -0
  70. package/pb_migrations/1760000019_telegram.js +109 -0
  71. package/pb_migrations/1760000020_service_messages.js +21 -0
  72. package/pb_migrations/1760000021_forward_view_index.js +41 -0
  73. package/pb_migrations/1760000022_link_previews.js +43 -0
  74. package/pb_migrations/1760000023_room_writers.js +49 -0
  75. package/pb_migrations/1760000024_message_search.js +51 -0
  76. package/pb_migrations/1760000025_search_dotted_i.js +16 -0
  77. package/pb_migrations/1760000026_push.js +67 -0
  78. package/pb_migrations/1760000027_push_tokens_per_user.js +31 -0
  79. package/pb_migrations/1760000028_media_grid_thumbs.js +18 -0
  80. package/pb_migrations/1760000029_media.js +71 -0
  81. package/pb_migrations/1760000030_collections.js +140 -0
  82. package/pb_migrations/1760000031_unread_limit.js +20 -0
  83. package/pb_migrations/1760000032_federation.js +227 -0
  84. package/pb_migrations/1760000033_federation_requests.js +44 -0
  85. package/pb_migrations/1760000034_telegram_parts.js +26 -0
  86. package/pb_migrations/1760000035_telegram_anonymous.js +16 -0
  87. package/pb_migrations/1760000036_chirkle.js +20 -0
  88. package/pb_migrations/1760000037_file_dims.js +35 -0
  89. package/pb_migrations/1760000038_drop_push.js +64 -0
  90. package/pb_migrations/1760000039_anonymous_rooms.js +40 -0
  91. package/pb_migrations/1760000040_moderators.js +42 -0
  92. package/pb_migrations/1760000041_shared_files.js +500 -0
  93. package/pb_migrations/1760000042_files_src.js +17 -0
  94. package/pb_public/assets/access-ALBW2MT2.js +2 -0
  95. package/pb_public/assets/access-ALBW2MT2.js.map +7 -0
  96. package/pb_public/assets/access-PB77EH4F.css +2 -0
  97. package/pb_public/assets/access-PB77EH4F.css.map +7 -0
  98. package/pb_public/assets/app-ECR34OBK.css +2 -0
  99. package/pb_public/assets/app-ECR34OBK.css.map +7 -0
  100. package/pb_public/assets/app-SILEBEXB.js +3 -0
  101. package/pb_public/assets/app-SILEBEXB.js.map +7 -0
  102. package/pb_public/assets/collections-VEOIOCAW.css +2 -0
  103. package/pb_public/assets/collections-VEOIOCAW.css.map +7 -0
  104. package/pb_public/assets/emoji-WSTVXAW5.css +2 -0
  105. package/pb_public/assets/emoji-WSTVXAW5.css.map +7 -0
  106. package/pb_public/assets/emoji-picker-QYRX6NYL.js +609 -0
  107. package/pb_public/assets/emoji-picker-QYRX6NYL.js.map +7 -0
  108. package/pb_public/assets/inter-cyrillic-JEOLYBOO.woff2 +0 -0
  109. package/pb_public/assets/inter-cyrillic-ext-IYF56FF6.woff2 +0 -0
  110. package/pb_public/assets/inter-greek-IRE366VL.woff2 +0 -0
  111. package/pb_public/assets/inter-latin-NRMW37G5.woff2 +0 -0
  112. package/pb_public/assets/inter-latin-ext-HA22NDSG.woff2 +0 -0
  113. package/pb_public/assets/lazy-SCCVSUZO.js +2 -0
  114. package/pb_public/assets/lazy-SCCVSUZO.js.map +7 -0
  115. package/pb_public/assets/media-DH7ECZSF.js +2 -0
  116. package/pb_public/assets/media-DH7ECZSF.js.map +7 -0
  117. package/pb_public/assets/media-L3EYTHKT.css +2 -0
  118. package/pb_public/assets/media-L3EYTHKT.css.map +7 -0
  119. package/pb_public/assets/notify-L6GERFNO.js +2 -0
  120. package/pb_public/assets/notify-L6GERFNO.js.map +7 -0
  121. package/pb_public/assets/search-3UT4HMES.js +2 -0
  122. package/pb_public/assets/search-3UT4HMES.js.map +7 -0
  123. package/pb_public/assets/switcher-CEUKRLEY.js +2 -0
  124. package/pb_public/assets/switcher-CEUKRLEY.js.map +7 -0
  125. package/pb_public/assets/viewer-DXT7SKOU.js +2 -0
  126. package/pb_public/assets/viewer-DXT7SKOU.js.map +7 -0
  127. package/pb_public/assets/viewer-P7GYCBYM.css +2 -0
  128. package/pb_public/assets/viewer-P7GYCBYM.css.map +7 -0
  129. package/pb_public/icons/apple-touch-icon.png +0 -0
  130. package/pb_public/icons/icon-192.png +0 -0
  131. package/pb_public/icons/icon-512.png +0 -0
  132. package/pb_public/icons/icon.svg +1 -0
  133. package/pb_public/icons/maskable-512.png +0 -0
  134. package/pb_public/index.html +21 -0
  135. package/pb_public/sw.js +1 -0
  136. package/scripts/fetch-pocketbase.sh +63 -0
  137. package/scripts/start.mjs +93 -0
@@ -0,0 +1,24 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // Bigger attachments (DESIGN.md §17.1): up to 10 files per message, 20 MB each, as many and as large as a Telegram
3
+ // album the bridge (§26) brings over. The request body limit grows with them (pb_hooks/lib/http.js MESSAGE_BODY_LIMIT).
4
+ // Going down keeps the files already stored: PocketBase checks the limits on uploads only.
5
+
6
+ /** @typedef {Parameters<Parameters<typeof migrate>[0]>[0]} MigrationApp */
7
+
8
+ /**
9
+ * @param {MigrationApp} app
10
+ * @param {number} maxSelect
11
+ * @param {number} maxSize
12
+ */
13
+ function setLimits(app, maxSelect, maxSize) {
14
+ const messages = app.findCollectionByNameOrId("messages");
15
+ const files = /** @type {core.FileField} */ (messages.fields.getByName("files"));
16
+ files.maxSelect = maxSelect;
17
+ files.maxSize = maxSize;
18
+ app.save(messages);
19
+ }
20
+
21
+ migrate(
22
+ (app) => setLimits(app, 10, 20 * 1024 * 1024),
23
+ (app) => setLimits(app, 4, 8 * 1024 * 1024),
24
+ );
@@ -0,0 +1,109 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // Telegram bridge (DESIGN.md §26, pb_hooks/telegram.pb.js): its own collections, all named tg_*, readable and writable by
3
+ // superusers only (null rules). Nothing outside the bridge reads them, so removing the bridge can leave them, or go down
4
+ // this migration to drop them.
5
+ // - tg_links: a Telegram group and the room it is linked to. The bridge adds a row (no room, disabled) for every group
6
+ // it is added to; a superuser picks the room and enables it in the admin UI.
7
+ // - tg_people: a Telegram sender and the stand-in user ("Ann (TG)") whose name their messages show under.
8
+ // - tg_messages: which Telegram messages a chat message is, both ways, for replies, edits and deletes.
9
+ // - tg_queue: work for the bridge's worker: messages to send to Telegram, edits and deletes to pass on, and the items
10
+ // of a Telegram album waiting for the rest of it.
11
+
12
+ /** @param {Record<string, unknown>} data */
13
+ const tgCollection = (data) =>
14
+ new Collection(
15
+ /** @type {any} */ ({ type: "base", listRule: null, viewRule: null, createRule: null, updateRule: null, deleteRule: null, ...data }),
16
+ );
17
+
18
+ const NAMES = ["tg_queue", "tg_messages", "tg_people", "tg_links"];
19
+
20
+ migrate(
21
+ (app) => {
22
+ const rooms = app.findCollectionByNameOrId("rooms");
23
+ const users = app.findCollectionByNameOrId("users");
24
+ const messages = app.findCollectionByNameOrId("messages");
25
+ const stamps = [
26
+ { type: "autodate", name: "created", onCreate: true, onUpdate: false },
27
+ { type: "autodate", name: "updated", onCreate: true, onUpdate: true },
28
+ ];
29
+
30
+ app.save(
31
+ tgCollection({
32
+ name: "tg_links",
33
+ fields: [
34
+ { type: "text", name: "chat_id", required: true, max: 32 },
35
+ { type: "text", name: "title", max: 255 },
36
+ { type: "relation", name: "room", maxSelect: 1, collectionId: rooms.id, cascadeDelete: true },
37
+ { type: "bool", name: "enabled" },
38
+ ...stamps,
39
+ ],
40
+ indexes: ["CREATE UNIQUE INDEX idx_tg_links_chat ON tg_links (chat_id)", "CREATE INDEX idx_tg_links_room ON tg_links (room)"],
41
+ }),
42
+ );
43
+
44
+ app.save(
45
+ tgCollection({
46
+ name: "tg_people",
47
+ fields: [
48
+ { type: "text", name: "tg_user_id", required: true, max: 32 },
49
+ { type: "relation", name: "user", required: true, maxSelect: 1, collectionId: users.id, cascadeDelete: true },
50
+ { type: "text", name: "name", max: 255 },
51
+ { type: "text", name: "username", max: 64 },
52
+ { type: "text", name: "photo", max: 255 },
53
+ { type: "date", name: "photo_checked" },
54
+ ...stamps,
55
+ ],
56
+ indexes: [
57
+ "CREATE UNIQUE INDEX idx_tg_people_tg_user ON tg_people (tg_user_id)",
58
+ "CREATE UNIQUE INDEX idx_tg_people_user ON tg_people (user)",
59
+ ],
60
+ }),
61
+ );
62
+
63
+ app.save(
64
+ tgCollection({
65
+ name: "tg_messages",
66
+ fields: [
67
+ { type: "relation", name: "message", required: true, maxSelect: 1, collectionId: messages.id, cascadeDelete: true },
68
+ { type: "text", name: "chat_id", required: true, max: 32 },
69
+ { type: "number", name: "tg_message_id", required: true, onlyInt: true },
70
+ { type: "select", name: "direction", required: true, maxSelect: 1, values: ["in", "out"] },
71
+ // Which Telegram message holds the text: "text" (a message) or "caption" (a file's); "media" holds none.
72
+ { type: "select", name: "kind", required: true, maxSelect: 1, values: ["text", "caption", "media"] },
73
+ ...stamps,
74
+ ],
75
+ indexes: [
76
+ "CREATE UNIQUE INDEX idx_tg_messages_tg ON tg_messages (chat_id, tg_message_id)",
77
+ "CREATE INDEX idx_tg_messages_message ON tg_messages (message)",
78
+ ],
79
+ }),
80
+ );
81
+
82
+ app.save(
83
+ tgCollection({
84
+ name: "tg_queue",
85
+ fields: [
86
+ { type: "select", name: "op", required: true, maxSelect: 1, values: ["send", "edit", "delete", "album"] },
87
+ // send, edit, delete: the chat message.
88
+ { type: "text", name: "message", max: 15 },
89
+ { type: "text", name: "chat_id", max: 32 },
90
+ // album: the media_group_id, and the Telegram message itself.
91
+ { type: "text", name: "album", max: 64 },
92
+ { type: "json", name: "payload", maxSize: 65536 },
93
+ { type: "number", name: "tries", onlyInt: true },
94
+ { type: "date", name: "next_at" },
95
+ { type: "date", name: "locked_until" },
96
+ { type: "text", name: "error", max: 1000 },
97
+ ...stamps,
98
+ ],
99
+ indexes: [
100
+ "CREATE INDEX idx_tg_queue_created ON tg_queue (created)",
101
+ "CREATE INDEX idx_tg_queue_album ON tg_queue (chat_id, album)",
102
+ ],
103
+ }),
104
+ );
105
+ },
106
+ (app) => {
107
+ for (const name of NAMES) app.delete(app.findCollectionByNameOrId(name));
108
+ },
109
+ );
@@ -0,0 +1,21 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // Service messages (DESIGN.md §5.3): a message without an author is the app's own notice in a room ("Telegram group
3
+ // … was disconnected"), made only by server code (lib/messages.js postService). The create rule still requires
4
+ // `author = @request.auth.id` and a signed-in caller, so no request can make one, and no update rule matches one.
5
+ // Deleting a user would now clear `author` on their messages rather than fail: a users delete hook
6
+ // (lib/messages.js beforeUserDelete) refuses it while they have any, as the required relation did.
7
+ // Going down drops them, since `author` is required again.
8
+
9
+ migrate(
10
+ (app) => {
11
+ const messages = app.findCollectionByNameOrId("messages");
12
+ /** @type {any} */ (messages.fields.getByName("author")).required = false;
13
+ app.save(messages);
14
+ },
15
+ (app) => {
16
+ for (const rec of app.findRecordsByFilter("messages", "author = ''", "", 0, 0)) if (rec) app.delete(rec);
17
+ const messages = app.findCollectionByNameOrId("messages");
18
+ /** @type {any} */ (messages.fields.getByName("author")).required = true;
19
+ app.save(messages);
20
+ },
21
+ );
@@ -0,0 +1,41 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // The messages view rule's forward condition (1760000016_forward_refs.js) as a back-relation, with an index on
3
+ // `forward_of`. PocketBase joins `@collection.messages:fwd` without a condition: every expanded message (a page's
4
+ // reply_to) was checked against every message there is, times their rooms' members, and a page with replies took
5
+ // ~20 s on a busy instance. `messages_via_forward_of` joins on `forward_of = id`, which the index finds at once. Same
6
+ // meaning: the "any of" operators all test the same joined forward, as with the alias.
7
+
8
+ const OLD = "@collection.messages:fwd.";
9
+ const NEW = "messages_via_forward_of.";
10
+ const INDEX = "CREATE INDEX `idx_messages_forward_of` ON `messages` (`forward_of`)";
11
+ /** The alias form's link to this message: the back-relation's join says it. */
12
+ const OLD_LINK = `${OLD}forward_of ?= id && `;
13
+ const NEW_LINK = `${NEW}id ?!= "" && `;
14
+
15
+ /**
16
+ * @param {string | null | undefined} rule
17
+ * @param {boolean} up
18
+ */
19
+ function rewrite(rule, up) {
20
+ const [fromLink, toLink, from, to] = up ? [OLD_LINK, NEW_LINK, OLD, NEW] : [NEW_LINK, OLD_LINK, NEW, OLD];
21
+ const r = rule ?? "";
22
+ if (!r.includes(fromLink)) throw new Error(`forward view migration: expected ${fromLink} in ${r}`);
23
+ return r.replace(fromLink, toLink).split(from).join(to);
24
+ }
25
+
26
+ /**
27
+ * @param {Parameters<Parameters<typeof migrate>[0]>[0]} app
28
+ * @param {boolean} up
29
+ */
30
+ function apply(app, up) {
31
+ const messages = app.findCollectionByNameOrId("messages");
32
+ messages.viewRule = rewrite(messages.viewRule, up);
33
+ const indexes = /** @type {string[]} */ (/** @type {any} */ (messages).indexes).filter((i) => i !== INDEX);
34
+ /** @type {any} */ (messages).indexes = up ? [...indexes, INDEX] : indexes;
35
+ app.save(messages);
36
+ }
37
+
38
+ migrate(
39
+ (app) => apply(app, true),
40
+ (app) => apply(app, false),
41
+ );
@@ -0,0 +1,43 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // Link previews (DESIGN.md §28): what the links in a message show under it, a favicon and a title each.
3
+ // - messages.previews: [{ url, title, icon }], set by the server only (pb_hooks/lib/previews.js); icon is a data: URL.
4
+ // - link_queue: messages whose links the previews worker is still to look at. No API access.
5
+
6
+ const NO_PREVIEWS = "@request.body.previews:isset = false";
7
+
8
+ migrate(
9
+ (app) => {
10
+ const messages = app.findCollectionByNameOrId("messages");
11
+ messages.fields.add(new JSONField(/** @type {any} */ ({ name: "previews", maxSize: 131072 })));
12
+ messages.createRule = `${messages.createRule} && ${NO_PREVIEWS}`;
13
+ messages.updateRule = `${messages.updateRule} && ${NO_PREVIEWS}`;
14
+ app.save(messages);
15
+
16
+ app.save(
17
+ new Collection(
18
+ /** @type {any} */ ({
19
+ type: "base",
20
+ name: "link_queue",
21
+ listRule: null,
22
+ viewRule: null,
23
+ createRule: null,
24
+ updateRule: null,
25
+ deleteRule: null,
26
+ fields: [
27
+ { type: "relation", name: "message", required: true, maxSelect: 1, collectionId: messages.id, cascadeDelete: true },
28
+ { type: "autodate", name: "created", onCreate: true, onUpdate: false },
29
+ ],
30
+ indexes: ["CREATE INDEX idx_link_queue_created ON link_queue (created)"],
31
+ }),
32
+ ),
33
+ );
34
+ },
35
+ (app) => {
36
+ app.delete(app.findCollectionByNameOrId("link_queue"));
37
+ const messages = app.findCollectionByNameOrId("messages");
38
+ messages.fields.removeByName("previews");
39
+ messages.createRule = (messages.createRule ?? "").replace(` && ${NO_PREVIEWS}`, "");
40
+ messages.updateRule = (messages.updateRule ?? "").replace(` && ${NO_PREVIEWS}`, "");
41
+ app.save(messages);
42
+ },
43
+ );
@@ -0,0 +1,49 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // Who can post in a room (DESIGN.md §23.6): everyone who can read it, or only the people its creator chose.
3
+ // - `rooms.posting`: "everyone" (the default, and every room from before) or "selected". DMs are always "everyone".
4
+ // - `rooms.writers`: who may post when "selected" (up to ROOM_MAX_WRITERS); only the creator changes it. The hooks keep
5
+ // it empty otherwise, always list the creator, and in a private room keep only its members.
6
+ // - Posting, editing and deleting a message need it on top of seeing the room. Reacting doesn't: readers may react.
7
+
8
+ const ROOM_MAX_WRITERS = 100;
9
+
10
+ /** The caller may post in the room in `rel` (seeing it is checked apart). */
11
+ const WRITER = (/** @type {string} */ rel) =>
12
+ `(${rel}posting != "selected" || ${rel}writers.id ?= @request.auth.id || ${rel}created_by = @request.auth.id)`;
13
+
14
+ const WRITERS_UNSET = "@request.body.writers:isset = false";
15
+
16
+ migrate(
17
+ (app) => {
18
+ const users = app.findCollectionByNameOrId("users");
19
+ const rooms = app.findCollectionByNameOrId("rooms");
20
+ rooms.fields.add(new SelectField({ name: "posting", required: true, maxSelect: 1, values: ["everyone", "selected"] }));
21
+ rooms.fields.add(
22
+ new RelationField(
23
+ /** @type {any} */ ({ name: "writers", maxSelect: ROOM_MAX_WRITERS, collectionId: users.id, cascadeDelete: false }),
24
+ ),
25
+ );
26
+ // A room starts with everyone posting, or only its creator: nobody else is chosen yet.
27
+ rooms.createRule = `${rooms.createRule} && ${WRITERS_UNSET}`;
28
+ app.save(rooms);
29
+ app.db().newQuery("UPDATE rooms SET posting = 'everyone', writers = '[]'").execute();
30
+
31
+ const messages = app.findCollectionByNameOrId("messages");
32
+ messages.createRule = `${messages.createRule} && ${WRITER("@request.body.room.")}`;
33
+ // Someone who may no longer post can't edit or delete what they wrote there either, as in an archived room.
34
+ messages.updateRule = `${messages.updateRule} && ${WRITER("room.")}`;
35
+ app.save(messages);
36
+ },
37
+ (app) => {
38
+ const messages = app.findCollectionByNameOrId("messages");
39
+ messages.createRule = (messages.createRule ?? "").replace(` && ${WRITER("@request.body.room.")}`, "");
40
+ messages.updateRule = (messages.updateRule ?? "").replace(` && ${WRITER("room.")}`, "");
41
+ app.save(messages);
42
+
43
+ const rooms = app.findCollectionByNameOrId("rooms");
44
+ rooms.createRule = (rooms.createRule ?? "").replace(` && ${WRITERS_UNSET}`, "");
45
+ rooms.fields.removeByName("writers");
46
+ rooms.fields.removeByName("posting");
47
+ app.save(rooms);
48
+ },
49
+ );
@@ -0,0 +1,51 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // Message search (DESIGN.md §29): messages.text_key, the text case folded like names are compared (§22.1), set by
3
+ // pb_hooks on every save. Searching filters on it (`text_key ~ {:word}`), so "привет" finds "Привет" and "strasse"
4
+ // "Straße": SQLite's LIKE only ignores the case of ASCII letters (QA BUG-55). Not hidden, so the list API (and its
5
+ // rules) can filter on it; clients never ask for it (MSG_FIELDS).
6
+
7
+ /**
8
+ * Same as nameKey() in pb_hooks/lib/text.js (migrations can't load the hooks' modules).
9
+ * @param {string} text
10
+ */
11
+ function textKey(text) {
12
+ const s = text.replace(/[­᠎​-‏‪-‮⁠-⁤⁦-⁩]/g, "");
13
+ return (typeof s.normalize === "function" ? s.normalize("NFKC") : s).toUpperCase().toLowerCase();
14
+ }
15
+
16
+ /** Messages backfilled per query. */
17
+ const BATCH = 500;
18
+
19
+ migrate(
20
+ (app) => {
21
+ const messages = app.findCollectionByNameOrId("messages");
22
+ // NFKC can make text longer (one ligature becomes up to 18 letters): room for any 2000-character message.
23
+ messages.fields.add(new TextField({ name: "text_key", max: 40000 }));
24
+ app.save(messages);
25
+
26
+ // Backfill with raw SQL (no hooks: they would stamp edits). Deleted messages and forwards have no text.
27
+ let after = "";
28
+ for (;;) {
29
+ const rows = arrayOf(new DynamicModel({ id: "", text: "" }));
30
+ app
31
+ .db()
32
+ .newQuery("SELECT id, text FROM messages WHERE text != '' AND id > {:after} ORDER BY id LIMIT {:n}")
33
+ .bind({ after, n: BATCH })
34
+ .all(rows);
35
+ for (const r of rows) {
36
+ app
37
+ .db()
38
+ .newQuery("UPDATE messages SET text_key = {:key} WHERE id = {:id}")
39
+ .bind({ key: textKey(r.text), id: r.id })
40
+ .execute();
41
+ }
42
+ if (rows.length < BATCH) break;
43
+ after = rows[rows.length - 1].id;
44
+ }
45
+ },
46
+ (app) => {
47
+ const messages = app.findCollectionByNameOrId("messages");
48
+ messages.fields.removeByName("text_key");
49
+ app.save(messages);
50
+ },
51
+ );
@@ -0,0 +1,16 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // Message search (DESIGN.md §29): "İ" is searched as "i" (QA BUG-113). Lowercased, "İ" is "i" followed by a combining
3
+ // dot above (U+0307), so the key of "İstanbul" was "i̇stanbul", which "istanbul" as typed never matched. The hooks now
4
+ // drop that dot (`searchKey` in pb_hooks/lib/text.js); this does the same to the keys already stored.
5
+
6
+ migrate(
7
+ (app) => {
8
+ app
9
+ .db()
10
+ .newQuery("UPDATE messages SET text_key = replace(text_key, 'i' || char(775), 'i') WHERE instr(text_key, 'i' || char(775)) > 0")
11
+ .execute();
12
+ },
13
+ () => {
14
+ // Nothing to undo: a key without the dot finds what it found before, and more.
15
+ },
16
+ );
@@ -0,0 +1,67 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // The Android app's server additions (docs/ANDROID.md §8, §12.4):
3
+ // - push_tokens: a device's push registration (an FCM token, or a UnifiedPush endpoint URL), the owner's only: they
4
+ // create, view and delete it; nobody lists them.
5
+ // - push_queue: messages whose pushes are still to be sent (pb_hooks/lib/push.js). No API access.
6
+ // - site.min_android_version: the oldest app the server still serves (0: any). Readable like the rest of the site.
7
+
8
+ const OWNER = "user = @request.auth.id";
9
+
10
+ migrate(
11
+ (app) => {
12
+ const users = app.findCollectionByNameOrId("users");
13
+ const messages = app.findCollectionByNameOrId("messages");
14
+ app.save(
15
+ new Collection(
16
+ /** @type {any} */ ({
17
+ type: "base",
18
+ name: "push_tokens",
19
+ listRule: null,
20
+ viewRule: OWNER,
21
+ createRule: `@request.auth.id != "" && @request.auth.collectionName = "users" && ${OWNER}`,
22
+ updateRule: null,
23
+ deleteRule: OWNER,
24
+ fields: [
25
+ { type: "relation", name: "user", required: true, maxSelect: 1, collectionId: users.id, cascadeDelete: true },
26
+ { type: "text", name: "token", required: true, min: 1, max: 1024 },
27
+ { type: "select", name: "provider", required: true, maxSelect: 1, values: ["fcm", "unifiedpush"] },
28
+ { type: "autodate", name: "created", onCreate: true, onUpdate: false },
29
+ { type: "autodate", name: "updated", onCreate: true, onUpdate: true },
30
+ ],
31
+ indexes: [
32
+ "CREATE UNIQUE INDEX idx_push_tokens_token ON push_tokens (token)",
33
+ "CREATE INDEX idx_push_tokens_user ON push_tokens (user)",
34
+ ],
35
+ }),
36
+ ),
37
+ );
38
+ app.save(
39
+ new Collection(
40
+ /** @type {any} */ ({
41
+ type: "base",
42
+ name: "push_queue",
43
+ listRule: null,
44
+ viewRule: null,
45
+ createRule: null,
46
+ updateRule: null,
47
+ deleteRule: null,
48
+ fields: [
49
+ { type: "relation", name: "message", required: true, maxSelect: 1, collectionId: messages.id, cascadeDelete: true },
50
+ { type: "autodate", name: "created", onCreate: true, onUpdate: false },
51
+ ],
52
+ indexes: ["CREATE INDEX idx_push_queue_created ON push_queue (created)"],
53
+ }),
54
+ ),
55
+ );
56
+ const site = app.findCollectionByNameOrId("site");
57
+ site.fields.add(new NumberField(/** @type {any} */ ({ name: "min_android_version", min: 0, onlyInt: true })));
58
+ app.save(site);
59
+ },
60
+ (app) => {
61
+ app.delete(app.findCollectionByNameOrId("push_queue"));
62
+ app.delete(app.findCollectionByNameOrId("push_tokens"));
63
+ const site = app.findCollectionByNameOrId("site");
64
+ site.fields.removeByName("min_android_version");
65
+ app.save(site);
66
+ },
67
+ );
@@ -0,0 +1,31 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // One device, several accounts (docs/ANDROID.md §17.4): the app registers the device's push token once per account, so
3
+ // two accounts on this server share a token. A token is now unique per user, not overall.
4
+
5
+ const PER_TOKEN = "CREATE UNIQUE INDEX idx_push_tokens_token ON push_tokens (token)";
6
+ const PER_USER = "CREATE UNIQUE INDEX idx_push_tokens_token_user ON push_tokens (token, user)";
7
+
8
+ /**
9
+ * @param {Parameters<Parameters<typeof migrate>[0]>[0]} app
10
+ * @param {string} from
11
+ * @param {string} to
12
+ */
13
+ function swap(app, from, to) {
14
+ const c = /** @type {any} */ (app.findCollectionByNameOrId("push_tokens"));
15
+ c.indexes = [.../** @type {string[]} */ (c.indexes).filter((i) => i !== from), to];
16
+ app.save(c);
17
+ }
18
+
19
+ migrate(
20
+ (app) => swap(app, PER_TOKEN, PER_USER),
21
+ (app) => {
22
+ // Back to one row per token: the newest registration of each keeps it.
23
+ app
24
+ .db()
25
+ .newQuery(
26
+ "DELETE FROM push_tokens WHERE id NOT IN (SELECT id FROM (SELECT id, ROW_NUMBER() OVER (PARTITION BY token ORDER BY updated DESC, id DESC) AS n FROM push_tokens) WHERE n = 1)",
27
+ )
28
+ .execute();
29
+ swap(app, PER_USER, PER_TOKEN);
30
+ },
31
+ );
@@ -0,0 +1,18 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // The room's media grid (DESIGN.md §30) shows square tiles: a "240x240" thumbnail is cropped to the square, centred,
3
+ // where the fitted ones (§17.5) would be blurry once cropped.
4
+
5
+ const BEFORE = ["800x480f", "480x240f"];
6
+ const AFTER = [...BEFORE, "240x240"];
7
+
8
+ /** @param {any} app @param {string[]} thumbs */
9
+ function setThumbs(app, thumbs) {
10
+ const messages = app.findCollectionByNameOrId("messages");
11
+ /** @type {any} */ (messages.fields.getByName("files")).thumbs = thumbs;
12
+ app.save(messages);
13
+ }
14
+
15
+ migrate(
16
+ (app) => setThumbs(app, AFTER),
17
+ (app) => setThumbs(app, BEFORE),
18
+ );
@@ -0,0 +1,71 @@
1
+ /// <reference path="../types/pocketbase.d.ts" />
2
+ // The media grid's index (DESIGN.md §30.1): one row per image or video a room shows, its own or a forward's (§21.3),
3
+ // so the grid reads a room's media a page at a time without looking at its other messages, and the media worker
4
+ // (pb_hooks/lib/media.js) knows which files still need their thumbnails or video poster. No API access: the grid
5
+ // reads it through GET /api/chat/media/{room}. Kept by the messages hooks; filled here from the messages there are.
6
+ //
7
+ // - message: the message in the room (a forward, or the message itself); source: the one whose file it is.
8
+ // - pos: the file's place in the source's files; sent: the message's `created` (the grid's order, as the room's).
9
+ // - ready: the worker is done with it (forwards' rows are: their source's row does the work); poster: a video's
10
+ // poster frame is stored (thumbs_{file}/poster.jpg next to the file).
11
+
12
+ /** As web/src/lib/files.ts and pb_hooks/lib/media.js say, when this ran. */
13
+ const IMAGE_EXT = ["jpg", "jpeg", "png", "gif", "webp", "avif"];
14
+ const VIDEO_EXT = ["mp4", "m4v", "webm", "mov", "ogv"];
15
+
16
+ /** @param {string[]} exts */
17
+ const endsWith = (exts) => `(${exts.map((x) => `f.value LIKE '%.${x}'`).join(" OR ")})`;
18
+
19
+ migrate(
20
+ (app) => {
21
+ const messages = app.findCollectionByNameOrId("messages");
22
+ const rooms = app.findCollectionByNameOrId("rooms");
23
+ app.save(
24
+ new Collection(
25
+ /** @type {any} */ ({
26
+ type: "base",
27
+ name: "media",
28
+ listRule: null,
29
+ viewRule: null,
30
+ createRule: null,
31
+ updateRule: null,
32
+ deleteRule: null,
33
+ fields: [
34
+ { type: "relation", name: "room", required: true, maxSelect: 1, collectionId: rooms.id, cascadeDelete: true },
35
+ { type: "relation", name: "message", required: true, maxSelect: 1, collectionId: messages.id, cascadeDelete: true },
36
+ { type: "relation", name: "source", required: true, maxSelect: 1, collectionId: messages.id, cascadeDelete: true },
37
+ { type: "text", name: "file", required: true, max: 255 },
38
+ { type: "number", name: "pos", onlyInt: true, min: 0 },
39
+ { type: "bool", name: "video" },
40
+ { type: "date", name: "sent", required: true },
41
+ { type: "bool", name: "ready" },
42
+ { type: "bool", name: "poster" },
43
+ ],
44
+ indexes: [
45
+ "CREATE INDEX idx_media_room ON media (room, sent, message, pos)",
46
+ "CREATE INDEX idx_media_message ON media (message)",
47
+ "CREATE INDEX idx_media_source ON media (source, file)",
48
+ "CREATE INDEX idx_media_pending ON media (sent) WHERE ready = FALSE",
49
+ ],
50
+ }),
51
+ ),
52
+ );
53
+
54
+ // Every message there is that shows media and isn't deleted; a forward shows its original's.
55
+ app
56
+ .db()
57
+ .newQuery(
58
+ `INSERT INTO media (id, room, message, source, file, pos, video, sent, ready, poster)
59
+ SELECT substr(lower(hex(randomblob(8))), 1, 15), m.room, m.id, s.id, f.value, CAST(f.key AS INTEGER),
60
+ ${endsWith(VIDEO_EXT)}, m.created, s.id != m.id, FALSE
61
+ FROM messages m
62
+ JOIN messages s ON s.id = (CASE WHEN m.forward_of != '' THEN m.forward_of ELSE m.id END)
63
+ JOIN json_each(CASE WHEN json_valid(s.files) THEN s.files ELSE '[]' END) f
64
+ WHERE m.deleted = FALSE AND s.deleted = FALSE AND (${endsWith(IMAGE_EXT)} OR ${endsWith(VIDEO_EXT)})`,
65
+ )
66
+ .execute();
67
+ },
68
+ (app) => {
69
+ app.delete(app.findCollectionByNameOrId("media"));
70
+ },
71
+ );