@wtfalch/chat 0.2.0 → 0.3.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 +49 -21
- package/dist/chat/admin/index.d.ts +53 -0
- package/dist/chat/admin/index.js +33 -0
- package/dist/chat/client.js +70 -19
- package/dist/chat/fake/server.d.ts +12 -0
- package/dist/chat/fake/server.js +59 -1
- package/dist/chat/index.d.ts +1 -1
- package/dist/chat/react/Attachment.d.ts +6 -10
- package/dist/chat/react/Attachment.js +21 -11
- package/dist/chat/react/MessageRow.js +2 -2
- package/dist/chat/types.d.ts +42 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -183,8 +183,14 @@ next reload; the reply itself is never lost.
|
|
|
183
183
|
Both refuse a message somebody else sent, and `event.mine` is what a UI draws
|
|
184
184
|
that rule from. Ownership is the whole rule: Matrix ignores an `m.replace`
|
|
185
185
|
from anybody but the original sender, and the default power levels let a
|
|
186
|
-
member redact their own event and nobody else's.
|
|
187
|
-
|
|
186
|
+
member redact their own event and nobody else's.
|
|
187
|
+
|
|
188
|
+
**Moderation** ([#29](https://github.com/wtfalch/chat/issues/29)). A member
|
|
189
|
+
reports a message with `reportMessage(roomId, eventId, reason)`. The admin SDK
|
|
190
|
+
reads reports with `listReports()`, removes anyone's message from a channel
|
|
191
|
+
with `redactMessage(roomId, eventId)`, and bans a person from a channel with
|
|
192
|
+
`banPerson(roomId, subject, reason?)` until `unbanPerson(roomId, subject)`.
|
|
193
|
+
Admins act as `@chat-admin`, and only in channels the service created.
|
|
188
194
|
|
|
189
195
|
**Pinned messages** ([#8](https://github.com/wtfalch/chat/issues/8)).
|
|
190
196
|
`chat.pinMessage(roomId, eventId)` and `chat.unpinMessage(roomId, eventId)` add
|
|
@@ -287,30 +293,43 @@ id from `timeline()` before calling `retryMessage` or `cancelMessage`.
|
|
|
287
293
|
until the upload completes. Images preview inline and all files have a named
|
|
288
294
|
Download link. A failed download offers Retry download.
|
|
289
295
|
|
|
290
|
-
Media bytes are fetched with the session bearer token from
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
296
|
+
Media bytes are fetched with the session bearer token from `apps/chat-service`'s
|
|
297
|
+
`GET /_chat/media/{serverName}/{mediaId}?roomId=...&eventId=...`, never from
|
|
298
|
+
Synapse's `/_matrix/client/v1/media/download/*` directly, including images inside
|
|
299
|
+
formatted messages. `loadMedia(mxcUri, roomId, eventId)` returns a blob URL or
|
|
300
|
+
null; every `ChatAttachment` carries the `eventId` of its message. Callers must
|
|
301
|
+
revoke returned URLs. The React views revoke theirs on replacement and unmount,
|
|
302
|
+
including downloads that finish after unmount. Uploads use Matrix's separate
|
|
303
|
+
authenticated `/_matrix/media/v3/upload` endpoint. Only references on the
|
|
304
|
+
session's own homeserver are accepted.
|
|
305
|
+
|
|
306
|
+
**Files are locked to their channels** ([#32](https://github.com/wtfalch/chat/issues/32)).
|
|
307
|
+
Synapse serves a file to any valid token that names its media id, whatever room
|
|
308
|
+
it was posted in. The media route in `apps/chat-service` (`src/media.ts`) makes
|
|
309
|
+
the check Synapse does not. It serves the file only when the caller is currently
|
|
310
|
+
joined to `roomId`, `eventId` is a message in that room that references the
|
|
311
|
+
file, and that message was sent by the person who uploaded it. An ex-member, or
|
|
312
|
+
someone who re-posts a kept media id in a room of their own, is refused. A
|
|
313
|
+
deleted message's file is refused too. The deleted file itself stays stored.
|
|
314
|
+
This only holds when the reverse proxy in front of Synapse does not route
|
|
315
|
+
Synapse's own media download and thumbnail paths to clients. No malware
|
|
316
|
+
scanning or file search is included.
|
|
302
317
|
|
|
303
318
|
**Every file in a room** ([#9](https://github.com/wtfalch/chat/issues/9)).
|
|
304
319
|
`chat.roomFiles(roomId, { limit, before })` lists a room's files without paging
|
|
305
320
|
its timeline: `GET .../messages` takes Matrix's own `contains_url` filter, and
|
|
306
321
|
Synapse answers with only file-bearing events no matter how much plain
|
|
307
322
|
conversation sits between them, confirmed live against this package's own test
|
|
308
|
-
Synapse. `
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
323
|
+
Synapse. It resolves with `{ files, next }`. `files` are `ChatAttachment`s, the
|
|
324
|
+
same shape `event.attachment` already carries, and bytes still only ever come
|
|
325
|
+
through `loadMedia`. `limit` stops once that many are collected; omitted, this
|
|
326
|
+
walks pages until Synapse says there is nothing older.
|
|
327
|
+
|
|
328
|
+
**Loading more files** ([#33](https://github.com/wtfalch/chat/issues/33)).
|
|
329
|
+
`next` is the cursor for the files after these. Pass it back as `before` to load
|
|
330
|
+
the next page; it is `null` once there is nothing older. It resumes exactly
|
|
331
|
+
after the last file returned, so none is skipped or repeated. The last page can
|
|
332
|
+
come back empty.
|
|
314
333
|
|
|
315
334
|
## First-ship local proof
|
|
316
335
|
|
|
@@ -331,6 +350,12 @@ The React timeline calls `markRoomRead` when the visible room is at the bottom.
|
|
|
331
350
|
Reading a thread, viewing older history, or leaving a tab in the background does
|
|
332
351
|
not clear unread room messages.
|
|
333
352
|
|
|
353
|
+
`readBy(roomId, eventId)` names who has seen a message
|
|
354
|
+
([#31](https://github.com/wtfalch/chat/issues/31)): every joined person whose
|
|
355
|
+
read receipt is on that message or a newer one. It leaves out the signed-in
|
|
356
|
+
person and the message's sender. `onTimelineChanged` fires when a receipt
|
|
357
|
+
moves, so a view re-reads it then. Only public read receipts count.
|
|
358
|
+
|
|
334
359
|
## Administration in Manage
|
|
335
360
|
|
|
336
361
|
`@wtfalch/chat/admin` exports `createChatAdmin`, the typed management
|
|
@@ -345,7 +370,10 @@ const page = await admin.listChannels({ limit: 25 });
|
|
|
345
370
|
```
|
|
346
371
|
|
|
347
372
|
It exposes channel list/details/create/update/archive/delete, private-channel
|
|
348
|
-
invitations/removals,
|
|
373
|
+
invitations/removals, moderation (`banPerson`, `unbanPerson`, `redactMessage`,
|
|
374
|
+
`listReports`), Chat-user deactivation, a person's Matrix sessions
|
|
375
|
+
(`listSessions(subject)` and `revokeSession(subject, sessionId)`,
|
|
376
|
+
[#30](https://github.com/wtfalch/chat/issues/30)) and audit pagination. Redirects are
|
|
349
377
|
refused; tokens never appear in URLs or rendered errors. Service authorization
|
|
350
378
|
still verifies the token, organisation and Chat project's admin role on every
|
|
351
379
|
request. Request the Chat project audience and qualified project-role claims
|
|
@@ -42,6 +42,23 @@ export type AuditPage = {
|
|
|
42
42
|
rows: AuditRow[];
|
|
43
43
|
next_cursor: string | null;
|
|
44
44
|
};
|
|
45
|
+
/** One Matrix session the exchange minted for a person -- one per device. */
|
|
46
|
+
export type PersonSession = {
|
|
47
|
+
session_id: string;
|
|
48
|
+
device_id: string | null;
|
|
49
|
+
created_at: string | null;
|
|
50
|
+
last_active_at: string | null;
|
|
51
|
+
};
|
|
52
|
+
/** A member's report of a message (#29). Never the message text. */
|
|
53
|
+
export type ContentReport = {
|
|
54
|
+
id: string;
|
|
55
|
+
received_at: string | null;
|
|
56
|
+
room_id: string | null;
|
|
57
|
+
event_id: string | null;
|
|
58
|
+
reporter: string | null;
|
|
59
|
+
sender: string | null;
|
|
60
|
+
reason: string | null;
|
|
61
|
+
};
|
|
45
62
|
export type PageInput = {
|
|
46
63
|
limit?: number;
|
|
47
64
|
cursor?: string;
|
|
@@ -80,10 +97,46 @@ export declare function createChatAdmin(options: ChatAdminOptions): {
|
|
|
80
97
|
room_id: string;
|
|
81
98
|
deleted: true;
|
|
82
99
|
}>;
|
|
100
|
+
/** Bans a person from one channel until `unbanPerson` (#29). */
|
|
101
|
+
banPerson: (roomId: string, subject: string, reason?: string) => Promise<{
|
|
102
|
+
room_id: string;
|
|
103
|
+
user_id: string;
|
|
104
|
+
banned: true;
|
|
105
|
+
}>;
|
|
106
|
+
unbanPerson: (roomId: string, subject: string) => Promise<{
|
|
107
|
+
room_id: string;
|
|
108
|
+
user_id: string;
|
|
109
|
+
banned: false;
|
|
110
|
+
}>;
|
|
111
|
+
/** Removes anyone's message from a channel (#29). */
|
|
112
|
+
redactMessage: (roomId: string, eventId: string) => Promise<{
|
|
113
|
+
room_id: string;
|
|
114
|
+
event_id: string;
|
|
115
|
+
redacted: true;
|
|
116
|
+
}>;
|
|
117
|
+
/** What members reported with `reportMessage`, newest first. */
|
|
118
|
+
listReports: (input?: PageInput) => Promise<{
|
|
119
|
+
reports: ContentReport[];
|
|
120
|
+
next_cursor: string | null;
|
|
121
|
+
}>;
|
|
83
122
|
deactivatePerson: (subject: string) => Promise<{
|
|
84
123
|
user_id: string;
|
|
85
124
|
deactivated: true;
|
|
86
125
|
}>;
|
|
126
|
+
/** Every Matrix session signed in as `subject` (#30). */
|
|
127
|
+
listSessions: (subject: string) => Promise<{
|
|
128
|
+
user_id: string;
|
|
129
|
+
sessions: PersonSession[];
|
|
130
|
+
}>;
|
|
131
|
+
/** Ends one of `subject`'s sessions; its Matrix token stops working at
|
|
132
|
+
* once. The person's next exchange on that device signs in again, so
|
|
133
|
+
* a stolen ZITADEL credential needs `deactivatePerson` or revoking in
|
|
134
|
+
* ZITADEL. */
|
|
135
|
+
revokeSession: (subject: string, sessionId: string) => Promise<{
|
|
136
|
+
user_id: string;
|
|
137
|
+
session_id: string;
|
|
138
|
+
revoked: true;
|
|
139
|
+
}>;
|
|
87
140
|
audit: (input?: PageInput) => Promise<AuditPage>;
|
|
88
141
|
};
|
|
89
142
|
export type ChatAdmin = ReturnType<typeof createChatAdmin>;
|
package/dist/chat/admin/index.js
CHANGED
|
@@ -106,7 +106,40 @@ export function createChatAdmin(options) {
|
|
|
106
106
|
removePerson: (roomId, subject) => request('DELETE', `${roomPath(roomId)}/people/${identifier(subject)}`, personResult),
|
|
107
107
|
archiveChannel: (roomId) => request('POST', `${roomPath(roomId)}/archive`, (data) => roomResult(data) && 'archived' in data && data.archived === true),
|
|
108
108
|
deleteChannel: (roomId) => request('DELETE', roomPath(roomId), (data) => roomResult(data) && 'deleted' in data && data.deleted === true),
|
|
109
|
+
/** Bans a person from one channel until `unbanPerson` (#29). */
|
|
110
|
+
banPerson: (roomId, subject, reason) => request('POST', `${roomPath(roomId)}/bans`, (data) => personResult(data) && 'banned' in data && data.banned === true, reason === undefined ? { subject } : { subject, reason }),
|
|
111
|
+
unbanPerson: (roomId, subject) => request('DELETE', `${roomPath(roomId)}/bans/${identifier(subject)}`, (data) => personResult(data) && 'banned' in data && data.banned === false),
|
|
112
|
+
/** Removes anyone's message from a channel (#29). */
|
|
113
|
+
redactMessage: (roomId, eventId) => request('DELETE', `${roomPath(roomId)}/messages/${identifier(eventId)}`, (data) => roomResult(data) &&
|
|
114
|
+
'event_id' in data &&
|
|
115
|
+
typeof data.event_id === 'string' &&
|
|
116
|
+
'redacted' in data &&
|
|
117
|
+
data.redacted === true),
|
|
118
|
+
/** What members reported with `reportMessage`, newest first. */
|
|
119
|
+
listReports: (input) => request('GET', `/reports${pageQuery(100, input)}`, (data) => record(data) &&
|
|
120
|
+
Array.isArray(data.reports) &&
|
|
121
|
+
data.reports.every((report) => record(report) &&
|
|
122
|
+
typeof report.id === 'string' &&
|
|
123
|
+
['received_at', 'room_id', 'event_id', 'reporter', 'sender', 'reason'].every((key) => nullableString(report[key]))) &&
|
|
124
|
+
nullableString(data.next_cursor)),
|
|
109
125
|
deactivatePerson: (subject) => request('DELETE', `/people/${identifier(subject)}`, (data) => record(data) && typeof data.user_id === 'string' && data.deactivated === true),
|
|
126
|
+
/** Every Matrix session signed in as `subject` (#30). */
|
|
127
|
+
listSessions: (subject) => request('GET', `/people/${identifier(subject)}/sessions`, (data) => record(data) &&
|
|
128
|
+
typeof data.user_id === 'string' &&
|
|
129
|
+
Array.isArray(data.sessions) &&
|
|
130
|
+
data.sessions.every((entry) => record(entry) &&
|
|
131
|
+
typeof entry.session_id === 'string' &&
|
|
132
|
+
nullableString(entry.device_id) &&
|
|
133
|
+
nullableString(entry.created_at) &&
|
|
134
|
+
nullableString(entry.last_active_at))),
|
|
135
|
+
/** Ends one of `subject`'s sessions; its Matrix token stops working at
|
|
136
|
+
* once. The person's next exchange on that device signs in again, so
|
|
137
|
+
* a stolen ZITADEL credential needs `deactivatePerson` or revoking in
|
|
138
|
+
* ZITADEL. */
|
|
139
|
+
revokeSession: (subject, sessionId) => request('DELETE', `/people/${identifier(subject)}/sessions/${identifier(sessionId)}`, (data) => record(data) &&
|
|
140
|
+
typeof data.user_id === 'string' &&
|
|
141
|
+
typeof data.session_id === 'string' &&
|
|
142
|
+
data.revoked === true),
|
|
110
143
|
audit: (input) => request('GET', `/audit${pageQuery(200, input)}`, (data) => record(data) &&
|
|
111
144
|
Array.isArray(data.rows) &&
|
|
112
145
|
data.rows.every(auditRow) &&
|
package/dist/chat/client.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ClientEvent, ContentHelpers, Direction, EventStatus, EventTimeline, EventType, Filter, HttpApiEvent, KnownMembership, MatrixError, MatrixEventEvent, MsgType, NotificationCountType, Preset, PushRuleActionName, RelationType, RoomEvent, RoomMemberEvent, SetPresence, SyncState, UserEvent, createClient as createMatrixClient, } from 'matrix-js-sdk';
|
|
1
|
+
import { ClientEvent, ContentHelpers, Direction, EventStatus, EventTimeline, EventType, Filter, HttpApiEvent, KnownMembership, MatrixError, MatrixEventEvent, Method, MsgType, NotificationCountType, Preset, PushRuleActionName, RelationType, RoomEvent, RoomMemberEvent, SetPresence, SyncState, UserEvent, createClient as createMatrixClient, } from 'matrix-js-sdk';
|
|
2
2
|
import { exchange } from "./exchange.js";
|
|
3
3
|
import { toFormattedBody } from "./markdown.js";
|
|
4
4
|
import { createChatStore } from "./store.js";
|
|
@@ -26,11 +26,19 @@ const START_OPTIONS = { threadSupport: true };
|
|
|
26
26
|
* so at once, rather than after the whole thing has gone up and Synapse
|
|
27
27
|
* has refused it. */
|
|
28
28
|
export const MAX_UPLOAD_BYTES = 50 * 1024 * 1024;
|
|
29
|
-
/**
|
|
30
|
-
*
|
|
29
|
+
/** Where `loadMedia` asks for bytes: `apps/chat-service`'s media proxy,
|
|
30
|
+
* next to `exchangeUrl`, never Synapse's `/_matrix/client/v1/media/download`
|
|
31
|
+
* directly. Synapse serves media to any valid token without checking the
|
|
32
|
+
* room it was posted in (wtfalch/chat#32); the proxy checks. The legacy
|
|
31
33
|
* `/_matrix/media/...` download paths are unauthenticated and are never
|
|
32
34
|
* used by this package (ticket #13). */
|
|
33
|
-
|
|
35
|
+
function mediaProxyUrl(exchangeUrl, mxc, roomId, eventId) {
|
|
36
|
+
const base = exchangeUrl.replace(/\/_chat\/exchange\/?$/, '');
|
|
37
|
+
const url = new URL(`${base}/_chat/media/${encodeURIComponent(mxc.serverName)}/${encodeURIComponent(mxc.mediaId)}`);
|
|
38
|
+
url.searchParams.set('roomId', roomId);
|
|
39
|
+
url.searchParams.set('eventId', eventId);
|
|
40
|
+
return url.toString();
|
|
41
|
+
}
|
|
34
42
|
/** What `sendTyping` asks the server to auto-expire a typing state after,
|
|
35
43
|
* in ms -- matrix-js-sdk's own default when a caller passes none
|
|
36
44
|
* (`client.js`'s `sendTyping`), used explicitly here since this package's
|
|
@@ -878,36 +886,41 @@ export function createChatClient(options) {
|
|
|
878
886
|
* the caller has torn down. */
|
|
879
887
|
async function roomFiles(roomId, options) {
|
|
880
888
|
if (!client)
|
|
881
|
-
return [];
|
|
889
|
+
return { files: [], next: null };
|
|
882
890
|
const target = client;
|
|
883
891
|
if (!target.getRoom(roomId))
|
|
884
|
-
return [];
|
|
892
|
+
return { files: [], next: null };
|
|
885
893
|
const filter = new Filter(target.getUserId());
|
|
886
894
|
filter.setDefinition({
|
|
887
895
|
room: { timeline: { types: [EventType.RoomMessage], contains_url: true } },
|
|
888
896
|
});
|
|
889
897
|
const mapper = target.getEventMapper();
|
|
890
898
|
const limit = options?.limit;
|
|
891
|
-
const
|
|
899
|
+
const files = [];
|
|
892
900
|
let from = options?.before ?? null;
|
|
893
901
|
while (client === target) {
|
|
894
|
-
|
|
902
|
+
if (limit !== undefined && files.length >= limit)
|
|
903
|
+
break;
|
|
904
|
+
// Never ask for more than the limit still wants. Every event in a
|
|
905
|
+
// page is then either returned or skipped, so the page's own `end`
|
|
906
|
+
// is an exact cursor for the next call.
|
|
907
|
+
const size = limit === undefined
|
|
908
|
+
? ROOM_FILES_PAGE_SIZE
|
|
909
|
+
: Math.min(ROOM_FILES_PAGE_SIZE, limit - files.length);
|
|
910
|
+
const page = await target.createMessagesRequest(roomId, from, size, Direction.Backward, filter);
|
|
895
911
|
for (const raw of page.chunk) {
|
|
896
912
|
const event = mapper(raw);
|
|
897
913
|
if (event.isRedacted())
|
|
898
914
|
continue;
|
|
899
915
|
const attachment = toAttachment(event);
|
|
900
|
-
if (
|
|
901
|
-
|
|
902
|
-
attachments.push(attachment);
|
|
903
|
-
if (limit !== undefined && attachments.length >= limit)
|
|
904
|
-
return attachments;
|
|
916
|
+
if (attachment)
|
|
917
|
+
files.push(attachment);
|
|
905
918
|
}
|
|
906
|
-
|
|
919
|
+
from = page.end && page.chunk.length > 0 ? page.end : null;
|
|
920
|
+
if (from === null)
|
|
907
921
|
break;
|
|
908
|
-
from = page.end;
|
|
909
922
|
}
|
|
910
|
-
return
|
|
923
|
+
return { files, next: from };
|
|
911
924
|
}
|
|
912
925
|
/** Builds the Matrix content once, here rather than in `markdown.ts`:
|
|
913
926
|
* `msgtype`/`format`/`formatted_body` are Matrix's own shape, and this
|
|
@@ -1039,6 +1052,14 @@ export function createChatClient(options) {
|
|
|
1039
1052
|
return;
|
|
1040
1053
|
await client.redactEvent(roomId, eventId);
|
|
1041
1054
|
}
|
|
1055
|
+
/** See `ChatClient.reportMessage`. Sent by hand rather than through
|
|
1056
|
+
* matrix-js-sdk's `reportEvent`, which always attaches a `score` the
|
|
1057
|
+
* spec has deprecated; a report here carries only the reason. */
|
|
1058
|
+
async function reportMessage(roomId, eventId, reason) {
|
|
1059
|
+
if (!client)
|
|
1060
|
+
throw new Error('chat: reportMessage called before start()');
|
|
1061
|
+
await client.http.authedRequest(Method.Post, `/rooms/${encodeURIComponent(roomId)}/report/${encodeURIComponent(eventId)}`, undefined, { reason });
|
|
1062
|
+
}
|
|
1042
1063
|
/** See `ChatClient.sendFile`. The row appears before the upload starts,
|
|
1043
1064
|
* so a person sees the file they just picked rather than a pause. */
|
|
1044
1065
|
function sendFile(roomId, file, options) {
|
|
@@ -1155,6 +1176,7 @@ export function createChatClient(options) {
|
|
|
1155
1176
|
// Nothing to point at yet: the bytes have no id until the upload
|
|
1156
1177
|
// finishes, and by then this row is gone.
|
|
1157
1178
|
mxcUri: '',
|
|
1179
|
+
eventId: '',
|
|
1158
1180
|
isImage: upload.file.type.startsWith('image/'),
|
|
1159
1181
|
},
|
|
1160
1182
|
upload: { loaded: upload.loaded, total: upload.file.size },
|
|
@@ -1217,6 +1239,27 @@ export function createChatClient(options) {
|
|
|
1217
1239
|
.map(toPerson)
|
|
1218
1240
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
1219
1241
|
}
|
|
1242
|
+
/** See `ChatClient.readBy`. matrix-js-sdk's `getUsersReadUpTo` only
|
|
1243
|
+
* names receipts sitting *on* this event, so a person whose receipt has
|
|
1244
|
+
* since moved to a newer message would drop out. `hasUserReadEvent`
|
|
1245
|
+
* compares positions instead, which is what "seen" means. */
|
|
1246
|
+
function readBy(roomId, eventId) {
|
|
1247
|
+
const room = client?.getRoom(roomId);
|
|
1248
|
+
const event = room?.findEventById(eventId);
|
|
1249
|
+
if (!room || !event)
|
|
1250
|
+
return [];
|
|
1251
|
+
const skip = new Set([client?.getUserId(), event.getSender()]);
|
|
1252
|
+
return room
|
|
1253
|
+
.getMembersWithMembership(KnownMembership.Join)
|
|
1254
|
+
.filter((member) => !skip.has(member.userId) && room.hasUserReadEvent(member.userId, eventId))
|
|
1255
|
+
.map(toPerson)
|
|
1256
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
1257
|
+
}
|
|
1258
|
+
/** A receipt moving changes what `readBy` answers, so a view drawing it
|
|
1259
|
+
* is told the same way it is told about a new message. */
|
|
1260
|
+
function onRoomReceipt(_event, room) {
|
|
1261
|
+
notifyTimeline(room.roomId);
|
|
1262
|
+
}
|
|
1220
1263
|
/** See `ChatClient.toggleReaction`. Removing is a redaction of the
|
|
1221
1264
|
* person's own reaction event, which is the only way Matrix has to take
|
|
1222
1265
|
* one back. */
|
|
@@ -1400,15 +1443,15 @@ export function createChatClient(options) {
|
|
|
1400
1443
|
}
|
|
1401
1444
|
/** See `ChatClient.loadMedia`. The request is made by hand rather than
|
|
1402
1445
|
* through matrix-js-sdk's `mxcUrlToHttp`, which answers with a URL: a
|
|
1403
|
-
* URL is exactly what cannot work here, because
|
|
1446
|
+
* URL is exactly what cannot work here, because the proxy serves media
|
|
1404
1447
|
* only to a request carrying the token. So the bytes are fetched with
|
|
1405
1448
|
* the header on and handed back as an object URL. */
|
|
1406
|
-
async function loadMedia(mxcUri) {
|
|
1449
|
+
async function loadMedia(mxcUri, roomId, eventId) {
|
|
1407
1450
|
const parsed = parseMxc(mxcUri);
|
|
1408
1451
|
if (!parsed || !client)
|
|
1409
1452
|
return null;
|
|
1410
1453
|
const target = client;
|
|
1411
|
-
const url =
|
|
1454
|
+
const url = mediaProxyUrl(exchangeUrl, parsed, roomId, eventId);
|
|
1412
1455
|
try {
|
|
1413
1456
|
const request = () => fetch(url, {
|
|
1414
1457
|
headers: { Authorization: `Bearer ${target.getAccessToken() ?? ''}` },
|
|
@@ -1438,6 +1481,9 @@ export function createChatClient(options) {
|
|
|
1438
1481
|
* audio and video are their own `msgtype`s with their own players, and
|
|
1439
1482
|
* neither is in the first ship. */
|
|
1440
1483
|
function toAttachment(event) {
|
|
1484
|
+
const eventId = event.getId();
|
|
1485
|
+
if (!eventId)
|
|
1486
|
+
return null;
|
|
1441
1487
|
const content = event.getContent();
|
|
1442
1488
|
const msgtype = content.msgtype;
|
|
1443
1489
|
if (msgtype !== MsgType.Image && msgtype !== MsgType.File)
|
|
@@ -1452,6 +1498,7 @@ export function createChatClient(options) {
|
|
|
1452
1498
|
mimeType,
|
|
1453
1499
|
size: typeof info.size === 'number' ? info.size : 0,
|
|
1454
1500
|
mxcUri,
|
|
1501
|
+
eventId,
|
|
1455
1502
|
// The sender's own `msgtype` decides, not the name: a `.png` that is
|
|
1456
1503
|
// not one would draw a broken picture, and the preview is fetched
|
|
1457
1504
|
// through `loadMedia` either way.
|
|
@@ -1495,6 +1542,7 @@ export function createChatClient(options) {
|
|
|
1495
1542
|
created.on(ClientEvent.Sync, onSync);
|
|
1496
1543
|
created.on(RoomEvent.Timeline, onRoomTimelineChanged);
|
|
1497
1544
|
created.on(RoomEvent.LocalEchoUpdated, onRoomTimelineChanged);
|
|
1545
|
+
created.on(RoomEvent.Receipt, onRoomReceipt);
|
|
1498
1546
|
created.on(UserEvent.Presence, onUserPresence);
|
|
1499
1547
|
created.on(RoomMemberEvent.Typing, onRoomMemberTyping);
|
|
1500
1548
|
scheduleRefresh(body.expires_at);
|
|
@@ -1531,6 +1579,7 @@ export function createChatClient(options) {
|
|
|
1531
1579
|
client.removeListener(ClientEvent.Sync, onSync);
|
|
1532
1580
|
client.removeListener(RoomEvent.Timeline, onRoomTimelineChanged);
|
|
1533
1581
|
client.removeListener(RoomEvent.LocalEchoUpdated, onRoomTimelineChanged);
|
|
1582
|
+
client.removeListener(RoomEvent.Receipt, onRoomReceipt);
|
|
1534
1583
|
client.removeListener(UserEvent.Presence, onUserPresence);
|
|
1535
1584
|
client.removeListener(RoomMemberEvent.Typing, onRoomMemberTyping);
|
|
1536
1585
|
client.stopClient();
|
|
@@ -1759,6 +1808,8 @@ export function createChatClient(options) {
|
|
|
1759
1808
|
userId: () => client?.getUserId() ?? null,
|
|
1760
1809
|
people: readPeople,
|
|
1761
1810
|
roomMembers,
|
|
1811
|
+
readBy,
|
|
1812
|
+
reportMessage,
|
|
1762
1813
|
toggleReaction,
|
|
1763
1814
|
pinMessage,
|
|
1764
1815
|
unpinMessage,
|
|
@@ -100,6 +100,13 @@ export declare class FakeChatServer {
|
|
|
100
100
|
roomId: string;
|
|
101
101
|
eventId: string;
|
|
102
102
|
}>;
|
|
103
|
+
/** Every content report the client filed (#29), with the exact body it
|
|
104
|
+
* sent. */
|
|
105
|
+
readonly reports: Array<{
|
|
106
|
+
roomId: string;
|
|
107
|
+
eventId: string;
|
|
108
|
+
body: unknown;
|
|
109
|
+
}>;
|
|
103
110
|
/** Every room `/createRoom` has made, in order -- for a test that asserts
|
|
104
111
|
* what shape a DM was created in. */
|
|
105
112
|
readonly createdRooms: Array<{
|
|
@@ -111,6 +118,7 @@ export declare class FakeChatServer {
|
|
|
111
118
|
* a test that asserts what relation a reply was sent with. */
|
|
112
119
|
readonly sent: Array<{
|
|
113
120
|
roomId: string;
|
|
121
|
+
eventId: string;
|
|
114
122
|
type: string;
|
|
115
123
|
content: Record<string, unknown>;
|
|
116
124
|
}>;
|
|
@@ -200,6 +208,10 @@ export declare class FakeChatServer {
|
|
|
200
208
|
* itself, so a test drives both halves explicitly. Throws if nothing is
|
|
201
209
|
* parked. */
|
|
202
210
|
receiveTyping(roomId: string, userIds: string[]): void;
|
|
211
|
+
/** Delivers a parked poll as `userId`'s public read receipt on
|
|
212
|
+
* `eventId` in `roomId` -- the `m.receipt` EDU another person's client
|
|
213
|
+
* causes by reading. Throws if nothing is parked. */
|
|
214
|
+
receiveReceipt(roomId: string, eventId: string, userId: string): void;
|
|
203
215
|
/** Delivers a parked poll as an `m.presence` EDU for somebody else --
|
|
204
216
|
* Matrix's own three states (`online`/`unavailable`/`offline`), or
|
|
205
217
|
* MSC3026's unstable `org.matrix.msc3026.busy` -- for a test that drives
|
package/dist/chat/fake/server.js
CHANGED
|
@@ -48,6 +48,9 @@ export class FakeChatServer {
|
|
|
48
48
|
/** Every `m.read` receipt this server has answered, in arrival order --
|
|
49
49
|
* for a test that asserts `markRoomRead` actually called the wire. */
|
|
50
50
|
receipts = [];
|
|
51
|
+
/** Every content report the client filed (#29), with the exact body it
|
|
52
|
+
* sent. */
|
|
53
|
+
reports = [];
|
|
51
54
|
/** Every room `/createRoom` has made, in order -- for a test that asserts
|
|
52
55
|
* what shape a DM was created in. */
|
|
53
56
|
createdRooms = [];
|
|
@@ -439,6 +442,19 @@ export class FakeChatServer {
|
|
|
439
442
|
[roomId]: this.#timelineEntry(roomId, [], [{ type: 'm.typing', content: { user_ids: userIds } }]),
|
|
440
443
|
});
|
|
441
444
|
}
|
|
445
|
+
/** Delivers a parked poll as `userId`'s public read receipt on
|
|
446
|
+
* `eventId` in `roomId` -- the `m.receipt` EDU another person's client
|
|
447
|
+
* causes by reading. Throws if nothing is parked. */
|
|
448
|
+
receiveReceipt(roomId, eventId, userId) {
|
|
449
|
+
this.#deliverParked({
|
|
450
|
+
[roomId]: this.#timelineEntry(roomId, [], [
|
|
451
|
+
{
|
|
452
|
+
type: 'm.receipt',
|
|
453
|
+
content: { [eventId]: { 'm.read': { [userId]: { ts: this.#now() } } } },
|
|
454
|
+
},
|
|
455
|
+
]),
|
|
456
|
+
});
|
|
457
|
+
}
|
|
442
458
|
/** Delivers a parked poll as an `m.presence` EDU for somebody else --
|
|
443
459
|
* Matrix's own three states (`online`/`unavailable`/`offline`), or
|
|
444
460
|
* MSC3026's unstable `org.matrix.msc3026.busy` -- for a test that drives
|
|
@@ -828,9 +844,9 @@ export class FakeChatServer {
|
|
|
828
844
|
const roomId = decodeURIComponent(send[1]);
|
|
829
845
|
const type = decodeURIComponent(send[2]);
|
|
830
846
|
const txnId = decodeURIComponent(send[3]);
|
|
831
|
-
this.sent.push({ roomId, type, content });
|
|
832
847
|
this.#eventSeq += 1;
|
|
833
848
|
const eventId = `$sent-${this.#eventSeq}`;
|
|
849
|
+
this.sent.push({ roomId, eventId, type, content });
|
|
834
850
|
// Echoed back through sync, the way a homeserver does, carrying the
|
|
835
851
|
// transaction id so matrix-js-sdk resolves its own local echo rather
|
|
836
852
|
// than showing the message twice. Without the echo a reaction would
|
|
@@ -927,6 +943,35 @@ export class FakeChatServer {
|
|
|
927
943
|
return jsonResponse(200, { content_uri: `mxc://${this.serverName}/${mediaId}` });
|
|
928
944
|
})();
|
|
929
945
|
}
|
|
946
|
+
// `apps/chat-service`'s media proxy (wtfalch/chat#32). The same two
|
|
947
|
+
// checks the real one makes: the account is still in `roomId`, and
|
|
948
|
+
// `eventId` is a message sent there that references this media.
|
|
949
|
+
const chatMedia = url.pathname.match(/^\/_chat\/media\/([^/]+)\/([^/]+)$/);
|
|
950
|
+
if (chatMedia) {
|
|
951
|
+
const authorised = this.#authorised(init);
|
|
952
|
+
this.mediaRequests.push({ path: url.pathname, authorised });
|
|
953
|
+
if (!authorised) {
|
|
954
|
+
return Promise.resolve(jsonResponse(401, { error: 'token_invalid' }));
|
|
955
|
+
}
|
|
956
|
+
const roomId = url.searchParams.get('roomId');
|
|
957
|
+
const eventId = url.searchParams.get('eventId');
|
|
958
|
+
if (!roomId || !this.#rooms.some((room) => room.id === roomId)) {
|
|
959
|
+
return Promise.resolve(jsonResponse(403, { error: 'not_a_member' }));
|
|
960
|
+
}
|
|
961
|
+
const mxcUri = `mxc://${decodeURIComponent(chatMedia[1])}/${decodeURIComponent(chatMedia[2])}`;
|
|
962
|
+
const carrier = this.sent.find((entry) => entry.roomId === roomId && entry.eventId === eventId && entry.content.url === mxcUri);
|
|
963
|
+
if (!carrier) {
|
|
964
|
+
return Promise.resolve(jsonResponse(403, { error: 'not_in_room' }));
|
|
965
|
+
}
|
|
966
|
+
const found = this.media.get(decodeURIComponent(chatMedia[2]));
|
|
967
|
+
if (!found) {
|
|
968
|
+
return Promise.resolve(jsonResponse(404, { error: 'not_found' }));
|
|
969
|
+
}
|
|
970
|
+
return Promise.resolve(new Response(found.bytes, {
|
|
971
|
+
status: 200,
|
|
972
|
+
headers: { 'Content-Type': found.contentType || 'application/octet-stream' },
|
|
973
|
+
}));
|
|
974
|
+
}
|
|
930
975
|
const download = url.pathname.match(/^\/_matrix\/client\/v1\/media\/download\/([^/]+)\/([^/]+)$/);
|
|
931
976
|
if (download) {
|
|
932
977
|
const authorised = this.#authorised(init);
|
|
@@ -1035,6 +1080,19 @@ export class FakeChatServer {
|
|
|
1035
1080
|
});
|
|
1036
1081
|
return Promise.resolve(jsonResponse(200, {}));
|
|
1037
1082
|
}
|
|
1083
|
+
const report = url.pathname.match(/^\/_matrix\/client\/v3\/rooms\/([^/]+)\/report\/([^/]+)$/);
|
|
1084
|
+
if (report && init?.method === 'POST') {
|
|
1085
|
+
const token = bearerToken(init);
|
|
1086
|
+
if (!token || !this.#validTokens.has(token)) {
|
|
1087
|
+
return Promise.resolve(jsonResponse(401, { errcode: 'M_UNKNOWN_TOKEN', error: 'Invalid access token' }));
|
|
1088
|
+
}
|
|
1089
|
+
this.reports.push({
|
|
1090
|
+
roomId: decodeURIComponent(report[1]),
|
|
1091
|
+
eventId: decodeURIComponent(report[2]),
|
|
1092
|
+
body: JSON.parse(String(init.body ?? '{}')),
|
|
1093
|
+
});
|
|
1094
|
+
return Promise.resolve(jsonResponse(200, {}));
|
|
1095
|
+
}
|
|
1038
1096
|
// Anything else at this homeserver -- capability discovery, RTC
|
|
1039
1097
|
// transports, and whatever else a client startup probes for -- is
|
|
1040
1098
|
// answered "not recognised" rather than falling through: this origin
|
package/dist/chat/index.d.ts
CHANGED
|
@@ -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, ChatClient, ChatConnectionStatus, ChatPerson, ChatRoomSummary, ChatUploadProgress, CreateChatClientOptions, ExchangeBody, } from './types.ts';
|
|
10
|
+
export type { ChatAttachment, ChatRoomFilesPage, ChatClient, ChatConnectionStatus, ChatPerson, ChatRoomSummary, ChatUploadProgress, CreateChatClientOptions, ExchangeBody, } from './types.ts';
|
|
@@ -2,14 +2,7 @@ import type { ChatAttachment, ChatClient, ChatUploadProgress } from '../types.ts
|
|
|
2
2
|
/** Bytes as a person reads them. Whole units above a kilobyte: a file's
|
|
3
3
|
* exact byte count is never what somebody wants to know. */
|
|
4
4
|
export declare function formatSize(bytes: number): string;
|
|
5
|
-
|
|
6
|
-
* for as long as the component is mounted, revoking it on the way out.
|
|
7
|
-
*
|
|
8
|
-
* Synapse serves media only to a request carrying the token, so there is
|
|
9
|
-
* no URL to put in an `<img src>` -- the bytes have to come through the
|
|
10
|
-
* client. `null` while it is still coming, and `null` for good if it
|
|
11
|
-
* cannot be had. */
|
|
12
|
-
export declare function useMediaUrl(client: ChatClient, mxcUri: string | null): {
|
|
5
|
+
export declare function useMediaUrl(client: ChatClient, mxcUri: string | null, roomId: string, eventId: string): {
|
|
13
6
|
url: string | null;
|
|
14
7
|
failed: boolean;
|
|
15
8
|
retry: () => void;
|
|
@@ -17,6 +10,9 @@ export declare function useMediaUrl(client: ChatClient, mxcUri: string | null):
|
|
|
17
10
|
export type AttachmentViewProps = {
|
|
18
11
|
attachment: ChatAttachment;
|
|
19
12
|
client: ChatClient;
|
|
13
|
+
/** The room this attachment's message was posted in -- `loadMedia` needs
|
|
14
|
+
* it to prove membership (wtfalch/chat#32). */
|
|
15
|
+
roomId: string;
|
|
20
16
|
/** Set while the file is still going up, in which case there is nothing
|
|
21
17
|
* on the server to fetch and this draws the count instead. */
|
|
22
18
|
upload: ChatUploadProgress | null;
|
|
@@ -28,7 +24,7 @@ export type AttachmentViewProps = {
|
|
|
28
24
|
* load, or that a reader cannot see, still has to say what it is -- the
|
|
29
25
|
* agora's Named Station Rule, which is why there is no bare thumbnail
|
|
30
26
|
* anywhere in here. */
|
|
31
|
-
export declare function AttachmentView({ attachment, client, upload }: AttachmentViewProps): import("react").JSX.Element;
|
|
27
|
+
export declare function AttachmentView({ attachment, client, roomId, upload }: AttachmentViewProps): import("react").JSX.Element;
|
|
32
28
|
/** Fills in the `src` of every `img[data-mxc]` the sanitiser left in a
|
|
33
29
|
* message's HTML, and revokes what it made when the message goes.
|
|
34
30
|
*
|
|
@@ -36,4 +32,4 @@ export declare function AttachmentView({ attachment, client, upload }: Attachmen
|
|
|
36
32
|
* nothing is requested until this runs and nothing is ever requested
|
|
37
33
|
* without the token. An image whose bytes cannot be had simply stays
|
|
38
34
|
* blank beside its own `alt`. */
|
|
39
|
-
export declare function useInlineMedia(client: ChatClient, host: React.RefObject<HTMLElement | null>, html: string | null): void;
|
|
35
|
+
export declare function useInlineMedia(client: ChatClient, host: React.RefObject<HTMLElement | null>, html: string | null, roomId: string, eventId: string): void;
|
|
@@ -22,7 +22,14 @@ export function formatSize(bytes) {
|
|
|
22
22
|
* no URL to put in an `<img src>` -- the bytes have to come through the
|
|
23
23
|
* client. `null` while it is still coming, and `null` for good if it
|
|
24
24
|
* cannot be had. */
|
|
25
|
-
|
|
25
|
+
/** A Matrix event id from the server starts with `$`. A local echo's
|
|
26
|
+
* (`~...`) is not one yet, and the media proxy only serves a file whose
|
|
27
|
+
* message it can find (wtfalch/chat#32), so nothing is fetched until the
|
|
28
|
+
* server's id arrives. */
|
|
29
|
+
function isServerEventId(eventId) {
|
|
30
|
+
return eventId.startsWith('$');
|
|
31
|
+
}
|
|
32
|
+
export function useMediaUrl(client, mxcUri, roomId, eventId) {
|
|
26
33
|
const [attempt, setAttempt] = useState(0);
|
|
27
34
|
const [result, setResult] = useState(null);
|
|
28
35
|
useEffect(() => {
|
|
@@ -32,7 +39,7 @@ export function useMediaUrl(client, mxcUri) {
|
|
|
32
39
|
let current = null;
|
|
33
40
|
let cancelled = false;
|
|
34
41
|
void client
|
|
35
|
-
.loadMedia(mxcUri)
|
|
42
|
+
.loadMedia(mxcUri, roomId, eventId)
|
|
36
43
|
.catch(() => null)
|
|
37
44
|
.then((url) => {
|
|
38
45
|
if (cancelled) {
|
|
@@ -41,15 +48,18 @@ export function useMediaUrl(client, mxcUri) {
|
|
|
41
48
|
return;
|
|
42
49
|
}
|
|
43
50
|
current = url;
|
|
44
|
-
setResult({ client, mxcUri, url, attempt });
|
|
51
|
+
setResult({ client, mxcUri, eventId, url, attempt });
|
|
45
52
|
});
|
|
46
53
|
return () => {
|
|
47
54
|
cancelled = true;
|
|
48
55
|
if (current)
|
|
49
56
|
URL.revokeObjectURL(current);
|
|
50
57
|
};
|
|
51
|
-
}, [client, mxcUri, attempt]);
|
|
52
|
-
const current = result?.client === client &&
|
|
58
|
+
}, [client, mxcUri, roomId, eventId, attempt]);
|
|
59
|
+
const current = result?.client === client &&
|
|
60
|
+
result.mxcUri === mxcUri &&
|
|
61
|
+
result.eventId === eventId &&
|
|
62
|
+
result.attempt === attempt
|
|
53
63
|
? result
|
|
54
64
|
: null;
|
|
55
65
|
return {
|
|
@@ -65,8 +75,8 @@ export function useMediaUrl(client, mxcUri) {
|
|
|
65
75
|
* load, or that a reader cannot see, still has to say what it is -- the
|
|
66
76
|
* agora's Named Station Rule, which is why there is no bare thumbnail
|
|
67
77
|
* anywhere in here. */
|
|
68
|
-
export function AttachmentView({ attachment, client, upload }) {
|
|
69
|
-
const { url, failed, retry } = useMediaUrl(client, upload ? null : attachment.mxcUri);
|
|
78
|
+
export function AttachmentView({ attachment, client, roomId, upload }) {
|
|
79
|
+
const { url, failed, retry } = useMediaUrl(client, upload || !isServerEventId(attachment.eventId) ? null : attachment.mxcUri, roomId, attachment.eventId);
|
|
70
80
|
return (_jsxs("div", { className: "chat-file", children: [attachment.isImage && url && (_jsx("img", { className: "chat-file-image", src: url, alt: attachment.name })), _jsxs("div", { className: "chat-file-row", children: [_jsx("span", { className: "chat-file-name", children: attachment.name }), _jsx("span", { className: "chat-file-size", children: formatSize(attachment.size) }), url && (_jsx("a", { className: "chat-file-get", href: url, download: attachment.name, children: "Download" }))] }), failed && (_jsxs("p", { className: "chat-message-status", children: ["Could not load this file.", ' ', _jsx(Button, { kind: "ghost", size: "sm", onPress: retry, children: "Retry download" })] })), upload && (_jsxs("p", { className: "chat-file-progress", children: [_jsx("progress", { "aria-label": `Uploading ${attachment.name}`, value: upload.loaded, max: upload.total || 1 }), _jsxs("span", { children: [Math.round((upload.loaded / (upload.total || 1)) * 100), "% sent"] })] }))] }));
|
|
71
81
|
}
|
|
72
82
|
/** Fills in the `src` of every `img[data-mxc]` the sanitiser left in a
|
|
@@ -76,10 +86,10 @@ export function AttachmentView({ attachment, client, upload }) {
|
|
|
76
86
|
* nothing is requested until this runs and nothing is ever requested
|
|
77
87
|
* without the token. An image whose bytes cannot be had simply stays
|
|
78
88
|
* blank beside its own `alt`. */
|
|
79
|
-
export function useInlineMedia(client, host, html) {
|
|
89
|
+
export function useInlineMedia(client, host, html, roomId, eventId) {
|
|
80
90
|
useEffect(() => {
|
|
81
91
|
const element = host.current;
|
|
82
|
-
if (!element || !html)
|
|
92
|
+
if (!element || !html || !isServerEventId(eventId))
|
|
83
93
|
return;
|
|
84
94
|
const images = Array.from(element.querySelectorAll('img[data-mxc]'));
|
|
85
95
|
if (images.length === 0)
|
|
@@ -91,7 +101,7 @@ export function useInlineMedia(client, host, html) {
|
|
|
91
101
|
if (!mxcUri)
|
|
92
102
|
continue;
|
|
93
103
|
client
|
|
94
|
-
.loadMedia(mxcUri)
|
|
104
|
+
.loadMedia(mxcUri, roomId, eventId)
|
|
95
105
|
.then((url) => {
|
|
96
106
|
if (!url)
|
|
97
107
|
return;
|
|
@@ -109,5 +119,5 @@ export function useInlineMedia(client, host, html) {
|
|
|
109
119
|
for (const url of made)
|
|
110
120
|
URL.revokeObjectURL(url);
|
|
111
121
|
};
|
|
112
|
-
}, [client, host, html]);
|
|
122
|
+
}, [client, host, html, roomId, eventId]);
|
|
113
123
|
}
|
|
@@ -25,7 +25,7 @@ export function MessageRow({ event, client, roomId, onOpenThread, highlighted })
|
|
|
25
25
|
// The sanitiser leaves an allowed image with a reference and no `src`;
|
|
26
26
|
// this is what puts one there, with the bytes fetched under the token.
|
|
27
27
|
const body = useRef(null);
|
|
28
|
-
useInlineMedia(client, body, html);
|
|
28
|
+
useInlineMedia(client, body, html, roomId, event.id);
|
|
29
29
|
const [picking, setPicking] = useState(false);
|
|
30
30
|
const [editing, setEditing] = useState(false);
|
|
31
31
|
const [confirmingDelete, setConfirmingDelete] = useState(false);
|
|
@@ -56,7 +56,7 @@ export function MessageRow({ event, client, roomId, onOpenThread, highlighted })
|
|
|
56
56
|
dangerouslySetInnerHTML: { __html: html } })) : event.attachment ? (
|
|
57
57
|
// A file names itself in its own row, so the body would say the
|
|
58
58
|
// same thing twice.
|
|
59
|
-
_jsx(AttachmentView, { attachment: event.attachment, client: client, upload: event.upload })) : (_jsx("div", { className: "chat-message-body", children: event.body })), (event.reactions.length > 0 || (event.status === 'sent' && !event.deleted)) && (_jsxs("div", { className: "chat-reactions", children: [event.reactions.map((reaction) => (_jsx(ReactionPill, { reaction: reaction, onPress: react }, reaction.key))), event.status === 'sent' && !event.deleted && (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: "chat-react", onClick: () => setPicking((open) => !open), "aria-expanded": picking, children: "React" }), picking && (_jsx("span", { className: "chat-react-choices", children: QUICK_REACTIONS.map((key) => (_jsx("button", { type: "button", className: "chat-react-choice", onClick: () => react(key), "aria-label": `React with ${key}`, children: key }, key))) }))] })), own && !editing && !confirmingDelete && (_jsxs(_Fragment, { children: [!event.attachment && (_jsx("button", { type: "button", className: "chat-react", onClick: () => setEditing(true), children: "Edit" })), _jsx("button", { type: "button", className: "chat-react", onClick: () => setConfirmingDelete(true), children: "Delete" })] })), own && confirmingDelete && (
|
|
59
|
+
_jsx(AttachmentView, { attachment: event.attachment, client: client, roomId: roomId, upload: event.upload })) : (_jsx("div", { className: "chat-message-body", children: event.body })), (event.reactions.length > 0 || (event.status === 'sent' && !event.deleted)) && (_jsxs("div", { className: "chat-reactions", children: [event.reactions.map((reaction) => (_jsx(ReactionPill, { reaction: reaction, onPress: react }, reaction.key))), event.status === 'sent' && !event.deleted && (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: "chat-react", onClick: () => setPicking((open) => !open), "aria-expanded": picking, children: "React" }), picking && (_jsx("span", { className: "chat-react-choices", children: QUICK_REACTIONS.map((key) => (_jsx("button", { type: "button", className: "chat-react-choice", onClick: () => react(key), "aria-label": `React with ${key}`, children: key }, key))) }))] })), own && !editing && !confirmingDelete && (_jsxs(_Fragment, { children: [!event.attachment && (_jsx("button", { type: "button", className: "chat-react", onClick: () => setEditing(true), children: "Edit" })), _jsx("button", { type: "button", className: "chat-react", onClick: () => setConfirmingDelete(true), children: "Delete" })] })), own && confirmingDelete && (
|
|
60
60
|
// Asked in the row rather than in a dialog: a redaction cannot
|
|
61
61
|
// be taken back, and a browser dialog would take the whole
|
|
62
62
|
// conversation away to ask one question.
|
package/dist/chat/types.d.ts
CHANGED
|
@@ -91,11 +91,21 @@ export type ChatAttachment = {
|
|
|
91
91
|
/** Bytes, as the sender reported them. `0` when the sender sent none. */
|
|
92
92
|
readonly size: number;
|
|
93
93
|
readonly mxcUri: string;
|
|
94
|
+
/** The message this file was posted in. `loadMedia` needs it: the media
|
|
95
|
+
* proxy only serves a file whose message it can find in the room
|
|
96
|
+
* (wtfalch/chat#32). */
|
|
97
|
+
readonly eventId: string;
|
|
94
98
|
/** Whether this package will preview it inline. An image, and only ever
|
|
95
99
|
* by the `mimeType` the sender declared -- which is why the preview is
|
|
96
100
|
* still fetched through `loadMedia` rather than pointed at. */
|
|
97
101
|
readonly isImage: boolean;
|
|
98
102
|
};
|
|
103
|
+
/** One page of `roomFiles`. `next` is `null` when the room has no older
|
|
104
|
+
* files; otherwise pass it back as `before` to continue. */
|
|
105
|
+
export type ChatRoomFilesPage = {
|
|
106
|
+
readonly files: readonly ChatAttachment[];
|
|
107
|
+
readonly next: string | null;
|
|
108
|
+
};
|
|
99
109
|
/** A file still going up, as `timeline()` shows it: a message that is not
|
|
100
110
|
* a message yet. `total` is the file's own size, so a caller draws a
|
|
101
111
|
* proportion without knowing anything about chunks. */
|
|
@@ -246,13 +256,14 @@ export type ChatClient = {
|
|
|
246
256
|
* caller ever paging the room's timeline to find them -- see
|
|
247
257
|
* `ChatAttachment` for what counts as a file. `options.limit` stops
|
|
248
258
|
* once that many have been collected; omitted, this keeps going until
|
|
249
|
-
* Synapse says there is nothing older. `
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
259
|
+
* Synapse says there is nothing older. The result's `next` is the
|
|
260
|
+
* cursor for the files after these: pass it back as `options.before` to
|
|
261
|
+
* load more (#33). It is Synapse's own opaque `/messages` token, not a
|
|
262
|
+
* `ChatTimelineEvent.id`, and `null` once there is nothing older. With
|
|
263
|
+
* a `limit`, `next` resumes exactly after the last file returned: no
|
|
264
|
+
* file is skipped and none comes back twice. Synapse can hand back a
|
|
265
|
+
* cursor that turns out to have nothing behind it, so the last page may
|
|
266
|
+
* be empty.
|
|
256
267
|
*
|
|
257
268
|
* Filtered server-side, not paged client-side: `GET .../messages`
|
|
258
269
|
* takes Matrix's own `contains_url` filter, and Synapse answers it with
|
|
@@ -287,17 +298,24 @@ export type ChatClient = {
|
|
|
287
298
|
* module comment), so an encrypted room's whole timeline is already
|
|
288
299
|
* unusable, not just this method.
|
|
289
300
|
*
|
|
290
|
-
*
|
|
291
|
-
* know. */
|
|
301
|
+
* No files and a `null` cursor before `start()` resolves, or for a room
|
|
302
|
+
* this client does not know. */
|
|
292
303
|
roomFiles: (roomId: string, options?: {
|
|
293
304
|
limit?: number;
|
|
294
305
|
before?: string;
|
|
295
|
-
}) => Promise<
|
|
306
|
+
}) => Promise<ChatRoomFilesPage>;
|
|
296
307
|
/** Everyone joined to `roomId`, by name, the signed-in person included --
|
|
297
308
|
* who a message in this room can mention. A room's own membership, not
|
|
298
309
|
* the company's: mentioning somebody who is not here would notify
|
|
299
310
|
* nobody. Empty for a room this client does not know. */
|
|
300
311
|
roomMembers: (roomId: string) => ChatPerson[];
|
|
312
|
+
/** Who has seen `eventId` in `roomId`: every joined person whose read
|
|
313
|
+
* receipt is at this message or a newer one, by name (#31). Leaves out
|
|
314
|
+
* the signed-in person and the message's own sender. Only people who
|
|
315
|
+
* send public read receipts appear. `onTimelineChanged` fires when a
|
|
316
|
+
* receipt moves, so a view re-reads this then. Empty for a room or
|
|
317
|
+
* message this client does not have loaded. */
|
|
318
|
+
readBy: (roomId: string, eventId: string) => ChatPerson[];
|
|
301
319
|
/** Adds the signed-in person's reaction to `eventId`, or takes it away
|
|
302
320
|
* if it is already theirs -- one call for both, because that is the one
|
|
303
321
|
* thing a person ever means by pressing a reaction.
|
|
@@ -334,6 +352,12 @@ export type ChatClient = {
|
|
|
334
352
|
* thread hanging off a deleted message keeps every reply, and a reader
|
|
335
353
|
* scrolling past does not have the page move under them. */
|
|
336
354
|
deleteMessage: (roomId: string, eventId: string) => Promise<void>;
|
|
355
|
+
/** Reports someone's message to the company's chat admins, with a
|
|
356
|
+
* reason (#29). The admins see who reported which message and why
|
|
357
|
+
* (`listReports` in `@wtfalch/chat/admin`), and can remove it or ban
|
|
358
|
+
* its sender. The message stays until they act. Rejects if the server
|
|
359
|
+
* refuses, for instance for a message this person cannot see. */
|
|
360
|
+
reportMessage: (roomId: string, eventId: string, reason: string) => Promise<void>;
|
|
337
361
|
/** Retries a `'failed'` send with the same content. A no-op if `localId`
|
|
338
362
|
* is no longer in the room's pending events (already retried, already
|
|
339
363
|
* settled). */
|
|
@@ -355,14 +379,18 @@ export type ChatClient = {
|
|
|
355
379
|
* `loadMedia`. This is what the sanitiser asks before it keeps an
|
|
356
380
|
* `img` at all. */
|
|
357
381
|
canLoadMedia: (mxcUri: string) => boolean;
|
|
358
|
-
/** Fetches `mxcUri`
|
|
359
|
-
*
|
|
360
|
-
*
|
|
382
|
+
/** Fetches `mxcUri` through `apps/chat-service`'s media proxy and
|
|
383
|
+
* resolves with an object URL for the bytes. Synapse's own download
|
|
384
|
+
* endpoint serves any valid token without checking the room
|
|
385
|
+
* (wtfalch/chat#32), so this package never asks it directly. The proxy
|
|
386
|
+
* checks that this session is still in `roomId` and that `eventId`, a
|
|
387
|
+
* message in that room, references `mxcUri`. Resolves with `null` if
|
|
388
|
+
* this session may not load it, either check fails, or the fetch fails.
|
|
361
389
|
*
|
|
362
390
|
* **The caller owns the URL and must `URL.revokeObjectURL` it**, or the
|
|
363
391
|
* blob is held for the life of the document. The React components in
|
|
364
392
|
* `@wtfalch/chat/react` do that on unmount. */
|
|
365
|
-
loadMedia: (mxcUri: string) => Promise<string | null>;
|
|
393
|
+
loadMedia: (mxcUri: string, roomId: string, eventId: string) => Promise<string | null>;
|
|
366
394
|
/** Sends `file` to `roomId` as `m.image` (an image) or `m.file`, and
|
|
367
395
|
* returns the id its row carries in `timeline()` at once -- before a
|
|
368
396
|
* byte has gone up. That row is `'pending'` with `upload` counting, and
|