chirkle 0.0.0-stage → 0.1.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/LICENSE +674 -0
- package/README.md +310 -2
- package/package.json +75 -4
- package/pb_hooks/federation/config.js +276 -0
- package/pb_hooks/federation/inbox.js +316 -0
- package/pb_hooks/federation/messages.js +420 -0
- package/pb_hooks/federation/mirror.js +277 -0
- package/pb_hooks/federation/net.js +247 -0
- package/pb_hooks/federation/peers.js +490 -0
- package/pb_hooks/federation/people.js +376 -0
- package/pb_hooks/federation/rooms.js +552 -0
- package/pb_hooks/federation/worker.js +413 -0
- package/pb_hooks/federation.pb.js +141 -0
- package/pb_hooks/lib/access.js +102 -0
- package/pb_hooks/lib/account.js +31 -0
- package/pb_hooks/lib/anonymous.js +189 -0
- package/pb_hooks/lib/bulk.js +187 -0
- package/pb_hooks/lib/collections.js +587 -0
- package/pb_hooks/lib/dms.js +123 -0
- package/pb_hooks/lib/emoji.js +28 -0
- package/pb_hooks/lib/enrich.js +75 -0
- package/pb_hooks/lib/features.js +31 -0
- package/pb_hooks/lib/files.js +375 -0
- package/pb_hooks/lib/http.js +146 -0
- package/pb_hooks/lib/images.js +160 -0
- package/pb_hooks/lib/keepalive.js +31 -0
- package/pb_hooks/lib/lists.js +173 -0
- package/pb_hooks/lib/media.js +635 -0
- package/pb_hooks/lib/messages.js +519 -0
- package/pb_hooks/lib/moderation.js +128 -0
- package/pb_hooks/lib/outbound.js +116 -0
- package/pb_hooks/lib/pages.js +171 -0
- package/pb_hooks/lib/previews.js +346 -0
- package/pb_hooks/lib/reactions.js +110 -0
- package/pb_hooks/lib/reads.js +181 -0
- package/pb_hooks/lib/records.js +77 -0
- package/pb_hooks/lib/rooms.js +148 -0
- package/pb_hooks/lib/site.js +102 -0
- package/pb_hooks/lib/text.js +110 -0
- package/pb_hooks/lib/users.js +81 -0
- package/pb_hooks/lib/worker.js +159 -0
- package/pb_hooks/main.pb.js +324 -0
- package/pb_hooks/telegram/api.js +128 -0
- package/pb_hooks/telegram/inbound.js +548 -0
- package/pb_hooks/telegram/links.js +157 -0
- package/pb_hooks/telegram/outbound.js +323 -0
- package/pb_hooks/telegram/people.js +166 -0
- package/pb_hooks/telegram/rich.js +192 -0
- package/pb_hooks/telegram/worker.js +181 -0
- package/pb_hooks/telegram.pb.js +47 -0
- package/pb_migrations/1760000000_init.js +150 -0
- package/pb_migrations/1760000001_qa_fixes.js +69 -0
- package/pb_migrations/1760000002_avatars.js +39 -0
- package/pb_migrations/1760000003_direct_messages.js +142 -0
- package/pb_migrations/1760000004_attachments.js +28 -0
- package/pb_migrations/1760000005_file_names.js +16 -0
- package/pb_migrations/1760000006_room_reads.js +41 -0
- package/pb_migrations/1760000007_replies.js +23 -0
- package/pb_migrations/1760000008_lowercase_emails.js +24 -0
- package/pb_migrations/1760000009_reactions.js +32 -0
- package/pb_migrations/1760000010_fit_thumbs.js +18 -0
- package/pb_migrations/1760000011_forwarding.js +39 -0
- package/pb_migrations/1760000012_accounts.js +55 -0
- package/pb_migrations/1760000013_qa_pass5.js +19 -0
- package/pb_migrations/1760000014_room_access.js +96 -0
- package/pb_migrations/1760000015_site.js +46 -0
- package/pb_migrations/1760000016_forward_refs.js +43 -0
- package/pb_migrations/1760000017_no_dms.js +18 -0
- package/pb_migrations/1760000018_attachment_limits.js +24 -0
- package/pb_migrations/1760000019_telegram.js +109 -0
- package/pb_migrations/1760000020_service_messages.js +21 -0
- package/pb_migrations/1760000021_forward_view_index.js +41 -0
- package/pb_migrations/1760000022_link_previews.js +43 -0
- package/pb_migrations/1760000023_room_writers.js +49 -0
- package/pb_migrations/1760000024_message_search.js +51 -0
- package/pb_migrations/1760000025_search_dotted_i.js +16 -0
- package/pb_migrations/1760000026_push.js +67 -0
- package/pb_migrations/1760000027_push_tokens_per_user.js +31 -0
- package/pb_migrations/1760000028_media_grid_thumbs.js +18 -0
- package/pb_migrations/1760000029_media.js +71 -0
- package/pb_migrations/1760000030_collections.js +140 -0
- package/pb_migrations/1760000031_unread_limit.js +20 -0
- package/pb_migrations/1760000032_federation.js +227 -0
- package/pb_migrations/1760000033_federation_requests.js +44 -0
- package/pb_migrations/1760000034_telegram_parts.js +26 -0
- package/pb_migrations/1760000035_telegram_anonymous.js +16 -0
- package/pb_migrations/1760000036_chirkle.js +20 -0
- package/pb_migrations/1760000037_file_dims.js +35 -0
- package/pb_migrations/1760000038_drop_push.js +64 -0
- package/pb_migrations/1760000039_anonymous_rooms.js +40 -0
- package/pb_migrations/1760000040_moderators.js +42 -0
- package/pb_migrations/1760000041_shared_files.js +500 -0
- package/pb_migrations/1760000042_files_src.js +17 -0
- package/pb_public/assets/access-ALBW2MT2.js +2 -0
- package/pb_public/assets/access-ALBW2MT2.js.map +7 -0
- package/pb_public/assets/access-PB77EH4F.css +2 -0
- package/pb_public/assets/access-PB77EH4F.css.map +7 -0
- package/pb_public/assets/app-ECR34OBK.css +2 -0
- package/pb_public/assets/app-ECR34OBK.css.map +7 -0
- package/pb_public/assets/app-SILEBEXB.js +3 -0
- package/pb_public/assets/app-SILEBEXB.js.map +7 -0
- package/pb_public/assets/collections-VEOIOCAW.css +2 -0
- package/pb_public/assets/collections-VEOIOCAW.css.map +7 -0
- package/pb_public/assets/emoji-WSTVXAW5.css +2 -0
- package/pb_public/assets/emoji-WSTVXAW5.css.map +7 -0
- package/pb_public/assets/emoji-picker-QYRX6NYL.js +609 -0
- package/pb_public/assets/emoji-picker-QYRX6NYL.js.map +7 -0
- package/pb_public/assets/inter-cyrillic-JEOLYBOO.woff2 +0 -0
- package/pb_public/assets/inter-cyrillic-ext-IYF56FF6.woff2 +0 -0
- package/pb_public/assets/inter-greek-IRE366VL.woff2 +0 -0
- package/pb_public/assets/inter-latin-NRMW37G5.woff2 +0 -0
- package/pb_public/assets/inter-latin-ext-HA22NDSG.woff2 +0 -0
- package/pb_public/assets/lazy-SCCVSUZO.js +2 -0
- package/pb_public/assets/lazy-SCCVSUZO.js.map +7 -0
- package/pb_public/assets/media-DH7ECZSF.js +2 -0
- package/pb_public/assets/media-DH7ECZSF.js.map +7 -0
- package/pb_public/assets/media-L3EYTHKT.css +2 -0
- package/pb_public/assets/media-L3EYTHKT.css.map +7 -0
- package/pb_public/assets/notify-L6GERFNO.js +2 -0
- package/pb_public/assets/notify-L6GERFNO.js.map +7 -0
- package/pb_public/assets/search-3UT4HMES.js +2 -0
- package/pb_public/assets/search-3UT4HMES.js.map +7 -0
- package/pb_public/assets/switcher-CEUKRLEY.js +2 -0
- package/pb_public/assets/switcher-CEUKRLEY.js.map +7 -0
- package/pb_public/assets/viewer-DXT7SKOU.js +2 -0
- package/pb_public/assets/viewer-DXT7SKOU.js.map +7 -0
- package/pb_public/assets/viewer-P7GYCBYM.css +2 -0
- package/pb_public/assets/viewer-P7GYCBYM.css.map +7 -0
- package/pb_public/icons/apple-touch-icon.png +0 -0
- package/pb_public/icons/icon-192.png +0 -0
- package/pb_public/icons/icon-512.png +0 -0
- package/pb_public/icons/icon.svg +1 -0
- package/pb_public/icons/maskable-512.png +0 -0
- package/pb_public/index.html +21 -0
- package/pb_public/sw.js +1 -0
- package/scripts/fetch-pocketbase.sh +63 -0
- package/scripts/start.mjs +93 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
/// <reference path="../../types/pocketbase.d.ts" />
|
|
2
|
+
// Shared rooms (DESIGN.md §33.5): a room lives on its home; each instance it is shared with keeps a mirror, an
|
|
3
|
+
// ordinary room whose `peer` is the home and `remote_id` the room's global id. The home sends the room's state (a
|
|
4
|
+
// `room` event) whenever it changes; a mirror takes it as it comes. Rooms are shared by superusers on both sides
|
|
5
|
+
// (fed_shares rows); DMs with a peer's people are shared as they are opened. This is the home's side and what both
|
|
6
|
+
// sides share; a mirror's side (the events it takes from the home, and its superusers' edits) is federation/mirror.js.
|
|
7
|
+
|
|
8
|
+
const { MAX_MEMBERS } = require(`${__hooks}/lib/access.js`);
|
|
9
|
+
const { postService } = require(`${__hooks}/lib/messages.js`);
|
|
10
|
+
const { fieldError } = require(`${__hooks}/lib/records.js`);
|
|
11
|
+
const { enqueue, gid, isActive, jsonOf, myHost, peerById, peerHost, splitGid } = require(`${__hooks}/federation/config.js`);
|
|
12
|
+
const { gidOf, peopleFor, standInRow, userOf } = require(`${__hooks}/federation/people.js`);
|
|
13
|
+
|
|
14
|
+
/** rooms.name max. */
|
|
15
|
+
const ROOM_NAME_MAX = 48;
|
|
16
|
+
/** What a share sends of a room's history when it starts (§33.11, milestone 6). */
|
|
17
|
+
const BACKFILL = 200;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A refusal of a peer's event: its status goes back in the inbox's answer (§33.6).
|
|
21
|
+
* @param {number} status
|
|
22
|
+
* @param {string} message
|
|
23
|
+
*/
|
|
24
|
+
function refusal(status, message) {
|
|
25
|
+
const err = /** @type {Error & { status?: number }} */ (new Error(message));
|
|
26
|
+
err.status = status;
|
|
27
|
+
return err;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @param {core.Record} room */
|
|
31
|
+
const isMirror = (room) => room.getString("peer") !== "";
|
|
32
|
+
|
|
33
|
+
/** The room's global id. @param {core.Record} room */
|
|
34
|
+
const roomGid = (room) => (isMirror(room) ? room.getString("remote_id") : gid(room.id));
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {core.App} app
|
|
38
|
+
* @param {string} id
|
|
39
|
+
*/
|
|
40
|
+
function findRoom(app, id) {
|
|
41
|
+
if (!id) return null;
|
|
42
|
+
try {
|
|
43
|
+
return app.findRecordById("rooms", id);
|
|
44
|
+
} catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The room's shares in these states.
|
|
51
|
+
* @param {core.App} app
|
|
52
|
+
* @param {string} roomId
|
|
53
|
+
* @param {string[]} [states]
|
|
54
|
+
*/
|
|
55
|
+
function sharesOf(app, roomId, states = ["active"]) {
|
|
56
|
+
const rows = /** @type {core.Record[]} */ (
|
|
57
|
+
app.findRecordsByFilter("fed_shares", "room = {:r}", "created", 0, 0, { r: roomId }).filter(Boolean)
|
|
58
|
+
);
|
|
59
|
+
return rows.filter((s) => states.includes(s.getString("state")));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The share of the room with `peerId` that isn't over, or null.
|
|
64
|
+
* @param {core.App} app
|
|
65
|
+
* @param {string} roomId
|
|
66
|
+
* @param {string} peerId
|
|
67
|
+
*/
|
|
68
|
+
function shareOf(app, roomId, peerId) {
|
|
69
|
+
const rows = app.findRecordsByFilter("fed_shares", "room = {:r} && peer = {:p} && state != 'ended'", "created", 1, 0, {
|
|
70
|
+
r: roomId,
|
|
71
|
+
p: peerId,
|
|
72
|
+
});
|
|
73
|
+
return rows[0] || null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* On a mirror's side: the share of the home's room `g` with peer `peerId` that isn't over, or null.
|
|
78
|
+
* @param {core.App} app
|
|
79
|
+
* @param {string} peerId
|
|
80
|
+
* @param {string} g
|
|
81
|
+
*/
|
|
82
|
+
function offerOf(app, peerId, g) {
|
|
83
|
+
const rows = app.findRecordsByFilter("fed_shares", "peer = {:p} && remote_id = {:g} && state != 'ended'", "created", 1, 0, {
|
|
84
|
+
p: peerId,
|
|
85
|
+
g,
|
|
86
|
+
});
|
|
87
|
+
return rows[0] || null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The room a peer's event names, as the peer may name it: one of ours shared with it (`home`), or a mirror of one of
|
|
92
|
+
* its rooms. Null otherwise.
|
|
93
|
+
* @param {core.App} app
|
|
94
|
+
* @param {core.Record} sender
|
|
95
|
+
* @param {unknown} g
|
|
96
|
+
* @returns {{ room: core.Record, home: boolean } | null}
|
|
97
|
+
*/
|
|
98
|
+
function roomFor(app, sender, g) {
|
|
99
|
+
const s = splitGid(g);
|
|
100
|
+
if (!s) return null;
|
|
101
|
+
if (s.host === myHost()) {
|
|
102
|
+
const room = findRoom(app, s.id);
|
|
103
|
+
if (!room || isMirror(room)) return null;
|
|
104
|
+
const share = shareOf(app, room.id, sender.id);
|
|
105
|
+
return share && share.getString("state") === "active" ? { room, home: true } : null;
|
|
106
|
+
}
|
|
107
|
+
if (s.host !== peerHost(sender)) return null;
|
|
108
|
+
const share = offerOf(app, sender.id, /** @type {string} */ (g));
|
|
109
|
+
if (share?.getString("state") !== "active") return null;
|
|
110
|
+
const room = findRoom(app, share.getString("room"));
|
|
111
|
+
return room ? { room, home: false } : null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ---------------------------------------------------------------- the home's side
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The room's state as a `room` event to `peer` (§33.6): people as global ids, with the profiles it may not have.
|
|
118
|
+
* @param {core.App} app
|
|
119
|
+
* @param {core.Record} room
|
|
120
|
+
* @param {core.Record} peer
|
|
121
|
+
* @param {string} [refused] why its `members` request was refused
|
|
122
|
+
*/
|
|
123
|
+
function roomEvent(app, room, peer, refused = "") {
|
|
124
|
+
const members = room.getStringSlice("members");
|
|
125
|
+
const writers = room.getStringSlice("writers");
|
|
126
|
+
const creator = room.getString("created_by");
|
|
127
|
+
const shared = jsonOf(room, "shared", {});
|
|
128
|
+
return {
|
|
129
|
+
type: "room",
|
|
130
|
+
room: gid(room.id),
|
|
131
|
+
name: room.getString("name"),
|
|
132
|
+
kind: room.getString("kind"),
|
|
133
|
+
visibility: room.getString("visibility"),
|
|
134
|
+
posting: room.getString("posting"),
|
|
135
|
+
anonymous: room.getBool("anonymous"),
|
|
136
|
+
archived: room.getBool("archived"),
|
|
137
|
+
created_by: gidOf(app, creator),
|
|
138
|
+
members: members.map((id) => gidOf(app, id)),
|
|
139
|
+
writers: writers.map((id) => gidOf(app, id)),
|
|
140
|
+
shared_with: [myHost(), ...(Array.isArray(shared.with) ? shared.with : [])],
|
|
141
|
+
people: peopleFor(app, peer, [creator, ...members, ...writers]),
|
|
142
|
+
...(refused ? { refused } : {}),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Send the room's state to every peer it is shared with (or only `onlyPeer`).
|
|
148
|
+
* @param {core.App} app
|
|
149
|
+
* @param {core.Record} room
|
|
150
|
+
* @param {string} [onlyPeer]
|
|
151
|
+
* @param {string} [refused]
|
|
152
|
+
*/
|
|
153
|
+
function sendRoom(app, room, onlyPeer = "", refused = "") {
|
|
154
|
+
for (const share of sharesOf(app, room.id)) {
|
|
155
|
+
const peer = peerById(app, share.getString("peer"));
|
|
156
|
+
if (!peer || (onlyPeer && peer.id !== onlyPeer)) continue;
|
|
157
|
+
enqueue(app, peer.id, roomEvent(app, room, peer, refused));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* A field's value, comparable with ===: lists of relations joined (getString has nothing for them), JSON as parsed.
|
|
163
|
+
* @param {core.Record} r
|
|
164
|
+
* @param {string} f
|
|
165
|
+
*/
|
|
166
|
+
const fieldValue = (r, f) =>
|
|
167
|
+
f === "members" || f === "writers" ? r.getStringSlice(f).join(",") : f === "shared" ? JSON.stringify(jsonOf(r, f, {})) : r.getString(f);
|
|
168
|
+
|
|
169
|
+
/** What a room event says, compared before and after a save. @param {core.Record} r */
|
|
170
|
+
const stateOf = (r) =>
|
|
171
|
+
["name", "kind", "visibility", "posting", "anonymous", "archived", "created_by", "members", "writers", "shared"]
|
|
172
|
+
.map((f) => fieldValue(r, f))
|
|
173
|
+
.join("|");
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* rooms update (saved): a home room that is shared sends its new state to every mirror.
|
|
177
|
+
* @param {core.App} app
|
|
178
|
+
* @param {core.Record} room
|
|
179
|
+
* @param {string} before stateOf the room before
|
|
180
|
+
*/
|
|
181
|
+
function roomChanged(app, room, before) {
|
|
182
|
+
if (isMirror(room) || stateOf(room) === before) return;
|
|
183
|
+
sendRoom(app, room);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Set the home room's `shared` (§33.10: who else holds it) from its active shares. Saves it when that changed, which
|
|
188
|
+
* sends its state to every mirror. Returns whether it saved.
|
|
189
|
+
* @param {core.App} app
|
|
190
|
+
* @param {core.Record} room
|
|
191
|
+
*/
|
|
192
|
+
function refreshShared(app, room) {
|
|
193
|
+
const hosts = sharesOf(app, room.id)
|
|
194
|
+
.map((s) => peerById(app, s.getString("peer")))
|
|
195
|
+
.filter((p) => !!p)
|
|
196
|
+
.map((p) => peerHost(/** @type {core.Record} */ (p)));
|
|
197
|
+
const want = hosts.length ? { with: hosts } : {};
|
|
198
|
+
if (JSON.stringify(jsonOf(room, "shared", {})) === JSON.stringify(want)) return false;
|
|
199
|
+
room.set("shared", want);
|
|
200
|
+
app.save(room);
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* rooms create (saved): a DM opened with a peer's person (POST /api/chat/dms) is shared with that peer at once
|
|
206
|
+
* (§33.5): the home is here, and the peer makes its mirror. Who may DM whom was checked by the DM route (no_dms on the
|
|
207
|
+
* stand-in says whether their instance takes DMs from here).
|
|
208
|
+
* @param {core.App} app
|
|
209
|
+
* @param {core.Record} room
|
|
210
|
+
*/
|
|
211
|
+
function roomCreated(app, room) {
|
|
212
|
+
if (isMirror(room) || room.getString("kind") !== "dm") return;
|
|
213
|
+
for (const id of room.getStringSlice("members")) {
|
|
214
|
+
const row = standInRow(app, id);
|
|
215
|
+
if (!row) continue;
|
|
216
|
+
const peer = peerById(app, row.getString("peer"));
|
|
217
|
+
if (!peer || !isActive(peer)) throw fieldError("members", "That instance can't be reached.");
|
|
218
|
+
const share = new Record(app.findCollectionByNameOrId("fed_shares"));
|
|
219
|
+
share.set("room", room.id);
|
|
220
|
+
share.set("peer", peer.id);
|
|
221
|
+
share.set("state", "active");
|
|
222
|
+
share.set("kind", "dm");
|
|
223
|
+
share.set("visibility", "private");
|
|
224
|
+
app.save(share);
|
|
225
|
+
refreshShared(app, room);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* rooms update request on a home room: people from other instances may be made members or writers only when the room
|
|
231
|
+
* is shared with their instance (§33.5), so its creator can't take it where the admins didn't.
|
|
232
|
+
* @param {core.RecordRequestEvent} e
|
|
233
|
+
*/
|
|
234
|
+
function checkHomeMembers(e) {
|
|
235
|
+
const rec = e.record;
|
|
236
|
+
if (!rec || isMirror(rec)) return;
|
|
237
|
+
const orig = rec.original();
|
|
238
|
+
for (const field of ["members", "writers"]) {
|
|
239
|
+
const was = new Set(orig.getStringSlice(field));
|
|
240
|
+
for (const id of rec.getStringSlice(field)) {
|
|
241
|
+
if (was.has(id)) continue;
|
|
242
|
+
const row = standInRow(e.app, id);
|
|
243
|
+
if (!row) continue;
|
|
244
|
+
const share = shareOf(e.app, rec.id, row.getString("peer"));
|
|
245
|
+
if (share?.getString("state") !== "active") {
|
|
246
|
+
throw fieldError(field, "This room isn't shared with that person's instance.");
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* A `members` request from a mirror (§33.5): its own people in or out of a private room. Applied unless it breaks a
|
|
254
|
+
* rule, then the room goes back to every mirror (the sender learns the outcome from it, `refused` saying why not).
|
|
255
|
+
* A refusal is answered, not thrown: the room sent back with it must be kept.
|
|
256
|
+
* @param {core.App} app
|
|
257
|
+
* @param {core.Record} sender
|
|
258
|
+
* @param {any} ev
|
|
259
|
+
* @returns {{ status: number, error: string } | void}
|
|
260
|
+
*/
|
|
261
|
+
function applyMembers(app, sender, ev) {
|
|
262
|
+
const found = roomFor(app, sender, ev.room);
|
|
263
|
+
if (!found?.home) throw refusal(403, "That room isn't shared with you from here.");
|
|
264
|
+
const room = found.room;
|
|
265
|
+
const refuse = (/** @type {string} */ why) => {
|
|
266
|
+
sendRoom(app, room, sender.id, why);
|
|
267
|
+
return { status: 403, error: why };
|
|
268
|
+
};
|
|
269
|
+
if (room.getString("kind") !== "public" || room.getString("visibility") !== "private")
|
|
270
|
+
return refuse("Only a private room's members are listed.");
|
|
271
|
+
if (room.getBool("archived")) return refuse("This room is archived.");
|
|
272
|
+
const add = Array.isArray(ev.add) ? ev.add.map(String) : [];
|
|
273
|
+
const remove = Array.isArray(ev.remove) ? ev.remove.map(String) : [];
|
|
274
|
+
const host = peerHost(sender);
|
|
275
|
+
if ([...add, ...remove].some((g) => splitGid(g)?.host !== host)) return refuse("Only your own people can be added or removed.");
|
|
276
|
+
const members = new Set(room.getStringSlice("members"));
|
|
277
|
+
for (const g of remove) {
|
|
278
|
+
const id = userOf(app, sender, g, {}, true);
|
|
279
|
+
if (id && id !== room.getString("created_by")) members.delete(id);
|
|
280
|
+
}
|
|
281
|
+
for (const g of add) {
|
|
282
|
+
const id = userOf(app, sender, g, ev.people || {}, true);
|
|
283
|
+
if (!id) return refuse("Someone to add is unknown.");
|
|
284
|
+
members.add(id);
|
|
285
|
+
}
|
|
286
|
+
if (members.size > MAX_MEMBERS) return refuse(`A room has at most ${MAX_MEMBERS} members.`);
|
|
287
|
+
const before = room.getStringSlice("members").join(",");
|
|
288
|
+
room.set("members", [...members]);
|
|
289
|
+
if (room.getStringSlice("members").join(",") === before) {
|
|
290
|
+
sendRoom(app, room, sender.id);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
app.save(room); // its state goes to every mirror (roomChanged)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Remove a peer's people from a home room's members and writers (a share that ended, §33.5).
|
|
298
|
+
* @param {core.App} app
|
|
299
|
+
* @param {core.Record} room
|
|
300
|
+
* @param {string} peerId
|
|
301
|
+
*/
|
|
302
|
+
function dropPeople(app, room, peerId) {
|
|
303
|
+
if (room.getString("kind") === "dm") return;
|
|
304
|
+
const theirs = (/** @type {string} */ id) => standInRow(app, id)?.getString("peer") === peerId;
|
|
305
|
+
const members = room.getStringSlice("members");
|
|
306
|
+
const writers = room.getStringSlice("writers");
|
|
307
|
+
if (!members.some(theirs) && !writers.some(theirs)) return;
|
|
308
|
+
room.set(
|
|
309
|
+
"members",
|
|
310
|
+
members.filter((id) => !theirs(id)),
|
|
311
|
+
);
|
|
312
|
+
room.set(
|
|
313
|
+
"writers",
|
|
314
|
+
writers.filter((id) => !theirs(id)),
|
|
315
|
+
);
|
|
316
|
+
app.save(room);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* A service message, unless the room is gone or archived.
|
|
321
|
+
* @param {core.App} app
|
|
322
|
+
* @param {core.Record} room
|
|
323
|
+
* @param {string} text
|
|
324
|
+
*/
|
|
325
|
+
function tell(app, room, text) {
|
|
326
|
+
const fresh = findRoom(app, room.id);
|
|
327
|
+
if (fresh && !fresh.getBool("archived") && fresh.getString("kind") !== "dm") postService(app, fresh.id, text);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* End a share on this side (§33.5): on the home, the peer's people leave the room; on a mirror, it is archived and
|
|
332
|
+
* keeps its history. Both sides say so in the room. `tellPeer`: send `unshare` (when it ends here first).
|
|
333
|
+
* @param {core.App} app
|
|
334
|
+
* @param {core.Record} share
|
|
335
|
+
* @param {boolean} tellPeer
|
|
336
|
+
*/
|
|
337
|
+
function endShare(app, share, tellPeer) {
|
|
338
|
+
const was = share.getString("state");
|
|
339
|
+
if (was === "ended") return;
|
|
340
|
+
const peer = peerById(app, share.getString("peer"));
|
|
341
|
+
const host = peer ? peerHost(peer) : "another instance";
|
|
342
|
+
const room = findRoom(app, share.getString("room"));
|
|
343
|
+
const mirror = share.getString("remote_id") !== "";
|
|
344
|
+
if (tellPeer && peer) enqueue(app, peer.id, { type: "unshare", room: mirror ? share.getString("remote_id") : room ? gid(room.id) : "" });
|
|
345
|
+
share.set("state", "ended");
|
|
346
|
+
app.save(share);
|
|
347
|
+
if (!room || was !== "active") return;
|
|
348
|
+
if (mirror) {
|
|
349
|
+
tell(app, room, `This room is no longer shared with ${host}.`);
|
|
350
|
+
// Kept as it was, read-only, and no longer said to be shared (§33.5). A DM is closed by its stand-in's no_dms.
|
|
351
|
+
const fresh = findRoom(app, room.id);
|
|
352
|
+
if (fresh) {
|
|
353
|
+
if (fresh.getString("kind") !== "dm") fresh.set("archived", true);
|
|
354
|
+
fresh.set("shared", {});
|
|
355
|
+
app.save(fresh);
|
|
356
|
+
}
|
|
357
|
+
} else {
|
|
358
|
+
tell(app, room, `This room is no longer shared with ${host}.`);
|
|
359
|
+
dropPeople(app, room, share.getString("peer"));
|
|
360
|
+
refreshShared(app, room);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Queue the room's latest messages for a peer that just took it (§33.11, milestone 6), oldest first.
|
|
366
|
+
* @param {core.App} app
|
|
367
|
+
* @param {core.Record} room
|
|
368
|
+
* @param {core.Record} peer
|
|
369
|
+
*/
|
|
370
|
+
function backfill(app, room, peer) {
|
|
371
|
+
const { messageEvent } = require(`${__hooks}/federation/messages.js`);
|
|
372
|
+
const recs = app.findRecordsByFilter("messages", "room = {:r} && deleted = false && author != ''", "-created,-id", BACKFILL, 0, {
|
|
373
|
+
r: room.id,
|
|
374
|
+
});
|
|
375
|
+
// Backwards by index: recs is a Go slice, which reverse() garbles in place (its second half became the first's
|
|
376
|
+
// copies, so half the messages went twice and the newest half never).
|
|
377
|
+
for (let i = recs.length - 1; i >= 0; i--) {
|
|
378
|
+
const rec = recs[i];
|
|
379
|
+
if (!rec) continue;
|
|
380
|
+
const ev = messageEvent(app, rec, peer);
|
|
381
|
+
if (ev) enqueue(app, peer.id, { ...ev, backfill: true });
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// ---------------------------------------------------------------- superusers sharing (fed_shares requests)
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* fed_shares create request: a superuser on the home offers a room to a peer (§33.5, step 1).
|
|
389
|
+
* @param {core.RecordRequestEvent} e
|
|
390
|
+
*/
|
|
391
|
+
function beforeOffer(e) {
|
|
392
|
+
const rec = /** @type {core.Record} */ (e.record);
|
|
393
|
+
const room = findRoom(e.app, rec.getString("room"));
|
|
394
|
+
if (!room) throw fieldError("room", "Choose the room to share.", "validation_required");
|
|
395
|
+
if (isMirror(room)) throw fieldError("room", "This room is shared with this instance; only its home can share it.");
|
|
396
|
+
if (room.getString("kind") === "dm") throw fieldError("room", "Direct messages are shared as they are opened.");
|
|
397
|
+
if (room.getBool("archived")) throw fieldError("room", "This room is archived.");
|
|
398
|
+
const peer = peerById(e.app, rec.getString("peer"));
|
|
399
|
+
if (!peer || !isActive(peer)) throw fieldError("peer", "Choose an instance this one is paired with.");
|
|
400
|
+
if (shareOf(e.app, room.id, peer.id)) throw fieldError("room", "This room is already shared with that instance.");
|
|
401
|
+
// An ended share before it makes way: sharing again starts afresh.
|
|
402
|
+
for (const old of e.app.findRecordsByFilter("fed_shares", "room = {:r} && peer = {:p}", "", 0, 0, { r: room.id, p: peer.id })) {
|
|
403
|
+
if (old) e.app.delete(old);
|
|
404
|
+
}
|
|
405
|
+
rec.set("remote_id", "");
|
|
406
|
+
rec.set("state", "offered");
|
|
407
|
+
rec.set("accepted", false);
|
|
408
|
+
rec.set("last_error", "");
|
|
409
|
+
rec.set("name", room.getString("name"));
|
|
410
|
+
rec.set("kind", room.getString("kind"));
|
|
411
|
+
rec.set("visibility", room.getString("visibility"));
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* fed_shares create request (saved): tell the peer.
|
|
416
|
+
* @param {core.App} app
|
|
417
|
+
* @param {core.Record} share
|
|
418
|
+
*/
|
|
419
|
+
function afterOffer(app, share) {
|
|
420
|
+
const room = /** @type {core.Record} */ (findRoom(app, share.getString("room")));
|
|
421
|
+
enqueue(app, share.getString("peer"), {
|
|
422
|
+
type: "share",
|
|
423
|
+
room: gid(room.id),
|
|
424
|
+
name: room.getString("name"),
|
|
425
|
+
kind: room.getString("kind"),
|
|
426
|
+
visibility: room.getString("visibility"),
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* fed_shares update request: a peer's superuser accepts an offer (ticks `accepted`), or either side ends a share
|
|
432
|
+
* (`state` → ended). Nothing else of a share changes by hand. Returns what to do once saved.
|
|
433
|
+
* @param {core.RecordRequestEvent} e
|
|
434
|
+
* @returns {"" | "accept" | "end"}
|
|
435
|
+
*/
|
|
436
|
+
function beforeShareEdit(e) {
|
|
437
|
+
const rec = /** @type {core.Record} */ (e.record);
|
|
438
|
+
const orig = rec.original();
|
|
439
|
+
for (const f of ["room", "peer", "remote_id", "name", "kind", "visibility"]) {
|
|
440
|
+
if (rec.getString(f) !== orig.getString(f)) throw fieldError(f, "This field can't be changed.");
|
|
441
|
+
}
|
|
442
|
+
const state = rec.getString("state");
|
|
443
|
+
if (state !== orig.getString("state")) {
|
|
444
|
+
if (state !== "ended") throw fieldError("state", "A share can only be ended here.");
|
|
445
|
+
// Ended once saved, by endShare, which needs it as it was.
|
|
446
|
+
rec.set("state", orig.getString("state"));
|
|
447
|
+
rec.set("accepted", orig.getBool("accepted"));
|
|
448
|
+
return "end";
|
|
449
|
+
}
|
|
450
|
+
if (rec.getBool("accepted") !== orig.getBool("accepted")) {
|
|
451
|
+
if (!orig.getString("remote_id")) throw fieldError("accepted", "The peer accepts a room you offer.");
|
|
452
|
+
if (!rec.getBool("accepted") || orig.getString("state") !== "offered")
|
|
453
|
+
throw fieldError("accepted", "It can't be taken back; end the share instead.");
|
|
454
|
+
return "accept";
|
|
455
|
+
}
|
|
456
|
+
return "";
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* fed_shares update request (saved).
|
|
461
|
+
* @param {core.App} app
|
|
462
|
+
* @param {core.Record} share
|
|
463
|
+
* @param {"" | "accept" | "end"} what
|
|
464
|
+
*/
|
|
465
|
+
function afterShareEdit(app, share, what) {
|
|
466
|
+
if (what === "accept") enqueue(app, share.getString("peer"), { type: "share", room: share.getString("remote_id"), accepted: true });
|
|
467
|
+
if (what === "end") endShare(app, app.findRecordById("fed_shares", share.id), true);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* fed_shares delete request: a superuser declines an offer or ends a share; the peer is told.
|
|
472
|
+
* @param {core.App} app
|
|
473
|
+
* @param {core.Record} share
|
|
474
|
+
*/
|
|
475
|
+
function beforeShareDelete(app, share) {
|
|
476
|
+
endShare(app, share, true);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// ---------------------------------------------------------------- both sides
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Before a room is deleted for good (a superuser): the peers to tell, as [peer id, room global id].
|
|
483
|
+
* @param {core.App} app
|
|
484
|
+
* @param {core.Record} room
|
|
485
|
+
* @returns {string[][]}
|
|
486
|
+
*/
|
|
487
|
+
function peersToTell(app, room) {
|
|
488
|
+
if (isMirror(room)) {
|
|
489
|
+
const share = app.findRecordsByFilter("fed_shares", "room = {:r} && state = 'active'", "", 1, 0, { r: room.id })[0];
|
|
490
|
+
return share ? [[share.getString("peer"), room.getString("remote_id")]] : [];
|
|
491
|
+
}
|
|
492
|
+
return sharesOf(app, room.id, ["offered", "active"]).map((s) => [s.getString("peer"), gid(room.id)]);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* A room deleted for good: its shares went with it; the peers hear that they ended.
|
|
497
|
+
* @param {core.App} app
|
|
498
|
+
* @param {core.Record} _room
|
|
499
|
+
* @param {string[][]} peers peersToTell
|
|
500
|
+
*/
|
|
501
|
+
function tellDeleted(app, _room, peers) {
|
|
502
|
+
for (const [peerId, g] of peers) enqueue(app, peerId, { type: "unshare", room: g });
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* The rooms of a peer that can't be reached (suspended here, or not answering) say so in their header (§33.7).
|
|
507
|
+
* @param {core.App} app
|
|
508
|
+
* @param {string} peerId
|
|
509
|
+
* @param {boolean} down
|
|
510
|
+
*/
|
|
511
|
+
function setDown(app, peerId, down) {
|
|
512
|
+
for (const room of app.findRecordsByFilter("rooms", "peer = {:p}", "", 0, 0, { p: peerId })) {
|
|
513
|
+
if (!room) continue;
|
|
514
|
+
const shared = jsonOf(room, "shared", {});
|
|
515
|
+
if (!!shared.down === down || !shared.home) continue;
|
|
516
|
+
if (down) shared.down = true;
|
|
517
|
+
else delete shared.down;
|
|
518
|
+
room.set("shared", shared);
|
|
519
|
+
app.save(room);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
module.exports = {
|
|
524
|
+
ROOM_NAME_MAX,
|
|
525
|
+
fieldValue,
|
|
526
|
+
refusal,
|
|
527
|
+
findRoom,
|
|
528
|
+
shareOf,
|
|
529
|
+
offerOf,
|
|
530
|
+
sendRoom,
|
|
531
|
+
tell,
|
|
532
|
+
backfill,
|
|
533
|
+
isMirror,
|
|
534
|
+
roomGid,
|
|
535
|
+
roomFor,
|
|
536
|
+
sharesOf,
|
|
537
|
+
stateOf,
|
|
538
|
+
roomChanged,
|
|
539
|
+
roomCreated,
|
|
540
|
+
refreshShared,
|
|
541
|
+
checkHomeMembers,
|
|
542
|
+
applyMembers,
|
|
543
|
+
endShare,
|
|
544
|
+
beforeOffer,
|
|
545
|
+
afterOffer,
|
|
546
|
+
beforeShareEdit,
|
|
547
|
+
afterShareEdit,
|
|
548
|
+
beforeShareDelete,
|
|
549
|
+
setDown,
|
|
550
|
+
peersToTell,
|
|
551
|
+
tellDeleted,
|
|
552
|
+
};
|