@wtfalch/chat 0.4.0 → 0.5.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 CHANGED
@@ -56,7 +56,15 @@ summary carries `unreadCount`, Matrix's own notification count; `chat.
56
56
  totalUnreadCount()` sums it across every room, for the agora's badge.
57
57
  `chat.markRoomRead(roomId)` sends `m.read` for the newest event and clears
58
58
  that room's count right away -- call it when a room is opened and again when
59
- the person reaches the newest message. `chat.connectionStatus()` (and
59
+ the person reaches the newest message. Each summary also carries
60
+ `lastMessage` -- the room's newest real message (sender, text, timestamp),
61
+ reduced the same way `timeline()` reduces one, so an edit's already-folded
62
+ text and an attachment's file name show here too, kept current by the same
63
+ sync loop as everything else on the summary. `null` for a room with nothing
64
+ to show yet, and always `null` for an unanswered invitation, since an
65
+ invitee's client has no timeline to read before joining. A redacted message
66
+ or a state event landing in the timeline (a pin, for instance) is skipped,
67
+ never shown as a preview. `chat.connectionStatus()` (and
60
68
  `onConnectionStatusChanged`) is `'connected'`, `'catching-up'` or
61
69
  `'offline'`, for a status the UI can render.
62
70
 
@@ -78,12 +86,14 @@ hears about it changing. `chat.sendTyping(roomId, typing)` sends `m.typing`;
78
86
  return.
79
87
 
80
88
  **Presence is off in production.** Every company's Synapse is generated by
81
- `bootstrap/steps/chat-instance.ts:142` in `wtfalch/app-template`, which
82
- still hardcodes it off, tracked at that project's
83
- [#352](https://github.com/wtfalch/app-template/issues/352). Until that
84
- lands, `presence()` reports `'offline'` for everyone, always -- not because
85
- nobody is online, but because Synapse never says. The test stack turns it
86
- on, so the integration suite proves all four states against a real server.
89
+ foundry's provisioning (the app-template bootstrap step this once cited is
90
+ archived; foundry owns per-company Synapse config generation now), which
91
+ still hardcodes it off, tracked at
92
+ [wtfalch/foundry#41](https://github.com/wtfalch/foundry/issues/41). Until
93
+ that lands, `presence()` reports `'offline'` for everyone, always -- not
94
+ because nobody is online, but because Synapse never says. The test stack
95
+ turns it on, so the integration suite proves all four states against a real
96
+ server.
87
97
  `sendTyping` and `onTypingChanged` are unaffected and work today.
88
98
 
89
99
  **Announcing idleness** ([#20](https://github.com/wtfalch/chat/issues/20)).
@@ -114,7 +124,7 @@ message rather than editing the old one.
114
124
  The room it creates carries `is_direct` and an invite list and nothing else.
115
125
  That is not a style choice: Synapse's `wtfalch_chat_gate` module refuses a
116
126
  member-created room that also carries a name, a topic, `initial_state` or
117
- `creation_content`, because a room dressed as a channel is the manage app's
127
+ `creation_content`, because a room dressed as a channel is Boule's
118
128
  to make (chat plan section 5).
119
129
 
120
130
  `chat.hideDirectMessage(roomId)` takes a direct room out of `rooms()` with a
@@ -260,7 +270,10 @@ reaction is taken back. It also answers `/search`, a plain case-insensitive
260
270
  substring match over every message body it has recorded, newest first,
261
271
  scoped to `filter.rooms` when the client sends one -- a room seeded with
262
272
  `encrypted: true` never matches, the same as a real, unindexed encrypted
263
- room would not.
273
+ room would not. A room seeded with `invitedAccount` delivers as `rooms.
274
+ invite` instead of `rooms.join` -- the stripped state real Matrix hands an
275
+ invitee, with no timeline of its own, for proving `lastMessage` (and
276
+ `membership`) against an invitation without a real homeserver.
264
277
 
265
278
  ## Integration tests
266
279
 
@@ -390,7 +403,7 @@ read receipt is on that message or a newer one. It leaves out the signed-in
390
403
  person and the message's sender. `onTimelineChanged` fires when a receipt
391
404
  moves, so a view re-reads it then. Only public read receipts count.
392
405
 
393
- ## Administration in Manage
406
+ ## Administration in Boule
394
407
 
395
408
  `@wtfalch/chat/admin` exports `createChatAdmin`, the typed management
396
409
  client, and `hasChatAdminRole` for already-verified identity claims. The admin
@@ -411,16 +424,16 @@ invitations/removals, moderation (`banPerson`, `unbanPerson`, `redactMessage`,
411
424
  refused; tokens never appear in URLs or rendered errors. Service authorization
412
425
  still verifies the token, organisation and Chat project's admin role on every
413
426
  request. Request the Chat project audience and qualified project-role claims
414
- when Manage signs in; a same-named role from another project is insufficient.
427
+ when Boule signs in; a same-named role from another project is insufficient.
415
428
 
416
429
  `@wtfalch/chat/admin/react` exports `ChatAdminPanel` plus its action
417
430
  contracts. Import `@wtfalch/chat/admin/admin.css` after design styles.
418
- The SDK owns the controls; Manage supplies an organisation-scoped `load` and
431
+ The SDK owns the controls; Boule supplies an organisation-scoped `load` and
419
432
  `run` action, its verified gates, and a roster mapping issuer subjects to names.
420
433
  Keep access tokens in server actions. Every action must check both host tenant
421
434
  membership and the Chat project role itself; hiding a menu entry is not a gate.
422
435
  Mail follows the same separation through `@wtfalch/email/postmaster`.
423
436
 
424
437
  The agora is the sole user-facing app and HTTP origin for Mail and Chat. Admin
425
- panels live in Manage; no separate Chat web host is inferred or provisioned.
438
+ panels live in Boule; no separate Chat web host is inferred or provisioned.
426
439
  Deployment and real-identity acceptance remain separate from local checks.
@@ -327,20 +327,44 @@ export function createChatClient(options) {
327
327
  .filter((room) => !(HIDDEN_TAG in room.tags))
328
328
  .map((room) => {
329
329
  const muted = isMuted(target, room.roomId);
330
+ const membership = room.getMyMembership() === KnownMembership.Invite
331
+ ? 'invite'
332
+ : 'join';
330
333
  return {
331
334
  id: room.roomId,
332
335
  name: room.name,
336
+ // An invitee's client never gets a timeline to read before
337
+ // joining -- there is nothing here `lastMessage` could show.
338
+ lastMessage: membership === 'invite' ? null : roomLastMessage(room),
333
339
  isDirect: direct.has(room.roomId) || Boolean(room.getDMInviter()),
334
- membership: room.getMyMembership() === KnownMembership.Invite
335
- ? 'invite'
336
- : 'join',
337
- unreadCount: room.getMyMembership() === KnownMembership.Invite
338
- ? 1
339
- : roomUnreadCount(target, room, muted),
340
+ membership,
341
+ unreadCount: membership === 'invite' ? 1 : roomUnreadCount(target, room, muted),
340
342
  muted,
341
343
  };
342
344
  });
343
345
  }
346
+ /** See `ChatRoomLastMessage`. Walks the room's live timeline newest
347
+ * first -- the same direction `unreadTimelineCount` walks it, for the
348
+ * same reason: the answer is almost always the last event, so this
349
+ * rarely looks past it. Built on `toTimelineEvent` rather than reading
350
+ * the raw event, so an edit's already-folded text and an attachment's
351
+ * name are exactly what a caller reading `timeline()` would see, not a
352
+ * second, slightly different reduction of the same event. */
353
+ function roomLastMessage(room) {
354
+ const events = room.getLiveTimeline().getEvents();
355
+ for (const event of [...events].reverse()) {
356
+ const mapped = toTimelineEvent(event, room, false);
357
+ if (!mapped || mapped.deleted)
358
+ continue;
359
+ return {
360
+ senderName: mapped.senderName,
361
+ body: mapped.attachment?.name ?? mapped.body,
362
+ ts: mapped.ts,
363
+ mine: mapped.mine,
364
+ };
365
+ }
366
+ return null;
367
+ }
344
368
  /** A room's own `unreadCount` -- see `ChatRoomSummary.unreadCount` for
345
369
  * why a muted room (or one `unreliableServerCount` still distrusts)
346
370
  * takes the larger of Synapse's own count and the local walk, rather
@@ -23,6 +23,15 @@ export type FakeRoomSeed = {
23
23
  * reads to report a room unsearchable (agora#252). Defaults to
24
24
  * `false`. */
25
25
  encrypted?: boolean;
26
+ /** Seeds this room as an unanswered invitation to the fake account
27
+ * itself, rather than a room it has joined -- a `rooms.invite` entry
28
+ * with the stripped state real Matrix delivers one in, the one shape
29
+ * `membership: 'invite'` ever comes from. `messages` is ignored: a real
30
+ * invitee's client has no timeline to read before joining. Defaults to
31
+ * `undefined` (the room is joined, as every other seed is). */
32
+ invitedAccount?: {
33
+ inviter?: string;
34
+ };
26
35
  };
27
36
  export type FakeMessageSeed = {
28
37
  /** Defaults to a minted `$seeded-<n>`. Give one to reply to it. */
@@ -386,6 +386,52 @@ export class FakeChatServer {
386
386
  unread_notifications: { highlight_count: 0, notification_count: seed.notificationCount ?? 0 },
387
387
  };
388
388
  }
389
+ /** A room's invite-state entry: the stripped state real Matrix delivers
390
+ * in `rooms.invite`, for a seed carrying `invitedAccount` -- the one
391
+ * shape `membership: 'invite'` ever comes from, and the only entry with
392
+ * no timeline of its own: a real invitee's client gets none to read
393
+ * before joining, which is why `ChatRoomSummary.lastMessage` is always
394
+ * `null` for such a room. `messages` on the seed is ignored, for the
395
+ * same reason. */
396
+ #inviteEntry(seed) {
397
+ const now = this.#now();
398
+ const inviter = seed.invitedAccount?.inviter ?? this.userId;
399
+ const events = [
400
+ {
401
+ type: 'm.room.create',
402
+ state_key: '',
403
+ event_id: `$create-${seed.id}`,
404
+ sender: inviter,
405
+ origin_server_ts: now,
406
+ content: { creator: inviter, room_version: '10' },
407
+ },
408
+ {
409
+ type: 'm.room.name',
410
+ state_key: '',
411
+ event_id: `$name-${seed.id}`,
412
+ sender: inviter,
413
+ origin_server_ts: now,
414
+ content: { name: seed.name },
415
+ },
416
+ {
417
+ type: 'm.room.member',
418
+ state_key: inviter,
419
+ event_id: `$member-${seed.id}-inviter`,
420
+ sender: inviter,
421
+ origin_server_ts: now,
422
+ content: { membership: 'join', displayname: inviter },
423
+ },
424
+ {
425
+ type: 'm.room.member',
426
+ state_key: this.userId,
427
+ event_id: `$member-${seed.id}-self`,
428
+ sender: inviter,
429
+ origin_server_ts: now,
430
+ content: { membership: 'invite', ...(seed.isDirect ? { is_direct: true } : {}) },
431
+ },
432
+ ];
433
+ return { invite_state: { events } };
434
+ }
389
435
  /** One `m.room.message`, threaded when the seed names a root. The
390
436
  * `m.thread` relation is the whole of what makes a reply a thread reply
391
437
  * -- matrix-js-sdk reads it off the event and routes it, exactly as it
@@ -562,11 +608,16 @@ export class FakeChatServer {
562
608
  }
563
609
  #fullSyncBody() {
564
610
  const join = {};
565
- for (const room of this.#rooms)
566
- join[room.id] = this.#joinEntry(room);
611
+ const invite = {};
612
+ for (const room of this.#rooms) {
613
+ if (room.invitedAccount)
614
+ invite[room.id] = this.#inviteEntry(room);
615
+ else
616
+ join[room.id] = this.#joinEntry(room);
617
+ }
567
618
  return {
568
619
  next_batch: this.#nextBatch(),
569
- rooms: { join },
620
+ rooms: { join, invite },
570
621
  account_data: { events: this.#accountDataEvents() },
571
622
  presence: { events: [] },
572
623
  to_device: { events: [] },
@@ -7,4 +7,4 @@
7
7
  * `README.md` and the chat plan, section 9.
8
8
  */
9
9
  export { createChatClient, MAX_UPLOAD_BYTES } from './client.ts';
10
- export type { ChatAttachment, ChatRoomFilesPage, ChatClient, ChatConnectionStatus, ChatPerson, ChatRoomSummary, ChatSearchResults, ChatTimelineEvent, ChatUploadProgress, CreateChatClientOptions, ExchangeBody, } from './types.ts';
10
+ export type { ChatAttachment, ChatRoomFilesPage, ChatClient, ChatConnectionStatus, ChatPerson, ChatRoomLastMessage, ChatRoomSummary, ChatSearchResults, ChatTimelineEvent, ChatUploadProgress, CreateChatClientOptions, ExchangeBody, } from './types.ts';
@@ -10,12 +10,38 @@ export type ExchangeBody = {
10
10
  access_token: string;
11
11
  expires_at: string;
12
12
  };
13
+ /** The newest visible message in a room's live timeline, for a chat list to
14
+ * draw under the room's name the way `@wtfalch/email`'s thread list already
15
+ * does. `null` for a room whose timeline holds nothing to show: an
16
+ * unanswered invitation (`ChatRoomSummary.membership === 'invite'`, always,
17
+ * since an invitee's client never gets one to read before joining) or a
18
+ * joined room with no message yet.
19
+ *
20
+ * An edited message shows its already-latest text, for free: matrix-js-sdk
21
+ * folds an `m.replace` onto the message it replaces before this ever reads
22
+ * it, the same as `ChatTimelineEvent.body` does (see `edited`'s own
23
+ * comment). A redacted message is skipped rather than shown as a
24
+ * tombstone -- the room's previous message stands in for it, one step
25
+ * further back, same as a list would rather not announce what was deleted
26
+ * to somebody who has not opened the room. */
27
+ export type ChatRoomLastMessage = {
28
+ readonly senderName: string;
29
+ /** The message's text, or an attachment's file name for one that carries
30
+ * no text of its own -- there is nothing else to preview a file with. */
31
+ readonly body: string;
32
+ readonly ts: number;
33
+ /** Whether the signed-in person sent it -- a list draws "You: ..." off
34
+ * this rather than comparing `senderName` to anything. */
35
+ readonly mine: boolean;
36
+ };
13
37
  /** A joined room or invitation, reduced to what a chat list needs to draw
14
38
  * itself. Nothing here is a Matrix type: a UI built on this package never
15
39
  * imports matrix-js-sdk. */
16
40
  export type ChatRoomSummary = {
17
41
  readonly id: string;
18
42
  readonly name: string;
43
+ /** See `ChatRoomLastMessage`. */
44
+ readonly lastMessage: ChatRoomLastMessage | null;
19
45
  /** Whether this is a direct message or group DM, from the
20
46
  * account's `m.direct` data (or an invitation's `is_direct` flag) --
21
47
  * rather than guessing from membership counts. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wtfalch/chat",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "description": "A Matrix chat client behind the agora's token exchange.",
5
5
  "license": "MIT",
6
6
  "type": "module",