@wtfalch/chat 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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
 
@@ -260,7 +268,10 @@ reaction is taken back. It also answers `/search`, a plain case-insensitive
260
268
  substring match over every message body it has recorded, newest first,
261
269
  scoped to `filter.rooms` when the client sends one -- a room seeded with
262
270
  `encrypted: true` never matches, the same as a real, unindexed encrypted
263
- room would not.
271
+ room would not. A room seeded with `invitedAccount` delivers as `rooms.
272
+ invite` instead of `rooms.join` -- the stripped state real Matrix hands an
273
+ invitee, with no timeline of its own, for proving `lastMessage` (and
274
+ `membership`) against an invitation without a real homeserver.
264
275
 
265
276
  ## Integration tests
266
277
 
@@ -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.0",
4
4
  "description": "A Matrix chat client behind the agora's token exchange.",
5
5
  "license": "MIT",
6
6
  "type": "module",