chirkle 0.4.1 → 0.6.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 (229) hide show
  1. package/README.md +56 -202
  2. package/bin/release/darwin-amd64/chirkle-server +2 -2
  3. package/bin/release/darwin-arm64/chirkle-server +0 -0
  4. package/bin/release/linux-amd64/chirkle-server +0 -0
  5. package/bin/release/linux-arm64/chirkle-server +0 -0
  6. package/package.json +18 -5
  7. package/pb_hooks/federation/inbox.js +2 -0
  8. package/pb_hooks/federation/messages.js +40 -2
  9. package/pb_hooks/federation/worker.js +7 -3
  10. package/pb_hooks/lib/collections.js +5 -3
  11. package/pb_hooks/lib/files.js +36 -0
  12. package/pb_hooks/lib/handles.js +24 -2
  13. package/pb_hooks/lib/http.js +2 -2
  14. package/pb_hooks/lib/inbox.js +6 -5
  15. package/pb_hooks/lib/media.js +16 -3
  16. package/pb_hooks/lib/messages.js +16 -6
  17. package/pb_hooks/lib/pins.js +199 -0
  18. package/pb_hooks/lib/rooms.js +4 -0
  19. package/pb_hooks/lib/users.js +33 -2
  20. package/pb_hooks/main.pb.js +17 -3
  21. package/pb_hooks/telegram/inbound.js +187 -16
  22. package/pb_hooks/telegram/links.js +12 -1
  23. package/pb_hooks/telegram/outbound.js +153 -12
  24. package/pb_hooks/telegram/worker.js +3 -1
  25. package/pb_migrations/1760000057_reactions_seen.js +17 -0
  26. package/pb_migrations/1760000058_users_kind_public.js +20 -0
  27. package/pb_migrations/1760000059_telegram_media_edits.js +37 -0
  28. package/pb_migrations/1760000060_pins.js +23 -0
  29. package/pb_public/assets/{access-UK3L54NR.js → access-23A7N3OD.js} +2 -2
  30. package/pb_public/assets/access-23A7N3OD.js.br +0 -0
  31. package/pb_public/assets/access-23A7N3OD.js.gz +0 -0
  32. package/pb_public/assets/access-XGNXIUR5.css +2 -0
  33. package/pb_public/assets/access-XGNXIUR5.css.br +0 -0
  34. package/pb_public/assets/access-XGNXIUR5.css.gz +0 -0
  35. package/pb_public/assets/access-XGNXIUR5.css.map +7 -0
  36. package/pb_public/assets/account-EBCPOLWT.css +2 -0
  37. package/pb_public/assets/account-EBCPOLWT.css.br +0 -0
  38. package/pb_public/assets/account-EBCPOLWT.css.gz +0 -0
  39. package/pb_public/assets/account-EBCPOLWT.css.map +7 -0
  40. package/pb_public/assets/account-api-J6A6IYVX.js.br +0 -0
  41. package/pb_public/assets/account-api-J6A6IYVX.js.gz +0 -0
  42. package/pb_public/assets/app-7VYWTXX4.js +3 -0
  43. package/pb_public/assets/app-7VYWTXX4.js.br +0 -0
  44. package/pb_public/assets/app-7VYWTXX4.js.gz +0 -0
  45. package/pb_public/assets/app-7VYWTXX4.js.map +7 -0
  46. package/pb_public/assets/app-LDOYFRVQ.css +2 -0
  47. package/pb_public/assets/app-LDOYFRVQ.css.br +0 -0
  48. package/pb_public/assets/app-LDOYFRVQ.css.gz +0 -0
  49. package/pb_public/assets/app-LDOYFRVQ.css.map +7 -0
  50. package/pb_public/assets/chunk-AG73QNUN.js.br +0 -0
  51. package/pb_public/assets/chunk-AG73QNUN.js.gz +0 -0
  52. package/pb_public/assets/chunk-DC4TJ7HF.js +2 -0
  53. package/pb_public/assets/chunk-DC4TJ7HF.js.br +0 -0
  54. package/pb_public/assets/chunk-DC4TJ7HF.js.gz +0 -0
  55. package/pb_public/assets/chunk-DC4TJ7HF.js.map +7 -0
  56. package/pb_public/assets/chunk-DCLJRQ53.js.br +0 -0
  57. package/pb_public/assets/chunk-DCLJRQ53.js.gz +0 -0
  58. package/pb_public/assets/chunk-RPCBA7VF.js.br +0 -0
  59. package/pb_public/assets/chunk-RPCBA7VF.js.gz +0 -0
  60. package/pb_public/assets/chunk-RYZHXNLF.js +2 -0
  61. package/pb_public/assets/chunk-RYZHXNLF.js.br +0 -0
  62. package/pb_public/assets/chunk-RYZHXNLF.js.gz +0 -0
  63. package/pb_public/assets/chunk-RYZHXNLF.js.map +7 -0
  64. package/pb_public/assets/chunk-SM542ZXL.js +2 -0
  65. package/pb_public/assets/chunk-SM542ZXL.js.br +0 -0
  66. package/pb_public/assets/chunk-SM542ZXL.js.gz +0 -0
  67. package/pb_public/assets/chunk-SM542ZXL.js.map +7 -0
  68. package/pb_public/assets/chunk-SZMAZMR4.js +2 -0
  69. package/pb_public/assets/chunk-SZMAZMR4.js.br +0 -0
  70. package/pb_public/assets/chunk-SZMAZMR4.js.gz +0 -0
  71. package/pb_public/assets/chunk-SZMAZMR4.js.map +7 -0
  72. package/pb_public/assets/chunk-UZDXAORM.js.br +0 -0
  73. package/pb_public/assets/chunk-UZDXAORM.js.gz +0 -0
  74. package/pb_public/assets/chunk-VSHXXDW4.js +2 -0
  75. package/pb_public/assets/chunk-VSHXXDW4.js.br +0 -0
  76. package/pb_public/assets/chunk-VSHXXDW4.js.gz +0 -0
  77. package/pb_public/assets/chunk-VSHXXDW4.js.map +7 -0
  78. package/pb_public/assets/chunk-VTOUI25E.js.br +0 -0
  79. package/pb_public/assets/chunk-VTOUI25E.js.gz +0 -0
  80. package/pb_public/assets/chunk-WDXT4S4J.js +2 -0
  81. package/pb_public/assets/chunk-WDXT4S4J.js.br +0 -0
  82. package/pb_public/assets/chunk-WDXT4S4J.js.gz +0 -0
  83. package/pb_public/assets/chunk-WDXT4S4J.js.map +7 -0
  84. package/pb_public/assets/collections-FZB6G4W4.css +2 -0
  85. package/pb_public/assets/collections-FZB6G4W4.css.br +0 -0
  86. package/pb_public/assets/collections-FZB6G4W4.css.gz +0 -0
  87. package/pb_public/assets/collections-FZB6G4W4.css.map +7 -0
  88. package/pb_public/assets/emoji-IHRUMGCY.css +2 -0
  89. package/pb_public/assets/emoji-IHRUMGCY.css.br +0 -0
  90. package/pb_public/assets/emoji-IHRUMGCY.css.gz +0 -0
  91. package/pb_public/assets/emoji-IHRUMGCY.css.map +7 -0
  92. package/pb_public/assets/{emoji-picker-K6JTK2GV.js → emoji-picker-K4DOKYL3.js} +2 -2
  93. package/pb_public/assets/emoji-picker-K4DOKYL3.js.br +0 -0
  94. package/pb_public/assets/emoji-picker-K4DOKYL3.js.gz +0 -0
  95. package/pb_public/assets/{emoji-picker-K6JTK2GV.js.map → emoji-picker-K4DOKYL3.js.map} +2 -2
  96. package/pb_public/assets/forward-OR7S3EHR.js +2 -0
  97. package/pb_public/assets/forward-OR7S3EHR.js.br +0 -0
  98. package/pb_public/assets/forward-OR7S3EHR.js.gz +0 -0
  99. package/pb_public/assets/forward-OR7S3EHR.js.map +7 -0
  100. package/pb_public/assets/inbox-2AXY3INA.css +2 -0
  101. package/pb_public/assets/inbox-2AXY3INA.css.br +0 -0
  102. package/pb_public/assets/inbox-2AXY3INA.css.gz +0 -0
  103. package/pb_public/assets/inbox-2AXY3INA.css.map +7 -0
  104. package/pb_public/assets/{inbox-CLMYESNI.js → inbox-A46J7ZU2.js} +2 -2
  105. package/pb_public/assets/inbox-A46J7ZU2.js.br +0 -0
  106. package/pb_public/assets/inbox-A46J7ZU2.js.gz +0 -0
  107. package/pb_public/assets/{inbox-CLMYESNI.js.map → inbox-A46J7ZU2.js.map} +2 -2
  108. package/pb_public/assets/{invite-OGT5E2MZ.js → invite-VGT2DI2L.js} +2 -2
  109. package/pb_public/assets/invite-VGT2DI2L.js.br +0 -0
  110. package/pb_public/assets/invite-VGT2DI2L.js.gz +0 -0
  111. package/pb_public/assets/invite-api-7G7XPXQW.js.br +0 -0
  112. package/pb_public/assets/invite-api-7G7XPXQW.js.gz +0 -0
  113. package/pb_public/assets/lazy-OVEJT5CP.js +2 -0
  114. package/pb_public/assets/lazy-OVEJT5CP.js.br +0 -0
  115. package/pb_public/assets/lazy-OVEJT5CP.js.gz +0 -0
  116. package/pb_public/assets/lazy-OVEJT5CP.js.map +7 -0
  117. package/pb_public/assets/{login-TK6K4WGB.js → login-TN6R7N2U.js} +2 -2
  118. package/pb_public/assets/login-TN6R7N2U.js.br +0 -0
  119. package/pb_public/assets/login-TN6R7N2U.js.gz +0 -0
  120. package/pb_public/assets/media-BCKZQR2R.css +2 -0
  121. package/pb_public/assets/media-BCKZQR2R.css.br +0 -0
  122. package/pb_public/assets/media-BCKZQR2R.css.gz +0 -0
  123. package/pb_public/assets/media-BCKZQR2R.css.map +7 -0
  124. package/pb_public/assets/{media-WZ4653L5.js → media-VCJWBQJZ.js} +2 -2
  125. package/pb_public/assets/media-VCJWBQJZ.js.br +0 -0
  126. package/pb_public/assets/media-VCJWBQJZ.js.gz +0 -0
  127. package/pb_public/assets/{mention-suggest-ODJD2IC5.js → mention-suggest-K3UUHR4D.js} +2 -2
  128. package/pb_public/assets/mention-suggest-K3UUHR4D.js.br +0 -0
  129. package/pb_public/assets/mention-suggest-K3UUHR4D.js.gz +0 -0
  130. package/pb_public/assets/notify-EPDM772N.js.br +0 -0
  131. package/pb_public/assets/notify-EPDM772N.js.gz +0 -0
  132. package/pb_public/assets/pins-6NQUEU3U.js +2 -0
  133. package/pb_public/assets/pins-6NQUEU3U.js.br +0 -0
  134. package/pb_public/assets/pins-6NQUEU3U.js.gz +0 -0
  135. package/pb_public/assets/pins-6NQUEU3U.js.map +7 -0
  136. package/pb_public/assets/pins-6ZVYPBB5.css +2 -0
  137. package/pb_public/assets/pins-6ZVYPBB5.css.br +0 -0
  138. package/pb_public/assets/pins-6ZVYPBB5.css.gz +0 -0
  139. package/pb_public/assets/pins-6ZVYPBB5.css.map +7 -0
  140. package/pb_public/assets/profile-NQ6QCQIB.js +2 -0
  141. package/pb_public/assets/profile-NQ6QCQIB.js.br +0 -0
  142. package/pb_public/assets/profile-NQ6QCQIB.js.gz +0 -0
  143. package/pb_public/assets/profile-NQ6QCQIB.js.map +7 -0
  144. package/pb_public/assets/rooms-forms-NYMPXVUQ.js +2 -0
  145. package/pb_public/assets/rooms-forms-NYMPXVUQ.js.br +0 -0
  146. package/pb_public/assets/rooms-forms-NYMPXVUQ.js.gz +0 -0
  147. package/pb_public/assets/rooms-forms-NYMPXVUQ.js.map +7 -0
  148. package/pb_public/assets/search-DRQHBOXE.js +2 -0
  149. package/pb_public/assets/search-DRQHBOXE.js.br +0 -0
  150. package/pb_public/assets/search-DRQHBOXE.js.gz +0 -0
  151. package/pb_public/assets/search-DRQHBOXE.js.map +7 -0
  152. package/pb_public/assets/search-QYHX6XDT.css +2 -0
  153. package/pb_public/assets/search-QYHX6XDT.css.br +0 -0
  154. package/pb_public/assets/search-QYHX6XDT.css.gz +0 -0
  155. package/pb_public/assets/search-QYHX6XDT.css.map +7 -0
  156. package/pb_public/assets/sidebar-forms-ETSRVSPY.css +2 -0
  157. package/pb_public/assets/sidebar-forms-ETSRVSPY.css.br +0 -0
  158. package/pb_public/assets/sidebar-forms-ETSRVSPY.css.gz +0 -0
  159. package/pb_public/assets/sidebar-forms-ETSRVSPY.css.map +7 -0
  160. package/pb_public/assets/stats-5GSWDHZI.css +2 -0
  161. package/pb_public/assets/stats-5GSWDHZI.css.br +0 -0
  162. package/pb_public/assets/stats-5GSWDHZI.css.gz +0 -0
  163. package/pb_public/assets/stats-5GSWDHZI.css.map +7 -0
  164. package/pb_public/assets/{stats-GTO2H6PG.js → stats-Y3LIXAZR.js} +2 -2
  165. package/pb_public/assets/stats-Y3LIXAZR.js.br +0 -0
  166. package/pb_public/assets/stats-Y3LIXAZR.js.gz +0 -0
  167. package/pb_public/assets/stats-Y3LIXAZR.js.map +7 -0
  168. package/pb_public/assets/switcher-TFSO45KV.js.br +0 -0
  169. package/pb_public/assets/switcher-TFSO45KV.js.gz +0 -0
  170. package/pb_public/assets/user-search-GUNMIBSB.js +2 -0
  171. package/pb_public/assets/user-search-GUNMIBSB.js.br +0 -0
  172. package/pb_public/assets/user-search-GUNMIBSB.js.gz +0 -0
  173. package/pb_public/assets/{viewer-2Y5FJBW4.js → viewer-BHKVAADP.js} +2 -2
  174. package/pb_public/assets/viewer-BHKVAADP.js.br +0 -0
  175. package/pb_public/assets/viewer-BHKVAADP.js.gz +0 -0
  176. package/pb_public/assets/viewer-JBZU46HP.css +2 -0
  177. package/pb_public/assets/viewer-JBZU46HP.css.br +0 -0
  178. package/pb_public/assets/viewer-JBZU46HP.css.gz +0 -0
  179. package/pb_public/assets/{viewer-P7GYCBYM.css.map → viewer-JBZU46HP.css.map} +2 -2
  180. package/pb_public/index.html +9 -8
  181. package/pb_public/stats.html +3 -3
  182. package/scripts/service.mjs +1 -1
  183. package/scripts/start.mjs +2 -1
  184. package/pb_public/assets/access-GPRLOZOO.css +0 -2
  185. package/pb_public/assets/access-GPRLOZOO.css.map +0 -7
  186. package/pb_public/assets/account-CT2BVMAX.css +0 -2
  187. package/pb_public/assets/account-CT2BVMAX.css.map +0 -7
  188. package/pb_public/assets/app-HB3NZHQ2.js +0 -3
  189. package/pb_public/assets/app-HB3NZHQ2.js.map +0 -7
  190. package/pb_public/assets/app-XJ73EU5O.css +0 -2
  191. package/pb_public/assets/app-XJ73EU5O.css.map +0 -7
  192. package/pb_public/assets/chunk-56ACD3V5.js +0 -2
  193. package/pb_public/assets/chunk-56ACD3V5.js.map +0 -7
  194. package/pb_public/assets/chunk-DZC4SY4M.js +0 -2
  195. package/pb_public/assets/chunk-DZC4SY4M.js.map +0 -7
  196. package/pb_public/assets/chunk-N2OFGBCZ.js +0 -2
  197. package/pb_public/assets/chunk-N2OFGBCZ.js.map +0 -7
  198. package/pb_public/assets/chunk-OKIIBH7O.js +0 -2
  199. package/pb_public/assets/chunk-OKIIBH7O.js.map +0 -7
  200. package/pb_public/assets/chunk-WR25F7FU.js +0 -2
  201. package/pb_public/assets/chunk-WR25F7FU.js.map +0 -7
  202. package/pb_public/assets/collections-DHJEOLHW.css +0 -2
  203. package/pb_public/assets/collections-DHJEOLHW.css.map +0 -7
  204. package/pb_public/assets/emoji-WWNPWCF3.css +0 -2
  205. package/pb_public/assets/emoji-WWNPWCF3.css.map +0 -7
  206. package/pb_public/assets/forward-267HRNPY.js +0 -2
  207. package/pb_public/assets/forward-267HRNPY.js.map +0 -7
  208. package/pb_public/assets/inbox-3FSOPY2T.css +0 -2
  209. package/pb_public/assets/inbox-3FSOPY2T.css.map +0 -7
  210. package/pb_public/assets/lazy-H3KIJVB5.js +0 -2
  211. package/pb_public/assets/lazy-H3KIJVB5.js.map +0 -7
  212. package/pb_public/assets/media-7TCBG7WM.css +0 -2
  213. package/pb_public/assets/media-7TCBG7WM.css.map +0 -7
  214. package/pb_public/assets/profile-3R6S5QYS.js +0 -2
  215. package/pb_public/assets/profile-3R6S5QYS.js.map +0 -7
  216. package/pb_public/assets/search-WCHOGAJQ.js +0 -2
  217. package/pb_public/assets/search-WCHOGAJQ.js.map +0 -7
  218. package/pb_public/assets/stats-A2TBRUI6.css +0 -2
  219. package/pb_public/assets/stats-A2TBRUI6.css.map +0 -7
  220. package/pb_public/assets/stats-GTO2H6PG.js.map +0 -7
  221. package/pb_public/assets/user-search-3HQOCQWL.js +0 -2
  222. package/pb_public/assets/viewer-P7GYCBYM.css +0 -2
  223. /package/pb_public/assets/{access-UK3L54NR.js.map → access-23A7N3OD.js.map} +0 -0
  224. /package/pb_public/assets/{invite-OGT5E2MZ.js.map → invite-VGT2DI2L.js.map} +0 -0
  225. /package/pb_public/assets/{login-TK6K4WGB.js.map → login-TN6R7N2U.js.map} +0 -0
  226. /package/pb_public/assets/{media-WZ4653L5.js.map → media-VCJWBQJZ.js.map} +0 -0
  227. /package/pb_public/assets/{mention-suggest-ODJD2IC5.js.map → mention-suggest-K3UUHR4D.js.map} +0 -0
  228. /package/pb_public/assets/{user-search-3HQOCQWL.js.map → user-search-GUNMIBSB.js.map} +0 -0
  229. /package/pb_public/assets/{viewer-2Y5FJBW4.js.map → viewer-BHKVAADP.js.map} +0 -0
