korero-chat 0.1.0 → 0.1.2

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
@@ -2,20 +2,16 @@
2
2
 
3
3
  Build clients and bots for the [Korero v1 protocol](../docs/protocol.md). The SDK runs in modern browsers with Web Crypto, Bun, and Node.js 22+. Each `KoreroClient` owns a connection to one server.
4
4
 
5
- ## Install from this checkout
5
+ ## Install
6
6
 
7
7
  ```sh
8
- cd sdk
9
- bun install --frozen-lockfile
10
- npm pack
8
+ npm install korero-chat
11
9
  ```
12
10
 
13
- In your project, install the generated package with `npm install /path/to/korero/sdk/korero-chat-sdk-0.1.0.tgz`.
14
-
15
11
  ## Listen and respond
16
12
 
17
13
  ```ts
18
- import { Channel, KoreroClient, identityFromPrivateKey } from "@korero-chat/sdk";
14
+ import { Channel, KoreroClient, identityFromPrivateKey } from "korero-chat";
19
15
 
20
16
  const client = new KoreroClient("https://chat.example.org:8443");
21
17
  const identity = identityFromPrivateKey(process.env.KORERO_PRIVATE_KEY!);
package/dist/channel.d.ts CHANGED
@@ -1,9 +1,16 @@
1
1
  import type { KoreroClient } from "./session.js";
2
- import type { ChannelData, Message } from "./types.js";
3
- export type MessageContent = string | {
2
+ import type { ChannelData, Message, MessageMentions, PinCursor, SearchCursor } from "./types.js";
3
+ export interface MessageBody {
4
4
  text: string;
5
5
  html?: string;
6
- };
6
+ /** Accounts and mentionable roles the message mentions. */
7
+ mentions?: Partial<MessageMentions>;
8
+ }
9
+ export type MessageContent = string | MessageBody;
10
+ /** `replyTo` is the ID of a message in the same channel. */
11
+ export type SendContent = string | (MessageBody & {
12
+ replyTo?: string;
13
+ });
7
14
  export interface ChannelOptions {
8
15
  name?: string;
9
16
  description?: string;
@@ -21,10 +28,12 @@ export declare class Channel {
21
28
  private readonly client;
22
29
  constructor(id: string, client: KoreroClient);
23
30
  get data(): ChannelData | undefined;
24
- send(content: MessageContent): Promise<{
31
+ send(value: SendContent): Promise<{
25
32
  messageId: string;
26
33
  }>;
27
34
  history(before?: number): Promise<ChannelMessage[]>;
35
+ pins(before?: PinCursor): Promise<import("./types.js").PinnedMessagePage>;
36
+ search(query: string, before?: SearchCursor): Promise<import("./types.js").MessageSearchPage>;
28
37
  follow(): Promise<void>;
29
38
  join(password?: string): Promise<void>;
30
39
  edit(changes: ChannelOptions): Promise<string>;
@@ -39,6 +48,13 @@ export declare class ChannelMessage {
39
48
  get id(): string;
40
49
  get text(): string;
41
50
  get senderId(): string;
42
- edit(content: MessageContent): Promise<void>;
51
+ get senderName(): string | undefined;
52
+ /** Passing `mentions` replaces the message's mentions; omitting it keeps them. */
53
+ edit(value: MessageContent): Promise<void>;
54
+ reply(value: MessageContent): Promise<{
55
+ messageId: string;
56
+ }>;
57
+ react(emoji: string, reacted?: boolean): Promise<void>;
43
58
  delete(): Promise<void>;
59
+ setPinned(pinned: boolean): Promise<void>;
44
60
  }
package/dist/channel.js CHANGED
@@ -1,3 +1,4 @@
1
+ const body = (value) => typeof value === "string" ? { text: value } : value;
1
2
  /** A channel on a particular signed-in client. */
2
3
  export class Channel {
3
4
  id;
@@ -9,14 +10,20 @@ export class Channel {
9
10
  get data() {
10
11
  return this.client.state.snapshot?.channels.find(({ id }) => id === this.id);
11
12
  }
12
- send(content) {
13
- const { text, html } = typeof content === "string" ? { text: content } : content;
14
- return this.client.sendMessage(this.id, text, html);
13
+ send(value) {
14
+ const { text, html, mentions, replyTo } = body(value);
15
+ return this.client.sendMessage(this.id, text, html, { mentions, replyTo });
15
16
  }
16
17
  history(before) {
17
18
  return this.client.rpc("message.history", { channelId: this.id, before })
18
19
  .then((messages) => messages.map((message) => new ChannelMessage(message, this, this.client)));
19
20
  }
21
+ pins(before) {
22
+ return this.client.pinnedMessages(this.id, before);
23
+ }
24
+ search(query, before) {
25
+ return this.client.searchMessages(this.id, query, before);
26
+ }
20
27
  async follow() {
21
28
  const connectionId = this.client.connectionId;
22
29
  if (!connectionId)
@@ -46,11 +53,22 @@ export class ChannelMessage {
46
53
  get id() { return this.data.id; }
47
54
  get text() { return this.data.text; }
48
55
  get senderId() { return this.data.senderId; }
49
- edit(content) {
50
- const { text, html } = typeof content === "string" ? { text: content } : content;
51
- return this.client.editMessage(this.id, text, html);
56
+ get senderName() { return this.data.senderName; }
57
+ /** Passing `mentions` replaces the message's mentions; omitting it keeps them. */
58
+ edit(value) {
59
+ const { text, html, mentions } = body(value);
60
+ return this.client.editMessage(this.id, text, html, mentions);
61
+ }
62
+ reply(value) {
63
+ return this.channel.send({ ...body(value), replyTo: this.id });
64
+ }
65
+ react(emoji, reacted = true) {
66
+ return this.client.reactToMessage(this.channel.id, this.id, emoji, reacted);
52
67
  }
53
68
  delete() {
54
69
  return this.client.deleteMessage(this.id);
55
70
  }
71
+ setPinned(pinned) {
72
+ return this.client.setMessagePin(this.channel.id, this.id, pinned);
73
+ }
56
74
  }
package/dist/http.d.ts CHANGED
@@ -17,7 +17,7 @@ export type ApiResult<T> = {
17
17
  };
18
18
  status: number;
19
19
  };
20
- export type ChannelPermission = "join" | "speak" | "text" | "deleteChannels" | "mute" | "attachFiles" | "shareScreen" | "useCamera" | "whisper";
20
+ export type ChannelPermission = "join" | "speak" | "text" | "deleteChannels" | "mute" | "attachFiles" | "shareScreen" | "useCamera" | "whisper" | "pinMessages";
21
21
  export type Permission = ChannelPermission | "createChannels" | "editChannels" | "move" | "kick" | "ban" | "manageRoles" | "manageInvitations" | "manageSettings" | "viewAdmin" | "sendDirectMessages" | "removeChannels";
22
22
  export interface ChannelPermissionTable {
23
23
  editable: ChannelPermission[];
@@ -34,9 +34,12 @@ export interface AccountPage {
34
34
  editable: boolean;
35
35
  bannable: boolean;
36
36
  banned: boolean;
37
+ bannedUntil?: number | null;
37
38
  })[];
38
39
  next: string | null;
39
40
  roles: string[];
41
+ /** Every role on the server, including those the caller cannot assign. */
42
+ allRoles: string[];
40
43
  builtInRoles: (RoleDefinition & {
41
44
  editable: boolean;
42
45
  defaultPermissions: Permission[];
@@ -47,9 +50,18 @@ export interface AccountPage {
47
50
  defaultRole: string;
48
51
  registrationPermissions: Permission[];
49
52
  }
53
+ export type AccountQuery = {
54
+ after?: string;
55
+ /** Matches part of the display name or the start of the account ID. */
56
+ search?: string;
57
+ role?: string;
58
+ status?: "banned" | "active";
59
+ };
50
60
  export interface RoleDefinition {
51
61
  name: string;
52
62
  permissions: Permission[];
63
+ /** Messages may @mention the role. Omit when saving to keep the current setting. */
64
+ mentionable?: boolean;
53
65
  }
54
66
  export interface AuditPage {
55
67
  events: {
@@ -66,6 +78,7 @@ export interface AuditPage {
66
78
  }
67
79
  export interface ServerPreferences {
68
80
  motd: string;
81
+ motdHtml: string;
69
82
  defaultRole: string;
70
83
  canSetDefaultRole: boolean;
71
84
  defaultRoles: string[];
@@ -81,6 +94,61 @@ export interface Invitation {
81
94
  expiresAt: number | null;
82
95
  usesLeft: number | null;
83
96
  revokedAt: number | null;
97
+ /** Accounts that registered through this invitation. */
98
+ uses: number;
99
+ }
100
+ export interface InvitationAnalytics {
101
+ /** Everyone who created a visible invitation, most uses first. */
102
+ inviters: {
103
+ accountId: string;
104
+ displayName: string;
105
+ invitations: number;
106
+ uses: number;
107
+ }[];
108
+ /** The most recent registrations through an invitation, newest first. */
109
+ redemptions: {
110
+ invitationId: string;
111
+ redeemedAt: number;
112
+ accountId: string;
113
+ displayName: string;
114
+ invitedBy: string;
115
+ invitedByName: string;
116
+ }[];
117
+ }
118
+ export interface StorageBudget {
119
+ used: number;
120
+ limit: number;
121
+ }
122
+ export interface ServerMetrics {
123
+ sampledAt: number;
124
+ connections: {
125
+ users: number;
126
+ connections: number;
127
+ voiceChannels: number;
128
+ maxUsers: number;
129
+ };
130
+ /** Live RTP bitrates in bits per second, summed over every open media transport. */
131
+ bandwidth: {
132
+ inboundBitrate: number;
133
+ outboundBitrate: number;
134
+ transports: number;
135
+ egressLimit: number;
136
+ };
137
+ /** Bytes used against each configured storage budget. */
138
+ storage: {
139
+ messages: StorageBudget;
140
+ attachments: StorageBudget & {
141
+ enabled: boolean;
142
+ };
143
+ hostedAccounts: StorageBudget & {
144
+ accounts: number;
145
+ accountLimit: number;
146
+ registration: "open" | "closed";
147
+ };
148
+ database: {
149
+ bytes: number;
150
+ };
151
+ };
84
152
  }
85
153
  export interface ConnectionDetails {
86
154
  displayName: string;
@@ -104,6 +172,11 @@ export interface AttachmentUpload {
104
172
  channelId: string;
105
173
  text: string;
106
174
  html?: string;
175
+ replyTo?: string;
176
+ mentions?: {
177
+ accounts?: string[];
178
+ roles?: string[];
179
+ };
107
180
  name: string;
108
181
  data: string;
109
182
  }
@@ -196,9 +269,21 @@ export declare function createApi(origin: string, send?: Requester): {
196
269
  };
197
270
  admin: {
198
271
  accounts: {
199
- get: (options?: QueryOptions<{
200
- after?: string;
201
- }>) => Promise<ApiResult<AccountPage>>;
272
+ get: (options?: QueryOptions<AccountQuery>) => Promise<ApiResult<AccountPage>>;
273
+ roles: {
274
+ post: (body: {
275
+ accountId: string;
276
+ role: string;
277
+ }) => Promise<ApiResult<{
278
+ roles: string[];
279
+ }>>;
280
+ delete: (body: {
281
+ accountId: string;
282
+ role: string;
283
+ }) => Promise<ApiResult<{
284
+ roles: string[];
285
+ }>>;
286
+ };
202
287
  };
203
288
  badges: {
204
289
  patch: (body: {
@@ -209,12 +294,6 @@ export declare function createApi(origin: string, send?: Requester): {
209
294
  }>>;
210
295
  };
211
296
  roles: {
212
- patch: (body: {
213
- accountId: string;
214
- role: string;
215
- }) => Promise<ApiResult<{
216
- role: string;
217
- }>>;
218
297
  post: (body: RoleDefinition) => Promise<ApiResult<RoleDefinition>>;
219
298
  definition: {
220
299
  patch: (body: RoleDefinition) => Promise<ApiResult<RoleDefinition>>;
@@ -230,9 +309,12 @@ export declare function createApi(origin: string, send?: Requester): {
230
309
  actorId?: string;
231
310
  }>) => Promise<ApiResult<AuditPage>>;
232
311
  };
312
+ metrics: {
313
+ get: () => Promise<ApiResult<ServerMetrics>>;
314
+ };
233
315
  preferences: {
234
316
  get: () => Promise<ApiResult<ServerPreferences>>;
235
- patch: (body: Partial<Pick<ServerPreferences, "motd" | "defaultRole" | "defaultAudioBitrate" | "maxAttachmentMiB">>) => Promise<ApiResult<ServerPreferences>>;
317
+ patch: (body: Partial<Pick<ServerPreferences, "motd" | "motdHtml" | "defaultRole" | "defaultAudioBitrate" | "maxAttachmentMiB">>) => Promise<ApiResult<ServerPreferences>>;
236
318
  };
237
319
  banner: {
238
320
  patch: (body: {
@@ -253,6 +335,9 @@ export declare function createApi(origin: string, send?: Requester): {
253
335
  get: () => Promise<ApiResult<{
254
336
  invitations: Invitation[];
255
337
  }>>;
338
+ analytics: {
339
+ get: () => Promise<ApiResult<InvitationAnalytics>>;
340
+ };
256
341
  post: (body: {
257
342
  expiresAt?: number;
258
343
  expiresInSeconds?: number;
package/dist/http.js CHANGED
@@ -46,6 +46,7 @@ export function createApi(origin, send = (request) => webTransport.fetch(request
46
46
  };
47
47
  }, {
48
48
  get: () => request("GET", "admin/invitations"),
49
+ analytics: { get: () => request("GET", "admin/invitations/analytics") },
49
50
  post: (body) => request("POST", "admin/invitations", body),
50
51
  });
51
52
  return {
@@ -65,10 +66,15 @@ export function createApi(origin, send = (request) => webTransport.fetch(request
65
66
  channels,
66
67
  chat: { attachments },
67
68
  admin: {
68
- accounts: { get: (options) => request("GET", "admin/accounts", undefined, options?.query) },
69
+ accounts: {
70
+ get: (options) => request("GET", "admin/accounts", undefined, options?.query),
71
+ roles: {
72
+ post: (body) => request("POST", "admin/accounts/roles", body),
73
+ delete: (body) => request("DELETE", "admin/accounts/roles", body),
74
+ },
75
+ },
69
76
  badges: { patch: (body) => request("PATCH", "admin/badges", body) },
70
77
  roles: {
71
- patch: (body) => request("PATCH", "admin/roles", body),
72
78
  post: (body) => request("POST", "admin/roles", body),
73
79
  definition: {
74
80
  patch: (body) => request("PATCH", "admin/roles/definition", body),
@@ -76,6 +82,7 @@ export function createApi(origin, send = (request) => webTransport.fetch(request
76
82
  },
77
83
  },
78
84
  audit: { get: (options) => request("GET", "admin/audit", undefined, options?.query) },
85
+ metrics: { get: () => request("GET", "admin/metrics") },
79
86
  preferences: {
80
87
  get: () => request("GET", "admin/preferences"),
81
88
  patch: (body) => request("PATCH", "admin/preferences", body),
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  export { Channel, ChannelMessage } from "./channel.js";
2
- export type { ChannelOptions, MessageContent } from "./channel.js";
2
+ export type { ChannelOptions, MessageBody, MessageContent, SendContent } from "./channel.js";
3
3
  export { KoreroClient } from "./session.js";
4
4
  export type { ClientEvents, ConnectOptions, SessionOptions, SessionMedia, SessionMediaAdapter } from "./session.js";
5
5
  export { createApi, serverApi } from "./http.js";
6
- export type { AccountPage, ApiResult, AttachmentUpload, AuditPage, ChannelOrder, ChannelPermission, ChannelPermissionTable, ConnectionDetails, Invitation, Permission, RoleDefinition, ServerCapabilities, ServerPreferences, } from "./http.js";
6
+ export type { AccountPage, ApiResult, AttachmentUpload, AuditPage, ChannelOrder, ChannelPermission, ChannelPermissionTable, ConnectionDetails, Invitation, InvitationAnalytics, Permission, RoleDefinition, ServerCapabilities, ServerMetrics, ServerPreferences, StorageBudget, } from "./http.js";
7
7
  export { serverOrigin, webTransport } from "./transport.js";
8
8
  export type { ServerSocket, Transport } from "./transport.js";
9
9
  export { messageFrom, requestError } from "./errors.js";
package/dist/session.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type DirectMessage, type DirectMessageChange, type DirectOperation, type MemberRequest, type Poke, type RelayResponse, type Snapshot, type VoiceState } from "./types.js";
1
+ import { type TypingEntry, type DirectMessage, type DirectMessageChange, type DirectOperation, type MemberRequest, type Message, type MessageMention, type MessageMentions, type PinCursor, type ReactionChange, type PinnedMessagePage, type MessageSearchPage, type SearchCursor, type Poke, type RelayResponse, type Snapshot, type VoiceState } from "./types.js";
2
2
  import { type IdentityAuthentication } from "./auth.js";
3
3
  import { createApi } from "./http.js";
4
4
  import { Channel, ChannelMessage, type ChannelOptions } from "./channel.js";
@@ -35,9 +35,14 @@ export interface ClientEvents {
35
35
  messageDelete: {
36
36
  id: string;
37
37
  };
38
+ /** Another member mentioned this account or one of its roles. */
39
+ mention: MessageMention;
40
+ reaction: ReactionChange;
38
41
  directMessage: DirectMessage;
39
42
  directMessageChange: DirectMessageChange;
40
43
  poke: Poke;
44
+ /** Another account is typing in a followed channel or a direct conversation. */
45
+ typing: TypingEntry;
41
46
  raw: {
42
47
  type: string;
43
48
  payload: unknown;
@@ -64,8 +69,11 @@ export declare class KoreroClient {
64
69
  private pending;
65
70
  private snapshot;
66
71
  private messages;
72
+ private messageRevisions;
67
73
  private directMessages;
68
74
  private lastPoke?;
75
+ private lastMention?;
76
+ private typingEntries;
69
77
  /** A channel whose chat is followed without joining it. */
70
78
  private viewedChannelId;
71
79
  private viewRequest?;
@@ -105,16 +113,26 @@ export declare class KoreroClient {
105
113
  viewChannel(connectionId: string, channelId: string | null): Promise<void>;
106
114
  private updateVoice;
107
115
  private show;
116
+ private reviseMessages;
108
117
  private event;
109
118
  joinChannel(channelId: string, password?: string): Promise<void>;
110
119
  leaveChannel(): Promise<void>;
111
120
  setPresence(payload: Record<string, unknown> | undefined): Promise<void>;
112
121
  setMemberVolume(accountId: string, level: number): void;
113
- sendMessage(channelId: string, text: string, html?: string): Promise<{
122
+ sendMessage(channelId: string, text: string, html?: string, options?: {
123
+ replyTo?: string;
124
+ mentions?: Partial<MessageMentions>;
125
+ }): Promise<{
114
126
  messageId: string;
115
127
  }>;
116
- editMessage(messageId: string, text: string, html?: string): Promise<void>;
128
+ editMessage(messageId: string, text: string, html?: string, mentions?: Partial<MessageMentions>): Promise<void>;
129
+ reactToMessage(channelId: string, messageId: string, emoji: string, reacted: boolean): Promise<void>;
117
130
  deleteMessage(messageId: string): Promise<void>;
131
+ pinnedMessages(channelId: string, before?: PinCursor): Promise<PinnedMessagePage>;
132
+ /** Searches a channel's stored messages, newest first. Pass the page's `next` as `before` to continue. */
133
+ searchMessages(channelId: string, query: string, before?: SearchCursor): Promise<MessageSearchPage>;
134
+ messageContext(channelId: string, messageId: string): Promise<Message[]>;
135
+ setMessagePin(channelId: string, messageId: string, pinned: boolean): Promise<void>;
118
136
  screenRequest<T>(connectionId: string, channelId: string, kind: string, payload: unknown): Promise<T>;
119
137
  directRequest<T>(connectionId: string, kind: DirectOperation, payload: unknown): Promise<T>;
120
138
  createChannel(options: ChannelOptions & {
@@ -131,6 +149,14 @@ export declare class KoreroClient {
131
149
  }>;
132
150
  editDirect(packet: DirectPacket): Promise<unknown>;
133
151
  deleteDirect(accountId: string, id: string): Promise<unknown>;
152
+ /** Tells a channel's readers or a direct conversation partner that this account is typing. */
153
+ typing(target: {
154
+ channelId: string;
155
+ } | {
156
+ accountId: string;
157
+ }): Promise<unknown>;
158
+ /** Removes a typing entry once that account's message arrives; returns whether one was removed. */
159
+ private clearTyping;
134
160
  poke(accountId: string, message?: string): Promise<unknown>;
135
161
  saveChannel(payload: ChannelOptions & {
136
162
  channelId?: string;
package/dist/session.js CHANGED
@@ -1,9 +1,17 @@
1
- import { offline, isSameDirectMessage } from "./types.js";
1
+ import { offline, typingExpiryMs, isSameDirectMessage } from "./types.js";
2
2
  import { messageFrom, requestError } from "./errors.js";
3
3
  import { authenticateIdentity } from "./auth.js";
4
4
  import { createApi } from "./http.js";
5
5
  import { Channel, ChannelMessage } from "./channel.js";
6
6
  import { webTransport } from "./transport.js";
7
+ /** Applies a live reaction change; `me` only changes when the reacting account is this one. */
8
+ function applyReaction(reactions = [], change, mine) {
9
+ const current = reactions.find(({ emoji }) => emoji === change.emoji);
10
+ const me = mine ? change.reacted : current?.me ?? false;
11
+ const next = current ? reactions.map((entry) => entry === current ? { ...entry, count: change.count, me } : entry)
12
+ : [...reactions, { emoji: change.emoji, count: change.count, me }];
13
+ return next.filter(({ count }) => count > 0);
14
+ }
7
15
  /** Direct messages kept in the view while connected. Older ones remain in server history. */
8
16
  const directMessageLimit = 200;
9
17
  const channelMessageLimit = 200;
@@ -27,8 +35,11 @@ export class KoreroClient {
27
35
  pending = new Map();
28
36
  snapshot = null;
29
37
  messages = [];
38
+ messageRevisions = {};
30
39
  directMessages = [];
31
40
  lastPoke;
41
+ lastMention;
42
+ typingEntries = [];
32
43
  /** A channel whose chat is followed without joining it. */
33
44
  viewedChannelId = null;
34
45
  viewRequest;
@@ -417,8 +428,12 @@ export class KoreroClient {
417
428
  this.voice?.update(snapshot);
418
429
  }
419
430
  show(directChange) {
420
- this.update({ ...offline, phase: "online", snapshot: this.snapshot, messages: this.messages, directMessages: this.directMessages,
421
- directChange, lastPoke: this.lastPoke, voice: this.voice?.voice ?? "idle", voiceError: this.voice?.error ?? null });
431
+ this.update({ ...offline, phase: "online", snapshot: this.snapshot, messages: this.messages, messageRevisions: this.messageRevisions, directMessages: this.directMessages,
432
+ directChange, lastPoke: this.lastPoke, lastMention: this.lastMention, typing: this.typingEntries, voice: this.voice?.voice ?? "idle", voiceError: this.voice?.error ?? null });
433
+ }
434
+ reviseMessages(channelId) {
435
+ if (channelId)
436
+ this.messageRevisions = { ...this.messageRevisions, [channelId]: (this.messageRevisions[channelId] ?? 0) + 1 };
422
437
  }
423
438
  event({ type, payload }) {
424
439
  const snapshot = this.snapshot;
@@ -456,6 +471,7 @@ export class KoreroClient {
456
471
  if (!message.channelId || !this.chatChannels().has(message.channelId) || this.messages.some(({ id }) => id === message.id))
457
472
  return;
458
473
  this.messages = [...this.messages, message];
474
+ this.clearTyping(message.channelId, message.senderId);
459
475
  const inChannel = this.messages.filter(({ channelId }) => channelId === message.channelId);
460
476
  if (inChannel.length > channelMessageLimit) {
461
477
  const oldest = new Set(inChannel.slice(0, inChannel.length - channelMessageLimit));
@@ -468,18 +484,54 @@ export class KoreroClient {
468
484
  const message = payload;
469
485
  if (message.senderId !== snapshot.account.id && message.recipientId !== snapshot.account.id)
470
486
  return;
487
+ const typed = this.clearTyping(undefined, message.senderId);
471
488
  if (this.addDirectMessage(message)) {
472
489
  this.show();
473
490
  this.emit("directMessage", message);
474
491
  }
492
+ else if (typed)
493
+ this.show();
494
+ }
495
+ else if (type === "chat.typing") {
496
+ const ping = payload;
497
+ if (ping.accountId === snapshot.account.id)
498
+ return;
499
+ const now = Date.now();
500
+ const entry = { ...ping, at: now };
501
+ this.typingEntries = [...this.typingEntries.filter((current) => now - current.at <= typingExpiryMs &&
502
+ !(current.accountId === entry.accountId && current.channelId === entry.channelId)), entry];
503
+ this.show();
504
+ this.emit("typing", entry);
475
505
  }
476
506
  else if (type === "message.edited") {
477
507
  const edit = payload;
478
508
  this.messages = this.messages.map((message) => message.id === edit.messageId
479
- ? { ...message, text: edit.text, html: edit.html, editedAt: edit.editedAt } : message);
509
+ ? { ...message, text: edit.text, html: edit.html, editedAt: edit.editedAt,
510
+ ...(edit.mentions ? { mentions: edit.mentions.accounts.length || edit.mentions.roles.length ? edit.mentions : undefined } : {}) } : message);
511
+ this.reviseMessages(edit.channelId);
480
512
  this.show();
481
513
  this.emit("messageUpdate", { id: edit.messageId, text: edit.text, html: edit.html, editedAt: edit.editedAt });
482
514
  }
515
+ else if (type === "message.reactionsChanged") {
516
+ const change = payload;
517
+ const mine = change.accountId === snapshot.account.id;
518
+ this.messages = this.messages.map((message) => message.id === change.messageId
519
+ ? { ...message, reactions: applyReaction(message.reactions, change, mine) } : message);
520
+ this.reviseMessages(change.channelId);
521
+ this.show();
522
+ this.emit("reaction", change);
523
+ }
524
+ else if (type === "message.mentioned") {
525
+ this.lastMention = payload;
526
+ this.show();
527
+ this.emit("mention", this.lastMention);
528
+ }
529
+ else if (type === "message.pinChanged") {
530
+ const { channelId, messageId, pinnedAt } = payload;
531
+ this.messages = this.messages.map((message) => message.id === messageId ? { ...message, pinnedAt } : message);
532
+ this.reviseMessages(channelId);
533
+ this.show();
534
+ }
483
535
  else if (type === "direct.edited") {
484
536
  const edit = payload;
485
537
  if (edit.senderId !== snapshot.account.id && edit.recipientId !== snapshot.account.id)
@@ -514,10 +566,11 @@ export class KoreroClient {
514
566
  this.emit("poke", poke);
515
567
  }
516
568
  else if (type === "message.deleted") {
517
- const { messageId } = payload;
569
+ const { channelId, messageId } = payload;
518
570
  for (const deleted of this.loadedHistory.values())
519
571
  deleted?.add(messageId);
520
572
  this.messages = this.messages.filter(({ id }) => id !== messageId);
573
+ this.reviseMessages(channelId);
521
574
  this.show();
522
575
  this.emit("messageDelete", { id: messageId });
523
576
  }
@@ -566,15 +619,31 @@ export class KoreroClient {
566
619
  this.volumes[accountId] = level;
567
620
  this.voice?.setVolume(accountId, level);
568
621
  }
569
- async sendMessage(channelId, text, html) {
570
- return this.rpc("message.send", { channelId, text, html });
622
+ async sendMessage(channelId, text, html, options = {}) {
623
+ return this.rpc("message.send", { channelId, text, html, ...options });
624
+ }
625
+ async editMessage(messageId, text, html, mentions) {
626
+ await this.rpc("message.edit", { messageId, text, html, mentions });
571
627
  }
572
- async editMessage(messageId, text, html) {
573
- await this.rpc("message.edit", { messageId, text, html });
628
+ async reactToMessage(channelId, messageId, emoji, reacted) {
629
+ await this.rpc("message.react", { channelId, messageId, emoji, reacted });
574
630
  }
575
631
  async deleteMessage(messageId) {
576
632
  await this.rpc("message.delete", { messageId });
577
633
  }
634
+ pinnedMessages(channelId, before) {
635
+ return this.rpc("message.pins", { channelId, before });
636
+ }
637
+ /** Searches a channel's stored messages, newest first. Pass the page's `next` as `before` to continue. */
638
+ searchMessages(channelId, query, before) {
639
+ return this.rpc("message.search", { channelId, query, before });
640
+ }
641
+ messageContext(channelId, messageId) {
642
+ return this.rpc("message.context", { channelId, messageId });
643
+ }
644
+ async setMessagePin(channelId, messageId, pinned) {
645
+ await this.rpc("message.pin", { channelId, messageId, pinned });
646
+ }
578
647
  screenRequest(connectionId, channelId, kind, payload) {
579
648
  if (this.snapshot?.connectionId !== connectionId || this.snapshot?.channelId !== channelId) {
580
649
  return Promise.reject(new Error("The active channel changed."));
@@ -604,6 +673,18 @@ export class KoreroClient {
604
673
  deleteDirect(accountId, id) {
605
674
  return this.rpc("direct.delete", { accountId, id });
606
675
  }
676
+ /** Tells a channel's readers or a direct conversation partner that this account is typing. */
677
+ typing(target) {
678
+ return this.rpc("chat.typing", target);
679
+ }
680
+ /** Removes a typing entry once that account's message arrives; returns whether one was removed. */
681
+ clearTyping(channelId, accountId) {
682
+ const remaining = this.typingEntries.filter((entry) => entry.accountId !== accountId || entry.channelId !== channelId);
683
+ if (remaining.length === this.typingEntries.length)
684
+ return false;
685
+ this.typingEntries = remaining;
686
+ return true;
687
+ }
607
688
  poke(accountId, message) {
608
689
  return this.rpc("direct.poke", { accountId, message });
609
690
  }
@@ -626,8 +707,11 @@ export class KoreroClient {
626
707
  this.media?.stop();
627
708
  this.snapshot = null;
628
709
  this.messages = [];
710
+ this.messageRevisions = {};
629
711
  this.directMessages = [];
630
712
  this.lastPoke = undefined;
713
+ this.lastMention = undefined;
714
+ this.typingEntries = [];
631
715
  this.viewedChannelId = null;
632
716
  this.viewRequest = undefined;
633
717
  this.loadedHistory.clear();