@voltro/plugin-chat 0.89.1 → 0.90.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/dist/service.js CHANGED
@@ -9,120 +9,124 @@ import { chatChannelsTable as c } from "./channels.entity.js";
9
9
  import { chatMembersTable as l } from "./members.entity.js";
10
10
  import { chatMessagesTable as u } from "./messages.entity.js";
11
11
  import { Effect as d, Result as f } from "effect";
12
- import { and as p, eq as m, gt as h, gte as g, inSet as _, jsonField as v, lt as y, lte as b, or as x, queryFor as S } from "@voltro/database";
13
- import { StorageService as C } from "@voltro/plugin-storage";
14
- import { createHash as w, randomUUID as ee } from "node:crypto";
15
- import { SanctionsPort as te } from "@voltro/plugin-sanctions/port";
16
- import { PlayersPort as ne } from "@voltro/plugin-players/port";
17
- import { GuildMembershipPort as T } from "@voltro/plugin-guilds/port";
18
- import { PartyMembershipPort as re } from "@voltro/plugin-matchmaking/port";
19
- import { SocialBlockPort as ie } from "@voltro/plugin-social/port";
12
+ import { and as p, eq as m, gt as h, gte as g, inSet as _, jsonField as v, lt as y, lte as ee, or as b, queryFor as x } from "@voltro/database";
13
+ import { StorageService as S } from "@voltro/plugin-storage";
14
+ import { createHash as C, randomUUID as te } from "node:crypto";
15
+ import { SanctionsPort as ne } from "@voltro/plugin-sanctions/port";
16
+ import { PlayersPort as re } from "@voltro/plugin-players/port";
17
+ import { GuildMembershipPort as w } from "@voltro/plugin-guilds/port";
18
+ import { PartyMembershipPort as ie } from "@voltro/plugin-matchmaking/port";
19
+ import { SocialBlockPort as ae } from "@voltro/plugin-social/port";
20
20
  // src/service.ts