@@ -10,8 +10,30 @@ const { nameKey } = require(`${__hooks}/lib/text.js`);
10
10
  const HANDLE_MIN = 3;
11
11
  const HANDLE_MAX = 24;
12
12
  const HANDLE = /^[a-z0-9_]{3,24}$/;
13
- /** Kept for what may one day mean everyone in a room. */
14
- const RESERVED = new Set(["all", "everyone", "here", "room", "channel"]);
13
+ /** Kept for what may one day mean everyone in a room: nobody has these (migration 46 took them back). */
14
+ const GROUP = ["all", "everyone", "here", "room", "channel"];
15
+ /**
16
+ * Words that would lend a stranger the site's authority in a mention ("@admin says…", QA SEC-2): not given to a new
17
+ * handle, nor taken in a change. Whoever has one already keeps it (and is still mentioned by it).
18
+ */
19
+ const STAFF = [
20
+ "admin",
21
+ "admins",
22
+ "administrator",
23
+ "chirkle",
24
+ "help",
25
+ "mod",
26
+ "moderator",
27
+ "moderators",
28
+ "mods",
29
+ "official",
30
+ "root",
31
+ "security",
32
+ "staff",
33
+ "support",
34
+ "system",
35
+ ];
36
+ const RESERVED = new Set([...GROUP, ...STAFF]);
15
37
  /** Most people one message mentions. */
16
38
  const MAX_MENTIONS = 20;
17
39
  /** Most people GET /api/chat/mentionable suggests. */
@@ -3,8 +3,8 @@
3
3
 
4
4
  const CSP = [
5
5
  "default-src 'self'",
6
- // The hash is index.html's inline theme script (web/src/theme.ts); tests/unit/build/csp.test.ts checks it.
7
- "script-src 'self' 'sha256-lMI93IRRLFIlto6GZfZdfPFeIgbs3CJfe2ncDiU/aWw='",
6
+ // The hash is index.html's inline theme and text size script (web/src/theme.ts); tests/unit/build/csp.test.ts checks it.
7
+ "script-src 'self' 'sha256-mOQF+ckehOwPXg3QQaTKo5nz/mkpPjspccYc517TGsE='",
8
8
  "style-src 'self'",
9
9
  "connect-src 'self'",
10
10
  "img-src 'self' data: blob:",
@@ -213,11 +213,12 @@ WHERE i.user = {:me} AND m.deleted = FALSE AND ${SEES_SQL}`;
213
213
 
214
214
  /**
215
215
  * A mention or reply whose message you have read in its room: seen there, so not new in the inbox either (2026-11-10,
216
- * user: no reading it twice). A reaction is only seen in the inbox: it comes after the message it is on. server/unread.go
217
- * counts by the same.
216
+ * user: no reading it twice). A reaction comes after the message it is on: it is seen there if it came before you last
217
+ * read the room (room_reads.reactions_seen_at, 2026-11-22, user: "when reading room, new reactions should be marked as
218
+ * seen"). server/unread.go counts by the same.
218
219
  */
219
- const READ_IN_ROOM_SQL = `(i.kind != 'reaction' AND m.created <= COALESCE((SELECT rr.last_read_at FROM room_reads rr
220
- WHERE rr.user = {:me} AND rr.room = i.room), ''))`;
220
+ const READ_IN_ROOM_SQL = `EXISTS (SELECT 1 FROM room_reads rr WHERE rr.user = {:me} AND rr.room = i.room
221
+ AND m.created <= rr.last_read_at AND (i.kind != 'reaction' OR i.created <= rr.reactions_seen_at))`;
221
222
 
222
223
  /** @param {string} s */
223
224
  const snippet = (s) => {
@@ -247,7 +248,7 @@ function shown(app, msg) {
247
248
  * GET /api/chat/inbox?before=&limit= → { items, more, seen_at }: the caller's rows, newest first, PAGE at a time
248
249
  * (`before`: the last item's `cursor`). Each item: { id, cursor, kind, emoji (a reaction row's: the actor's emoji on
249
250
  * the message now, at most EMOJI_SHOWN then " +n"; "" otherwise), emojis (all of them, in the message's order), created, read (a mention or reply read in
250
- * its room: not new, whatever seen_at says), room, message: { id, text, files,
251
+ * its room, or a reaction seen there: not new, whatever seen_at says), room, message: { id, text, files,
251
252
  * created }, quote (a reply's: the caller's message it answers, { id, text, files }), actor: { id, name, avatar,
252
253
  * instance } }. In an anonymous room (§35) a mention or reply shows the room, not who wrote it.
253
254
  * @param {core.RequestEvent} e
@@ -94,6 +94,19 @@ function drop(app, id) {
94
94
  app.db().newQuery("DELETE FROM media WHERE message = {:id} OR source = {:id}").bind({ id }).execute();
95
95
  }
96
96
 
97
+ /**
98
+ * A message's attachments replaced (lib/files.js replaceMessageFiles): its rows and its forwards' are made again.
99
+ * @param {core.App} app
100
+ * @param {core.Record} rec
101
+ */
102
+ function refile(app, rec) {
103
+ drop(app, rec.id);
104
+ add(app, rec);
105
+ for (const fwd of app.findRecordsByFilter("messages", "forward_of = {:id} && deleted = false", "", 0, 0, { id: rec.id })) {
106
+ if (fwd) add(app, fwd);
107
+ }
108
+ }
109
+
97
110
  // ---------------------------------------------------------------- the grid's page
98
111
 
99
112
  /**
@@ -312,8 +325,8 @@ function thumbs(app, fs, row) {
312
325
  if (!THUMB_EXT.includes(ext(file))) return;
313
326
  const all = /** @type {any} */ (row.collection().fields.getByName("file")).thumbs ?? [];
314
327
  const recent = Date.now() - new Date(row.getString("created").replace(" ", "T")).getTime() < RECENT_MS;
315
- // A GIF shows itself in the room (§17.5): only the grid shows its thumbnail.
316
- const sizes = recent && ext(file) !== "gif" ? all : all.filter((/** @type {string} */ s) => s === GRID_THUMB);
328
+ // A GIF or a WebP shows itself in the room (§17.5): only the grid shows its thumbnail.
329
+ const sizes = recent && ext(file) !== "gif" && ext(file) !== "webp" ? all : all.filter((/** @type {string} */ s) => s === GRID_THUMB);
317
330
  for (const size of sizes) {
318
331
  const thumb = `${row.baseFilesPath()}/thumbs_${file}/${size}_${file}`;
319
332
  if (!fs.exists(thumb)) fs.createThumb(key, thumb, size);
@@ -594,4 +607,4 @@ function tick(e) {
594
607
  return e.noContent(204);
595
608
  }
596
609
 
597
- module.exports = { add, drop, list, run, wake, tick };
610
+ module.exports = { add, drop, refile, list, run, wake, tick };
@@ -365,18 +365,25 @@ function beforeMessageUpdate(e) {
365
365
  return;
366
366
  }
367
367
 
368
- rec.set("file_names", orig.get("file_names")); // never changed by an edit
369
- rec.set("file_sources", orig.get("file_sources")); // nor this (§31)
370
- // Only the create hook and the media worker set these; the worker's may be newer than the loaded record's.
371
- rec.set("file_dims", e.app.findRecordById("messages", rec.id).get("file_dims"));
368
+ // A replaceMessageFiles (the server's: a Telegram media edit, §26.4, or a linked instance's, §33.6) set them all.
369
+ const refiled = require(`${__hooks}/lib/files.js`).replacing(rec);
370
+ if (!refiled) {
371
+ rec.set("file_names", orig.get("file_names")); // never changed by an edit
372
+ rec.set("file_sources", orig.get("file_sources")); // nor this (§31)
373
+ // Only the create hook and the media worker set these; the worker's may be newer than the loaded record's.
374
+ rec.set("file_dims", e.app.findRecordById("messages", rec.id).get("file_dims"));
375
+ }
372
376
  // Reactions only change through POST /api/chat/reactions (§20.2). Any other save (an edit) keeps the ones stored
373
377
  // now, not the ones loaded with the record: a reaction added meanwhile would be lost. (Edits save in a transaction.)
374
378
  if (rec.getString("reactions") === orig.getString("reactions")) {
375
379
  rec.set("reactions", e.app.findRecordById("messages", rec.id).get("reactions"));
376
380
  }
377
- // Attachments are immutable after sending (backs up the update rule, as above).
381
+ // Attachments are immutable after sending (backs up the update rule, as above), but for the server's own replacing.
378
382
  const files = fileNames(rec);
379
- if (files.join("/") !== orig.getStringSlice("files").join("/")) throw fieldError("files", "Attachments can't be changed.");
383
+ const changed = files.join("/") !== orig.getStringSlice("files").join("/");
384
+ if (changed && !refiled) throw fieldError("files", "Attachments can't be changed.");
385
+ if (changed && orig.getString("forwarded_from") !== "") throw fieldError("files", "Forwarded messages can't be edited.");
386
+ if (changed) rec.set("edited_at", new DateTime());
380
387
  assertText(rec, files.length > 0 || isForward(rec));
381
388
  if (rec.getString("text") !== orig.getString("text")) {
382
389
  rec.set("previews", []); // the worker looks at the new text's links (§28)
@@ -398,6 +405,8 @@ function beforeMessageUpdate(e) {
398
405
  */
399
406
  function beforeMessageUpdateRequest(e) {
400
407
  const rec = recordOf(e);
408
+ // Only the server replaces attachments (lib/files.js replaceMessageFiles), never a request naming its key.
409
+ rec.set(require(`${__hooks}/lib/files.js`).REPLACING, null);
401
410
  // Uploads and modifiers aren't a file field's any more (§37), so the update rule doesn't see them all: refused
402
411
  // here, not dropped.
403
412
  const body = /** @type {Record<string, unknown>} */ (e.requestInfo().body ?? {});
@@ -427,6 +436,7 @@ function changesForwards(rec) {
427
436
  const orig = rec.original();
428
437
  return (
429
438
  rec.getString("text") !== orig.getString("text") ||
439
+ rec.getStringSlice("files").join("/") !== orig.getStringSlice("files").join("/") ||
430
440
  rec.getBool("deleted") !== orig.getBool("deleted") ||
431
441
  rec.getString("previews") !== orig.getString("previews")
432
442
  );
@@ -0,0 +1,199 @@
1
+ /// <reference path="../../types/pocketbase.d.ts" />
2
+ // Pinned messages (DESIGN.md §45): POST /api/chat/pins pins or unpins a message in its room. The pins live on the room
3
+ // (`rooms.pins`), newest first, so they reach clients with it: the room list, realtime room updates and the room cache
4
+ // need nothing new. A pin keeps the start of its message's text, so the pin bar shows it without loading the message;
5
+ // the messages hooks keep that up to date (edits) and drop the pin when the message goes (deletes).
6
+
7
+ const { accessOf, sees, viewerOf } = require(`${__hooks}/lib/access.js`);
8
+ const { isService } = require(`${__hooks}/lib/messages.js`);
9
+ const { isDm } = require(`${__hooks}/lib/rooms.js`);
10
+
11
+ /** Most pins a room keeps. */
12
+ const MAX_PINS = 20;
13
+ /** How much of a message's text a pin keeps (characters). */
14
+ const PIN_TEXT = 200;
15
+
16
+ /**
17
+ * @typedef {{ message: string, by: string, at: string, text: string }} Pin
18
+ */
19
+
20
+ /**
21
+ * @param {string} field
22
+ * @param {string} message
23
+ */
24
+ const badRequest = (field, message) =>
25
+ new BadRequestError("Failed to update the pin.", { [field]: new ValidationError("validation_invalid_value", message) });
26
+
27
+ /**
28
+ * A room's pins, newest first. Anything malformed is dropped.
29
+ * @param {core.Record} room
30
+ * @returns {Pin[]}
31
+ */
32
+ function pinsOf(room) {
33
+ /** @type {unknown} */
34
+ let raw;
35
+ try {
36
+ raw = JSON.parse(room.getString("pins") || "[]");
37
+ } catch {
38
+ return [];
39
+ }
40
+ if (!Array.isArray(raw)) return [];
41
+ return raw
42
+ .filter((p) => p && typeof p.message === "string" && p.message)
43
+ .map((p) => ({
44
+ message: p.message,
45
+ by: typeof p.by === "string" ? p.by : "",
46
+ at: typeof p.at === "string" ? p.at : "",
47
+ text: typeof p.text === "string" ? p.text : "",
48
+ }));
49
+ }
50
+
51
+ /**
52
+ * The start of a message's text, on one line, as a pin keeps it.
53
+ * @param {string} text
54
+ */
55
+ function pinText(text) {
56
+ const line = text.replace(/\s+/g, " ").trim();
57
+ if (line.length <= PIN_TEXT) return line;
58
+ // Not between the two halves of an emoji (a surrogate pair), which would be stored as "�".
59
+ let end = PIN_TEXT - 1;
60
+ const last = line.charCodeAt(end - 1);
61
+ if (last >= 0xd800 && last <= 0xdbff) end--;
62
+ return `${line.slice(0, end)}…`;
63
+ }
64
+
65
+ /**
66
+ * May `user` pin and unpin messages in `room`? Either member of a DM; in any other room its creator or a moderator
67
+ * (as for its access and archiving, §36). Never in an archived room: it is read-only.
68
+ * @param {core.Record} room
69
+ * @param {core.Record | null | undefined} user
70
+ */
71
+ function canPin(room, user) {
72
+ if (!user || room.getBool("archived")) return false;
73
+ if (isDm(room)) return room.getStringSlice("members").includes(user.id);
74
+ const { canManage } = require(`${__hooks}/lib/moderation.js`);
75
+ return canManage(room, user);
76
+ }
77
+
78
+ /**
79
+ * `list` with `message` pinned (`on`, first, `text` its text) or unpinned; null when that changes nothing.
80
+ * @param {Pin[]} list
81
+ * @param {string} message
82
+ * @param {boolean} on
83
+ * @param {{ by: string, at: string, text: string }} pin
84
+ * @returns {Pin[] | null}
85
+ */
86
+ function setPin(list, message, on, pin) {
87
+ const has = list.some((p) => p.message === message);
88
+ if (has === on) return null;
89
+ if (!on) return list.filter((p) => p.message !== message);
90
+ return [{ message, ...pin }, ...list];
91
+ }
92
+
93
+ /**
94
+ * POST /api/chat/pins { message, pin } → the room, as GET /api/collections/rooms/records/:id answers it (the same
95
+ * ?expand and ?fields apply): `message` is pinned in its room (`pin: true`) or not (`pin: false`). Idempotent, so a
96
+ * retry or a double click can't flip it back. Only on messages the caller can see, that aren't deleted, in rooms they
97
+ * may pin in (canPin).
98
+ * @param {core.RequestEvent} e
99
+ */
100
+ function pin(e) {
101
+ const me = e.auth;
102
+ if (!me) throw new UnauthorizedError("The request requires a logged-in user.");
103
+ const info = e.requestInfo();
104
+ const body = /** @type {Record<string, unknown>} */ (info.body);
105
+ const id = typeof body.message === "string" ? body.message : "";
106
+ const on = body.pin;
107
+ if (typeof on !== "boolean") throw badRequest("pin", "Must be true or false.");
108
+
109
+ // Someone else's DM gets the same answer as a message that doesn't exist.
110
+ const notFound = () => new NotFoundError("The message doesn't exist.");
111
+ let msg;
112
+ try {
113
+ msg = e.app.findRecordById("messages", id);
114
+ } catch {
115
+ throw notFound();
116
+ }
117
+ /** @type {core.Record} */
118
+ let room;
119
+ try {
120
+ room = e.app.findRecordById("rooms", msg.getString("room"));
121
+ } catch {
122
+ throw notFound();
123
+ }
124
+ if (!sees(viewerOf(me), accessOf(room))) throw notFound();
125
+ if (room.getBool("archived")) throw badRequest("message", "This room is archived and read-only.");
126
+ if (!canPin(room, me)) throw new ForbiddenError("Only the room's creator or a moderator can pin messages here.");
127
+ if (on && isService(msg)) throw badRequest("message", "Service messages can't be pinned.");
128
+
129
+ // Read and write in one transaction: two people pinning at once both count. (Errors are thrown after it, so they
130
+ // keep their status.)
131
+ let problem = "";
132
+ e.app.runInTransaction((tx) => {
133
+ const m = tx.findRecordById("messages", id);
134
+ const r = tx.findRecordById("rooms", m.getString("room"));
135
+ room = r;
136
+ if (on && m.getBool("deleted")) problem = "That message was deleted.";
137
+ if (problem) return;
138
+ const list = pinsOf(r);
139
+ if (on && list.length >= MAX_PINS && !list.some((p) => p.message === id)) {
140
+ problem = `A room can have at most ${MAX_PINS} pinned messages. Unpin one first.`;
141
+ return;
142
+ }
143
+ const next = setPin(list, id, on, { by: me.id, at: new Date().toISOString(), text: pinText(m.getString("text")) });
144
+ if (!next) return;
145
+ r.set("pins", next);
146
+ tx.save(r);
147
+ });
148
+ if (problem) throw badRequest("message", problem);
149
+ $apis.enrichRecord(e, room);
150
+ return e.json(200, room);
151
+ }
152
+
153
+ /**
154
+ * After a message was saved (messages onRecordUpdate): a deleted one is unpinned, and an edited one's pin shows its
155
+ * new text. Nothing to do for a message that isn't pinned (most saves: one read of its room).
156
+ * @param {core.App} app
157
+ * @param {core.Record} rec
158
+ */
159
+ function messageChanged(app, rec) {
160
+ const deleted = rec.getBool("deleted");
161
+ const edited = rec.getString("text") !== rec.original().getString("text");
162
+ if (!deleted && !edited) return;
163
+ dropOrRefresh(app, rec.getString("room"), rec.id, deleted ? null : pinText(rec.getString("text")));
164
+ }
165
+
166
+ /**
167
+ * A message deleted for good (messages onRecordDelete): unpinned.
168
+ * @param {core.App} app
169
+ * @param {core.Record} rec
170
+ */
171
+ function messageDeleted(app, rec) {
172
+ dropOrRefresh(app, rec.getString("room"), rec.id, null);
173
+ }
174
+
175
+ /**
176
+ * Unpin `message` from its room (`text` null), or give its pin `text`.
177
+ * @param {core.App} app
178
+ * @param {string} roomId
179
+ * @param {string} message
180
+ * @param {string | null} text
181
+ */
182
+ function dropOrRefresh(app, roomId, message, text) {
183
+ let room;
184
+ try {
185
+ room = app.findRecordById("rooms", roomId);
186
+ } catch {
187
+ return; // the room is going too
188
+ }
189
+ const list = pinsOf(room);
190
+ const at = list.findIndex((p) => p.message === message);
191
+ if (at < 0) return;
192
+ const pinned = list[at];
193
+ if (text !== null && pinned && pinned.text === text) return;
194
+ const next = text === null ? list.filter((_, i) => i !== at) : list.map((p, i) => (i === at ? { ...p, text } : p));
195
+ room.set("pins", next);
196
+ app.save(room);
197
+ }
198
+
199
+ module.exports = { MAX_PINS, PIN_TEXT, canPin, messageChanged, messageDeleted, pin, pinText, pinsOf, setPin };
@@ -107,6 +107,8 @@ function beforeRoomCreateRequest(e) {
107
107
  for (const f of DM_FIELDS.slice(1)) {
108
108
  if (dmValue(rec, f) !== "") throw fieldError(f, "This field can't be set.");
109
109
  }
110
+ // Only POST /api/chat/pins pins (§45).
111
+ if (!["", "null", "[]"].includes(rec.getString("pins"))) throw fieldError("pins", "This field can't be set.");
110
112
  }
111
113
 
112
114
  /**
@@ -120,6 +122,8 @@ function beforeRoomUpdateRequest(e) {
120
122
  if (f === "members" && !isDm(orig)) continue;
121
123
  if (dmValue(rec, f) !== dmValue(orig, f)) throw fieldError(f, "This field can't be changed.");
122
124
  }
125
+ // Only POST /api/chat/pins pins (§45).
126
+ if (rec.getString("pins") !== orig.getString("pins")) throw fieldError("pins", "This field can't be changed.");
123
127
  }
124
128
 
125
129
  /**
@@ -109,9 +109,11 @@ function searchUsers(e) {
109
109
  function beforeUserSave(e) {
110
110
  const rec = recordOf(e);
111
111
  const email = rec.getString("email");
112
- if (rec.isNew() || email !== rec.original().getString("email")) rec.set("email", email.trim().toLowerCase());
112
+ const newEmail = rec.isNew() || email !== rec.original().getString("email");
113
+ if (newEmail) rec.set("email", email.trim().toLowerCase());
113
114
  // Signups and accounts a superuser makes are people; the bridges set their stand-ins' kind before saving.
114
115
  if (rec.isNew() && !rec.getString("kind")) rec.set("kind", "person");
116
+ if (newEmail && kindOf(rec) === "person") refuseMadeUpEmail(rec.getString("email"));
115
117
  if (kindOf(rec) === "deleted") {
116
118
  // A deleted account (§43, lib/account.js erase): one name for all of them, each with a key of its own (no name
117
119
  // is cleaned to a control character, so no one else's key is ever this), and no handle.
@@ -152,6 +154,25 @@ function findLoginByEmail(e) {
152
154
  }
153
155
  }
154
156
 
157
+ /**
158
+ * A person's address must be one mail can reach: not `….invalid` (RFC 2606), the made-up domain of stand-ins
159
+ * (`@telegram.invalid`, `@federation.invalid`, `@deleted.invalid`). Stand-ins are told apart by `kind` now, but a person
160
+ * with such an address looked like one to anything that went by the domain, and gets no mail (QA SEC-3).
161
+ * @param {string} email in lower case
162
+ */
163
+ function refuseMadeUpEmail(email) {
164
+ if (/\.invalid\.?$/.test(email)) throw fieldError("email", "Use an address that can receive e-mail.", "validation_invalid_email");
165
+ }
166
+
167
+ /**
168
+ * users request-email-change: the new address is refused at once, not only when its confirmation saves it.
169
+ * @param {core.RecordRequestEmailChangeRequestEvent} e
170
+ */
171
+ function checkEmailChange(e) {
172
+ if (!e.record || kindOf(e.record) === "person") refuseMadeUpEmail(e.newEmail.trim().toLowerCase());
173
+ return e.next();
174
+ }
175
+
155
176
  /**
156
177
  * users request-password-reset (§39): no email to a stand-in (Telegram, another instance): its address is made
157
178
  * up (`….invalid`) and nobody can log in as it (`kind`, §5.1). Answered like any other request, as PocketBase does for an unknown
@@ -164,4 +185,14 @@ function skipStandInReset(e) {
164
185
  return e.next();
165
186
  }
166
187
 
167
- module.exports = { DELETED_NAME, beforeUserSave, checkPasswordLength, findLoginByEmail, isStandIn, kindOf, searchUsers, skipStandInReset };
188
+ module.exports = {
189
+ DELETED_NAME,
190
+ beforeUserSave,
191
+ checkEmailChange,
192
+ checkPasswordLength,
193
+ findLoginByEmail,
194
+ isStandIn,
195
+ kindOf,
196
+ searchUsers,
197
+ skipStandInReset,
198
+ };
@@ -37,6 +37,7 @@ routerAdd("GET", "/api/{path...}", () => {
37
37
  routerAdd("POST", "/api/previews/tick", (e) => require(`${__hooks}/lib/previews.js`).tick(e), $apis.requireSuperuserAuth());
38
38
  routerAdd("POST", "/api/chat/dms", (e) => require(`${__hooks}/lib/dms.js`).openDm(e), $apis.requireAuth("users"));
39
39
  routerAdd("POST", "/api/chat/reactions", (e) => require(`${__hooks}/lib/reactions.js`).react(e), $apis.requireAuth("users"));
40
+ routerAdd("POST", "/api/chat/pins", (e) => require(`${__hooks}/lib/pins.js`).pin(e), $apis.requireAuth("users"));
40
41
  routerAdd("POST", "/api/chat/delete", (e) => require(`${__hooks}/lib/bulk.js`).deleteMessages(e), $apis.requireAuth("users"));
41
42
  routerAdd("POST", "/api/chat/ban", (e) => require(`${__hooks}/lib/moderation.js`).ban(e), $apis.requireAuth("users"));
42
43
  routerAdd("POST", "/api/chat/forward", (e) => require(`${__hooks}/lib/bulk.js`).forward(e), $apis.requireAuth("users"));
@@ -131,6 +132,7 @@ onRecordAuthWithPasswordRequest((e) => {
131
132
  }, "users");
132
133
 
133
134
  onRecordRequestPasswordResetRequest((e) => require(`${__hooks}/lib/users.js`).skipStandInReset(e), "users");
135
+ onRecordRequestEmailChangeRequest((e) => require(`${__hooks}/lib/users.js`).checkEmailChange(e), "users");
134
136
  onRecordConfirmPasswordResetRequest((e) => {
135
137
  require(`${__hooks}/lib/users.js`).checkPasswordLength(e.requestInfo().body.password);
136
138
  e.next();
@@ -248,6 +250,8 @@ onRecordDelete((e) => {
248
250
  const names = e.record ? files.namedBy(e.app, "messages", e.record.id) : [];
249
251
  e.next();
250
252
  files.unlink(e.app, names);
253
+ // Gone for good: unpinned (§45).
254
+ if (e.record) require(`${__hooks}/lib/pins.js`).messageDeleted(e.app, e.record);
251
255
  }, "messages");
252
256
 
253
257
  onRecordCreate((e) => {
@@ -334,20 +338,30 @@ onRecordUpdate((e) => {
334
338
  const touch = messages.changesForwards(rec);
335
339
  const edited = rec.getString("text") !== rec.original().getString("text");
336
340
  const deleted = rec.getBool("deleted") && !rec.original().getBool("deleted");
341
+ const shared = require(`${__hooks}/lib/files.js`);
342
+ // Its attachments replaced (lib/files.js replaceMessageFiles: a Telegram media edit, §26.4, a linked instance's, §33.6).
343
+ const refiled = !deleted && rec.getStringSlice("files").join("/") !== rec.original().getStringSlice("files").join("/");
337
344
  // As stored, not as loaded: another delete of it may have been committed since (QA BUG-223).
338
- const files = deleted ? require(`${__hooks}/lib/files.js`).namedBy(e.app, "messages", rec.id) : [];
345
+ const files = deleted || refiled ? shared.namedBy(e.app, "messages", rec.id) : [];
339
346
  const tg = features.telegram();
340
347
  const change = tg ? tg.outbound.changeOf(e.app, rec) : "";
348
+ if (refiled) shared.linkPending(e.app, rec); // the new ones, in this save's transaction (the caller's)
341
349
  e.next();
350
+ rec.set(shared.REPLACING, null);
342
351
  // Saved: mentions, replies and reactions told or taken back (§41).
343
352
  inbox.messageChanged(e.app, rec, told);
344
- if (tg) tg.outbound.queueUpdate(e.app, rec, change);
353
+ if (tg) tg.outbound.queueUpdate(e.app, rec, change, files);
345
354
  // Saved: a deleted message's media leave the grid, its forwards' too (§30.1), and its files are named once less (§37).
346
355
  if (deleted) require(`${__hooks}/lib/media.js`).drop(e.app, rec.id);
347
- if (deleted) require(`${__hooks}/lib/files.js`).unlink(e.app, files);
356
+ if (deleted) shared.unlink(e.app, files);
357
+ // Saved with other attachments: the old ones are named once less, and the grid shows the new ones, its forwards' too.
358
+ if (refiled) shared.unlink(e.app, files);
359
+ if (refiled) require(`${__hooks}/lib/media.js`).refile(e.app, rec);
348
360
  // Saved: forwards of it tell their readers (§21.3).
349
361
  if (touch) messages.touchForwards(e.app, rec);
350
362
  if (edited) require(`${__hooks}/lib/previews.js`).queue(e.app, rec);
363
+ // Saved: a deleted message is unpinned, an edited one's pin shows its new text (§45).
364
+ if (edited || deleted) require(`${__hooks}/lib/pins.js`).messageChanged(e.app, rec);
351
365
  if (fed && was) fed.messages.messageChanged(e.app, rec, was);
352
366
  }, "messages");
353
367