21
- var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n instanceof e ? n : t("unavailable"), O = (e) => d.tryPromise({
21
+ var T = (e) => C("sha256").update(JSON.stringify(e)).digest("hex"), oe = (e, t, n) => "cchan_" + T([
22
+ e,
23
+ t,
24
+ n
25
+ ]).slice(0, 56), E = (n) => n instanceof e ? n : t("unavailable"), D = (e) => d.tryPromise({
22
26
  try: e,
23
- catch: D
24
- }), k = (e) => d.try({
27
+ catch: E
28
+ }), O = (e) => d.try({
25
29
  try: e,
26
- catch: D
27
- }), A = (e) => {
30
+ catch: E
31
+ }), k = (e) => {
28
32
  if (typeof e != "string" || !e.trim() || e.length > 256 || e.includes("\0") || e === "~") throw t("invalid_input");
29
- }, j = (e) => {
33
+ }, A = (e) => {
30
34
  if (e.type !== "user") throw t("identity_required");
31
- return A(e.id), A(e.tenantId), e;
32
- }, M = (e) => {
35
+ return k(e.id), k(e.tenantId), e;
36
+ }, j = (e) => {
33
37
  let n = e instanceof Date ? e.getTime() : typeof e == "string" ? Date.parse(e) : NaN;
34
38
  if (!Number.isSafeInteger(n) || n < 0) throw t("unavailable");
35
39
  return n;
36
- }, N = (e) => e === null ? null : M(e), P = (e, n) => {
40
+ }, M = (e) => e === null ? null : j(e), N = (e, n) => {
37
41
  let r = e[n];
38
42
  if (typeof r != "string") throw t("unavailable");
39
43
  return r;
40
- }, F = (e) => e === !0 || e === 1, I = (e) => ({
41
- id: P(e, "id"),
44
+ }, P = (e) => e === !0 || e === 1, F = (e) => ({
45
+ id: N(e, "id"),
42
46
  kind: e.kind,
43
- refId: P(e, "refId"),
44
- name: P(e, "name"),
47
+ refId: N(e, "refId"),
48
+ name: N(e, "name"),
45
49
  slowModeMs: Number(e.slowModeMs),
46
50
  retentionMs: e.retentionMs === null ? null : Number(e.retentionMs),
47
51
  maxMembers: Number(e.maxMembers),
48
- frozen: F(e.frozen),
49
- createdAt: M(e.createdAt),
52
+ frozen: P(e.frozen),
53
+ createdAt: j(e.createdAt),
50
54
  metadata: e.metadata
51
- }), L = (e) => ({
52
- id: P(e, "id"),
53
- channelId: P(e, "channelId"),
54
- subjectId: P(e, "subjectId"),
55
+ }), I = (e) => ({
56
+ id: N(e, "id"),
57
+ channelId: N(e, "channelId"),
58
+ subjectId: N(e, "subjectId"),
55
59
  role: e.role,
56
- mutedUntil: N(e.mutedUntil),
57
- lastReadAt: M(e.lastReadAt),
58
- lastReadId: e.lastReadId === null ? null : P(e, "lastReadId"),
59
- notify: F(e.notify),
60
+ mutedUntil: M(e.mutedUntil),
61
+ lastReadAt: j(e.lastReadAt),
62
+ lastReadId: e.lastReadId === null ? null : N(e, "lastReadId"),
63
+ notify: P(e.notify),
60
64
  metadata: e.metadata
61
- }), R = (e, t = !1) => ({
62
- id: P(e, "id"),
63
- channelId: P(e, "channelId"),
65
+ }), L = (e, t = !1) => ({
66
+ id: N(e, "id"),
67
+ channelId: N(e, "channelId"),
64
68
  authorId: e.authorId,
65
69
  kind: e.kind,
66
- body: e.deletedAt !== null && !t ? "" : P(e, "body"),
70
+ body: e.deletedAt !== null && !t ? "" : N(e, "body"),
67
71
  payload: e.deletedAt !== null && !t ? {} : e.payload,
68
72
  replyToId: e.replyToId,
69
- editedAt: N(e.editedAt),
70
- deletedAt: N(e.deletedAt),
71
- flagged: F(e.flagged),
72
- createdAt: M(e.createdAt),
73
+ editedAt: M(e.editedAt),
74
+ deletedAt: M(e.deletedAt),
75
+ flagged: P(e.flagged),
76
+ createdAt: j(e.createdAt),
73
77
  version: Number(e.version),
74
78
  metadata: e.metadata
75
- }), z = (w, z, ae) => {
76
- let B = () => {
77
- let e = z.now();
79
+ }), R = (C, R, se) => {
80
+ let z = () => {
81
+ let e = R.now();
78
82
  if (!Number.isSafeInteger(e) || e < 0 || e > 864e13) throw t("unavailable");
79
83
  return e;
80
- }, oe = (e) => {
81
- if (typeof e != "string" || !e.trim() || [...e].length > z.maxBodyLength || /[\u0000-\u0008\u000b\u000c\u000e-\u001f]/u.test(e)) throw t("invalid_input");
84
+ }, ce = (e) => {
85
+ if (typeof e != "string" || !e.trim() || [...e].length > R.maxBodyLength || /[\u0000-\u0008\u000b\u000c\u000e-\u001f]/u.test(e)) throw t("invalid_input");
82
86
  return e;
83
- }, V = (e, t, n) => "cmem_" + E([
87
+ }, B = (e, t, n) => "cmem_" + T([
84
88
  e,
85
89
  t,
86
90
  n
87
- ]).slice(0, 56), H = async (e, n, r, i = !1) => {
88
- A(r);
89
- let a = S(c).where(m("id", r)).limit(1), o = (await e.query((i ? a.forUpdate() : a).descriptor))[0];
91
+ ]).slice(0, 56), V = async (e, n, r, i = !1) => {
92
+ k(r);
93
+ let a = x(c).where(m("id", r)).limit(1), o = (await e.query((i ? a.forUpdate() : a).descriptor))[0];
90
94
  if (!o || o.tenantId !== n) throw t("not_found");
91
95
  return o;
92
- }, U = async (e, n, r, i) => {
93
- let a = (await e.query(S(l).where(m("id", V(n, r, i))).limit(1).descriptor))[0];
96
+ }, H = async (e, n, r, i) => {
97
+ let a = (await e.query(x(l).where(m("id", B(n, r, i))).limit(1).descriptor))[0];
94
98
  if (!a || a.tenantId !== n || a.channelId !== r || a.subjectId !== i) throw t("forbidden");
95
99
  return a;
96
- }, W = async (e, n, r) => {
97
- A(r);
98
- let i = (await e.query(S(u).where(m("id", r)).limit(1).descriptor))[0];
100
+ }, U = async (e, n, r) => {
101
+ k(r);
102
+ let i = (await e.query(x(u).where(m("id", r)).limit(1).descriptor))[0];
99
103
  if (!i || i.tenantId !== n) throw t("not_found");
100
104
  return i;
101
- }, G = (e, t) => "cadm_" + E([e, t]).slice(0, 56), se = async (e, t) => {
102
- A(e);
103
- for (let r of [...new Set(t)].sort()) A(r), await w.insertIgnore(n.tableName, {
104
- id: G(e, r),
105
+ }, W = (e, t) => "cadm_" + T([e, t]).slice(0, 56), le = async (e, t) => {
106
+ k(e);
107
+ for (let r of [...new Set(t)].sort()) k(r), await C.insertIgnore(n.tableName, {
108
+ id: W(e, r),
105
109
  tenantId: e,
106
110
  subjectId: r,
107
111
  admission: 0,
108
112
  erased: !1
109
113
  }, { conflictColumns: ["id"] });
110
- }, ce = async (e, r, i, a = !1) => {
114
+ }, G = async (e, r, i, a = !1) => {
111
115
  for (let o of i) {
112
- let i = (await e.query(S(n).where(m("id", G(r, o))).limit(1).descriptor))[0];
116
+ let i = (await e.query(x(n).where(m("id", W(r, o))).limit(1).descriptor))[0];
113
117
  if (i) {
114
- if (i.tenantId !== r || !F(i.erased) && i.subjectId !== o) throw t("unavailable");
115
- if (F(i.erased) && !a) throw t("identity_required");
118
+ if (i.tenantId !== r || !P(i.erased) && i.subjectId !== o) throw t("unavailable");
119
+ if (P(i.erased) && !a) throw t("identity_required");
116
120
  }
117
121
  }
118
- }, le = async (e, r, i) => {
119
- let a = [...new Set(i)].sort((e, t) => G(r, e) < G(r, t) ? -1 : 1);
120
- for (let i of a) if (await e.updateMany(n.tableName, { admission: 0 }, { where: m("id", G(r, i)) }) !== 1) throw t("unavailable");
122
+ }, ue = async (e, r, i) => {
123
+ let a = [...new Set(i)].sort((e, t) => W(r, e) < W(r, t) ? -1 : 1);
124
+ for (let i of a) if (await e.updateMany(n.tableName, { admission: 0 }, { where: m("id", W(r, i)) }) !== 1) throw t("unavailable");
121
125
  return a;
122
- }, ue = async (e, t, n, r = !1) => ce(e, t, await le(e, t, n), r), K = async (e, n, r, i = []) => (i.length && await se(e, i), w.transactional(async (a) => {
123
- let o = i.length ? await le(a, e, i) : [];
126
+ }, de = async (e, t, n, r = !1) => G(e, t, await ue(e, t, n), r), K = async (e, n, r, i = []) => (i.length && await le(e, i), C.transactional(async (a) => {
127
+ let o = i.length ? await ue(a, e, i) : [];
124
128
  if (await a.updateMany(c.tableName, { admission: 0 }, { where: m("id", n) }) !== 1) throw t("not_found");
125
- return await ce(a, e, o), r(a, await H(a, e, n, !0));
129
+ return await G(a, e, o), r(a, await V(a, e, n, !0));
126
130
  })), q = async (e, n, r) => {
127
131
  if (!e || n.id === r) return !1;
128
132
  try {
@@ -130,11 +134,11 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
130
134
  } catch {
131
135
  throw t("unavailable");
132
136
  }
133
- }, J = d.serviceOption(ie).pipe(d.map((e) => e._tag === "Some" ? e.value : void 0)), de = d.serviceOption(C).pipe(d.map((e) => e._tag === "Some" ? e.value : void 0)), Y = d.all([d.serviceOption(T), d.serviceOption(re)]).pipe(d.map(([e, t]) => ({
137
+ }, J = d.serviceOption(ae).pipe(d.map((e) => e._tag === "Some" ? e.value : void 0)), fe = d.serviceOption(S).pipe(d.map((e) => e._tag === "Some" ? e.value : void 0)), Y = d.all([d.serviceOption(w), d.serviceOption(ie)]).pipe(d.map(([e, t]) => ({
134
138
  ...e._tag === "Some" ? { guild: e.value } : {},
135
139
  ...t._tag === "Some" ? { party: t.value } : {}
136
- }))), fe = (e, t) => t.kind === "guild" ? e.guild : t.kind === "party" ? e.party : void 0, X = async (e, n, r, i) => {
137
- let a = fe(e, r);
140
+ }))), pe = (e, t) => t.kind === "guild" ? e.guild : t.kind === "party" ? e.party : void 0, X = async (e, n, r, i) => {
141
+ let a = pe(e, r);
138
142
  if (!a) return;
139
143
  let o;
140
144
  try {
@@ -142,12 +146,12 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
142
146
  type: "user",
143
147
  id: i,
144
148
  tenantId: n
145
- }, P(r, "refId")));
149
+ }, N(r, "refId")));
146
150
  } catch {
147
151
  throw t("unavailable");
148
152
  }
149
153
  if (o !== !0) throw t("forbidden");
150
- }, pe = async (t, n, r, i) => {
154
+ }, me = async (t, n, r, i) => {
151
155
  try {
152
156
  return await X(t, n, r, i), !0;
153
157
  } catch (t) {
@@ -170,16 +174,16 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
170
174
  if (!e) throw t("unavailable");
171
175
  if (!e.ok) throw e.error;
172
176
  return e.value;
173
- }, me = (e, t) => "cmed_" + E([e, t]).slice(0, 56), he = (e) => E(e.mediaId === void 0 ? [e.body, e.replyToId ?? null] : [
177
+ }, he = (e, t) => "cmed_" + T([e, t]).slice(0, 56), ge = (e) => T(e.mediaId === void 0 ? [e.body, e.replyToId ?? null] : [
174
178
  e.body,
175
179
  e.replyToId ?? null,
176
180
  e.mediaId
177
- ]), ge = (e, n) => d.gen(function* () {
178
- let r = yield* de;
181
+ ]), _e = (e, n) => d.gen(function* () {
182
+ let r = yield* fe;
179
183
  if (!r) return yield* d.fail(t("forbidden"));
180
184
  let i = yield* d.suspend(() => r.head(n, { tenantId: e.tenantId })).pipe(d.mapError(() => t("unavailable")), d.catchDefect(() => d.fail(t("unavailable"))));
181
185
  if (i === null) return yield* d.fail(t("not_found"));
182
- let a = yield* k(() => {
186
+ let a = yield* O(() => {
183
187
  if (i.id !== n || i.tenantId !== e.tenantId || i.ownerId !== e.id || (i.derivedFrom ?? null) !== null || i.passwordHash !== null) throw t("forbidden");
184
188
  let r = (e) => {
185
189
  if (e == null) return null;
@@ -194,44 +198,44 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
194
198
  width: r(i.width),
195
199
  height: r(i.height)
196
200
  };
197
- }), o = yield* d.suspend(() => z.strategies.moderateMedia({
201
+ }), o = yield* d.suspend(() => R.strategies.moderateMedia({
198
202
  subject: e,
199
203
  ref: a,
200
- contentTypes: z.mediaContentTypes,
201
- maxBytes: z.maxMediaBytes
204
+ contentTypes: R.mediaContentTypes,
205
+ maxBytes: R.maxMediaBytes
202
206
  })).pipe(d.catchDefect(() => d.fail(t("unavailable"))));
203
207
  return o.allowed === !0 ? typeof o.flagged == "boolean" ? {
204
208
  media: a,
205
209
  flagged: o.flagged
206
210
  } : yield* d.fail(t("unavailable")) : yield* d.fail(t("content_rejected"));
207
- }), _e = (e, n) => d.gen(function* () {
208
- let r = yield* d.serviceOption(te);
211
+ }), ve = (e, n) => d.gen(function* () {
212
+ let r = yield* d.serviceOption(ne);
209
213
  if (r._tag !== "None" && (yield* d.suspend(() => r.value.check(e, "chat", { channelId: n })).pipe(d.mapError(() => t("unavailable")), d.catchDefect(() => d.fail(t("unavailable"))))).allowed !== !0) return yield* d.fail(t("sanctioned"));
210
- }), ve = (e) => d.suspend(() => z.strategies.moderate({
214
+ }), ye = (e) => d.suspend(() => R.strategies.moderate({
211
215
  body: e,
212
- denyWords: z.denyWords,
213
- action: z.moderationAction
214
- })).pipe(d.flatMap((e) => e.allowed === !0 ? k(() => {
215
- if (oe(e.body), typeof e.flagged != "boolean") throw t("unavailable");
216
+ denyWords: R.denyWords,
217
+ action: R.moderationAction
218
+ })).pipe(d.flatMap((e) => e.allowed === !0 ? O(() => {
219
+ if (ce(e.body), typeof e.flagged != "boolean") throw t("unavailable");
216
220
  return e;
217
221
  }) : d.fail(t("content_rejected"))), d.catchDefect(() => d.fail(t("unavailable")))), $ = (e) => {
218
- let n = z.strategies.retention(I(e));
222
+ let n = R.strategies.retention(F(e));
219
223
  if (n !== null && (!Number.isSafeInteger(n) || n < 1 || n > 31536e7)) throw t("unavailable");
220
224
  return n;
221
- }, ye = s({
222
- store: w,
223
- now: B,
224
- options: z,
225
- limiter: ae,
225
+ }, be = s({
226
+ store: C,
227
+ now: z,
228
+ options: R,
229
+ limiter: se,
226
230
  social: J,
227
- sanctioned: _e,
228
- readAccess: (t, n, r) => Y.pipe(d.flatMap((i) => O(async () => {
229
- let a = j(t), o = await w.transactional(async (t) => {
230
- let i = await H(t, a.tenantId, n), o = await U(t, a.tenantId, n, a.id), s = null;
231
+ sanctioned: ve,
232
+ readAccess: (t, n, r) => Y.pipe(d.flatMap((i) => D(async () => {
233
+ let a = A(t), o = await C.transactional(async (t) => {
234
+ let i = await V(t, a.tenantId, n), o = await H(t, a.tenantId, n, a.id), s = null;
231
235
  if (r !== void 0) {
232
- A(r);
236
+ k(r);
233
237
  try {
234
- s = L(await U(t, a.tenantId, n, r));
238
+ s = I(await H(t, a.tenantId, n, r));
235
239
  } catch (t) {
236
240
  if (!(t instanceof e) || t.code !== "forbidden") throw t;
237
241
  }
@@ -243,91 +247,91 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
243
247
  };
244
248
  });
245
249
  await X(i, a.tenantId, o.current, a.id);
246
- let s = o.peer !== null && r !== void 0 && !await pe(i, a.tenantId, o.current, r) ? null : o.peer;
250
+ let s = o.peer !== null && r !== void 0 && !await me(i, a.tenantId, o.current, r) ? null : o.peer;
247
251
  return {
248
- channel: I(o.current),
249
- member: L(o.own),
252
+ channel: F(o.current),
253
+ member: I(o.own),
250
254
  peer: s
251
255
  };
252
256
  })))
253
- }), be = {
254
- prepareTyping: ye.prepare,
257
+ }), xe = {
258
+ prepareTyping: be.prepare,
255
259
  mentionCandidates: (e, n) => {
256
260
  let r, i;
257
261
  try {
258
- r = structuredClone(j(e)), i = structuredClone(n), A(i.channelId), i.after !== void 0 && A(i.after);
262
+ r = structuredClone(A(e)), i = structuredClone(n), k(i.channelId), i.after !== void 0 && k(i.after);
259
263
  } catch (e) {
260
- return d.fail(D(e));
264
+ return d.fail(E(e));
261
265
  }
262
266
  return d.gen(function* () {
263
- let e = j(r), n = yield* Y, a = yield* O(async () => {
264
- let r = await H(w, e.tenantId, i.channelId);
265
- await U(w, e.tenantId, i.channelId, e.id), await X(n, e.tenantId, r, e.id);
266
- let a = await w.query(S(l).where(p(m("tenantId", e.tenantId), m("channelId", i.channelId), ...i.after === void 0 ? [] : [h("id", i.after)])).orderBy("id", "asc").limit(129).descriptor);
267
+ let e = A(r), n = yield* Y, a = yield* D(async () => {
268
+ let r = await V(C, e.tenantId, i.channelId);
269
+ await H(C, e.tenantId, i.channelId, e.id), await X(n, e.tenantId, r, e.id);
270
+ let a = await C.query(x(l).where(p(m("tenantId", e.tenantId), m("channelId", i.channelId), ...i.after === void 0 ? [] : [h("id", i.after)])).orderBy("id", "asc").limit(129).descriptor);
267
271
  if (a.some((t) => t.tenantId !== e.tenantId || t.channelId !== i.channelId)) throw t("unavailable");
268
272
  return a;
269
273
  }), o = a.slice(0, 128);
270
274
  return {
271
- authors: yield* be.authors(e, {
275
+ authors: yield* xe.authors(e, {
272
276
  channelId: i.channelId,
273
- subjectIds: o.map((e) => P(e, "subjectId"))
277
+ subjectIds: o.map((e) => N(e, "subjectId"))
274
278
  }),
275
- after: a.length > 128 ? P(o[o.length - 1], "id") : null
279
+ after: a.length > 128 ? N(o[o.length - 1], "id") : null
276
280
  };
277
281
  }).pipe(d.catchDefect(() => d.fail(t("unavailable"))));
278
282
  },
279
283
  authors: (n, r) => {
280
284
  let i, a, o;
281
285
  try {
282
- if (i = structuredClone(j(n)), a = r.channelId, A(a), !Array.isArray(r.subjectIds) || r.subjectIds.length > 128 || (o = [...r.subjectIds], o.forEach(A), new Set(o).size !== o.length)) throw t("invalid_input");
286
+ if (i = structuredClone(A(n)), a = r.channelId, k(a), !Array.isArray(r.subjectIds) || r.subjectIds.length > 128 || (o = [...r.subjectIds], o.forEach(k), new Set(o).size !== o.length)) throw t("invalid_input");
283
287
  } catch (e) {
284
- return d.fail(D(e));
288
+ return d.fail(E(e));
285
289
  }
286
290
  return d.gen(function* () {
287
- let n = j(i), r = yield* J, s = yield* d.serviceOption(ne), c = yield* Y, l = async (t) => {
288
- let i = await H(w, n.tenantId, a);
289
- await U(w, n.tenantId, a, n.id), await X(c, n.tenantId, i, n.id);
291
+ let n = A(i), r = yield* J, s = yield* d.serviceOption(re), c = yield* Y, l = async (t) => {
292
+ let i = await V(C, n.tenantId, a);
293
+ await H(C, n.tenantId, a, n.id), await X(c, n.tenantId, i, n.id);
290
294
  try {
291
- await U(w, n.tenantId, a, t);
295
+ await H(C, n.tenantId, a, t);
292
296
  } catch (t) {
293
297
  if (t instanceof e && t.code === "forbidden") return !1;
294
298
  throw t;
295
299
  }
296
- return await pe(c, n.tenantId, i, t) ? !await q(r, n, t) : !1;
300
+ return await me(c, n.tenantId, i, t) ? !await q(r, n, t) : !1;
297
301
  };
298
- if (yield* O(async () => {
299
- let e = await H(w, n.tenantId, a);
300
- await U(w, n.tenantId, a, n.id), await X(c, n.tenantId, e, n.id);
302
+ if (yield* D(async () => {
303
+ let e = await V(C, n.tenantId, a);
304
+ await H(C, n.tenantId, a, n.id), await X(c, n.tenantId, e, n.id);
301
305
  }), s._tag === "None") return [];
302
306
  let u = [];
303
307
  for (let e of o) {
304
- if (!(yield* O(() => l(e)))) continue;
308
+ if (!(yield* D(() => l(e)))) continue;
305
309
  let r = yield* d.suspend(() => s.value.resolveMember(n, e)).pipe(d.mapError(() => t("unavailable")), d.catchDefect(() => d.fail(t("unavailable"))));
306
- r !== null && (yield* k(() => {
310
+ r !== null && (yield* O(() => {
307
311
  if (typeof r.displayName != "string" || !r.displayName.trim() || r.displayName.length > 256 || r.avatar !== null && (typeof r.avatar != "string" || r.avatar.length > 2048)) throw t("unavailable");
308
- }), (yield* O(() => l(e))) && u.push({
312
+ }), (yield* D(() => l(e))) && u.push({
309
313
  subjectId: e,
310
314
  displayName: r.displayName,
311
315
  avatar: r.avatar
312
316
  }));
313
317
  }
314
318
  let f = [];
315
- for (let e of u) (yield* O(() => l(e.subjectId))) && f.push(e);
316
- return yield* O(async () => {
317
- await H(w, n.tenantId, a), await U(w, n.tenantId, a, n.id);
319
+ for (let e of u) (yield* D(() => l(e.subjectId))) && f.push(e);
320
+ return yield* D(async () => {
321
+ await V(C, n.tenantId, a), await H(C, n.tenantId, a, n.id);
318
322
  }), f;
319
323
  });
320
324
  },
321
325
  dm: (e, n) => d.gen(function* () {
322
- let r = yield* k(() => {
323
- let r = j(e);
324
- if (A(n), r.id === n) throw t("invalid_input");
326
+ let r = yield* O(() => {
327
+ let r = A(e);
328
+ if (k(n), r.id === n) throw t("invalid_input");
325
329
  return r;
326
330
  }), i = yield* J;
327
331
  if (!i) return yield* d.fail(t("forbidden"));
328
332
  if (!(yield* i.canMessage(r, n).pipe(d.mapError(() => t("unavailable"))))) return yield* d.fail(t("blocked"));
329
333
  let a = [r.id, n].sort();
330
- return yield* De({
334
+ return yield* Oe({
331
335
  type: "serviceAccount",
332
336
  id: "chat:dm",
333
337
  tenantId: r.tenantId,
@@ -335,7 +339,7 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
335
339
  }, {
336
340
  tenantId: r.tenantId,
337
341
  kind: "dm",
338
- refId: E([r.tenantId, a]),
342
+ refId: T([r.tenantId, a]),
339
343
  name: "",
340
344
  maxMembers: 2,
341
345
  members: a.map((e) => ({
@@ -345,69 +349,69 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
345
349
  });
346
350
  }),
347
351
  send: (e, n) => d.gen(function* () {
348
- let r = yield* k(() => {
349
- let t = j(e);
350
- return A(n.channelId), A(n.requestId), (n.mediaId === void 0 || n.body !== "") && oe(n.body), n.mediaId !== void 0 && A(n.mediaId), t;
351
- }), i = yield* O(async () => {
352
- let e = "cmsg_" + E([
352
+ let r = yield* O(() => {
353
+ let t = A(e);
354
+ return k(n.channelId), k(n.requestId), (n.mediaId === void 0 || n.body !== "") && ce(n.body), n.mediaId !== void 0 && k(n.mediaId), t;
355
+ }), i = yield* D(async () => {
356
+ let e = "cmsg_" + T([
353
357
  r.tenantId,
354
358
  n.channelId,
355
359
  r.id,
356
360
  n.requestId
357
- ]).slice(0, 56), i = (await w.query(S(u).where(m("id", e)).limit(1).descriptor))[0];
361
+ ]).slice(0, 56), i = (await C.query(x(u).where(m("id", e)).limit(1).descriptor))[0];
358
362
  if (!i) return null;
359
- if (await U(w, r.tenantId, n.channelId, r.id), i.tenantId !== r.tenantId || i.channelId !== n.channelId || i.authorId !== r.id || i.requestKey !== he(n)) throw t("conflict");
360
- return R(i);
363
+ if (await H(C, r.tenantId, n.channelId, r.id), i.tenantId !== r.tenantId || i.channelId !== n.channelId || i.authorId !== r.id || i.requestKey !== ge(n)) throw t("conflict");
364
+ return L(i);
361
365
  });
362
366
  if (i !== null) return i;
363
- let o = yield* J, s = yield* O(() => H(w, r.tenantId, n.channelId));
364
- yield* O(() => U(w, r.tenantId, n.channelId, r.id));
367
+ let o = yield* J, s = yield* D(() => V(C, r.tenantId, n.channelId));
368
+ yield* D(() => H(C, r.tenantId, n.channelId, r.id));
365
369
  let c = n.body === "" ? {
366
370
  body: "",
367
371
  flagged: !1
368
- } : yield* ve(n.body), f = n.body === "" ? [] : yield* d.suspend(() => z.strategies.mentionResolver({
372
+ } : yield* ye(n.body), f = n.body === "" ? [] : yield* d.suspend(() => R.strategies.mentionResolver({
369
373
  body: c.body,
370
374
  subject: r,
371
- maxMentions: z.maxMentions
375
+ maxMentions: R.maxMentions
372
376
  })).pipe(d.catchDefect(() => d.fail(t("unavailable"))));
373
- yield* k(() => {
374
- if (f.length > z.maxMentions || new Set(f).size !== f.length) throw t("invalid_input");
375
- f.forEach(A);
377
+ yield* O(() => {
378
+ if (f.length > R.maxMentions || new Set(f).size !== f.length) throw t("invalid_input");
379
+ f.forEach(k);
376
380
  });
377
- let p = yield* k(() => {
378
- let e = z.strategies.rateLimit({
379
- channel: I(s),
380
- perChannel: z.perChannelRate,
381
- global: z.globalRate
381
+ let p = yield* O(() => {
382
+ let e = R.strategies.rateLimit({
383
+ channel: F(s),
384
+ perChannel: R.perChannelRate,
385
+ global: R.globalRate
382
386
  });
383
387
  if (![e.perChannel, e.global].every((e) => Number.isSafeInteger(e) && e > 0 && e <= 1e4)) throw t("unavailable");
384
388
  return e;
385
389
  });
386
- for (let [e, i] of [[E([
390
+ for (let [e, i] of [[T([
387
391
  r.tenantId,
388
392
  r.id,
389
393
  n.channelId
390
- ]), p.perChannel], [E([r.tenantId, r.id]), p.global]]) {
391
- let n = yield* ae.consume("chat:send:" + e, {
394
+ ]), p.perChannel], [T([r.tenantId, r.id]), p.global]]) {
395
+ let n = yield* se.consume("chat:send:" + e, {
392
396
  limit: i,
393
397
  windowMs: 1e3,
394
398
  algorithm: "fixed-window",
395
399
  burst: i
396
- }, B()).pipe(d.catchDefect(() => d.fail(t("unavailable"))));
400
+ }, z()).pipe(d.catchDefect(() => d.fail(t("unavailable"))));
397
401
  if (n.failOpenReason !== void 0) return yield* d.fail(t("unavailable"));
398
402
  if (!n.allowed) return yield* d.fail(t("rate_limited"));
399
403
  }
400
- yield* _e(r, n.channelId);
401
- let h = yield* Y, g = n.mediaId === void 0 ? null : yield* ge(r, n.mediaId), _ = yield* O(async () => {
404
+ yield* ve(r, n.channelId);
405
+ let h = yield* Y, g = n.mediaId === void 0 ? null : yield* _e(r, n.mediaId), _ = yield* D(async () => {
402
406
  let e = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = async (e) => {
403
407
  i.has(e) || i.set(e, await Z(() => q(o, r, e)));
404
408
  };
405
409
  e.set(r.id, await Z(() => X(h, r.tenantId, s, r.id)));
406
410
  let c, u;
407
411
  if (s.kind === "dm" && o) {
408
- let e = (await w.query(S(l).where(m("channelId", n.channelId)).limit(3).descriptor)).find((e) => e.tenantId === r.tenantId && e.subjectId !== r.id);
412
+ let e = (await C.query(x(l).where(m("channelId", n.channelId)).limit(3).descriptor)).find((e) => e.tenantId === r.tenantId && e.subjectId !== r.id);
409
413
  if (e) {
410
- let n = P(e, "subjectId");
414
+ let n = N(e, "subjectId");
411
415
  c = n, await a(n), u = await Z(async () => {
412
416
  try {
413
417
  return await d.runPromise(o.canMessage(r, n));
@@ -418,8 +422,8 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
418
422
  }
419
423
  }
420
424
  if (n.replyToId !== void 0) {
421
- let e = await Z(() => W(w, r.tenantId, n.replyToId));
422
- e.ok && e.value.authorId !== null && await a(P(e.value, "authorId"));
425
+ let e = await Z(() => U(C, r.tenantId, n.replyToId));
426
+ e.ok && e.value.authorId !== null && await a(N(e.value, "authorId"));
423
427
  }
424
428
  for (let t of f) e.set(t, await Z(() => X(h, r.tenantId, s, t))), await a(t);
425
429
  return {
@@ -429,43 +433,43 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
429
433
  canMessage: u
430
434
  };
431
435
  });
432
- return yield* O(() => K(r.tenantId, n.channelId, async (e, i) => {
433
- let s = await U(e, r.tenantId, n.channelId, r.id), d = B();
436
+ return yield* D(() => K(r.tenantId, n.channelId, async (e, i) => {
437
+ let s = await H(e, r.tenantId, n.channelId, r.id), d = z();
434
438
  Q(_.roster.get(r.id));
435
- let p = "cmsg_" + E([
439
+ let p = "cmsg_" + T([
436
440
  r.tenantId,
437
441
  n.channelId,
438
442
  r.id,
439
443
  n.requestId
440
- ]).slice(0, 56), h = (await e.query(S(u).where(m("id", p)).limit(1).descriptor))[0];
444
+ ]).slice(0, 56), h = (await e.query(x(u).where(m("id", p)).limit(1).descriptor))[0];
441
445
  if (h) {
442
- if (h.tenantId !== r.tenantId || h.channelId !== n.channelId || h.authorId !== r.id || h.requestKey !== he(n)) throw t("conflict");
443
- return R(h);
446
+ if (h.tenantId !== r.tenantId || h.channelId !== n.channelId || h.authorId !== r.id || h.requestKey !== ge(n)) throw t("conflict");
447
+ return L(h);
444
448
  }
445
- if (F(i.frozen)) throw t("frozen");
446
- if (s.mutedUntil !== null && M(s.mutedUntil) > d) throw t("muted");
447
- if (s.lastSentAt !== null && d - M(s.lastSentAt) < Number(i.slowModeMs)) throw t("slow_mode");
449
+ if (P(i.frozen)) throw t("frozen");
450
+ if (s.mutedUntil !== null && j(s.mutedUntil) > d) throw t("muted");
451
+ if (s.lastSentAt !== null && d - j(s.lastSentAt) < Number(i.slowModeMs)) throw t("slow_mode");
448
452
  if (i.kind === "dm") {
449
- let i = await e.query(S(l).where(m("channelId", n.channelId)).limit(3).descriptor);
453
+ let i = await e.query(x(l).where(m("channelId", n.channelId)).limit(3).descriptor);
450
454
  if (i.length !== 2 || i.some((e) => e.tenantId !== r.tenantId)) throw t("unavailable");
451
455
  let a = i.find((e) => e.subjectId !== r.id);
452
456
  if (!a || !o) throw t("forbidden");
453
- let s = P(a, "subjectId");
457
+ let s = N(a, "subjectId");
454
458
  if (s !== _.recipientId) throw t("unavailable");
455
459
  if (Q(_.block.get(s))) throw t("blocked");
456
460
  if (!Q(_.canMessage)) throw t("forbidden");
457
461
  }
458
462
  if (n.replyToId !== void 0) {
459
- let i = await W(e, r.tenantId, n.replyToId);
460
- if (i.channelId !== n.channelId || i.deletedAt !== null || i.authorId !== null && Q(_.block.get(P(i, "authorId")))) throw t("forbidden");
463
+ let i = await U(e, r.tenantId, n.replyToId);
464
+ if (i.channelId !== n.channelId || i.deletedAt !== null || i.authorId !== null && Q(_.block.get(N(i, "authorId")))) throw t("forbidden");
461
465
  }
462
- for (let i of f) if (await U(e, r.tenantId, n.channelId, i), Q(_.roster.get(i)), Q(_.block.get(i))) throw t("blocked");
466
+ for (let i of f) if (await H(e, r.tenantId, n.channelId, i), Q(_.roster.get(i)), Q(_.block.get(i))) throw t("blocked");
463
467
  let v = await e.insert(u.tableName, {
464
468
  id: p,
465
469
  tenantId: r.tenantId,
466
470
  channelId: n.channelId,
467
471
  authorId: r.id,
468
- requestKey: he(n),
472
+ requestKey: ge(n),
469
473
  kind: g ? "media" : "text",
470
474
  body: c.body,
471
475
  originalBody: c.body,
@@ -482,7 +486,7 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
482
486
  metadata: {}
483
487
  });
484
488
  return g && await e.insert(a.tableName, {
485
- id: me(r.tenantId, p),
489
+ id: he(r.tenantId, p),
486
490
  tenantId: r.tenantId,
487
491
  channelId: n.channelId,
488
492
  messageId: p,
@@ -490,124 +494,124 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
490
494
  ownerId: r.id,
491
495
  state: "active",
492
496
  createdAt: new Date(d)
493
- }), await e.update(l.tableName, V(r.tenantId, n.channelId, r.id), { lastSentAt: new Date(d) }), R(v);
497
+ }), await e.update(l.tableName, B(r.tenantId, n.channelId, r.id), { lastSentAt: new Date(d) }), L(v);
494
498
  }));
495
499
  }),
496
500
  edit: (e, n) => d.gen(function* () {
497
- let r = yield* k(() => {
498
- let r = j(e);
499
- if (oe(n.body), !Number.isSafeInteger(n.baseVersion) || n.baseVersion < 0 || n.baseVersion >= 2147483647) throw t("invalid_input");
501
+ let r = yield* O(() => {
502
+ let r = A(e);
503
+ if (ce(n.body), !Number.isSafeInteger(n.baseVersion) || n.baseVersion < 0 || n.baseVersion >= 2147483647) throw t("invalid_input");
500
504
  return r;
501
- }), i = yield* ve(n.body), a = yield* J, o = yield* O(() => W(w, r.tenantId, n.messageId));
502
- return yield* _e(r, P(o, "channelId")), yield* O(async () => {
505
+ }), i = yield* ye(n.body), a = yield* J, o = yield* D(() => U(C, r.tenantId, n.messageId));
506
+ return yield* ve(r, N(o, "channelId")), yield* D(async () => {
503
507
  let e = /* @__PURE__ */ new Map();
504
- for (let t of await w.query(S(l).where(m("channelId", P(o, "channelId"))).limit(3).descriptor)) {
505
- let n = P(t, "subjectId");
508
+ for (let t of await C.query(x(l).where(m("channelId", N(o, "channelId"))).limit(3).descriptor)) {
509
+ let n = N(t, "subjectId");
506
510
  n !== r.id && e.set(n, await Z(() => q(a, r, n)));
507
511
  }
508
- return K(r.tenantId, P(o, "channelId"), async (a, o) => {
509
- let s = await U(a, r.tenantId, P(o, "id"), r.id), c = await W(a, r.tenantId, n.messageId);
512
+ return K(r.tenantId, N(o, "channelId"), async (a, o) => {
513
+ let s = await H(a, r.tenantId, N(o, "id"), r.id), c = await U(a, r.tenantId, n.messageId);
510
514
  if (c.authorId !== r.id || c.deletedAt !== null) throw t("forbidden");
511
- if (F(o.frozen)) throw t("frozen");
512
- if (s.mutedUntil !== null && M(s.mutedUntil) > B()) throw t("muted");
515
+ if (P(o.frozen)) throw t("frozen");
516
+ if (s.mutedUntil !== null && j(s.mutedUntil) > z()) throw t("muted");
513
517
  if (o.kind === "dm") {
514
- let n = await a.query(S(l).where(m("channelId", o.id)).limit(3).descriptor);
515
- for (let i of n) if (i.subjectId !== r.id && Q(e.get(P(i, "subjectId")))) throw t("blocked");
518
+ let n = await a.query(x(l).where(m("channelId", o.id)).limit(3).descriptor);
519
+ for (let i of n) if (i.subjectId !== r.id && Q(e.get(N(i, "subjectId")))) throw t("blocked");
516
520
  }
517
521
  if (c.version !== n.baseVersion) throw t("conflict");
518
522
  let d = await a.update(u.tableName, n.messageId, {
519
523
  body: i.body,
520
524
  flagged: i.flagged,
521
525
  payload: c.kind === "media" && typeof c.payload == "object" && c.payload !== null ? { media: c.payload.media } : {},
522
- editedAt: new Date(B()),
526
+ editedAt: new Date(z()),
523
527
  version: n.baseVersion + 1
524
528
  });
525
529
  if (!d) throw t("unavailable");
526
- return R(d);
530
+ return L(d);
527
531
  });
528
532
  });
529
533
  }),
530
- remove: (e, n) => O(async () => {
531
- let r = j(e);
534
+ remove: (e, n) => D(async () => {
535
+ let r = A(e);
532
536
  if (!Number.isSafeInteger(n.baseVersion) || n.baseVersion < 0 || n.baseVersion >= 2147483647) throw t("invalid_input");
533
- let i = await W(w, r.tenantId, n.messageId);
534
- return K(r.tenantId, P(i, "channelId"), async (e, i) => {
535
- let a = await U(e, r.tenantId, P(i, "id"), r.id), o = await W(e, r.tenantId, n.messageId);
537
+ let i = await U(C, r.tenantId, n.messageId);
538
+ return K(r.tenantId, N(i, "channelId"), async (e, i) => {
539
+ let a = await H(e, r.tenantId, N(i, "id"), r.id), o = await U(e, r.tenantId, n.messageId);
536
540
  if (o.authorId !== r.id && a.role !== "mod" && a.role !== "owner") throw t("forbidden");
537
- if (o.deletedAt !== null) return R(o);
541
+ if (o.deletedAt !== null) return L(o);
538
542
  if (o.version !== n.baseVersion) throw t("conflict");
539
543
  let s = await e.update(u.tableName, n.messageId, {
540
- deletedAt: new Date(B()),
544
+ deletedAt: new Date(z()),
541
545
  version: n.baseVersion + 1
542
546
  });
543
547
  if (!s) throw t("unavailable");
544
- return R(s);
548
+ return L(s);
545
549
  });
546
550
  }),
547
551
  messages: (e, n) => d.gen(function* () {
548
- let r = yield* k(() => j(e)), i = yield* J, a = yield* Y;
549
- return yield* O(async () => {
550
- let e = await H(w, r.tenantId, n.channelId);
551
- await U(w, r.tenantId, n.channelId, r.id), await X(a, r.tenantId, e, r.id);
552
+ let r = yield* O(() => A(e)), i = yield* J, a = yield* Y;
553
+ return yield* D(async () => {
554
+ let e = await V(C, r.tenantId, n.channelId);
555
+ await H(C, r.tenantId, n.channelId, r.id), await X(a, r.tenantId, e, r.id);
552
556
  let o = n.before;
553
- if (o !== void 0 && (A(o.id), !Number.isSafeInteger(o.at) || o.at < 0 || o.at > B())) throw t("invalid_input");
554
- let s = $(e), c = p(m("tenantId", r.tenantId), m("channelId", n.channelId), ...s === null ? [] : [g("createdAt", new Date(Math.max(0, B() - s)))], ...o === void 0 ? [] : [x(y("createdAt", new Date(o.at)), p(m("createdAt", new Date(o.at)), y("id", o.id)))]), l = await w.query(S(u).where(c).orderBy("createdAt", "desc").orderBy("id", "desc").limit(z.pageSize).descriptor), d = [];
557
+ if (o !== void 0 && (k(o.id), !Number.isSafeInteger(o.at) || o.at < 0 || o.at > z())) throw t("invalid_input");
558
+ let s = $(e), c = p(m("tenantId", r.tenantId), m("channelId", n.channelId), ...s === null ? [] : [g("createdAt", new Date(Math.max(0, z() - s)))], ...o === void 0 ? [] : [b(y("createdAt", new Date(o.at)), p(m("createdAt", new Date(o.at)), y("id", o.id)))]), l = await C.query(x(u).where(c).orderBy("createdAt", "desc").orderBy("id", "desc").limit(R.pageSize).descriptor), d = [];
555
559
  for (let e of l) {
556
560
  if (e.tenantId !== r.tenantId || e.channelId !== n.channelId) throw t("unavailable");
557
- e.authorId !== null && await q(i, r, P(e, "authorId")) || d.push(R(e));
561
+ e.authorId !== null && await q(i, r, N(e, "authorId")) || d.push(L(e));
558
562
  }
559
563
  let f = l.at(-1);
560
564
  return {
561
565
  messages: d,
562
- before: l.length === z.pageSize && f ? {
563
- at: M(f.createdAt),
564
- id: P(f, "id")
566
+ before: l.length === R.pageSize && f ? {
567
+ at: j(f.createdAt),
568
+ id: N(f, "id")
565
569
  } : null
566
570
  };
567
571
  });
568
572
  }),
569
573
  channel: (e, t) => d.gen(function* () {
570
- let n = yield* k(() => j(e)), r = yield* Y;
571
- return yield* O(async () => {
572
- let e = await H(w, n.tenantId, t);
573
- return await U(w, n.tenantId, t, n.id), await X(r, n.tenantId, e, n.id), I(e);
574
+ let n = yield* O(() => A(e)), r = yield* Y;
575
+ return yield* D(async () => {
576
+ let e = await V(C, n.tenantId, t);
577
+ return await H(C, n.tenantId, t, n.id), await X(r, n.tenantId, e, n.id), F(e);
574
578
  });
575
579
  }),
576
580
  membership: (e, t) => d.gen(function* () {
577
- let n = yield* k(() => j(e)), r = yield* Y;
578
- return yield* O(async () => {
579
- let e = await w.transactional(async (e) => ({
580
- row: await H(e, n.tenantId, t),
581
- own: L(await U(e, n.tenantId, t, n.id))
581
+ let n = yield* O(() => A(e)), r = yield* Y;
582
+ return yield* D(async () => {
583
+ let e = await C.transactional(async (e) => ({
584
+ row: await V(e, n.tenantId, t),
585
+ own: I(await H(e, n.tenantId, t, n.id))
582
586
  }));
583
587
  return await X(r, n.tenantId, e.row, n.id), e.own;
584
588
  });
585
589
  }),
586
590
  channels: (e) => d.gen(function* () {
587
- let n = yield* k(() => j(e)), r = yield* Y;
588
- return yield* O(async () => {
589
- let e = await w.query(S(l).where(p(m("tenantId", n.tenantId), m("subjectId", n.id))).orderBy("id", "asc").limit(z.maxChannelsPerSubject + 1).descriptor);
590
- if (e.length > z.maxChannelsPerSubject) throw t("unavailable");
591
+ let n = yield* O(() => A(e)), r = yield* Y;
592
+ return yield* D(async () => {
593
+ let e = await C.query(x(l).where(p(m("tenantId", n.tenantId), m("subjectId", n.id))).orderBy("id", "asc").limit(R.maxChannelsPerSubject + 1).descriptor);
594
+ if (e.length > R.maxChannelsPerSubject) throw t("unavailable");
591
595
  let i = [];
592
596
  for (let a of e) {
593
597
  if (a.tenantId !== n.tenantId || a.subjectId !== n.id) throw t("unavailable");
594
- let e = await H(w, n.tenantId, P(a, "channelId"));
595
- await pe(r, n.tenantId, e, n.id) && i.push(I(e));
598
+ let e = await V(C, n.tenantId, N(a, "channelId"));
599
+ await me(r, n.tenantId, e, n.id) && i.push(F(e));
596
600
  }
597
601
  return i;
598
602
  });
599
603
  }),
600
604
  unread: (e) => d.gen(function* () {
601
- let n = yield* k(() => j(e)), r = yield* J, i = yield* Y;
602
- return yield* O(async () => {
603
- let e = await w.transactional(async (e) => {
604
- let r = await e.query(S(l).where(p(m("tenantId", n.tenantId), m("subjectId", n.id))).orderBy("id", "asc").limit(z.maxChannelsPerSubject + 1).descriptor);
605
- if (r.length > z.maxChannelsPerSubject) throw t("unavailable");
605
+ let n = yield* O(() => A(e)), r = yield* J, i = yield* Y;
606
+ return yield* D(async () => {
607
+ let e = await C.transactional(async (e) => {
608
+ let r = await e.query(x(l).where(p(m("tenantId", n.tenantId), m("subjectId", n.id))).orderBy("id", "asc").limit(R.maxChannelsPerSubject + 1).descriptor);
609
+ if (r.length > R.maxChannelsPerSubject) throw t("unavailable");
606
610
  let i = [];
607
611
  for (let a of r) {
608
612
  if (a.tenantId !== n.tenantId || a.subjectId !== n.id) throw t("unavailable");
609
- let r = P(a, "channelId"), o = await H(e, n.tenantId, r), s = $(o), c = M(a.lastReadAt), l = a.lastReadId === null ? g("createdAt", new Date(c)) : x(h("createdAt", new Date(c)), p(m("createdAt", new Date(c)), h("id", P(a, "lastReadId")))), d = await e.query(S(u).where(p(m("tenantId", n.tenantId), m("channelId", r), l, ...s === null ? [] : [g("createdAt", new Date(Math.max(0, B() - s)))])).orderBy("createdAt", "asc").orderBy("id", "asc").limit(z.maxExportRows + 1).descriptor);
610
- for (let e of d.slice(0, z.maxExportRows)) if (e.tenantId !== n.tenantId || e.channelId !== r) throw t("unavailable");
613
+ let r = N(a, "channelId"), o = await V(e, n.tenantId, r), s = $(o), c = j(a.lastReadAt), l = a.lastReadId === null ? g("createdAt", new Date(c)) : b(h("createdAt", new Date(c)), p(m("createdAt", new Date(c)), h("id", N(a, "lastReadId")))), d = await e.query(x(u).where(p(m("tenantId", n.tenantId), m("channelId", r), l, ...s === null ? [] : [g("createdAt", new Date(Math.max(0, z() - s)))])).orderBy("createdAt", "asc").orderBy("id", "asc").limit(R.maxExportRows + 1).descriptor);
614
+ for (let e of d.slice(0, R.maxExportRows)) if (e.tenantId !== n.tenantId || e.channelId !== r) throw t("unavailable");
611
615
  i.push({
612
616
  channelId: r,
613
617
  c: o,
@@ -617,110 +621,110 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
617
621
  return i;
618
622
  }), a = [];
619
623
  for (let { channelId: t, c: o, candidates: s } of e) {
620
- if (!await pe(i, n.tenantId, o, n.id)) continue;
624
+ if (!await me(i, n.tenantId, o, n.id)) continue;
621
625
  let e = 0;
622
- for (let t of s.slice(0, z.maxExportRows)) t.deletedAt !== null || t.authorId === n.id || t.authorId !== null && await q(r, n, P(t, "authorId")) || e++;
626
+ for (let t of s.slice(0, R.maxExportRows)) t.deletedAt !== null || t.authorId === n.id || t.authorId !== null && await q(r, n, N(t, "authorId")) || e++;
623
627
  a.push({
624
628
  channelId: t,
625
629
  count: e,
626
- truncated: s.length > z.maxExportRows
630
+ truncated: s.length > R.maxExportRows
627
631
  });
628
632
  }
629
633
  return a;
630
634
  });
631
635
  }),
632
- original: (e, n) => O(() => w.transactional(async (r) => {
633
- let i = j(e), a = await W(r, i.tenantId, n), o = await U(r, i.tenantId, P(a, "channelId"), i.id);
634
- if (await H(r, i.tenantId, P(a, "channelId")), o.role !== "owner" && o.role !== "mod") throw t("forbidden");
636
+ original: (e, n) => D(() => C.transactional(async (r) => {
637
+ let i = A(e), a = await U(r, i.tenantId, n), o = await H(r, i.tenantId, N(a, "channelId"), i.id);
638
+ if (await V(r, i.tenantId, N(a, "channelId")), o.role !== "owner" && o.role !== "mod") throw t("forbidden");
635
639
  return {
636
- message: R(a, !0),
637
- originalBody: P(a, "originalBody")
640
+ message: L(a, !0),
641
+ originalBody: N(a, "originalBody")
638
642
  };
639
643
  })),
640
644
  mediaUrl: (e, n) => d.gen(function* () {
641
- let r = yield* k(() => j(e)), i = yield* J, o = yield* de, s = yield* Y, c = yield* O(async () => {
642
- let e = await W(w, r.tenantId, n), o = P(e, "channelId"), c = await H(w, r.tenantId, o), l = await U(w, r.tenantId, o, r.id);
645
+ let r = yield* O(() => A(e)), i = yield* J, o = yield* fe, s = yield* Y, c = yield* D(async () => {
646
+ let e = await U(C, r.tenantId, n), o = N(e, "channelId"), c = await V(C, r.tenantId, o), l = await H(C, r.tenantId, o, r.id);
643
647
  if (e.kind !== "media" || (await X(s, r.tenantId, c, r.id), e.deletedAt !== null && l.role !== "mod" && l.role !== "owner")) throw t("not_found");
644
648
  let u = $(c);
645
- if (u !== null && M(e.createdAt) < B() - u) throw t("not_found");
646
- if (e.authorId !== null && await q(i, r, P(e, "authorId"))) throw t("forbidden");
647
- let d = (await w.query(S(a).where(m("id", me(r.tenantId, n))).limit(1).descriptor))[0];
649
+ if (u !== null && j(e.createdAt) < z() - u) throw t("not_found");
650
+ if (e.authorId !== null && await q(i, r, N(e, "authorId"))) throw t("forbidden");
651
+ let d = (await C.query(x(a).where(m("id", he(r.tenantId, n))).limit(1).descriptor))[0];
648
652
  if (!d || d.state !== "active") throw t("not_found");
649
653
  if (d.tenantId !== r.tenantId || d.messageId !== n || d.channelId !== o) throw t("unavailable");
650
- return P(d, "storageId");
654
+ return N(d, "storageId");
651
655
  });
652
656
  if (!o) return yield* d.fail(t("unavailable"));
653
- let l = yield* k(B), u = yield* d.suspend(() => o.head(c, { tenantId: r.tenantId })).pipe(d.mapError(() => t("unavailable")), d.catchDefect(() => d.fail(t("unavailable"))));
657
+ let l = yield* O(z), u = yield* d.suspend(() => o.head(c, { tenantId: r.tenantId })).pipe(d.mapError(() => t("unavailable")), d.catchDefect(() => d.fail(t("unavailable"))));
654
658
  return u === null || u.tenantId !== r.tenantId ? yield* d.fail(t("not_found")) : {
655
659
  url: yield* d.suspend(() => o.getUrl(c, {
656
660
  tenantId: r.tenantId,
657
- expiresInSec: z.mediaUrlTtlSec
661
+ expiresInSec: R.mediaUrlTtlSec
658
662
  })).pipe(d.mapError(() => t("unavailable")), d.catchDefect(() => d.fail(t("unavailable")))),
659
- expiresAt: l + z.mediaUrlTtlSec * 1e3
663
+ expiresAt: l + R.mediaUrlTtlSec * 1e3
660
664
  };
661
665
  }),
662
- markRead: (e, n, r) => O(async () => {
663
- let i = j(e);
666
+ markRead: (e, n, r) => D(async () => {
667
+ let i = A(e);
664
668
  return K(i.tenantId, n, async (e) => {
665
- let a = await U(e, i.tenantId, n, i.id), o = await W(e, i.tenantId, r);
669
+ let a = await H(e, i.tenantId, n, i.id), o = await U(e, i.tenantId, r);
666
670
  if (o.channelId !== n) throw t("forbidden");
667
- let s = M(a.lastReadAt), c = M(o.createdAt), u = P(o, "id");
668
- if (c < s || c === s && a.lastReadId !== null && u <= P(a, "lastReadId")) return L(a);
669
- let d = await e.update(l.tableName, P(a, "id"), {
671
+ let s = j(a.lastReadAt), c = j(o.createdAt), u = N(o, "id");
672
+ if (c < s || c === s && a.lastReadId !== null && u <= N(a, "lastReadId")) return I(a);
673
+ let d = await e.update(l.tableName, N(a, "id"), {
670
674
  lastReadAt: new Date(c),
671
675
  lastReadId: u
672
676
  });
673
677
  if (!d) throw t("unavailable");
674
- return L(d);
678
+ return I(d);
675
679
  });
676
680
  }),
677
- moderateMember: (e, n) => O(async () => {
678
- let r = j(e);
681
+ moderateMember: (e, n) => D(async () => {
682
+ let r = A(e);
679
683
  return K(r.tenantId, n.channelId, async (e) => {
680
- let i = await U(e, r.tenantId, n.channelId, r.id), a = await U(e, r.tenantId, n.channelId, n.subjectId);
684
+ let i = await H(e, r.tenantId, n.channelId, r.id), a = await H(e, r.tenantId, n.channelId, n.subjectId);
681
685
  if (i.role !== "owner" && (i.role !== "mod" || a.role !== "member")) throw t("forbidden");
682
- if (a.role === "owner" || n.mutedUntil !== null && (!Number.isSafeInteger(n.mutedUntil) || n.mutedUntil < B() || n.mutedUntil > B() + 31536e6)) throw t("invalid_input");
683
- let o = await e.update(l.tableName, P(a, "id"), { mutedUntil: n.mutedUntil === null ? null : new Date(n.mutedUntil) });
686
+ if (a.role === "owner" || n.mutedUntil !== null && (!Number.isSafeInteger(n.mutedUntil) || n.mutedUntil < z() || n.mutedUntil > z() + 31536e6)) throw t("invalid_input");
687
+ let o = await e.update(l.tableName, N(a, "id"), { mutedUntil: n.mutedUntil === null ? null : new Date(n.mutedUntil) });
684
688
  if (!o) throw t("unavailable");
685
- return L(o);
689
+ return I(o);
686
690
  });
687
691
  }),
688
- configure: (e, n) => O(async () => {
689
- let r = j(e);
692
+ configure: (e, n) => D(async () => {
693
+ let r = A(e);
690
694
  return K(r.tenantId, n.channelId, async (e, i) => {
691
- let a = await U(e, r.tenantId, n.channelId, r.id);
695
+ let a = await H(e, r.tenantId, n.channelId, r.id);
692
696
  if (a.role !== "mod" && a.role !== "owner") throw t("forbidden");
693
697
  if (n.slowModeMs !== void 0 && (!Number.isSafeInteger(n.slowModeMs) || n.slowModeMs < 0 || n.slowModeMs > 864e5) || n.frozen !== void 0 && typeof n.frozen != "boolean") throw t("invalid_input");
694
- let o = await e.update(c.tableName, P(i, "id"), {
698
+ let o = await e.update(c.tableName, N(i, "id"), {
695
699
  slowModeMs: n.slowModeMs ?? i.slowModeMs,
696
- frozen: n.frozen ?? F(i.frozen)
700
+ frozen: n.frozen ?? P(i.frozen)
697
701
  });
698
702
  if (!o) throw t("unavailable");
699
- return I(o);
703
+ return F(o);
700
704
  });
701
705
  }),
702
706
  join: (e, n) => d.gen(function* () {
703
- let r = yield* k(() => {
704
- let t = j(e);
705
- return A(n), t;
707
+ let r = yield* O(() => {
708
+ let t = A(e);
709
+ return k(n), t;
706
710
  });
707
- yield* _e(r, n);
708
- let i = yield* Y, a = yield* O(async () => {
709
- let e = await Z(() => H(w, r.tenantId, n));
711
+ yield* ve(r, n);
712
+ let i = yield* Y, a = yield* D(async () => {
713
+ let e = await Z(() => V(C, r.tenantId, n));
710
714
  return e.ok ? Z(() => X(i, r.tenantId, e.value, r.id)) : void 0;
711
715
  });
712
- return yield* O(() => K(r.tenantId, n, async (e, o) => {
713
- if (o.kind !== "global" && fe(i, o) === void 0) throw t("forbidden");
714
- if (Q(a), F(o.frozen)) throw t("frozen");
715
- let s = V(r.tenantId, n, r.id), c = (await e.query(S(l).where(m("id", s)).limit(1).forUpdate().descriptor))[0];
716
+ return yield* D(() => K(r.tenantId, n, async (e, o) => {
717
+ if (o.kind !== "global" && pe(i, o) === void 0) throw t("forbidden");
718
+ if (Q(a), P(o.frozen)) throw t("frozen");
719
+ let s = B(r.tenantId, n, r.id), c = (await e.query(x(l).where(m("id", s)).limit(1).forUpdate().descriptor))[0];
716
720
  if (c) {
717
721
  if (c.tenantId !== r.tenantId || c.channelId !== n || c.subjectId !== r.id) throw t("unavailable");
718
- return L(c);
722
+ return I(c);
719
723
  }
720
- let u = await e.query(S(l).where(p(m("tenantId", r.tenantId), m("channelId", n))).limit(Number(o.maxMembers) + 1).forUpdate().descriptor);
724
+ let u = await e.query(x(l).where(p(m("tenantId", r.tenantId), m("channelId", n))).limit(Number(o.maxMembers) + 1).forUpdate().descriptor);
721
725
  if (u.some((e) => e.tenantId !== r.tenantId || e.channelId !== n)) throw t("unavailable");
722
726
  if (u.length >= Number(o.maxMembers)) throw t("member_limit");
723
- return L(await e.insert(l.tableName, {
727
+ return I(await e.insert(l.tableName, {
724
728
  id: s,
725
729
  tenantId: r.tenantId,
726
730
  channelId: n,
@@ -735,90 +739,90 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
735
739
  }));
736
740
  }, [r.id]));
737
741
  }),
738
- leave: (e, n) => O(async () => {
739
- let r = j(e);
742
+ leave: (e, n) => D(async () => {
743
+ let r = A(e);
740
744
  return K(r.tenantId, n, async (e, i) => {
741
745
  if (i.kind === "dm") throw t("forbidden");
742
- let a = (await e.query(S(l).where(m("id", V(r.tenantId, n, r.id))).limit(1).descriptor))[0];
746
+ let a = (await e.query(x(l).where(m("id", B(r.tenantId, n, r.id))).limit(1).descriptor))[0];
743
747
  if (a) {
744
748
  if (a.tenantId !== r.tenantId || a.channelId !== n || a.subjectId !== r.id) throw t("unavailable");
745
749
  if (a.role === "owner") throw t("forbidden");
746
- await e.delete(l.tableName, P(a, "id"));
750
+ await e.delete(l.tableName, N(a, "id"));
747
751
  }
748
752
  });
749
753
  }),
750
- transferOwnership: (e, n, r) => O(async () => {
751
- let i = j(e);
752
- return A(r), K(i.tenantId, n, async (e, a) => {
754
+ transferOwnership: (e, n, r) => D(async () => {
755
+ let i = A(e);
756
+ return k(r), K(i.tenantId, n, async (e, a) => {
753
757
  if (a.kind === "dm") throw t("forbidden");
754
- let o = await U(e, i.tenantId, n, i.id);
758
+ let o = await H(e, i.tenantId, n, i.id);
755
759
  if (o.role !== "owner") throw t("forbidden");
756
760
  if (r === i.id) return;
757
- let s = await U(e, i.tenantId, n, r), c = await e.query(S(l).where(p(m("tenantId", i.tenantId), m("channelId", n), m("role", "owner"))).limit(2).descriptor);
761
+ let s = await H(e, i.tenantId, n, r), c = await e.query(x(l).where(p(m("tenantId", i.tenantId), m("channelId", n), m("role", "owner"))).limit(2).descriptor);
758
762
  if (c.length !== 1 || c[0]?.subjectId !== i.id) throw t("conflict");
759
- if (s.mutedUntil !== null && M(s.mutedUntil) > B()) throw t("muted");
760
- await e.update(l.tableName, P(o, "id"), { role: "member" }), await e.update(l.tableName, P(s, "id"), { role: "owner" });
763
+ if (s.mutedUntil !== null && j(s.mutedUntil) > z()) throw t("muted");
764
+ await e.update(l.tableName, N(o, "id"), { role: "member" }), await e.update(l.tableName, N(s, "id"), { role: "owner" });
761
765
  });
762
766
  })
763
- }, xe = (e, n, r) => {
764
- if (A(n), e.type !== "system" && !(e.type === "serviceAccount" && e.tenantId === n && e.scopes?.includes(r))) throw t("identity_required");
765
- }, Se = (e, n) => {
767
+ }, Se = (e, n, r) => {
768
+ if (k(n), e.type !== "system" && !(e.type === "serviceAccount" && e.tenantId === n && e.scopes?.includes(r))) throw t("identity_required");
769
+ }, Ce = (e, n) => {
766
770
  let i, a;
767
771
  try {
768
- if (i = structuredClone(j(e)), a = structuredClone(n), A(a.channelId), A(a.messageId), A(a.targetId), i.id === a.targetId) throw t("invalid_input");
772
+ if (i = structuredClone(A(e)), a = structuredClone(n), k(a.channelId), k(a.messageId), k(a.targetId), i.id === a.targetId) throw t("invalid_input");
769
773
  } catch (e) {
770
- return d.fail(D(e));
774
+ return d.fail(E(e));
771
775
  }
772
- return O(() => {
773
- let e = j(i);
776
+ return D(() => {
777
+ let e = A(i);
774
778
  return K(e.tenantId, a.channelId, async (n, i) => {
775
- await U(n, e.tenantId, a.channelId, e.id);
776
- let o = await W(n, e.tenantId, a.messageId), s = B(), c = $(i);
777
- if (o.channelId !== a.channelId || o.authorId !== a.targetId || c !== null && M(o.createdAt) < s - c) throw t("forbidden");
778
- let l = M(o.createdAt), d = p(m("tenantId", e.tenantId), m("channelId", a.channelId), ...c === null ? [] : [g("createdAt", new Date(Math.max(0, s - c)))]), f = await n.query(S(u).where(p(d, x(y("createdAt", new Date(l)), p(m("createdAt", new Date(l)), y("id", a.messageId))))).orderBy("createdAt", "desc").orderBy("id", "desc").limit(20).select("id", "tenantId", "channelId", "authorId", "kind", "body", "originalBody", "version", "createdAt", "editedAt", "deletedAt").descriptor), _ = await n.query(S(u).where(p(d, x(h("createdAt", new Date(l)), p(m("createdAt", new Date(l)), h("id", a.messageId))))).orderBy("createdAt", "asc").orderBy("id", "asc").limit(20).select("id", "tenantId", "channelId", "authorId", "kind", "body", "originalBody", "version", "createdAt", "editedAt", "deletedAt").descriptor), v = [
779
+ await H(n, e.tenantId, a.channelId, e.id);
780
+ let o = await U(n, e.tenantId, a.messageId), s = z(), c = $(i);
781
+ if (o.channelId !== a.channelId || o.authorId !== a.targetId || c !== null && j(o.createdAt) < s - c) throw t("forbidden");
782
+ let l = j(o.createdAt), d = p(m("tenantId", e.tenantId), m("channelId", a.channelId), ...c === null ? [] : [g("createdAt", new Date(Math.max(0, s - c)))]), f = await n.query(x(u).where(p(d, b(y("createdAt", new Date(l)), p(m("createdAt", new Date(l)), y("id", a.messageId))))).orderBy("createdAt", "desc").orderBy("id", "desc").limit(20).select("id", "tenantId", "channelId", "authorId", "kind", "body", "originalBody", "version", "createdAt", "editedAt", "deletedAt").descriptor), _ = await n.query(x(u).where(p(d, b(h("createdAt", new Date(l)), p(m("createdAt", new Date(l)), h("id", a.messageId))))).orderBy("createdAt", "asc").orderBy("id", "asc").limit(20).select("id", "tenantId", "channelId", "authorId", "kind", "body", "originalBody", "version", "createdAt", "editedAt", "deletedAt").descriptor), v = [
779
783
  ...[...f].reverse(),
780
784
  o,
781
785
  ..._
782
786
  ];
783
787
  if (v.some((t) => t.tenantId !== e.tenantId || t.channelId !== a.channelId || !Number.isSafeInteger(t.version) || Number(t.version) < 0)) throw t("unavailable");
784
- let b = await W(n, e.tenantId, a.messageId);
785
- if (b.version !== o.version || b.authorId !== a.targetId) throw t("conflict");
786
- let C = s + z.evidenceRetentionMs;
787
- if (!Number.isSafeInteger(C) || C > 864e13) throw t("unavailable");
788
- let w = [];
788
+ let ee = await U(n, e.tenantId, a.messageId);
789
+ if (ee.version !== o.version || ee.authorId !== a.targetId) throw t("conflict");
790
+ let S = s + R.evidenceRetentionMs;
791
+ if (!Number.isSafeInteger(S) || S > 864e13) throw t("unavailable");
792
+ let C = [];
789
793
  for (let i of v) {
790
794
  let o = null;
791
795
  if (i.authorId !== null) {
792
- let c = P(i, "id"), l = P(i, "authorId"), u = await n.query(S(r).where(p(m("tenantId", e.tenantId), m("channelId", a.channelId), m("messageId", c), m("messageVersion", i.version), h("expiresAt", new Date(s)))).orderBy("id", "asc").limit(2).descriptor);
796
+ let c = N(i, "id"), l = N(i, "authorId"), u = await n.query(x(r).where(p(m("tenantId", e.tenantId), m("channelId", a.channelId), m("messageId", c), m("messageVersion", i.version), h("expiresAt", new Date(s)))).orderBy("id", "asc").limit(2).descriptor);
793
797
  if (u.length > 1 || u.some((t) => t.tenantId !== e.tenantId || t.channelId !== a.channelId || t.messageId !== c || t.messageVersion !== i.version)) throw t("unavailable");
794
798
  let d = u[0];
795
799
  if (d) {
796
800
  if (d.erasedAt === null && (d.authorId !== l || d.body !== i.body || d.originalBody !== i.originalBody)) throw t("unavailable");
797
- o = P(d, "id");
798
- } else o = "cevi_" + ee().replaceAll("-", ""), await n.insert(r.tableName, {
801
+ o = N(d, "id");
802
+ } else o = "cevi_" + te().replaceAll("-", ""), await n.insert(r.tableName, {
799
803
  id: o,
800
804
  tenantId: e.tenantId,
801
805
  channelId: a.channelId,
802
806
  messageId: c,
803
807
  messageVersion: i.version,
804
- kind: P(i, "kind"),
808
+ kind: N(i, "kind"),
805
809
  authorId: l,
806
- body: P(i, "body"),
807
- originalBody: P(i, "originalBody"),
810
+ body: N(i, "body"),
811
+ originalBody: N(i, "originalBody"),
808
812
  messageCreatedAt: i.createdAt,
809
813
  messageEditedAt: i.editedAt,
810
814
  messageDeletedAt: i.deletedAt,
811
815
  capturedAt: new Date(s),
812
- expiresAt: new Date(C),
816
+ expiresAt: new Date(S),
813
817
  erasedAt: null
814
818
  });
815
819
  }
816
- w.push({
820
+ C.push({
817
821
  evidenceId: o,
818
- messageId: P(i, "id"),
822
+ messageId: N(i, "id"),
819
823
  version: Number(i.version),
820
- createdAt: M(i.createdAt),
821
- deletedAt: N(i.deletedAt)
824
+ createdAt: j(i.createdAt),
825
+ deletedAt: M(i.deletedAt)
822
826
  });
823
827
  }
824
828
  return {
@@ -826,74 +830,74 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
826
830
  channelId: a.channelId,
827
831
  anchorMessageId: a.messageId,
828
832
  observedAt: s,
829
- messages: w
833
+ messages: C
830
834
  };
831
835
  });
832
836
  });
833
- }, Ce = (e, n) => {
837
+ }, we = (e, n) => {
834
838
  let i, a;
835
839
  try {
836
- if (i = structuredClone(e), a = structuredClone(n), A(a.tenantId), i.type !== "system" && (i.type !== "user" && i.type !== "serviceAccount" || i.tenantId !== a.tenantId || !i.scopes?.includes("sanctions:manage") && !i.scopes?.includes("admin:full"))) throw t("forbidden");
840
+ if (i = structuredClone(e), a = structuredClone(n), k(a.tenantId), i.type !== "system" && (i.type !== "user" && i.type !== "serviceAccount" || i.tenantId !== a.tenantId || !i.scopes?.includes("sanctions:manage") && !i.scopes?.includes("admin:full"))) throw t("forbidden");
837
841
  if (!Array.isArray(a.evidenceIds) || a.evidenceIds.length > 41 || new Set(a.evidenceIds).size !== a.evidenceIds.length) throw t("invalid_input");
838
- a.evidenceIds.forEach(A);
842
+ a.evidenceIds.forEach(k);
839
843
  } catch (e) {
840
- return d.fail(D(e));
844
+ return d.fail(E(e));
841
845
  }
842
- return O(async () => {
846
+ return D(async () => {
843
847
  if (!a.evidenceIds.length) return [];
844
- let e = await w.query(S(r).where(p(m("tenantId", a.tenantId), _("id", a.evidenceIds))).limit(42).descriptor);
845
- if (e.length > 41 || e.some((e) => e.tenantId !== a.tenantId || !a.evidenceIds.includes(P(e, "id")))) throw t("unavailable");
846
- return e.filter((e) => M(e.expiresAt) > B()).map((e) => ({
847
- id: P(e, "id"),
848
- channelId: P(e, "channelId"),
849
- messageId: P(e, "messageId"),
848
+ let e = await C.query(x(r).where(p(m("tenantId", a.tenantId), _("id", a.evidenceIds))).limit(42).descriptor);
849
+ if (e.length > 41 || e.some((e) => e.tenantId !== a.tenantId || !a.evidenceIds.includes(N(e, "id")))) throw t("unavailable");
850
+ return e.filter((e) => j(e.expiresAt) > z()).map((e) => ({
851
+ id: N(e, "id"),
852
+ channelId: N(e, "channelId"),
853
+ messageId: N(e, "messageId"),
850
854
  messageVersion: Number(e.messageVersion),
851
- kind: P(e, "kind"),
852
- authorId: e.erasedAt === null ? P(e, "authorId") : null,
853
- body: e.erasedAt === null ? P(e, "body") : null,
854
- originalBody: e.erasedAt === null ? P(e, "originalBody") : null,
855
- createdAt: M(e.messageCreatedAt),
856
- editedAt: N(e.messageEditedAt),
857
- deletedAt: N(e.messageDeletedAt),
858
- capturedAt: M(e.capturedAt),
859
- expiresAt: M(e.expiresAt),
860
- erasedAt: N(e.erasedAt)
855
+ kind: N(e, "kind"),
856
+ authorId: e.erasedAt === null ? N(e, "authorId") : null,
857
+ body: e.erasedAt === null ? N(e, "body") : null,
858
+ originalBody: e.erasedAt === null ? N(e, "originalBody") : null,
859
+ createdAt: j(e.messageCreatedAt),
860
+ editedAt: M(e.messageEditedAt),
861
+ deletedAt: M(e.messageDeletedAt),
862
+ capturedAt: j(e.capturedAt),
863
+ expiresAt: j(e.expiresAt),
864
+ erasedAt: M(e.erasedAt)
861
865
  }));
862
866
  });
863
- }, we = (e) => {
867
+ }, Te = (e) => {
864
868
  let n;
865
869
  try {
866
870
  let r = structuredClone(e);
867
871
  if (r.type === "system") n = null;
868
- else if (r.type === "serviceAccount" && r.scopes?.includes("chat:retention:write")) A(r.tenantId), n = r.tenantId;
872
+ else if (r.type === "serviceAccount" && r.scopes?.includes("chat:retention:write")) k(r.tenantId), n = r.tenantId;
869
873
  else throw t("identity_required");
870
874
  } catch (e) {
871
- return d.fail(D(e));
875
+ return d.fail(E(e));
872
876
  }
873
- return O(async () => {
874
- let e = "cret_" + E(["chat.retention", n]).slice(0, 56), i = S(o).where(m("id", e)).limit(1).descriptor;
875
- if (!(await w.query(i))[0]) try {
876
- await w.insert(o.tableName, {
877
+ return D(async () => {
878
+ let e = "cret_" + T(["chat.retention", n]).slice(0, 56), i = x(o).where(m("id", e)).limit(1).descriptor;
879
+ if (!(await C.query(i))[0]) try {
880
+ await C.insert(o.tableName, {
877
881
  id: e,
878
882
  tenantId: n,
879
883
  afterChannelId: null,
880
884
  admission: 0,
881
- updatedAt: new Date(B())
885
+ updatedAt: new Date(z())
882
886
  });
883
887
  } catch (e) {
884
- let t = (await w.query(i))[0];
888
+ let t = (await C.query(i))[0];
885
889
  if (!t || t.tenantId !== n) throw e;
886
890
  }
887
- return w.transactional(async (s) => {
891
+ return C.transactional(async (s) => {
888
892
  if (await s.updateMany(o.tableName, { admission: 0 }, { where: m("id", e) }) !== 1) throw t("unavailable");
889
893
  let l = (await s.query(i))[0];
890
894
  if (!l || l.tenantId !== n || l.afterChannelId !== null && typeof l.afterChannelId != "string") throw t("unavailable");
891
- let d = await s.query(S(c).where(p(h("id", ""), ...n === null ? [] : [m("tenantId", n)], ...l.afterChannelId === null ? [] : [h("id", l.afterChannelId)])).orderBy("id", "asc").limit(2).select("id", "tenantId").descriptor);
895
+ let d = await s.query(x(c).where(p(h("id", ""), ...n === null ? [] : [m("tenantId", n)], ...l.afterChannelId === null ? [] : [h("id", l.afterChannelId)])).orderBy("id", "asc").limit(2).select("id", "tenantId").descriptor);
892
896
  if (d.some((e) => typeof e.tenantId != "string" || n !== null && e.tenantId !== n)) throw t("unavailable");
893
897
  let f = d[0];
894
898
  if (!f) return await s.update(o.tableName, e, {
895
899
  afterChannelId: null,
896
- updatedAt: new Date(B())
900
+ updatedAt: new Date(z())
897
901
  }), {
898
902
  deleted: 0,
899
903
  evidenceDeleted: 0,
@@ -902,65 +906,65 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
902
906
  evidenceRemainingInChannel: !1,
903
907
  cycleComplete: !0
904
908
  };
905
- let g = P(f, "id"), v = P(f, "tenantId");
906
- if (A(v), await s.updateMany(c.tableName, { admission: 0 }, { where: m("id", g) }) !== 1) throw t("unavailable");
907
- let x = await H(s, v, g), C = $(x), w = 0, ee = !1;
908
- if (C !== null) {
909
- let e = new Date(Math.max(0, B() - C)), n = await s.query(S(u).where(p(m("tenantId", v), m("channelId", g), y("createdAt", e))).orderBy("createdAt", "asc").orderBy("id", "asc").limit(201).select("id", "tenantId", "channelId").descriptor);
909
+ let g = N(f, "id"), v = N(f, "tenantId");
910
+ if (k(v), await s.updateMany(c.tableName, { admission: 0 }, { where: m("id", g) }) !== 1) throw t("unavailable");
911
+ let b = await V(s, v, g), S = $(b), C = 0, te = !1;
912
+ if (S !== null) {
913
+ let e = new Date(Math.max(0, z() - S)), n = await s.query(x(u).where(p(m("tenantId", v), m("channelId", g), y("createdAt", e))).orderBy("createdAt", "asc").orderBy("id", "asc").limit(201).select("id", "tenantId", "channelId").descriptor);
910
914
  if (n.some((e) => e.tenantId !== v || e.channelId !== g || typeof e.id != "string")) throw t("unavailable");
911
- ee = n.length > 200;
915
+ te = n.length > 200;
912
916
  let r = n.slice(0, 200);
913
- if (r.length && (w = await s.deleteMany(u.tableName, { where: p(m("tenantId", v), m("channelId", g), y("createdAt", e), _("id", r.map((e) => P(e, "id")))) })), !Number.isSafeInteger(w) || w < 0 || w > r.length) throw t("unavailable");
917
+ if (r.length && (C = await s.deleteMany(u.tableName, { where: p(m("tenantId", v), m("channelId", g), y("createdAt", e), _("id", r.map((e) => N(e, "id")))) })), !Number.isSafeInteger(C) || C < 0 || C > r.length) throw t("unavailable");
914
918
  r.length && await s.updateMany(a.tableName, {
915
919
  state: "deleting",
916
920
  ownerId: null
917
- }, { where: p(m("tenantId", v), m("channelId", g), _("messageId", r.map((e) => P(e, "id")))) });
921
+ }, { where: p(m("tenantId", v), m("channelId", g), _("messageId", r.map((e) => N(e, "id")))) });
918
922
  }
919
- let te = await s.query(S(r).where(p(m("tenantId", v), m("channelId", g), b("expiresAt", new Date(B())))).orderBy("expiresAt", "asc").orderBy("id", "asc").limit(201).select("id", "tenantId", "channelId").descriptor);
920
- if (te.some((e) => e.tenantId !== v || e.channelId !== g || typeof e.id != "string")) throw t("unavailable");
921
- let ne = te.slice(0, 200), T = ne.length ? await s.deleteMany(r.tableName, { where: p(m("tenantId", v), m("channelId", g), b("expiresAt", new Date(B())), _("id", ne.map((e) => P(e, "id")))) }) : 0;
922
- if (!Number.isSafeInteger(T) || T < 0 || T > ne.length) throw t("unavailable");
923
- let re = d.length < 2;
923
+ let ne = await s.query(x(r).where(p(m("tenantId", v), m("channelId", g), ee("expiresAt", new Date(z())))).orderBy("expiresAt", "asc").orderBy("id", "asc").limit(201).select("id", "tenantId", "channelId").descriptor);
924
+ if (ne.some((e) => e.tenantId !== v || e.channelId !== g || typeof e.id != "string")) throw t("unavailable");
925
+ let re = ne.slice(0, 200), w = re.length ? await s.deleteMany(r.tableName, { where: p(m("tenantId", v), m("channelId", g), ee("expiresAt", new Date(z())), _("id", re.map((e) => N(e, "id")))) }) : 0;
926
+ if (!Number.isSafeInteger(w) || w < 0 || w > re.length) throw t("unavailable");
927
+ let ie = d.length < 2;
924
928
  return await s.update(o.tableName, e, {
925
- afterChannelId: re ? null : g,
926
- updatedAt: new Date(B())
929
+ afterChannelId: ie ? null : g,
930
+ updatedAt: new Date(z())
927
931
  }), {
928
- deleted: w,
929
- evidenceDeleted: T,
932
+ deleted: C,
933
+ evidenceDeleted: w,
930
934
  channelId: g,
931
- remainingInChannel: ee,
932
- evidenceRemainingInChannel: te.length > 200,
933
- cycleComplete: re
935
+ remainingInChannel: te,
936
+ evidenceRemainingInChannel: ne.length > 200,
937
+ cycleComplete: ie
934
938
  };
935
939
  });
936
940
  });
937
- }, Te = (e) => {
941
+ }, Ee = (e) => {
938
942
  let n;
939
943
  try {
940
944
  let r = structuredClone(e);
941
945
  if (r.type === "system") n = null;
942
- else if (r.type === "serviceAccount" && r.scopes?.includes("chat:retention:write")) A(r.tenantId), n = r.tenantId;
946
+ else if (r.type === "serviceAccount" && r.scopes?.includes("chat:retention:write")) k(r.tenantId), n = r.tenantId;
943
947
  else throw t("identity_required");
944
948
  } catch (e) {
945
- return d.fail(D(e));
949
+ return d.fail(E(e));
946
950
  }
947
951
  let r = n;
948
952
  return d.gen(function* () {
949
- let e = yield* de, n = yield* O(() => w.query(S(a).where(p(m("state", "deleting"), ...r === null ? [] : [m("tenantId", r)])).orderBy("id", "asc").limit(51).descriptor));
953
+ let e = yield* fe, n = yield* D(() => C.query(x(a).where(p(m("state", "deleting"), ...r === null ? [] : [m("tenantId", r)])).orderBy("id", "asc").limit(51).descriptor));
950
954
  if (n.some((e) => e.state !== "deleting" || typeof e.tenantId != "string" || r !== null && e.tenantId !== r)) return yield* d.fail(t("unavailable"));
951
955
  let i = n.slice(0, 50);
952
956
  if (i.length > 0 && !e) return yield* d.fail(t("unavailable"));
953
957
  let o = 0, s = 0;
954
958
  for (let n of i) {
955
- let r = P(n, "tenantId"), i = P(n, "storageId");
956
- if ((yield* O(() => w.query(S(a).where(p(m("tenantId", r), m("storageId", i), m("state", "active"))).limit(1).descriptor))).length === 0) {
959
+ let r = N(n, "tenantId"), i = N(n, "storageId");
960
+ if ((yield* D(() => C.query(x(a).where(p(m("tenantId", r), m("storageId", i), m("state", "active"))).limit(1).descriptor))).length === 0) {
957
961
  let n = yield* d.result(d.suspend(() => e.delete(i, { tenantId: r })).pipe(d.catchDefect(() => d.fail(t("unavailable")))));
958
962
  if (f.isFailure(n) && !("status" in n.failure && n.failure.status === 404)) {
959
963
  s++;
960
964
  continue;
961
965
  }
962
966
  }
963
- yield* O(() => w.delete(a.tableName, P(n, "id"))), o++;
967
+ yield* D(() => C.delete(a.tableName, N(n, "id"))), o++;
964
968
  }
965
969
  return {
966
970
  removed: o,
@@ -968,51 +972,51 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
968
972
  remaining: n.length > 50
969
973
  };
970
974
  });
971
- }, Ee = (t, n) => {
975
+ }, De = (t, n) => {
972
976
  let r, i;
973
977
  try {
974
- r = structuredClone(t), i = structuredClone(n), xe(r, i.tenantId, "chat:notifications:send"), A(i.channelId), A(i.messageId), A(i.recipientId);
978
+ r = structuredClone(t), i = structuredClone(n), Se(r, i.tenantId, "chat:notifications:send"), k(i.channelId), k(i.messageId), k(i.recipientId);
975
979
  } catch (e) {
976
- return d.fail(D(e));
980
+ return d.fail(E(e));
977
981
  }
978
982
  return d.gen(function* () {
979
983
  let t = yield* J;
980
- return yield* O(async () => {
984
+ return yield* D(async () => {
981
985
  try {
982
- let e = await H(w, i.tenantId, i.channelId), n = await W(w, i.tenantId, i.messageId);
986
+ let e = await V(C, i.tenantId, i.channelId), n = await U(C, i.tenantId, i.messageId);
983
987
  if (n.channelId !== i.channelId || n.deletedAt !== null || typeof n.authorId != "string" || n.authorId === i.recipientId) return !1;
984
988
  let r = $(e);
985
- if (r !== null && M(n.createdAt) < B() - r) return !1;
989
+ if (r !== null && j(n.createdAt) < z() - r) return !1;
986
990
  let a = n.payload, o = a && typeof a == "object" && !Array.isArray(a) ? a.mentions : void 0;
987
- if (!Array.isArray(o) || o.length > z.maxMentions || !o.includes(i.recipientId) || !F((await U(w, i.tenantId, i.channelId, i.recipientId)).notify)) return !1;
988
- await U(w, i.tenantId, i.channelId, n.authorId);
991
+ if (!Array.isArray(o) || o.length > R.maxMentions || !o.includes(i.recipientId) || !P((await H(C, i.tenantId, i.channelId, i.recipientId)).notify)) return !1;
992
+ await H(C, i.tenantId, i.channelId, n.authorId);
989
993
  let s = {
990
994
  type: "user",
991
995
  id: i.recipientId,
992
996
  tenantId: i.tenantId
993
997
  };
994
998
  if (await q(t, s, n.authorId)) return !1;
995
- let c = await W(w, i.tenantId, i.messageId);
999
+ let c = await U(C, i.tenantId, i.messageId);
996
1000
  if (c.version !== n.version || c.deletedAt !== null || c.authorId !== n.authorId) return !1;
997
- await U(w, i.tenantId, i.channelId, n.authorId);
998
- let l = $(await H(w, i.tenantId, i.channelId));
999
- return l !== null && M(c.createdAt) < B() - l ? !1 : F((await U(w, i.tenantId, i.channelId, i.recipientId)).notify);
1001
+ await H(C, i.tenantId, i.channelId, n.authorId);
1002
+ let l = $(await V(C, i.tenantId, i.channelId));
1003
+ return l !== null && j(c.createdAt) < z() - l ? !1 : P((await H(C, i.tenantId, i.channelId, i.recipientId)).notify);
1000
1004
  } catch (t) {
1001
1005
  if (t instanceof e && (t.code === "not_found" || t.code === "forbidden")) return !1;
1002
1006
  throw t;
1003
1007
  }
1004
1008
  });
1005
1009
  });
1006
- }, De = (n, r) => {
1010
+ }, Oe = (n, r) => {
1007
1011
  let i, a;
1008
1012
  try {
1009
1013
  i = structuredClone(n), a = structuredClone(r);
1010
1014
  } catch {
1011
1015
  return d.fail(t("invalid_input"));
1012
1016
  }
1013
- return O(async () => {
1017
+ return D(async () => {
1014
1018
  if (i.type !== "system" && !(i.type === "serviceAccount" && i.tenantId === a.tenantId && i.scopes?.includes("chat:channels:write"))) throw t("identity_required");
1015
- if (A(a.tenantId), A(a.refId), typeof a.name != "string" || a.name.length > 256 || a.name.includes("\0") || a.kind !== "dm" && !a.name.trim() || ![
1019
+ if (k(a.tenantId), k(a.refId), typeof a.name != "string" || a.name.length > 256 || a.name.includes("\0") || a.kind !== "dm" && !a.name.trim() || ![
1016
1020
  "global",
1017
1021
  "lobby",
1018
1022
  "party",
@@ -1024,21 +1028,17 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1024
1028
  "global",
1025
1029
  "guild",
1026
1030
  "party"
1027
- ].includes(a.kind) || a.members.length > (a.maxMembers ?? z.maxMembers) || !Number.isSafeInteger(a.maxMembers ?? z.maxMembers) || (a.maxMembers ?? z.maxMembers) < 1 || (a.maxMembers ?? z.maxMembers) > z.maxMembers || a.kind === "dm" && a.members.length !== 2 || new Set(a.members.map((e) => e.subjectId)).size !== a.members.length) throw t("invalid_input");
1031
+ ].includes(a.kind) || a.members.length > (a.maxMembers ?? R.maxMembers) || !Number.isSafeInteger(a.maxMembers ?? R.maxMembers) || (a.maxMembers ?? R.maxMembers) < 1 || (a.maxMembers ?? R.maxMembers) > R.maxMembers || a.kind === "dm" && a.members.length !== 2 || new Set(a.members.map((e) => e.subjectId)).size !== a.members.length) throw t("invalid_input");
1028
1032
  a.members.forEach((e) => {
1029
- if (A(e.subjectId), ![
1033
+ if (k(e.subjectId), ![
1030
1034
  "owner",
1031
1035
  "mod",
1032
1036
  "member"
1033
1037
  ].includes(e.role)) throw t("invalid_input");
1034
1038
  });
1035
- let n = a.slowModeMs ?? 0, r = a.retentionMs === void 0 ? z.retentionMs : a.retentionMs;
1039
+ let n = a.slowModeMs ?? 0, r = a.retentionMs === void 0 ? R.retentionMs : a.retentionMs;
1036
1040
  if (!Number.isSafeInteger(n) || n < 0 || n > 864e5 || r !== null && (!Number.isSafeInteger(r) || r < 1 || r > 31536e7)) throw t("invalid_input");
1037
- let o = "cchan_" + E([
1038
- a.tenantId,
1039
- a.kind,
1040
- a.refId
1041
- ]).slice(0, 56), s = E([
1041
+ let o = oe(a.tenantId, a.kind, a.refId), s = T([
1042
1042
  a.tenantId,
1043
1043
  a.kind,
1044
1044
  a.refId,
@@ -1046,13 +1046,17 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1046
1046
  [...a.members].sort((e, t) => e.subjectId < t.subjectId ? -1 : +(e.subjectId > t.subjectId)),
1047
1047
  n,
1048
1048
  r,
1049
- a.maxMembers ?? z.maxMembers
1050
- ]), u = a.members.map((e) => e.subjectId);
1051
- await se(a.tenantId, u);
1049
+ a.maxMembers ?? R.maxMembers
1050
+ ]), u = a.members.map((e) => e.subjectId), d = (await C.query(x(c).where(m("id", o)).limit(1).descriptor))[0];
1051
+ if (d) {
1052
+ if (await G(C, a.tenantId, u), d.tenantId !== a.tenantId || d.kind !== a.kind || d.refId !== a.refId || d.creationKey !== s) throw t("conflict");
1053
+ return F(d);
1054
+ }
1055
+ await le(a.tenantId, u);
1052
1056
  try {
1053
- return await w.transactional(async (e) => {
1054
- await ue(e, a.tenantId, u);
1055
- let t = B(), i = await e.insert(c.tableName, {
1057
+ return await C.transactional(async (e) => {
1058
+ await de(e, a.tenantId, u);
1059
+ let i = z(), d = await e.insertIgnoreWithOutcome(c.tableName, {
1056
1060
  id: o,
1057
1061
  tenantId: a.tenantId,
1058
1062
  kind: a.kind,
@@ -1060,15 +1064,19 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1060
1064
  name: a.name,
1061
1065
  slowModeMs: n,
1062
1066
  retentionMs: r,
1063
- maxMembers: a.maxMembers ?? z.maxMembers,
1067
+ maxMembers: a.maxMembers ?? R.maxMembers,
1064
1068
  frozen: !1,
1065
- createdAt: new Date(t),
1069
+ createdAt: new Date(i),
1066
1070
  admission: 0,
1067
1071
  creationKey: s,
1068
1072
  metadata: {}
1069
- });
1073
+ }, { conflictColumns: ["id"] }), f = d.row;
1074
+ if (d.outcome === "ignored") {
1075
+ if (f.tenantId !== a.tenantId || f.kind !== a.kind || f.refId !== a.refId || f.creationKey !== s) throw t("conflict");
1076
+ return F(f);
1077
+ }
1070
1078
  for (let t of a.members) await e.insert(l.tableName, {
1071
- id: V(a.tenantId, o, t.subjectId),
1079
+ id: B(a.tenantId, o, t.subjectId),
1072
1080
  tenantId: a.tenantId,
1073
1081
  channelId: o,
1074
1082
  subjectId: t.subjectId,
@@ -1080,39 +1088,39 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1080
1088
  notify: !0,
1081
1089
  metadata: {}
1082
1090
  });
1083
- return I(i);
1091
+ return F(f);
1084
1092
  });
1085
1093
  } catch (n) {
1086
1094
  if (n instanceof e) throw n;
1087
- await ce(w, a.tenantId, u);
1088
- let r = (await w.query(S(c).where(m("id", o)).limit(1).descriptor))[0];
1095
+ await G(C, a.tenantId, u);
1096
+ let r = (await C.query(x(c).where(m("id", o)).limit(1).descriptor))[0];
1089
1097
  if (!r) throw n;
1090
1098
  if (r.tenantId !== a.tenantId || r.kind !== a.kind || r.refId !== a.refId || r.creationKey !== s) throw t("conflict");
1091
- return I(r);
1099
+ return F(r);
1092
1100
  }
1093
1101
  });
1094
- }, Oe = (e, n) => {
1102
+ }, ke = (e, n) => {
1095
1103
  let r, i;
1096
1104
  try {
1097
1105
  r = structuredClone(n), i = structuredClone(e);
1098
1106
  } catch {
1099
1107
  return d.fail(t("invalid_input"));
1100
1108
  }
1101
- return O(async () => {
1102
- if (xe(i, r.tenantId, "chat:members:write"), A(r.subjectId), r.role !== "member" && r.role !== "mod") throw t("invalid_input");
1109
+ return D(async () => {
1110
+ if (Se(i, r.tenantId, "chat:members:write"), k(r.subjectId), r.role !== "member" && r.role !== "mod") throw t("invalid_input");
1103
1111
  return K(r.tenantId, r.channelId, async (e, n) => {
1104
1112
  if (n.kind === "dm" || n.kind === "match") throw t("forbidden");
1105
- if (F(n.frozen)) throw t("frozen");
1106
- let i = V(r.tenantId, r.channelId, r.subjectId), a = (await e.query(S(l).where(m("id", i)).limit(1).forUpdate().descriptor))[0];
1113
+ if (P(n.frozen)) throw t("frozen");
1114
+ let i = B(r.tenantId, r.channelId, r.subjectId), a = (await e.query(x(l).where(m("id", i)).limit(1).forUpdate().descriptor))[0];
1107
1115
  if (a) {
1108
1116
  if (a.tenantId !== r.tenantId || a.channelId !== r.channelId || a.subjectId !== r.subjectId) throw t("unavailable");
1109
1117
  if (a.role !== r.role) throw t("conflict");
1110
- return L(a);
1118
+ return I(a);
1111
1119
  }
1112
- let o = await e.query(S(l).where(p(m("tenantId", r.tenantId), m("channelId", r.channelId))).limit(Number(n.maxMembers) + 1).forUpdate().descriptor);
1120
+ let o = await e.query(x(l).where(p(m("tenantId", r.tenantId), m("channelId", r.channelId))).limit(Number(n.maxMembers) + 1).forUpdate().descriptor);
1113
1121
  if (o.some((e) => e.tenantId !== r.tenantId || e.channelId !== r.channelId)) throw t("unavailable");
1114
1122
  if (o.length >= Number(n.maxMembers)) throw t("member_limit");
1115
- return L(await e.insert(l.tableName, {
1123
+ return I(await e.insert(l.tableName, {
1116
1124
  id: i,
1117
1125
  tenantId: r.tenantId,
1118
1126
  channelId: r.channelId,
@@ -1127,51 +1135,51 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1127
1135
  }));
1128
1136
  }, [r.subjectId]);
1129
1137
  });
1130
- }, ke = (e, n) => {
1138
+ }, Ae = (e, n) => {
1131
1139
  let r, i;
1132
1140
  try {
1133
1141
  r = structuredClone(n), i = structuredClone(e);
1134
1142
  } catch {
1135
1143
  return d.fail(t("invalid_input"));
1136
1144
  }
1137
- return O(async () => (xe(i, r.tenantId, "chat:members:write"), A(r.subjectId), K(r.tenantId, r.channelId, async (e, n) => {
1145
+ return D(async () => (Se(i, r.tenantId, "chat:members:write"), k(r.subjectId), K(r.tenantId, r.channelId, async (e, n) => {
1138
1146
  if (n.kind === "dm") throw t("forbidden");
1139
- let i = (await e.query(S(l).where(m("id", V(r.tenantId, r.channelId, r.subjectId))).limit(1).descriptor))[0];
1147
+ let i = (await e.query(x(l).where(m("id", B(r.tenantId, r.channelId, r.subjectId))).limit(1).descriptor))[0];
1140
1148
  if (i) {
1141
1149
  if (i.tenantId !== r.tenantId || i.channelId !== r.channelId || i.subjectId !== r.subjectId) throw t("unavailable");
1142
1150
  if (i.role === "owner") throw t("forbidden");
1143
- await e.delete(l.tableName, P(i, "id"));
1151
+ await e.delete(l.tableName, N(i, "id"));
1144
1152
  }
1145
1153
  })));
1146
- }, Ae = (n, r) => {
1154
+ }, je = (n, r) => {
1147
1155
  let i, a;
1148
1156
  try {
1149
- if (i = structuredClone(n), a = structuredClone(r), xe(i, a.tenantId, "chat:archive:write"), A(a.matchId), typeof a.name != "string" || !a.name.trim() || a.name.length > 256 || a.name.includes("\0") || !Array.isArray(a.members) || a.members.length < 1 || a.members.length > z.maxMembers || !Array.isArray(a.messages) || a.messages.length > z.maxExportRows) throw t("invalid_input");
1157
+ if (i = structuredClone(n), a = structuredClone(r), Se(i, a.tenantId, "chat:archive:write"), k(a.matchId), typeof a.name != "string" || !a.name.trim() || a.name.length > 256 || a.name.includes("\0") || !Array.isArray(a.members) || a.members.length < 1 || a.members.length > R.maxMembers || !Array.isArray(a.messages) || a.messages.length > R.maxExportRows) throw t("invalid_input");
1150
1158
  let e = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set();
1151
1159
  for (let n of a.members) {
1152
- if (A(n.subjectId), e.has(n.subjectId) || ![
1160
+ if (k(n.subjectId), e.has(n.subjectId) || ![
1153
1161
  "member",
1154
1162
  "mod",
1155
1163
  "owner"
1156
1164
  ].includes(n.role)) throw t("invalid_input");
1157
1165
  e.add(n.subjectId);
1158
1166
  }
1159
- let s = B();
1167
+ let s = z();
1160
1168
  for (let n of a.messages) {
1161
- if (A(n.id), A(n.authorId), oe(n.body), o.has(n.id) || !e.has(n.authorId) || !Number.isSafeInteger(n.at) || n.at < 0 || n.at > s) throw t("invalid_input");
1169
+ if (k(n.id), k(n.authorId), ce(n.body), o.has(n.id) || !e.has(n.authorId) || !Number.isSafeInteger(n.at) || n.at < 0 || n.at > s) throw t("invalid_input");
1162
1170
  o.add(n.id);
1163
1171
  }
1164
1172
  if (a.retentionMs !== void 0 && a.retentionMs !== null && (!Number.isSafeInteger(a.retentionMs) || a.retentionMs < 1 || a.retentionMs > 31536e7)) throw t("invalid_input");
1165
1173
  } catch (e) {
1166
- return d.fail(D(e));
1174
+ return d.fail(E(e));
1167
1175
  }
1168
1176
  let o = a;
1169
1177
  return d.gen(function* () {
1170
- let n = o.tenantId, r = "cchan_" + E([
1178
+ let n = o.tenantId, r = "cchan_" + T([
1171
1179
  n,
1172
1180
  "match",
1173
1181
  o.matchId
1174
- ]).slice(0, 56), i = o.retentionMs === void 0 ? z.retentionMs : o.retentionMs, a = o.members.map((e) => ({
1182
+ ]).slice(0, 56), i = o.retentionMs === void 0 ? R.retentionMs : o.retentionMs, a = o.members.map((e) => ({
1175
1183
  subjectId: e.subjectId,
1176
1184
  role: e.role
1177
1185
  })).sort((e, t) => e.subjectId < t.subjectId ? -1 : +(e.subjectId > t.subjectId)), s = o.messages.map((e) => ({
@@ -1179,7 +1187,7 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1179
1187
  authorId: e.authorId,
1180
1188
  body: e.body,
1181
1189
  at: e.at
1182
- })).sort((e, t) => e.id < t.id ? -1 : +(e.id > t.id)), d = E([
1190
+ })).sort((e, t) => e.id < t.id ? -1 : +(e.id > t.id)), d = T([
1183
1191
  "chat:archive:1",
1184
1192
  n,
1185
1193
  o.matchId,
@@ -1188,33 +1196,33 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1188
1196
  s,
1189
1197
  i
1190
1198
  ]), f = async () => {
1191
- await ce(w, n, a.map((e) => e.subjectId));
1192
- let e = (await w.query(S(c).where(m("id", r)).limit(1).descriptor))[0];
1199
+ await G(C, n, a.map((e) => e.subjectId));
1200
+ let e = (await C.query(x(c).where(m("id", r)).limit(1).descriptor))[0];
1193
1201
  if (!e) return null;
1194
1202
  if (e.tenantId !== n || e.kind !== "match" || e.refId !== o.matchId || e.creationKey !== d) throw t("conflict");
1195
1203
  for (let e of s) {
1196
- let i = "cmsg_" + E([
1204
+ let i = "cmsg_" + T([
1197
1205
  n,
1198
1206
  r,
1199
1207
  "archive",
1200
1208
  e.id
1201
- ]).slice(0, 56), a = (await w.query(S(u).where(m("id", i)).limit(1).descriptor))[0];
1202
- if (!a || a.tenantId !== n || a.channelId !== r || a.authorId !== e.authorId || a.requestKey !== E([d, e.id])) throw t("conflict");
1209
+ ]).slice(0, 56), a = (await C.query(x(u).where(m("id", i)).limit(1).descriptor))[0];
1210
+ if (!a || a.tenantId !== n || a.channelId !== r || a.authorId !== e.authorId || a.requestKey !== T([d, e.id])) throw t("conflict");
1203
1211
  }
1204
- return I(e);
1205
- }, p = yield* O(f);
1212
+ return F(e);
1213
+ }, p = yield* D(f);
1206
1214
  if (p !== null) return p;
1207
1215
  let h = [];
1208
- for (let e of s) h.push(yield* ve(e.body));
1209
- return yield* O(async () => {
1216
+ for (let e of s) h.push(yield* ye(e.body));
1217
+ return yield* D(async () => {
1210
1218
  let t = s.map((e, t) => ({
1211
1219
  entry: e,
1212
1220
  moderated: h[t]
1213
1221
  }));
1214
- await se(n, a.map((e) => e.subjectId));
1222
+ await le(n, a.map((e) => e.subjectId));
1215
1223
  try {
1216
- return await w.transactional(async (e) => {
1217
- await ue(e, n, a.map((e) => e.subjectId));
1224
+ return await C.transactional(async (e) => {
1225
+ await de(e, n, a.map((e) => e.subjectId));
1218
1226
  let s = await e.insert(c.tableName, {
1219
1227
  id: r,
1220
1228
  tenantId: n,
@@ -1225,13 +1233,13 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1225
1233
  retentionMs: i,
1226
1234
  maxMembers: a.length,
1227
1235
  frozen: !0,
1228
- createdAt: new Date(B()),
1236
+ createdAt: new Date(z()),
1229
1237
  admission: 0,
1230
1238
  creationKey: d,
1231
1239
  metadata: {}
1232
1240
  });
1233
1241
  for (let t of a) await e.insert(l.tableName, {
1234
- id: V(n, r, t.subjectId),
1242
+ id: B(n, r, t.subjectId),
1235
1243
  tenantId: n,
1236
1244
  channelId: r,
1237
1245
  subjectId: t.subjectId,
@@ -1244,7 +1252,7 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1244
1252
  metadata: {}
1245
1253
  });
1246
1254
  for (let { entry: i, moderated: a } of t) await e.insert(u.tableName, {
1247
- id: "cmsg_" + E([
1255
+ id: "cmsg_" + T([
1248
1256
  n,
1249
1257
  r,
1250
1258
  "archive",
@@ -1253,7 +1261,7 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1253
1261
  tenantId: n,
1254
1262
  channelId: r,
1255
1263
  authorId: i.authorId,
1256
- requestKey: E([d, i.id]),
1264
+ requestKey: T([d, i.id]),
1257
1265
  kind: "text",
1258
1266
  body: a.body,
1259
1267
  originalBody: a.body,
@@ -1266,7 +1274,7 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1266
1274
  version: 0,
1267
1275
  metadata: {}
1268
1276
  });
1269
- return I(s);
1277
+ return F(s);
1270
1278
  });
1271
1279
  } catch (t) {
1272
1280
  if (t instanceof e) throw t;
@@ -1276,25 +1284,25 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1276
1284
  }
1277
1285
  });
1278
1286
  });
1279
- }, je = async (e, i) => {
1287
+ }, Me = async (e, i) => {
1280
1288
  if (i.tenantId === null) throw t("identity_required");
1281
- A(i.tenantId), A(i.subjectId);
1282
- let o = await e.query(S(l).where(p(m("tenantId", i.tenantId), m("subjectId", i.subjectId))).orderBy("id", "asc").limit(z.maxExportRows + 1).descriptor), s = await e.query(S(u).where(p(m("tenantId", i.tenantId), m("authorId", i.subjectId))).orderBy("id", "asc").limit(z.maxExportRows + 1).descriptor), c = await e.query(S(r).where(p(m("tenantId", i.tenantId), m("authorId", i.subjectId))).orderBy("id", "asc").limit(z.maxExportRows + 1).descriptor), d = await e.query(S(a).where(p(m("tenantId", i.tenantId), m("ownerId", i.subjectId))).orderBy("id", "asc").limit(z.maxExportRows + 1).descriptor), f = /* @__PURE__ */ new Map();
1283
- for (let t = 0; t < 100; t += 20) for (let n of await e.query(S(u).where(p(m("tenantId", i.tenantId), x(...Array.from({ length: 20 }, (e, n) => v("payload", "mentions", t + n).eq(i.subjectId))))).orderBy("id", "asc").limit(z.maxExportRows + 1).descriptor)) f.set(P(n, "id"), n);
1284
- let h = [...f.values()].sort((e, t) => P(e, "id") < P(t, "id") ? -1 : 1);
1285
- if (h.length > z.maxExportRows || h.some((e) => e.tenantId !== i.tenantId)) throw t("export_limit");
1286
- let g = h.filter((e) => e.authorId !== i.subjectId && e.payload && typeof e.payload == "object" && Array.isArray(e.payload.mentions) && e.payload.mentions.includes(i.subjectId)), _ = (await e.query(S(n).where(m("id", G(i.tenantId, i.subjectId))).limit(1).descriptor))[0] ?? null;
1287
- if (_ && (_.tenantId !== i.tenantId || !F(_.erased) && _.subjectId !== i.subjectId)) throw t("unavailable");
1289
+ k(i.tenantId), k(i.subjectId);
1290
+ let o = await e.query(x(l).where(p(m("tenantId", i.tenantId), m("subjectId", i.subjectId))).orderBy("id", "asc").limit(R.maxExportRows + 1).descriptor), s = await e.query(x(u).where(p(m("tenantId", i.tenantId), m("authorId", i.subjectId))).orderBy("id", "asc").limit(R.maxExportRows + 1).descriptor), c = await e.query(x(r).where(p(m("tenantId", i.tenantId), m("authorId", i.subjectId))).orderBy("id", "asc").limit(R.maxExportRows + 1).descriptor), d = await e.query(x(a).where(p(m("tenantId", i.tenantId), m("ownerId", i.subjectId))).orderBy("id", "asc").limit(R.maxExportRows + 1).descriptor), f = /* @__PURE__ */ new Map();
1291
+ for (let t = 0; t < 100; t += 20) for (let n of await e.query(x(u).where(p(m("tenantId", i.tenantId), b(...Array.from({ length: 20 }, (e, n) => v("payload", "mentions", t + n).eq(i.subjectId))))).orderBy("id", "asc").limit(R.maxExportRows + 1).descriptor)) f.set(N(n, "id"), n);
1292
+ let h = [...f.values()].sort((e, t) => N(e, "id") < N(t, "id") ? -1 : 1);
1293
+ if (h.length > R.maxExportRows || h.some((e) => e.tenantId !== i.tenantId)) throw t("export_limit");
1294
+ let g = h.filter((e) => e.authorId !== i.subjectId && e.payload && typeof e.payload == "object" && Array.isArray(e.payload.mentions) && e.payload.mentions.includes(i.subjectId)), _ = (await e.query(x(n).where(m("id", W(i.tenantId, i.subjectId))).limit(1).descriptor))[0] ?? null;
1295
+ if (_ && (_.tenantId !== i.tenantId || !P(_.erased) && _.subjectId !== i.subjectId)) throw t("unavailable");
1288
1296
  let y = _ === null ? 0 : 1;
1289
- if (o.length + s.length + c.length + g.length + d.length + y > z.maxExportRows) throw t("export_limit");
1297
+ if (o.length + s.length + c.length + g.length + d.length + y > R.maxExportRows) throw t("export_limit");
1290
1298
  if (o.some((e) => e.tenantId !== i.tenantId || e.subjectId !== i.subjectId) || s.some((e) => e.tenantId !== i.tenantId || e.authorId !== i.subjectId) || c.some((e) => e.tenantId !== i.tenantId || e.authorId !== i.subjectId) || d.some((e) => e.tenantId !== i.tenantId || e.ownerId !== i.subjectId)) throw t("unavailable");
1291
- let b = new Set(o.filter((e) => e.role === "owner").map((e) => P(e, "channelId"))), C = [];
1292
- for (let t of b) C.push(await H(e, i.tenantId, t));
1293
- if (o.length + s.length + c.length + g.length + d.length + C.length + y > z.maxExportRows) throw t("export_limit");
1299
+ let ee = new Set(o.filter((e) => e.role === "owner").map((e) => N(e, "channelId"))), S = [];
1300
+ for (let t of ee) S.push(await V(e, i.tenantId, t));
1301
+ if (o.length + s.length + c.length + g.length + d.length + S.length + y > R.maxExportRows) throw t("export_limit");
1294
1302
  return {
1295
1303
  memberships: o,
1296
1304
  authored: s,
1297
- ownedChannels: C,
1305
+ ownedChannels: S,
1298
1306
  evidence: c,
1299
1307
  mentioned: g,
1300
1308
  media: d,
@@ -1302,20 +1310,20 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1302
1310
  };
1303
1311
  };
1304
1312
  return {
1305
- service: be,
1306
- typingPolicy: ye.policy,
1307
- disposeTyping: ye.close,
1308
- readReportEvidence: Ce,
1309
- collectReportContext: Se,
1310
- prune: we,
1311
- sweepMedia: Te,
1312
- canNotifyMention: Ee,
1313
- createChannel: De,
1314
- addMember: Oe,
1315
- removeMember: ke,
1316
- archiveMatch: Ae,
1317
- exportSubject: (e) => O(() => w.transactional(async (t) => {
1318
- let n = await je(t, e);
1313
+ service: xe,
1314
+ typingPolicy: be.policy,
1315
+ disposeTyping: be.close,
1316
+ readReportEvidence: we,
1317
+ collectReportContext: Ce,
1318
+ prune: Te,
1319
+ sweepMedia: Ee,
1320
+ canNotifyMention: De,
1321
+ createChannel: Oe,
1322
+ addMember: ke,
1323
+ removeMember: Ae,
1324
+ archiveMatch: je,
1325
+ exportSubject: (e) => D(() => C.transactional(async (t) => {
1326
+ let n = await Me(t, e);
1319
1327
  return JSON.parse(JSON.stringify({
1320
1328
  memberships: n.memberships,
1321
1329
  messages: n.authored,
@@ -1325,6 +1333,85 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1325
1333
  channels: n.ownedChannels.map(({ admission: e, ...t }) => t)
1326
1334
  }));
1327
1335
  })),
1336
+ mergeSubject: (e) => D(async () => {
1337
+ let o = e.store, s = e.tenantId, d = e.sourceId, f = e.targetId;
1338
+ if (k(s), k(d), k(f), d === f) throw t("invalid_input");
1339
+ for (let e of [d, f].sort()) await o.insertIgnore(n.tableName, {
1340
+ id: W(s, e),
1341
+ tenantId: s,
1342
+ subjectId: e,
1343
+ admission: 0,
1344
+ erased: !1
1345
+ }, { conflictColumns: ["id"] });
1346
+ await de(o, s, [d, f]);
1347
+ let p = await Me(o, {
1348
+ tenantId: s,
1349
+ subjectId: d,
1350
+ kind: "guest"
1351
+ }), h = [...new Set([
1352
+ ...p.memberships,
1353
+ ...p.authored,
1354
+ ...p.evidence,
1355
+ ...p.mentioned,
1356
+ ...p.media
1357
+ ].map((e) => N(e, "channelId")))].sort();
1358
+ for (let e of h) if (await o.updateMany(c.tableName, { admission: 0 }, { where: m("id", e) }) > 1) throw t("unavailable");
1359
+ for (let e of p.authored) await o.update(u.tableName, N(e, "id"), { authorId: f });
1360
+ for (let e of p.evidence) await o.update(r.tableName, N(e, "id"), { authorId: f });
1361
+ for (let e of p.media) await o.update(a.tableName, N(e, "id"), { ownerId: f });
1362
+ for (let e of p.mentioned) {
1363
+ let t = e.payload, n = [...new Set(t.mentions.map((e) => e === d ? f : e))];
1364
+ await o.update(u.tableName, N(e, "id"), {
1365
+ payload: {
1366
+ ...t,
1367
+ mentions: n
1368
+ },
1369
+ version: Math.min(Number(e.version) + 1, i)
1370
+ });
1371
+ }
1372
+ let g = (e) => e === "owner" ? 2 : +(e === "mod"), _ = async (e, t, n) => {
1373
+ let r = B(s, t, n), i = (await o.query(x(l).where(m("id", r)).limit(1).descriptor))[0];
1374
+ await o.delete(l.tableName, N(e, "id")), i ? g(e.role) > g(i.role) && await o.update(l.tableName, r, { role: e.role }) : await o.insert(l.tableName, {
1375
+ ...e,
1376
+ id: r,
1377
+ channelId: t,
1378
+ subjectId: n
1379
+ });
1380
+ };
1381
+ for (let e of p.memberships) {
1382
+ let t = N(e, "channelId"), n = await V(o, s, t);
1383
+ if (n.kind !== "dm") {
1384
+ await _(e, t, f);
1385
+ continue;
1386
+ }
1387
+ let i = (await o.query(x(l).where(m("channelId", t)).descriptor)).map((e) => N(e, "subjectId")).find((e) => e !== d);
1388
+ if (i === void 0 || i === f) {
1389
+ await o.delete(l.tableName, N(e, "id"));
1390
+ continue;
1391
+ }
1392
+ let p = T([s, [f, i].sort()]), h = "cchan_" + T([
1393
+ s,
1394
+ "dm",
1395
+ p
1396
+ ]).slice(0, 56);
1397
+ (await o.query(x(c).where(m("id", h)).limit(1).descriptor))[0] || await o.insert(c.tableName, {
1398
+ ...n,
1399
+ id: h,
1400
+ refId: p
1401
+ });
1402
+ for (let e of [
1403
+ u,
1404
+ r,
1405
+ a
1406
+ ]) for (let n of await o.query(x(e).where(m("channelId", t)).descriptor)) await o.update(e.tableName, N(n, "id"), { channelId: h });
1407
+ for (let e of await o.query(x(l).where(m("channelId", t)).descriptor)) await _(e, h, N(e, "subjectId") === d ? f : N(e, "subjectId"));
1408
+ await o.delete(c.tableName, t);
1409
+ }
1410
+ await o.update(n.tableName, W(s, d), {
1411
+ subjectId: null,
1412
+ erased: !0
1413
+ });
1414
+ }),
1328
1415
  eraseSubject: (e) => {
1329
1416
  let o;
1330
1417
  try {
@@ -1332,51 +1419,51 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1332
1419
  } catch {
1333
1420
  return d.fail(t("invalid_input"));
1334
1421
  }
1335
- return O(async () => {
1422
+ return D(async () => {
1336
1423
  if (o.tenantId === null) throw t("identity_required");
1337
- A(o.subjectId), A(o.tenantId), await se(o.tenantId, [o.subjectId]);
1424
+ k(o.subjectId), k(o.tenantId), await le(o.tenantId, [o.subjectId]);
1338
1425
  let e = Symbol("chat-erasure-channel-set-changed"), s = (e) => [.../* @__PURE__ */ new Set([
1339
- ...e.memberships.map((e) => P(e, "channelId")),
1340
- ...e.authored.map((e) => P(e, "channelId")),
1341
- ...e.evidence.map((e) => P(e, "channelId")),
1342
- ...e.mentioned.map((e) => P(e, "channelId")),
1343
- ...e.media.map((e) => P(e, "channelId")),
1344
- ...e.ownedChannels.map((e) => P(e, "id"))
1426
+ ...e.memberships.map((e) => N(e, "channelId")),
1427
+ ...e.authored.map((e) => N(e, "channelId")),
1428
+ ...e.evidence.map((e) => N(e, "channelId")),
1429
+ ...e.mentioned.map((e) => N(e, "channelId")),
1430
+ ...e.media.map((e) => N(e, "channelId")),
1431
+ ...e.ownedChannels.map((e) => N(e, "id"))
1345
1432
  ])].sort();
1346
1433
  for (let d = 0; d < 4; d++) {
1347
- let d = s(await je(w, o));
1434
+ let d = s(await Me(C, o));
1348
1435
  try {
1349
- await w.transactional(async (f) => {
1350
- await le(f, o.tenantId, [o.subjectId]);
1436
+ await C.transactional(async (f) => {
1437
+ await ue(f, o.tenantId, [o.subjectId]);
1351
1438
  let p = [];
1352
1439
  for (let e of d) {
1353
1440
  let n = await f.updateMany(c.tableName, { admission: 0 }, { where: m("id", e) });
1354
1441
  if (n > 1) throw t("unavailable");
1355
1442
  n === 1 && p.push(e);
1356
1443
  }
1357
- await ce(f, o.tenantId, [o.subjectId], !0);
1358
- for (let e of p) await H(f, o.tenantId, e);
1359
- let h = await je(f, o);
1444
+ await G(f, o.tenantId, [o.subjectId], !0);
1445
+ for (let e of p) await V(f, o.tenantId, e);
1446
+ let h = await Me(f, o);
1360
1447
  if (s(h).some((e) => !d.includes(e))) throw e;
1361
- for (let e of h.authored) await f.update(u.tableName, P(e, "id"), {
1448
+ for (let e of h.authored) await f.update(u.tableName, N(e, "id"), {
1362
1449
  authorId: null,
1363
1450
  requestKey: "",
1364
1451
  body: "",
1365
1452
  originalBody: "",
1366
1453
  payload: {},
1367
1454
  metadata: {},
1368
- deletedAt: new Date(B()),
1455
+ deletedAt: new Date(z()),
1369
1456
  version: Math.min(Number(e.version) + 1, i)
1370
1457
  });
1371
- for (let e of h.evidence) await f.update(r.tableName, P(e, "id"), {
1458
+ for (let e of h.evidence) await f.update(r.tableName, N(e, "id"), {
1372
1459
  authorId: null,
1373
1460
  body: "",
1374
1461
  originalBody: "",
1375
- erasedAt: new Date(B())
1462
+ erasedAt: new Date(z())
1376
1463
  });
1377
1464
  for (let e of h.mentioned) {
1378
1465
  let t = e.payload;
1379
- await f.update(u.tableName, P(e, "id"), {
1466
+ await f.update(u.tableName, N(e, "id"), {
1380
1467
  payload: {
1381
1468
  ...t,
1382
1469
  mentions: t.mentions.filter((e) => e !== o.subjectId)
@@ -1384,16 +1471,16 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1384
1471
  version: Math.min(Number(e.version) + 1, i)
1385
1472
  });
1386
1473
  }
1387
- for (let e of h.media) await f.update(a.tableName, P(e, "id"), {
1474
+ for (let e of h.media) await f.update(a.tableName, N(e, "id"), {
1388
1475
  ownerId: null,
1389
1476
  state: "deleting"
1390
1477
  });
1391
- for (let e of h.memberships) await f.delete(l.tableName, P(e, "id"));
1392
- for (let e of h.ownedChannels) await f.update(c.tableName, P(e, "id"), {
1478
+ for (let e of h.memberships) await f.delete(l.tableName, N(e, "id"));
1479
+ for (let e of h.ownedChannels) await f.update(c.tableName, N(e, "id"), {
1393
1480
  metadata: {},
1394
1481
  name: ""
1395
1482
  });
1396
- await f.update(n.tableName, G(o.tenantId, o.subjectId), {
1483
+ await f.update(n.tableName, W(o.tenantId, o.subjectId), {
1397
1484
  subjectId: null,
1398
1485
  erased: !0
1399
1486
  });
@@ -1408,4 +1495,4 @@ var E = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), D = (n) => n
1408
1495
  }
1409
1496
  };
1410
1497
  };
1411
- export { z as dataStoreChat };
1498
+ export { oe as chatChannelId, R as dataStoreChat };