@voltro/plugin-guilds 0.89.2 → 0.91.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
@@ -1,13 +1,14 @@
1
- import { GuildAnnouncementSchema as e, GuildApplicationSchema as t, GuildInviteSchema as n, GuildLogSchema as r, GuildMemberSchema as i, GuildRoleSchema as a, GuildSchema as o, GuildSummarySchema as s } from "./contract.js";
2
- import { guildAdmissionsTable as c, guildAnnouncementsTable as l, guildApplicationsTable as u, guildInvitesTable as d, guildLogTable as f, guildMembersTable as p, guildRolesTable as m, guildTables as h, guildsTable as g } from "./entities.js";
1
+ import { guildsUnavailable as e } from "./unavailable.server.js";
2
+ import { guildAdmissionsTable as t, guildAnnouncementsTable as n, guildApplicationsTable as r, guildInvitesTable as i, guildLogTable as a, guildMembersTable as o, guildRolesTable as s, guildTables as c, guildsTable as l } from "./entities.js";
3
+ import { GuildAnnouncementSchema as u, GuildApplicationSchema as d, GuildInviteSchema as f, GuildLogSchema as p, GuildMemberSchema as m, GuildRoleSchema as h, GuildSchema as g, GuildSummarySchema as ee } from "./contract.js";
3
4
  import { GuildsError as _, guildsError as v } from "./errors.js";
4
5
  import { Effect as y, Schema as b } from "effect";
5
- import { createHash as x, randomUUID as ee } from "node:crypto";
6
- import { and as S, eq as C, isNull as te, or as w, queryFor as ne } from "@voltro/database";
7
- import { keywordProvider as re } from "@voltro/plugin-moderation";
8
- import { SocialBlockPort as ie } from "@voltro/plugin-social/port";
6
+ import { and as x, eq as S, isNull as te, or as C, queryFor as ne } from "@voltro/database";
7
+ import { createHash as w, randomUUID as re } from "node:crypto";
8
+ import { keywordProvider as ie } from "@voltro/plugin-moderation";
9
+ import { SocialBlockPort as ae } from "@voltro/plugin-social/port";
9
10
  // src/service.ts
10
- var T = (e) => x("sha256").update(JSON.stringify(e)).digest("hex"), E = (e, t) => e + "_" + T(t).slice(0, 56), D = (e) => e instanceof _ ? e : v("unavailable"), O = (e) => y.tryPromise({
11
+ var T = (e) => w("sha256").update(JSON.stringify(e)).digest("hex"), E = (e, t) => e + "_" + T(t).slice(0, 56), D = (t) => t instanceof _ ? t : e(t), O = (e) => y.tryPromise({
11
12
  try: e,
12
13
  catch: D
13
14
  }), k = (e) => y.try({
@@ -31,58 +32,58 @@ var T = (e) => x("sha256").update(JSON.stringify(e)).digest("hex"), E = (e, t) =
31
32
  for (let e of n) i[e] = i[e] === null ? null : N(i[e]);
32
33
  for (let e of r) i[e] = P(i[e]);
33
34
  return b.decodeUnknownSync(e)(i);
34
- }, I = (e) => F(o, e, ["createdAt"]), L = (e) => F(i, e, ["joinedAt", "lastActiveAt"]), R = (e) => F(t, e, ["createdAt", "decidedAt"]), z = (e) => F(n, e, ["createdAt", "expiresAt"]), B = (t, n) => {
35
- let i = () => {
36
- let e = n.now();
35
+ }, I = (e) => F(g, e, ["createdAt"]), L = (e) => F(m, e, ["joinedAt", "lastActiveAt"]), R = (e) => F(d, e, ["createdAt", "decidedAt"]), z = (e) => F(f, e, ["createdAt", "expiresAt"]), B = (d, f) => {
36
+ let m = () => {
37
+ let e = f.now();
37
38
  if (!Number.isSafeInteger(e) || e < 0 || e > 864e13) throw v("unavailable");
38
39
  return e;
39
- }, o = (e, t, n = !1) => {
40
+ }, g = (e, t, n = !1) => {
40
41
  if (typeof e != "string" || !n && !e.trim() || [...e].length > t || /[\u0000-\u0008\u000b\u000c\u000e-\u001f]/u.test(e)) throw v("invalid_input");
41
42
  return e;
42
43
  }, b = (e) => {
43
- let t = o(e, 64).normalize("NFKC").trim().replace(/\s+/gu, " ");
44
+ let t = g(e, 64).normalize("NFKC").trim().replace(/\s+/gu, " ");
44
45
  if ([...t].length < 3 || [...t].length > 64) throw v("invalid_input");
45
46
  return t;
46
- }, x = (e) => {
47
+ }, w = (e) => {
47
48
  if (typeof e != "string" || !/^[a-zA-Z0-9]{3,5}$/u.test(e)) throw v("invalid_input");
48
49
  return e.toUpperCase();
49
- }, D = (e) => e == null ? null : o(e, 64), B = (e, t, n) => E("gmem", [
50
+ }, D = (e) => e == null ? null : g(e, 64), B = (e, t, n) => E("gmem", [
50
51
  e,
51
52
  t,
52
53
  n
53
- ]), V = (e, t) => E("grole", [e, t]), H = (e, t) => E("gadm", [e, t]), U = (e, t, r, i = n.maxExportRows + 1) => e.query(ne(t).where(r).orderBy("id", "asc").limit(i).descriptor), W = async (e, t, n, r) => {
54
+ ]), V = (e, t) => E("grole", [e, t]), H = (e, t) => E("gadm", [e, t]), U = (e, t, n, r = f.maxExportRows + 1) => e.query(ne(t).where(n).orderBy("id", "asc").limit(r).descriptor), W = async (e, t, n, r) => {
54
55
  A(r);
55
- let i = (await U(e, t, C("id", r), 1))[0];
56
+ let i = (await U(e, t, S("id", r), 1))[0];
56
57
  if (!i || i.tenantId !== n) throw v("not_found");
57
58
  return i;
58
59
  }, G = async (e, t, n) => {
59
- let r = await W(e, g, t, n);
60
+ let r = await W(e, l, t, n);
60
61
  if (r.dissolvedAt !== null) throw v("not_found");
61
62
  return r;
62
63
  }, K = async (e, t, n, r) => {
63
- let i = await W(e, p, t, B(t, n, r));
64
+ let i = await W(e, o, t, B(t, n, r));
64
65
  if (i.guildId !== n || i.subjectId !== r) throw v("unavailable");
65
66
  return i;
66
67
  }, q = async (e, t, n, r) => {
67
- let i = await W(e, m, t, M(r, "roleId"));
68
+ let i = await W(e, s, t, M(r, "roleId"));
68
69
  if (i.guildId !== n) throw v("unavailable");
69
- return F(a, i);
70
- }, J = async (e, t, r, i) => {
71
- let a = await K(e, t.tenantId, r, t.id), o = await q(e, t.tenantId, r, a);
72
- if (n.strategies.permissions({
73
- role: o,
74
- permission: i
70
+ return F(h, i);
71
+ }, J = async (e, t, n, r) => {
72
+ let i = await K(e, t.tenantId, n, t.id), a = await q(e, t.tenantId, n, i);
73
+ if (f.strategies.permissions({
74
+ role: a,
75
+ permission: r
75
76
  }) !== !0) throw v("forbidden");
76
77
  return {
77
- member: a,
78
- role: o
78
+ member: i,
79
+ role: a
79
80
  };
80
- }, Y = async (e, n) => {
81
+ }, oe = async (e, n) => {
81
82
  for (let r of [...new Set(n)].sort()) {
82
83
  A(r);
83
84
  let n = H(e, r);
84
85
  try {
85
- await t.insertIgnore(c.tableName, {
86
+ await d.insertIgnore(t.tableName, {
86
87
  id: n,
87
88
  tenantId: e,
88
89
  subjectId: r,
@@ -91,33 +92,33 @@ var T = (e) => x("sha256").update(JSON.stringify(e)).digest("hex"), E = (e, t) =
91
92
  metadata: {}
92
93
  }, { conflictColumns: ["id"] });
93
94
  } catch (i) {
94
- let a = (await U(t, c, C("id", n), 1))[0];
95
+ let a = (await U(d, t, S("id", n), 1))[0];
95
96
  if (!a || a.tenantId !== e || a.subjectId !== r && !P(a.erased)) throw i;
96
97
  }
97
98
  }
98
- }, ae = async (e, t, n, r = !1, i = async () => {}) => {
99
- let a = [...new Set(n)].map((e) => ({
99
+ }, Y = async (e, n, r, i = !1, a = async () => {}) => {
100
+ let o = [...new Set(r)].map((e) => ({
100
101
  subject: e,
101
- key: H(t, e)
102
+ key: H(n, e)
102
103
  })).sort((e, t) => e.key < t.key ? -1 : 1);
103
- for (let { key: t } of a) if (await e.updateMany(c.tableName, { admission: 0 }, { where: C("id", t) }) !== 1) throw v("unavailable");
104
- await i();
105
- for (let { subject: n, key: i } of a) {
106
- let a = await W(e, c, t, i);
107
- if (P(a.erased)) {
108
- if (!r) throw v("identity_required");
109
- } else if (a.subjectId !== n) throw v("unavailable");
104
+ for (let { key: n } of o) if (await e.updateMany(t.tableName, { admission: 0 }, { where: S("id", n) }) !== 1) throw v("unavailable");
105
+ await a();
106
+ for (let { subject: r, key: a } of o) {
107
+ let o = await W(e, t, n, a);
108
+ if (P(o.erased)) {
109
+ if (!i) throw v("identity_required");
110
+ } else if (o.subjectId !== r) throw v("unavailable");
110
111
  }
111
- }, X = async (e, n, r, i) => {
112
- A(n);
113
- let a = [e.id, ...r];
114
- return await Y(e.tenantId, a), t.transactional(async (t) => (await ae(t, e.tenantId, a, !1, async () => {
115
- if (await t.updateMany(g.tableName, { admission: 0 }, { where: C("id", n) }) !== 1) throw v("not_found");
116
- }), i(t, await G(t, e.tenantId, n))));
117
- }, Z = (e, t, r) => {
118
- if (e.length > n.maxExportRows || e.some((e) => e.tenantId !== t || r !== void 0 && e.guildId !== r)) throw v("unavailable");
112
+ }, X = async (e, t, n, r) => {
113
+ A(t);
114
+ let i = [e.id, ...n];
115
+ return await oe(e.tenantId, i), d.transactional(async (n) => (await Y(n, e.tenantId, i, !1, async () => {
116
+ if (await n.updateMany(l.tableName, { admission: 0 }, { where: S("id", t) }) !== 1) throw v("not_found");
117
+ }), r(n, await G(n, e.tenantId, t))));
118
+ }, Z = (e, t, n) => {
119
+ if (e.length > f.maxExportRows || e.some((e) => e.tenantId !== t || n !== void 0 && e.guildId !== n)) throw v("unavailable");
119
120
  return e;
120
- }, Q = (e, t, n, r, a, o = {}, s = ee()) => e.insert(f.tableName, {
121
+ }, Q = (e, t, n, r, i, o = {}, s = re()) => e.insert(a.tableName, {
121
122
  id: E("glog", [
122
123
  t.tenantId,
123
124
  t.id,
@@ -128,148 +129,148 @@ var T = (e) => x("sha256").update(JSON.stringify(e)).digest("hex"), E = (e, t) =
128
129
  guildId: t.id,
129
130
  kind: n,
130
131
  actorId: r,
131
- targetId: a,
132
+ targetId: i,
132
133
  detail: o,
133
- at: new Date(i())
134
- }), oe = async (e, t, r) => {
135
- let a = M(t, "tenantId"), o = M(t, "id"), s = B(a, o, r), c = (await U(e, p, C("id", s), 1))[0];
136
- if (c) {
137
- if (c.tenantId !== a || c.subjectId !== r || c.guildId !== o) throw v("unavailable");
138
- return L(c);
134
+ at: new Date(m())
135
+ }), se = async (e, t, n) => {
136
+ let r = M(t, "tenantId"), i = M(t, "id"), a = B(r, i, n), s = (await U(e, o, S("id", a), 1))[0];
137
+ if (s) {
138
+ if (s.tenantId !== r || s.subjectId !== n || s.guildId !== i) throw v("unavailable");
139
+ return L(s);
139
140
  }
140
- let l = Z(await U(e, p, S(C("tenantId", a), C("subjectId", r))), a);
141
- if (l.some((e) => e.subjectId !== r)) throw v("unavailable");
142
- if (l.length >= n.maxGuildsPerSubject) throw v("guild_limit");
143
- if (Z(await U(e, p, S(C("tenantId", a), C("guildId", o))), a, o).length >= Number(t.memberCap)) throw v("member_limit");
144
- return L(await e.insert(p.tableName, {
145
- id: s,
146
- tenantId: a,
147
- guildId: o,
148
- subjectId: r,
149
- roleId: V(o, "member"),
150
- joinedAt: new Date(i()),
151
- lastActiveAt: new Date(i()),
141
+ let c = Z(await U(e, o, x(S("tenantId", r), S("subjectId", n))), r);
142
+ if (c.some((e) => e.subjectId !== n)) throw v("unavailable");
143
+ if (c.length >= f.maxGuildsPerSubject) throw v("guild_limit");
144
+ if (Z(await U(e, o, x(S("tenantId", r), S("guildId", i))), r, i).length >= Number(t.memberCap)) throw v("member_limit");
145
+ return L(await e.insert(o.tableName, {
146
+ id: a,
147
+ tenantId: r,
148
+ guildId: i,
149
+ subjectId: n,
150
+ roleId: V(i, "member"),
151
+ joinedAt: new Date(m()),
152
+ lastActiveAt: new Date(m()),
152
153
  contribution: {},
153
154
  metadata: {}
154
155
  }));
155
- }, $ = (e) => re(n.denyWords)(e).pipe(y.flatMap((t) => t.flagged ? y.fail(v("content_rejected")) : y.succeed(e)), y.catchDefect(() => y.fail(v("unavailable")))), se = async (e, t, n, r = !1) => {
156
- let i = M(t, "tenantId"), a = M(t, "id"), o = M(t, "ownerId"), s = await K(e, i, a, n);
157
- if (o === n) return I(t);
156
+ }, $ = (t) => ie(f.denyWords)(t).pipe(y.flatMap((e) => e.flagged ? y.fail(v("content_rejected")) : y.succeed(t)), y.catchDefect((t) => y.fail(e(t)))), ce = async (e, t, n, r = !1) => {
157
+ let i = M(t, "tenantId"), a = M(t, "id"), s = M(t, "ownerId"), c = await K(e, i, a, n);
158
+ if (s === n) return I(t);
158
159
  if (!r) {
159
- let t = await K(e, i, a, o);
160
- await e.update(p.tableName, M(t, "id"), { roleId: V(a, "officer") });
160
+ let t = await K(e, i, a, s);
161
+ await e.update(o.tableName, M(t, "id"), { roleId: V(a, "officer") });
161
162
  }
162
- await e.update(p.tableName, M(s, "id"), { roleId: V(a, "owner") });
163
- let c = await e.update(g.tableName, a, {
163
+ await e.update(o.tableName, M(c, "id"), { roleId: V(a, "owner") });
164
+ let u = await e.update(l.tableName, a, {
164
165
  ownerId: n,
165
166
  version: Number(t.version) + 1
166
167
  });
167
- if (!c) throw v("unavailable");
168
- return await Q(e, t, "ownerTransferred", r ? null : o, n), I(c);
169
- }, ce = async (e, t, n, r) => {
170
- let a = M(t, "tenantId"), o = M(t, "id"), s = await K(e, a, o, n);
168
+ if (!u) throw v("unavailable");
169
+ return await Q(e, t, "ownerTransferred", r ? null : s, n), I(u);
170
+ }, le = async (e, t, n, r) => {
171
+ let i = M(t, "tenantId"), a = M(t, "id"), s = await K(e, i, a, n);
171
172
  if (t.ownerId === n) {
172
- let r = Z(await U(e, p, S(C("tenantId", a), C("guildId", o))), a, o), s = [];
173
+ let r = Z(await U(e, o, x(S("tenantId", i), S("guildId", a))), i, a), s = [];
173
174
  for (let t of r) t.subjectId !== n && s.push({
174
175
  row: t,
175
- role: await q(e, a, o, t)
176
+ role: await q(e, i, a, t)
176
177
  });
177
- s.sort((e, t) => e.role.rank - t.role.rank || N(e.row.joinedAt) - N(t.row.joinedAt) || (M(e.row, "subjectId") < M(t.row, "subjectId") ? -1 : 1)), s[0] ? await se(e, t, M(s[0].row, "subjectId"), !0) : await e.update(g.tableName, o, {
178
+ s.sort((e, t) => e.role.rank - t.role.rank || N(e.row.joinedAt) - N(t.row.joinedAt) || (M(e.row, "subjectId") < M(t.row, "subjectId") ? -1 : 1)), s[0] ? await ce(e, t, M(s[0].row, "subjectId"), !0) : await e.update(l.tableName, a, {
178
179
  ownerId: null,
179
- dissolvedAt: new Date(i()),
180
+ dissolvedAt: new Date(m()),
180
181
  version: Number(t.version) + 1,
181
182
  name: "",
182
183
  description: "",
183
184
  emblem: null,
184
185
  nameKey: T([
185
186
  "dissolved",
186
- o,
187
+ a,
187
188
  "name"
188
189
  ]),
189
190
  tagKey: T([
190
191
  "dissolved",
191
- o,
192
+ a,
192
193
  "tag"
193
194
  ]),
194
195
  tag: "",
195
196
  metadata: {}
196
197
  });
197
198
  }
198
- await e.delete(p.tableName, M(s, "id")), r || await Q(e, t, "left", n, n);
199
- }, le = {
200
- create: (e, r) => y.gen(function* () {
201
- let a = yield* k(() => {
202
- let t = j(e);
203
- A(r.requestId);
204
- let i = b(r.name), a = x(r.tag), s = o(r.description ?? "", n.maxDescriptionLength, !0), c = r.visibility ?? "open";
199
+ await e.delete(o.tableName, M(s, "id")), r || await Q(e, t, "left", n, n);
200
+ }, ue = {
201
+ create: (e, t) => y.gen(function* () {
202
+ let n = yield* k(() => {
203
+ let n = j(e);
204
+ A(t.requestId);
205
+ let r = b(t.name), i = w(t.tag), a = g(t.description ?? "", f.maxDescriptionLength, !0), o = t.visibility ?? "open";
205
206
  if (![
206
207
  "open",
207
208
  "apply",
208
209
  "invite"
209
- ].includes(c)) throw v("invalid_input");
210
+ ].includes(o)) throw v("invalid_input");
210
211
  return {
211
- who: t,
212
- guildName: i,
213
- guildTag: a,
214
- description: s,
215
- visibility: c,
216
- region: D(r.region),
217
- language: D(r.language)
212
+ who: n,
213
+ guildName: r,
214
+ guildTag: i,
215
+ description: a,
216
+ visibility: o,
217
+ region: D(t.region),
218
+ language: D(t.language)
218
219
  };
219
220
  });
220
- return yield* $(a.guildName + "\n" + a.description), yield* O(async () => {
221
- let { who: e, guildName: o, guildTag: s, description: c, visibility: l, region: u, language: d } = a, f = E("guild", [
221
+ return yield* $(n.guildName + "\n" + n.description), yield* O(async () => {
222
+ let { who: e, guildName: r, guildTag: i, description: a, visibility: c, region: u, language: p } = n, h = E("guild", [
222
223
  e.tenantId,
223
224
  e.id,
224
- r.requestId
225
- ]), h = T([
225
+ t.requestId
226
+ ]), g = T([
226
227
  e.id,
227
- o,
228
- s,
228
+ r,
229
+ i,
230
+ a,
229
231
  c,
230
- l,
231
232
  u,
232
- d
233
+ p
233
234
  ]);
234
- await Y(e.tenantId, [e.id]);
235
+ await oe(e.tenantId, [e.id]);
235
236
  try {
236
- return await t.transactional(async (t) => {
237
- await ae(t, e.tenantId, [e.id]);
238
- let a = (await U(t, g, C("id", f), 1))[0];
239
- if (a) {
240
- if (a.tenantId !== e.tenantId || a.creationKey !== h || a.dissolvedAt !== null) throw v("conflict");
241
- return I(a);
237
+ return await d.transactional(async (n) => {
238
+ await Y(n, e.tenantId, [e.id]);
239
+ let d = (await U(n, l, S("id", h), 1))[0];
240
+ if (d) {
241
+ if (d.tenantId !== e.tenantId || d.creationKey !== g || d.dissolvedAt !== null) throw v("conflict");
242
+ return I(d);
242
243
  }
243
- let _ = Z(await U(t, p, S(C("tenantId", e.tenantId), C("subjectId", e.id))), e.tenantId);
244
- if (_.some((t) => t.subjectId !== e.id)) throw v("unavailable");
245
- if (_.length >= n.maxGuildsPerSubject) throw v("guild_limit");
246
- let y = T([e.tenantId, o.toLowerCase()]), b = T([e.tenantId, s]);
247
- if ((await U(t, g, w(C("nameKey", y), C("tagKey", b)), 1)).length) throw v("name_taken");
248
- let x = await t.insert(g.tableName, {
249
- id: f,
244
+ let ee = Z(await U(n, o, x(S("tenantId", e.tenantId), S("subjectId", e.id))), e.tenantId);
245
+ if (ee.some((t) => t.subjectId !== e.id)) throw v("unavailable");
246
+ if (ee.length >= f.maxGuildsPerSubject) throw v("guild_limit");
247
+ let _ = T([e.tenantId, r.toLowerCase()]), y = T([e.tenantId, i]);
248
+ if ((await U(n, l, C(S("nameKey", _), S("tagKey", y)), 1)).length) throw v("name_taken");
249
+ let b = await n.insert(l.tableName, {
250
+ id: h,
250
251
  tenantId: e.tenantId,
251
- name: o,
252
- nameKey: y,
253
- tag: s,
254
- tagKey: b,
255
- description: c,
252
+ name: r,
253
+ nameKey: _,
254
+ tag: i,
255
+ tagKey: y,
256
+ description: a,
256
257
  emblem: null,
257
- visibility: l,
258
+ visibility: c,
258
259
  minLevel: 0,
259
260
  region: u,
260
- language: d,
261
+ language: p,
261
262
  level: 1,
262
263
  xp: 0,
263
- memberCap: n.baseMemberCap,
264
- createdAt: new Date(i()),
264
+ memberCap: f.baseMemberCap,
265
+ createdAt: new Date(m()),
265
266
  ownerId: e.id,
266
267
  version: 0,
267
268
  admission: 0,
268
- creationKey: h,
269
+ creationKey: g,
269
270
  dissolvedAt: null,
270
271
  metadata: {}
271
272
  });
272
- for (let [n, r, i] of [
273
+ for (let [t, r, i] of [
273
274
  [
274
275
  "owner",
275
276
  0,
@@ -285,322 +286,322 @@ var T = (e) => x("sha256").update(JSON.stringify(e)).digest("hex"), E = (e, t) =
285
286
  100,
286
287
  0
287
288
  ]
288
- ]) await t.insert(m.tableName, {
289
- id: V(f, n),
289
+ ]) await n.insert(s.tableName, {
290
+ id: V(h, t),
290
291
  tenantId: e.tenantId,
291
- guildId: f,
292
- name: n,
292
+ guildId: h,
293
+ name: t,
293
294
  rank: r,
294
295
  permissions: i,
295
296
  metadata: {}
296
297
  });
297
- return await t.insert(p.tableName, {
298
- id: B(e.tenantId, f, e.id),
298
+ return await n.insert(o.tableName, {
299
+ id: B(e.tenantId, h, e.id),
299
300
  tenantId: e.tenantId,
300
- guildId: f,
301
+ guildId: h,
301
302
  subjectId: e.id,
302
- roleId: V(f, "owner"),
303
- joinedAt: new Date(i()),
304
- lastActiveAt: new Date(i()),
303
+ roleId: V(h, "owner"),
304
+ joinedAt: new Date(m()),
305
+ lastActiveAt: new Date(m()),
305
306
  contribution: {},
306
307
  metadata: {}
307
- }), await Q(t, x, "created", e.id, null, {}, r.requestId), I(x);
308
+ }), await Q(n, b, "created", e.id, null, {}, t.requestId), I(b);
308
309
  });
309
- } catch (n) {
310
- if (n instanceof _) throw n;
311
- let r = await U(t, g, w(C("nameKey", T([e.tenantId, o.toLowerCase()])), C("tagKey", T([e.tenantId, s]))), 1);
312
- throw r.length && r[0]?.id !== f ? v("name_taken") : n;
310
+ } catch (t) {
311
+ if (t instanceof _) throw t;
312
+ let n = await U(d, l, C(S("nameKey", T([e.tenantId, r.toLowerCase()])), S("tagKey", T([e.tenantId, i]))), 1);
313
+ throw n.length && n[0]?.id !== h ? v("name_taken") : t;
313
314
  }
314
315
  });
315
316
  }),
316
317
  mine: (e) => O(async () => {
317
- let n = j(e), r = Z(await U(t, p, S(C("tenantId", n.tenantId), C("subjectId", n.id))), n.tenantId);
318
- if (r.some((e) => e.subjectId !== n.id)) throw v("unavailable");
319
- return Promise.all(r.map(async (e) => I(await G(t, n.tenantId, M(e, "guildId")))));
318
+ let t = j(e), n = Z(await U(d, o, x(S("tenantId", t.tenantId), S("subjectId", t.id))), t.tenantId);
319
+ if (n.some((e) => e.subjectId !== t.id)) throw v("unavailable");
320
+ return Promise.all(n.map(async (e) => I(await G(d, t.tenantId, M(e, "guildId")))));
320
321
  }),
321
- get: (e, n) => O(async () => {
322
- let r = j(e);
323
- return await K(t, r.tenantId, n, r.id), I(await G(t, r.tenantId, n));
322
+ get: (e, t) => O(async () => {
323
+ let n = j(e);
324
+ return await K(d, n.tenantId, t, n.id), I(await G(d, n.tenantId, t));
324
325
  }),
325
- search: (e, r) => O(async () => {
326
- let i = j(e), a = [C("tenantId", i.tenantId), te("dissolvedAt")];
327
- return r.name !== void 0 && a.push(C("nameKey", T([i.tenantId, b(r.name).toLowerCase()]))), r.tag !== void 0 && a.push(C("tagKey", T([i.tenantId, x(r.tag)]))), r.region !== void 0 && a.push(C("region", D(r.region))), r.language !== void 0 && a.push(C("language", D(r.language))), Z(await U(t, g, S(...a), n.pageSize), i.tenantId).map((e) => F(s, e, ["createdAt"]));
326
+ search: (e, t) => O(async () => {
327
+ let n = j(e), r = [S("tenantId", n.tenantId), te("dissolvedAt")];
328
+ return t.name !== void 0 && r.push(S("nameKey", T([n.tenantId, b(t.name).toLowerCase()]))), t.tag !== void 0 && r.push(S("tagKey", T([n.tenantId, w(t.tag)]))), t.region !== void 0 && r.push(S("region", D(t.region))), t.language !== void 0 && r.push(S("language", D(t.language))), Z(await U(d, l, x(...r), f.pageSize), n.tenantId).map((e) => F(ee, e, ["createdAt"]));
328
329
  }),
329
- members: (e, n) => O(async () => {
330
- let r = j(e);
331
- return await G(t, r.tenantId, n), await K(t, r.tenantId, n, r.id), Z(await U(t, p, S(C("tenantId", r.tenantId), C("guildId", n))), r.tenantId, n).map(L);
330
+ members: (e, t) => O(async () => {
331
+ let n = j(e);
332
+ return await G(d, n.tenantId, t), await K(d, n.tenantId, t, n.id), Z(await U(d, o, x(S("tenantId", n.tenantId), S("guildId", t))), n.tenantId, t).map(L);
332
333
  }),
333
- roles: (e, n) => O(async () => {
334
- let r = j(e);
335
- return await G(t, r.tenantId, n), await K(t, r.tenantId, n, r.id), Z(await U(t, m, S(C("tenantId", r.tenantId), C("guildId", n))), r.tenantId, n).map((e) => F(a, e));
334
+ roles: (e, t) => O(async () => {
335
+ let n = j(e);
336
+ return await G(d, n.tenantId, t), await K(d, n.tenantId, t, n.id), Z(await U(d, s, x(S("tenantId", n.tenantId), S("guildId", t))), n.tenantId, t).map((e) => F(h, e));
336
337
  }),
337
338
  join: (e, t) => O(async () => {
338
339
  let n = j(e);
339
340
  return X(n, t, [], async (e, r) => {
340
341
  if (r.visibility !== "open") throw v("forbidden");
341
- let i = (await U(e, p, C("id", B(n.tenantId, t, n.id)), 1))[0], a = await oe(e, r, n.id);
342
+ let i = (await U(e, o, S("id", B(n.tenantId, t, n.id)), 1))[0], a = await se(e, r, n.id);
342
343
  return i || await Q(e, r, "joined", n.id, n.id), a;
343
344
  });
344
345
  }),
345
346
  leave: (e, t) => O(async () => {
346
347
  let n = j(e);
347
- return X(n, t, [], (e, t) => ce(e, t, n.id, !1));
348
+ return X(n, t, [], (e, t) => le(e, t, n.id, !1));
348
349
  }),
349
350
  apply: (e, t) => y.gen(function* () {
350
- let r = yield* k(() => {
351
- let r = j(e);
352
- return A(t.requestId), o(t.message, n.maxDescriptionLength, !0), r;
351
+ let n = yield* k(() => {
352
+ let n = j(e);
353
+ return A(t.requestId), g(t.message, f.maxDescriptionLength, !0), n;
353
354
  });
354
- return yield* $(t.message), yield* O(() => X(r, t.guildId, [], async (e, a) => {
355
- if (a.visibility !== "apply") throw v("forbidden");
356
- let o = E("gapp", [
357
- r.tenantId,
355
+ return yield* $(t.message), yield* O(() => X(n, t.guildId, [], async (e, i) => {
356
+ if (i.visibility !== "apply") throw v("forbidden");
357
+ let a = E("gapp", [
358
+ n.tenantId,
358
359
  t.guildId,
359
- r.id,
360
+ n.id,
360
361
  t.requestId
361
- ]), s = (await U(e, u, C("id", o), 1))[0];
362
- if (s) {
363
- if (s.tenantId !== r.tenantId || s.subjectId !== r.id || s.guildId !== t.guildId || s.message !== t.message) throw v("conflict");
364
- return R(s);
362
+ ]), o = (await U(e, r, S("id", a), 1))[0];
363
+ if (o) {
364
+ if (o.tenantId !== n.tenantId || o.subjectId !== n.id || o.guildId !== t.guildId || o.message !== t.message) throw v("conflict");
365
+ return R(o);
365
366
  }
366
- let c = Z(await U(e, u, S(C("tenantId", r.tenantId), C("guildId", t.guildId), C("state", "pending"))), r.tenantId, t.guildId);
367
- if (c.some((e) => e.subjectId === r.id)) throw v("conflict");
368
- if (c.length >= n.maxPending) throw v("pending_limit");
369
- return R(await e.insert(u.tableName, {
370
- id: o,
371
- tenantId: r.tenantId,
367
+ let s = Z(await U(e, r, x(S("tenantId", n.tenantId), S("guildId", t.guildId), S("state", "pending"))), n.tenantId, t.guildId);
368
+ if (s.some((e) => e.subjectId === n.id)) throw v("conflict");
369
+ if (s.length >= f.maxPending) throw v("pending_limit");
370
+ return R(await e.insert(r.tableName, {
371
+ id: a,
372
+ tenantId: n.tenantId,
372
373
  guildId: t.guildId,
373
- subjectId: r.id,
374
+ subjectId: n.id,
374
375
  message: t.message,
375
376
  state: "pending",
376
377
  decidedBy: null,
377
378
  decidedAt: null,
378
- createdAt: new Date(i()),
379
+ createdAt: new Date(m()),
379
380
  metadata: {}
380
381
  }));
381
382
  }));
382
383
  }),
383
- applications: (e, n) => O(async () => {
384
- let r = j(e);
385
- return await G(t, r.tenantId, n), await J(t, r, n, "invite"), Z(await U(t, u, S(C("tenantId", r.tenantId), C("guildId", n))), r.tenantId, n).map(R);
384
+ applications: (e, t) => O(async () => {
385
+ let n = j(e);
386
+ return await G(d, n.tenantId, t), await J(d, n, t, "invite"), Z(await U(d, r, x(S("tenantId", n.tenantId), S("guildId", t))), n.tenantId, t).map(R);
386
387
  }),
387
- decideApplication: (e, n) => O(async () => {
388
- let r = j(e);
389
- if (typeof n.accept != "boolean") throw v("invalid_input");
390
- let a = await W(t, u, r.tenantId, n.applicationId);
391
- return X(r, M(a, "guildId"), [M(a, "subjectId")], async (e, t) => {
392
- await J(e, r, M(t, "id"), "invite");
393
- let o = await W(e, u, r.tenantId, n.applicationId);
394
- if (o.guildId !== t.id || o.subjectId !== a.subjectId) throw v("unavailable");
395
- let s = n.accept ? "accepted" : "declined";
388
+ decideApplication: (e, t) => O(async () => {
389
+ let n = j(e);
390
+ if (typeof t.accept != "boolean") throw v("invalid_input");
391
+ let i = await W(d, r, n.tenantId, t.applicationId);
392
+ return X(n, M(i, "guildId"), [M(i, "subjectId")], async (e, a) => {
393
+ await J(e, n, M(a, "id"), "invite");
394
+ let o = await W(e, r, n.tenantId, t.applicationId);
395
+ if (o.guildId !== a.id || o.subjectId !== i.subjectId) throw v("unavailable");
396
+ let s = t.accept ? "accepted" : "declined";
396
397
  if (o.state !== "pending") {
397
398
  if (o.state !== s) throw v("conflict");
398
399
  return R(o);
399
400
  }
400
- n.accept && await oe(e, t, M(o, "subjectId"));
401
- let c = await e.update(u.tableName, n.applicationId, {
401
+ t.accept && await se(e, a, M(o, "subjectId"));
402
+ let c = await e.update(r.tableName, t.applicationId, {
402
403
  state: s,
403
- decidedBy: r.id,
404
- decidedAt: new Date(i())
404
+ decidedBy: n.id,
405
+ decidedAt: new Date(m())
405
406
  });
406
407
  if (!c) throw v("unavailable");
407
- return await Q(e, t, "applicationDecided", r.id, M(o, "subjectId"), { accept: n.accept }, n.applicationId), R(c);
408
+ return await Q(e, a, "applicationDecided", n.id, M(o, "subjectId"), { accept: t.accept }, t.applicationId), R(c);
408
409
  });
409
410
  }),
410
411
  invite: (e, t) => y.gen(function* () {
411
- let r = yield* k(() => {
412
+ let n = yield* k(() => {
412
413
  let n = j(e);
413
414
  if (A(t.subjectId), A(t.requestId), n.id === t.subjectId) throw v("invalid_input");
414
415
  return n;
415
- }), a = yield* y.serviceOption(ie);
416
- return a._tag === "Some" && !(yield* a.value.canInvite(r, t.subjectId).pipe(y.mapError(() => v("unavailable")))) ? yield* y.fail(v("blocked")) : yield* O(() => X(r, t.guildId, [t.subjectId], async (e, a) => {
417
- await J(e, r, t.guildId, "invite");
418
- let o = E("ginv", [
419
- r.tenantId,
416
+ }), r = yield* y.serviceOption(ae);
417
+ return r._tag === "Some" && !(yield* r.value.canInvite(n, t.subjectId).pipe(y.mapError(() => v("unavailable")))) ? yield* y.fail(v("blocked")) : yield* O(() => X(n, t.guildId, [t.subjectId], async (e, r) => {
418
+ await J(e, n, t.guildId, "invite");
419
+ let a = E("ginv", [
420
+ n.tenantId,
420
421
  t.guildId,
421
- r.id,
422
+ n.id,
422
423
  t.subjectId,
423
424
  t.requestId
424
- ]), s = (await U(e, d, C("id", o), 1))[0];
425
- if (s) {
426
- if (s.tenantId !== r.tenantId || s.guildId !== t.guildId || s.toId !== t.subjectId || s.fromId !== r.id) throw v("conflict");
427
- return z(s);
425
+ ]), o = (await U(e, i, S("id", a), 1))[0];
426
+ if (o) {
427
+ if (o.tenantId !== n.tenantId || o.guildId !== t.guildId || o.toId !== t.subjectId || o.fromId !== n.id) throw v("conflict");
428
+ return z(o);
428
429
  }
429
- let c = Z(await U(e, d, S(C("tenantId", r.tenantId), C("guildId", t.guildId), C("state", "pending"))), r.tenantId, t.guildId).filter((e) => N(e.expiresAt) > i());
430
- if (c.some((e) => e.toId === t.subjectId)) throw v("conflict");
431
- if (c.length >= n.maxPending) throw v("pending_limit");
432
- return z(await e.insert(d.tableName, {
433
- id: o,
434
- tenantId: r.tenantId,
430
+ let s = Z(await U(e, i, x(S("tenantId", n.tenantId), S("guildId", t.guildId), S("state", "pending"))), n.tenantId, t.guildId).filter((e) => N(e.expiresAt) > m());
431
+ if (s.some((e) => e.toId === t.subjectId)) throw v("conflict");
432
+ if (s.length >= f.maxPending) throw v("pending_limit");
433
+ return z(await e.insert(i.tableName, {
434
+ id: a,
435
+ tenantId: n.tenantId,
435
436
  guildId: t.guildId,
436
437
  toId: t.subjectId,
437
- fromId: r.id,
438
- expiresAt: new Date(i() + n.inviteTtlMs),
438
+ fromId: n.id,
439
+ expiresAt: new Date(m() + f.inviteTtlMs),
439
440
  state: "pending",
440
- createdAt: new Date(i()),
441
+ createdAt: new Date(m()),
441
442
  metadata: {}
442
443
  }));
443
444
  }));
444
445
  }),
445
446
  invites: (e) => O(async () => {
446
- let n = j(e), r = Z(await U(t, d, S(C("tenantId", n.tenantId), C("toId", n.id), C("state", "pending"))), n.tenantId);
447
- if (r.some((e) => e.toId !== n.id)) throw v("unavailable");
448
- return r.filter((e) => N(e.expiresAt) > i()).map(z);
447
+ let t = j(e), n = Z(await U(d, i, x(S("tenantId", t.tenantId), S("toId", t.id), S("state", "pending"))), t.tenantId);
448
+ if (n.some((e) => e.toId !== t.id)) throw v("unavailable");
449
+ return n.filter((e) => N(e.expiresAt) > m()).map(z);
449
450
  }),
450
- decideInvite: (e, r) => y.gen(function* () {
451
- let a = yield* k(() => {
452
- let t = j(e);
453
- if (typeof r.accept != "boolean") throw v("invalid_input");
454
- return t;
455
- }), o = yield* O(() => W(t, d, a.tenantId, r.inviteId));
456
- if (o.toId !== a.id) return yield* y.fail(v("forbidden"));
457
- let s = yield* y.serviceOption(ie);
458
- return r.accept && s._tag === "Some" && (yield* s.value.isBlocked(a, M(o, "fromId")).pipe(y.mapError(() => v("unavailable")))) ? yield* y.fail(v("blocked")) : yield* O(() => X(a, M(o, "guildId"), [], async (e, t) => {
459
- let o = await W(e, d, a.tenantId, r.inviteId);
460
- if (o.toId !== a.id || o.guildId !== t.id) throw v("forbidden");
461
- let s = r.accept ? "accepted" : "declined";
462
- if (o.state !== "pending") {
463
- if (o.state !== s) throw v("conflict");
464
- return z(o);
451
+ decideInvite: (e, t) => y.gen(function* () {
452
+ let n = yield* k(() => {
453
+ let n = j(e);
454
+ if (typeof t.accept != "boolean") throw v("invalid_input");
455
+ return n;
456
+ }), r = yield* O(() => W(d, i, n.tenantId, t.inviteId));
457
+ if (r.toId !== n.id) return yield* y.fail(v("forbidden"));
458
+ let a = yield* y.serviceOption(ae);
459
+ return t.accept && a._tag === "Some" && (yield* a.value.isBlocked(n, M(r, "fromId")).pipe(y.mapError(() => v("unavailable")))) ? yield* y.fail(v("blocked")) : yield* O(() => X(n, M(r, "guildId"), [], async (e, r) => {
460
+ let a = await W(e, i, n.tenantId, t.inviteId);
461
+ if (a.toId !== n.id || a.guildId !== r.id) throw v("forbidden");
462
+ let o = t.accept ? "accepted" : "declined";
463
+ if (a.state !== "pending") {
464
+ if (a.state !== o) throw v("conflict");
465
+ return z(a);
465
466
  }
466
- if (N(o.expiresAt) <= i()) throw v("expired");
467
- let c = await K(e, a.tenantId, M(t, "id"), M(o, "fromId")), l = await q(e, a.tenantId, M(t, "id"), c);
468
- if (r.accept && n.strategies.permissions({
469
- role: l,
467
+ if (N(a.expiresAt) <= m()) throw v("expired");
468
+ let s = await K(e, n.tenantId, M(r, "id"), M(a, "fromId")), c = await q(e, n.tenantId, M(r, "id"), s);
469
+ if (t.accept && f.strategies.permissions({
470
+ role: c,
470
471
  permission: "invite"
471
472
  }) !== !0) throw v("forbidden");
472
- r.accept && await oe(e, t, a.id);
473
- let u = await e.update(d.tableName, r.inviteId, { state: s });
474
- if (!u) throw v("unavailable");
475
- return await Q(e, t, "inviteDecided", a.id, a.id, { accept: r.accept }, r.inviteId), z(u);
473
+ t.accept && await se(e, r, n.id);
474
+ let l = await e.update(i.tableName, t.inviteId, { state: o });
475
+ if (!l) throw v("unavailable");
476
+ return await Q(e, r, "inviteDecided", n.id, n.id, { accept: t.accept }, t.inviteId), z(l);
476
477
  }));
477
478
  }),
478
479
  kick: (e, t, n) => O(async () => {
479
480
  let r = j(e);
480
481
  return A(n), X(r, t, [n], async (e, i) => {
481
- let a = await J(e, r, t, "kick"), o = await K(e, r.tenantId, t, n), s = await q(e, r.tenantId, t, o);
482
- if (n === r.id || i.ownerId === n || s.rank <= a.role.rank) throw v("forbidden");
483
- await e.delete(p.tableName, M(o, "id")), await Q(e, i, "kicked", r.id, n);
482
+ let a = await J(e, r, t, "kick"), s = await K(e, r.tenantId, t, n), c = await q(e, r.tenantId, t, s);
483
+ if (n === r.id || i.ownerId === n || c.rank <= a.role.rank) throw v("forbidden");
484
+ await e.delete(o.tableName, M(s, "id")), await Q(e, i, "kicked", r.id, n);
484
485
  });
485
486
  }),
486
487
  setRole: (e, t, n, r) => O(async () => {
487
488
  let i = j(e);
488
489
  return A(n), X(i, t, [n], async (e, a) => {
489
- let o = await J(e, i, t, "promote"), s = await K(e, i.tenantId, t, n), c = await q(e, i.tenantId, t, s), l = await W(e, m, i.tenantId, r);
490
- if (n === i.id || a.ownerId === n || l.guildId !== t || r === V(t, "owner") || c.rank <= o.role.rank || Number(l.rank) <= o.role.rank) throw v("forbidden");
491
- let u = await e.update(p.tableName, M(s, "id"), { roleId: r });
492
- if (!u) throw v("unavailable");
493
- return await Q(e, a, "roleChanged", i.id, n, { roleId: r }), L(u);
490
+ let c = await J(e, i, t, "promote"), l = await K(e, i.tenantId, t, n), u = await q(e, i.tenantId, t, l), d = await W(e, s, i.tenantId, r);
491
+ if (n === i.id || a.ownerId === n || d.guildId !== t || r === V(t, "owner") || u.rank <= c.role.rank || Number(d.rank) <= c.role.rank) throw v("forbidden");
492
+ let f = await e.update(o.tableName, M(l, "id"), { roleId: r });
493
+ if (!f) throw v("unavailable");
494
+ return await Q(e, a, "roleChanged", i.id, n, { roleId: r }), L(f);
494
495
  });
495
496
  }),
496
497
  configureRole: (e, t) => O(async () => {
497
- let r = j(e);
498
- if (o(t.name, 64), A(t.roleId), t.roleId.length > 64 || !Number.isSafeInteger(t.rank) || t.rank < 1 || t.rank > 1e4 || !Number.isSafeInteger(t.permissions) || t.permissions < 0 || t.permissions > 127) throw v("invalid_input");
499
- return X(r, t.guildId, [], async (e, i) => {
500
- if (await J(e, r, t.guildId, "promote"), i.ownerId !== r.id || t.roleId === V(t.guildId, "owner")) throw v("forbidden");
501
- let o = (await U(e, m, C("id", t.roleId), 1))[0];
502
- if (o && (o.tenantId !== r.tenantId || o.guildId !== t.guildId)) throw v("forbidden");
503
- let s = Z(await U(e, m, S(C("tenantId", r.tenantId), C("guildId", t.guildId))), r.tenantId, t.guildId);
504
- if (!o && s.length >= n.maxRoles) throw v("member_limit");
505
- let c = {
498
+ let n = j(e);
499
+ if (g(t.name, 64), A(t.roleId), t.roleId.length > 64 || !Number.isSafeInteger(t.rank) || t.rank < 1 || t.rank > 1e4 || !Number.isSafeInteger(t.permissions) || t.permissions < 0 || t.permissions > 127) throw v("invalid_input");
500
+ return X(n, t.guildId, [], async (e, r) => {
501
+ if (await J(e, n, t.guildId, "promote"), r.ownerId !== n.id || t.roleId === V(t.guildId, "owner")) throw v("forbidden");
502
+ let i = (await U(e, s, S("id", t.roleId), 1))[0];
503
+ if (i && (i.tenantId !== n.tenantId || i.guildId !== t.guildId)) throw v("forbidden");
504
+ let a = Z(await U(e, s, x(S("tenantId", n.tenantId), S("guildId", t.guildId))), n.tenantId, t.guildId);
505
+ if (!i && a.length >= f.maxRoles) throw v("member_limit");
506
+ let o = {
506
507
  name: t.name,
507
508
  rank: t.rank,
508
509
  permissions: t.permissions
509
- }, l = o ? await e.update(m.tableName, t.roleId, c) : await e.insert(m.tableName, {
510
+ }, c = i ? await e.update(s.tableName, t.roleId, o) : await e.insert(s.tableName, {
510
511
  id: t.roleId,
511
- tenantId: r.tenantId,
512
+ tenantId: n.tenantId,
512
513
  guildId: t.guildId,
513
- ...c,
514
+ ...o,
514
515
  metadata: {}
515
516
  });
516
- if (!l) throw v("unavailable");
517
- return await Q(e, i, "roleConfigured", r.id, null, { roleId: t.roleId }), F(a, l);
517
+ if (!c) throw v("unavailable");
518
+ return await Q(e, r, "roleConfigured", n.id, null, { roleId: t.roleId }), F(h, c);
518
519
  });
519
520
  }),
520
521
  transferOwner: (e, t, n) => O(async () => {
521
522
  let r = j(e);
522
523
  return A(n), X(r, t, [n], async (e, t) => {
523
524
  if (t.ownerId !== r.id) throw v("forbidden");
524
- return se(e, t, n);
525
+ return ce(e, t, n);
525
526
  });
526
527
  }),
527
528
  updateProfile: (e, t) => y.gen(function* () {
528
- let r = yield* k(() => {
529
- let r = j(e);
529
+ let n = yield* k(() => {
530
+ let n = j(e);
530
531
  if (!Number.isSafeInteger(t.baseVersion) || t.baseVersion < 0 || t.visibility !== void 0 && ![
531
532
  "open",
532
533
  "apply",
533
534
  "invite"
534
535
  ].includes(t.visibility)) throw v("invalid_input");
535
- return t.description !== void 0 && o(t.description, n.maxDescriptionLength, !0), D(t.region), D(t.language), r;
536
+ return t.description !== void 0 && g(t.description, f.maxDescriptionLength, !0), D(t.region), D(t.language), n;
536
537
  });
537
- return t.description !== void 0 && (yield* $(t.description)), yield* O(() => X(r, t.guildId, [], async (e, n) => {
538
- if (await J(e, r, t.guildId, "editProfile"), n.version !== t.baseVersion) throw v("conflict");
539
- let i = await e.update(g.tableName, t.guildId, {
540
- description: t.description ?? n.description,
541
- visibility: t.visibility ?? n.visibility,
542
- region: t.region === void 0 ? n.region : D(t.region),
543
- language: t.language === void 0 ? n.language : D(t.language),
538
+ return t.description !== void 0 && (yield* $(t.description)), yield* O(() => X(n, t.guildId, [], async (e, r) => {
539
+ if (await J(e, n, t.guildId, "editProfile"), r.version !== t.baseVersion) throw v("conflict");
540
+ let i = await e.update(l.tableName, t.guildId, {
541
+ description: t.description ?? r.description,
542
+ visibility: t.visibility ?? r.visibility,
543
+ region: t.region === void 0 ? r.region : D(t.region),
544
+ language: t.language === void 0 ? r.language : D(t.language),
544
545
  version: t.baseVersion + 1
545
546
  });
546
547
  if (!i) throw v("unavailable");
547
- return await Q(e, n, "profileChanged", r.id, null), I(i);
548
+ return await Q(e, r, "profileChanged", n.id, null), I(i);
548
549
  }));
549
550
  }),
550
- announce: (t, r) => y.gen(function* () {
551
- let a = yield* k(() => {
552
- let e = j(t);
553
- if (A(r.requestId), o(r.body, n.maxAnnouncementLength), r.pinned !== void 0 && typeof r.pinned != "boolean") throw v("invalid_input");
554
- return e;
551
+ announce: (e, t) => y.gen(function* () {
552
+ let r = yield* k(() => {
553
+ let n = j(e);
554
+ if (A(t.requestId), g(t.body, f.maxAnnouncementLength), t.pinned !== void 0 && typeof t.pinned != "boolean") throw v("invalid_input");
555
+ return n;
555
556
  });
556
- return yield* $(r.body), yield* O(() => X(a, r.guildId, [], async (t, n) => {
557
- await J(t, a, r.guildId, "announce");
558
- let o = E("gann", [
559
- a.tenantId,
560
- r.guildId,
561
- a.id,
562
- r.requestId
563
- ]), s = (await U(t, l, C("id", o), 1))[0];
564
- if (s) {
565
- if (s.tenantId !== a.tenantId || s.guildId !== r.guildId || s.authorId !== a.id || s.body !== r.body || P(s.pinned) !== (r.pinned ?? !1)) throw v("conflict");
566
- return F(e, s, ["createdAt"], ["pinned"]);
557
+ return yield* $(t.body), yield* O(() => X(r, t.guildId, [], async (e, i) => {
558
+ await J(e, r, t.guildId, "announce");
559
+ let a = E("gann", [
560
+ r.tenantId,
561
+ t.guildId,
562
+ r.id,
563
+ t.requestId
564
+ ]), o = (await U(e, n, S("id", a), 1))[0];
565
+ if (o) {
566
+ if (o.tenantId !== r.tenantId || o.guildId !== t.guildId || o.authorId !== r.id || o.body !== t.body || P(o.pinned) !== (t.pinned ?? !1)) throw v("conflict");
567
+ return F(u, o, ["createdAt"], ["pinned"]);
567
568
  }
568
- let c = await t.insert(l.tableName, {
569
- id: o,
570
- tenantId: a.tenantId,
571
- guildId: r.guildId,
572
- authorId: a.id,
573
- body: r.body,
574
- pinned: r.pinned ?? !1,
575
- createdAt: new Date(i()),
569
+ let s = await e.insert(n.tableName, {
570
+ id: a,
571
+ tenantId: r.tenantId,
572
+ guildId: t.guildId,
573
+ authorId: r.id,
574
+ body: t.body,
575
+ pinned: t.pinned ?? !1,
576
+ createdAt: new Date(m()),
576
577
  metadata: {}
577
578
  });
578
- return await Q(t, n, "announced", a.id, null, { announcementId: o }, r.requestId), F(e, c, ["createdAt"], ["pinned"]);
579
+ return await Q(e, i, "announced", r.id, null, { announcementId: a }, t.requestId), F(u, s, ["createdAt"], ["pinned"]);
579
580
  }));
580
581
  }),
581
- announcements: (r, i) => O(async () => {
582
- let a = j(r);
583
- return await G(t, a.tenantId, i), await K(t, a.tenantId, i, a.id), Z(await U(t, l, S(C("tenantId", a.tenantId), C("guildId", i)), n.pageSize), a.tenantId, i).map((t) => F(e, t, ["createdAt"], ["pinned"]));
582
+ announcements: (e, t) => O(async () => {
583
+ let r = j(e);
584
+ return await G(d, r.tenantId, t), await K(d, r.tenantId, t, r.id), Z(await U(d, n, x(S("tenantId", r.tenantId), S("guildId", t)), f.pageSize), r.tenantId, t).map((e) => F(u, e, ["createdAt"], ["pinned"]));
584
585
  }),
585
- log: (e, i) => O(async () => {
586
- let a = j(e);
587
- return await G(t, a.tenantId, i), await K(t, a.tenantId, i, a.id), Z(await U(t, f, S(C("tenantId", a.tenantId), C("guildId", i)), n.pageSize), a.tenantId, i).map((e) => F(r, e, ["at"]));
586
+ log: (e, t) => O(async () => {
587
+ let n = j(e);
588
+ return await G(d, n.tenantId, t), await K(d, n.tenantId, t, n.id), Z(await U(d, a, x(S("tenantId", n.tenantId), S("guildId", t)), f.pageSize), n.tenantId, t).map((e) => F(p, e, ["at"]));
588
589
  })
589
- }, ue = (e, t, n) => {
590
+ }, de = (e, t, n) => {
590
591
  if (A(t), e.type !== "system" && !(e.type === "serviceAccount" && e.tenantId === t && e.scopes?.includes(n))) throw v("identity_required");
591
- }, de = async (e, t) => {
592
+ }, fe = async (e, t) => {
592
593
  if (t.tenantId === null) throw v("identity_required");
593
594
  A(t.tenantId), A(t.subjectId);
594
- let r = [
595
- C("ownerId", t.subjectId),
596
- C("subjectId", t.subjectId),
597
- C("id", ""),
598
- w(C("subjectId", t.subjectId), C("decidedBy", t.subjectId)),
599
- w(C("toId", t.subjectId), C("fromId", t.subjectId)),
600
- w(C("actorId", t.subjectId), C("targetId", t.subjectId)),
601
- C("authorId", t.subjectId),
602
- C("id", H(t.tenantId, t.subjectId))
603
- ], i = [
595
+ let n = [
596
+ S("ownerId", t.subjectId),
597
+ S("subjectId", t.subjectId),
598
+ S("id", ""),
599
+ C(S("subjectId", t.subjectId), S("decidedBy", t.subjectId)),
600
+ C(S("toId", t.subjectId), S("fromId", t.subjectId)),
601
+ C(S("actorId", t.subjectId), S("targetId", t.subjectId)),
602
+ S("authorId", t.subjectId),
603
+ S("id", H(t.tenantId, t.subjectId))
604
+ ], r = [
604
605
  (e) => e.ownerId === t.subjectId,
605
606
  (e) => e.subjectId === t.subjectId,
606
607
  (e) => !1,
@@ -609,119 +610,175 @@ var T = (e) => x("sha256").update(JSON.stringify(e)).digest("hex"), E = (e, t) =
609
610
  (e) => e.actorId === t.subjectId || e.targetId === t.subjectId,
610
611
  (e) => e.authorId === t.subjectId,
611
612
  (e) => e.id === H(t.tenantId, t.subjectId)
612
- ], a = {}, o = 0;
613
- for (let n = 0; n < h.length; n++) {
614
- let s = h[n], c = Z(await U(e, s, S(C("tenantId", t.tenantId), r[n])), t.tenantId).filter(i[n]);
615
- o += c.length, a[s.tableName] = c;
613
+ ], i = {}, a = 0;
614
+ for (let o = 0; o < c.length; o++) {
615
+ let s = c[o], l = Z(await U(e, s, x(S("tenantId", t.tenantId), n[o])), t.tenantId).filter(r[o]);
616
+ a += l.length, i[s.tableName] = l;
616
617
  }
617
- for (let n of a[g.tableName] ?? []) {
618
- let r = Z(await U(e, m, S(C("tenantId", t.tenantId), C("guildId", n.id))), t.tenantId, M(n, "id"));
619
- a[m.tableName] = [...a[m.tableName] ?? [], ...r], o += r.length;
618
+ for (let n of i[l.tableName] ?? []) {
619
+ let r = Z(await U(e, s, x(S("tenantId", t.tenantId), S("guildId", n.id))), t.tenantId, M(n, "id"));
620
+ i[s.tableName] = [...i[s.tableName] ?? [], ...r], a += r.length;
620
621
  }
621
- if (o > n.maxExportRows) throw v("export_limit");
622
- return a;
622
+ if (a > f.maxExportRows) throw v("export_limit");
623
+ return i;
623
624
  };
624
625
  return {
625
- service: le,
626
+ service: ue,
626
627
  recordContribution: (e, t) => O(async () => {
627
- if (ue(e, t.tenantId, "guilds:contributions:write"), A(t.subjectId), A(t.requestId), !Number.isSafeInteger(t.xp) || t.xp < 1 || t.xp > 1e6) throw v("invalid_input");
628
- let r = {
628
+ if (de(e, t.tenantId, "guilds:contributions:write"), A(t.subjectId), A(t.requestId), !Number.isSafeInteger(t.xp) || t.xp < 1 || t.xp > 1e6) throw v("invalid_input");
629
+ let n = {
629
630
  type: "user",
630
631
  id: t.subjectId,
631
632
  tenantId: t.tenantId
632
633
  };
633
- return X(r, t.guildId, [], async (e, r) => {
634
- let a = E("glog", [
634
+ return X(n, t.guildId, [], async (e, n) => {
635
+ let r = E("glog", [
635
636
  t.tenantId,
636
637
  t.guildId,
637
638
  "contribution",
638
639
  t.requestId
639
- ]), o = (await U(e, f, C("id", a), 1))[0];
640
- if (o) {
641
- let e = o.detail;
642
- if (o.tenantId !== t.tenantId || o.guildId !== t.guildId || o.targetId !== t.subjectId || e.xp !== t.xp) throw v("conflict");
643
- return I(r);
640
+ ]), i = (await U(e, a, S("id", r), 1))[0];
641
+ if (i) {
642
+ let e = i.detail;
643
+ if (i.tenantId !== t.tenantId || i.guildId !== t.guildId || i.targetId !== t.subjectId || e.xp !== t.xp) throw v("conflict");
644
+ return I(n);
644
645
  }
645
- let s = await K(e, t.tenantId, t.guildId, t.subjectId), c = Number(r.xp) + t.xp, l = Number(s.contribution.xp ?? 0) + t.xp;
646
- if (!Number.isSafeInteger(c) || c > 2e9 || !Number.isSafeInteger(l) || l > 2e9) throw v("invalid_input");
647
- let u = n.strategies.levelCurve({
646
+ let s = await K(e, t.tenantId, t.guildId, t.subjectId), c = Number(n.xp) + t.xp, u = Number(s.contribution.xp ?? 0) + t.xp;
647
+ if (!Number.isSafeInteger(c) || c > 2e9 || !Number.isSafeInteger(u) || u > 2e9) throw v("invalid_input");
648
+ let d = f.strategies.levelCurve({
648
649
  xp: c,
649
- baseMemberCap: n.baseMemberCap,
650
- maximumMemberCap: n.maximumMemberCap
650
+ baseMemberCap: f.baseMemberCap,
651
+ maximumMemberCap: f.maximumMemberCap
651
652
  });
652
- if (!Number.isSafeInteger(u.level) || u.level < 1 || u.level > 1e6 || !Number.isSafeInteger(u.memberCap) || u.memberCap < n.baseMemberCap || u.memberCap > n.maximumMemberCap) throw v("unavailable");
653
- let d = await e.update(g.tableName, t.guildId, {
653
+ if (!Number.isSafeInteger(d.level) || d.level < 1 || d.level > 1e6 || !Number.isSafeInteger(d.memberCap) || d.memberCap < f.baseMemberCap || d.memberCap > f.maximumMemberCap) throw v("unavailable");
654
+ let p = await e.update(l.tableName, t.guildId, {
654
655
  xp: c,
655
- level: u.level,
656
- memberCap: u.memberCap,
657
- version: Number(r.version) + 1
656
+ level: d.level,
657
+ memberCap: d.memberCap,
658
+ version: Number(n.version) + 1
658
659
  });
659
- if (!d) throw v("unavailable");
660
- return await e.update(p.tableName, M(s, "id"), {
660
+ if (!p) throw v("unavailable");
661
+ return await e.update(o.tableName, M(s, "id"), {
661
662
  contribution: {
662
663
  ...s.contribution,
663
- xp: l
664
+ xp: u
664
665
  },
665
- lastActiveAt: new Date(i())
666
- }), await Q(e, r, "contribution", null, t.subjectId, { xp: t.xp }, t.requestId), I(d);
666
+ lastActiveAt: new Date(m())
667
+ }), await Q(e, n, "contribution", null, t.subjectId, { xp: t.xp }, t.requestId), I(p);
667
668
  });
668
669
  }),
669
- inactivityProposal: (e, r) => O(async () => {
670
- ue(e, r.tenantId, "guilds:inactivity:read");
671
- let a = await G(t, r.tenantId, r.guildId), o = await K(t, r.tenantId, r.guildId, M(a, "ownerId")), s = [], c = Z(await U(t, p, S(C("tenantId", r.tenantId), C("guildId", r.guildId))), r.tenantId, r.guildId);
672
- for (let e of c) e.subjectId !== a.ownerId && s.push({
670
+ inactivityProposal: (e, t) => O(async () => {
671
+ de(e, t.tenantId, "guilds:inactivity:read");
672
+ let n = await G(d, t.tenantId, t.guildId), r = await K(d, t.tenantId, t.guildId, M(n, "ownerId")), i = [], a = Z(await U(d, o, x(S("tenantId", t.tenantId), S("guildId", t.guildId))), t.tenantId, t.guildId);
673
+ for (let e of a) e.subjectId !== n.ownerId && i.push({
673
674
  subjectId: M(e, "subjectId"),
674
- rank: (await q(t, r.tenantId, r.guildId, e)).rank,
675
+ rank: (await q(d, t.tenantId, t.guildId, e)).rank,
675
676
  lastActiveAt: N(e.lastActiveAt),
676
677
  joinedAt: N(e.joinedAt)
677
678
  });
678
- let l = n.strategies.inactivityPolicy({
679
- now: i(),
680
- inactiveAfterMs: n.inactiveAfterMs,
681
- ownerLastActiveAt: N(o.lastActiveAt),
682
- members: s
679
+ let s = f.strategies.inactivityPolicy({
680
+ now: m(),
681
+ inactiveAfterMs: f.inactiveAfterMs,
682
+ ownerLastActiveAt: N(r.lastActiveAt),
683
+ members: i
684
+ });
685
+ if (s !== null && !i.some((e) => e.subjectId === s)) throw v("unavailable");
686
+ return s;
687
+ }),
688
+ exportSubject: (e) => O(() => d.transactional(async (t) => JSON.parse(JSON.stringify(await fe(t, e))))),
689
+ mergeSubject: (e) => O(async () => {
690
+ let s = e.store, c = e.tenantId, u = e.sourceId, d = e.targetId;
691
+ if (A(c), A(u), A(d), u === d) throw v("invalid_input");
692
+ for (let e of [u, d]) await s.insertIgnore(t.tableName, {
693
+ id: H(c, e),
694
+ tenantId: c,
695
+ subjectId: e,
696
+ admission: 0,
697
+ erased: !1,
698
+ metadata: {}
699
+ }, { conflictColumns: ["id"] });
700
+ let p = await fe(s, {
701
+ tenantId: c,
702
+ subjectId: u,
703
+ kind: "guest"
704
+ }), m = [...p[o.tableName] ?? []].sort((e, t) => M(e, "guildId") < M(t, "guildId") ? -1 : 1);
705
+ await Y(s, c, [u, d], !1, async () => {
706
+ for (let e of m) if (await s.updateMany(l.tableName, { admission: 0 }, { where: S("id", M(e, "guildId")) }) !== 1) throw v("unavailable");
707
+ });
708
+ let h = new Set((await U(s, o, x(S("tenantId", c), S("subjectId", d)))).map((e) => M(e, "guildId")));
709
+ if (h.size + m.filter((e) => !h.has(M(e, "guildId"))).length > f.maxGuildsPerSubject) throw v("guild_limit");
710
+ for (let e of m) {
711
+ let t = M(e, "guildId"), n = await W(s, l, c, t);
712
+ if (await s.delete(o.tableName, M(e, "id")), h.has(t)) {
713
+ if (n.ownerId === u) {
714
+ let r = (await U(s, o, S("id", B(c, t, d)), 1))[0];
715
+ await s.update(l.tableName, t, {
716
+ ownerId: d,
717
+ version: Number(n.version) + 1
718
+ }), r && await s.update(o.tableName, M(r, "id"), { roleId: e.roleId });
719
+ }
720
+ } else await s.insert(o.tableName, {
721
+ ...e,
722
+ id: B(c, t, d),
723
+ subjectId: d
724
+ }), h.add(t), n.ownerId === u && await s.update(l.tableName, t, {
725
+ ownerId: d,
726
+ version: Number(n.version) + 1
727
+ });
728
+ }
729
+ for (let e of p[r.tableName] ?? []) e.subjectId === u && e.state === "pending" ? await s.delete(r.tableName, M(e, "id")) : await s.update(r.tableName, M(e, "id"), {
730
+ subjectId: e.subjectId === u ? d : e.subjectId,
731
+ decidedBy: e.decidedBy === u ? d : e.decidedBy
732
+ });
733
+ for (let e of p[i.tableName] ?? []) await s.delete(i.tableName, M(e, "id"));
734
+ for (let e of p[a.tableName] ?? []) await s.update(a.tableName, M(e, "id"), {
735
+ actorId: e.actorId === u ? d : e.actorId,
736
+ targetId: e.targetId === u ? d : e.targetId
737
+ });
738
+ for (let e of p[n.tableName] ?? []) await s.update(n.tableName, M(e, "id"), { authorId: d });
739
+ await s.update(t.tableName, H(c, u), {
740
+ subjectId: null,
741
+ erased: !0,
742
+ metadata: {}
683
743
  });
684
- if (l !== null && !s.some((e) => e.subjectId === l)) throw v("unavailable");
685
- return l;
686
744
  }),
687
- exportSubject: (e) => O(() => t.transactional(async (t) => JSON.parse(JSON.stringify(await de(t, e))))),
688
745
  eraseSubject: (e) => O(async () => {
689
746
  if (e.tenantId === null) throw v("identity_required");
690
- A(e.tenantId), A(e.subjectId), await Y(e.tenantId, [e.subjectId]), await t.transactional(async (t) => {
691
- await ae(t, e.tenantId, [e.subjectId], !0);
692
- let n = await de(t, e), r = n[p.tableName] ?? [];
693
- for (let n of [...r].sort((e, t) => M(e, "guildId") < M(t, "guildId") ? -1 : 1)) {
694
- let r = M(n, "guildId");
695
- if (await t.updateMany(g.tableName, { admission: 0 }, { where: C("id", r) }) !== 1) throw v("unavailable");
696
- let i = await G(t, e.tenantId, r);
697
- if (await ce(t, i, e.subjectId, !0), i.ownerId === e.subjectId) {
698
- let n = await W(t, g, e.tenantId, r);
699
- await t.update(g.tableName, r, {
700
- name: n.ownerId === null ? "" : "Guild " + T(r).slice(0, 12),
701
- nameKey: T(["privacy", r]),
747
+ A(e.tenantId), A(e.subjectId), await oe(e.tenantId, [e.subjectId]), await d.transactional(async (c) => {
748
+ await Y(c, e.tenantId, [e.subjectId], !0);
749
+ let u = await fe(c, e), d = u[o.tableName] ?? [];
750
+ for (let t of [...d].sort((e, t) => M(e, "guildId") < M(t, "guildId") ? -1 : 1)) {
751
+ let n = M(t, "guildId");
752
+ if (await c.updateMany(l.tableName, { admission: 0 }, { where: S("id", n) }) !== 1) throw v("unavailable");
753
+ let r = await G(c, e.tenantId, n);
754
+ if (await le(c, r, e.subjectId, !0), r.ownerId === e.subjectId) {
755
+ let t = await W(c, l, e.tenantId, n);
756
+ await c.update(l.tableName, n, {
757
+ name: t.ownerId === null ? "" : "Guild " + T(n).slice(0, 12),
758
+ nameKey: T(["privacy", n]),
702
759
  description: "",
703
760
  emblem: null,
704
761
  metadata: {}
705
762
  });
706
763
  }
707
764
  }
708
- for (let r of n[u.tableName] ?? []) r.subjectId === e.subjectId ? await t.delete(u.tableName, M(r, "id")) : await t.update(u.tableName, M(r, "id"), {
765
+ for (let t of u[r.tableName] ?? []) t.subjectId === e.subjectId ? await c.delete(r.tableName, M(t, "id")) : await c.update(r.tableName, M(t, "id"), {
709
766
  decidedBy: null,
710
767
  metadata: {}
711
768
  });
712
- for (let e of n[d.tableName] ?? []) await t.delete(d.tableName, M(e, "id"));
713
- for (let r of n[f.tableName] ?? []) await t.update(f.tableName, M(r, "id"), {
714
- actorId: r.actorId === e.subjectId ? null : r.actorId,
715
- targetId: r.targetId === e.subjectId ? null : r.targetId,
769
+ for (let e of u[i.tableName] ?? []) await c.delete(i.tableName, M(e, "id"));
770
+ for (let t of u[a.tableName] ?? []) await c.update(a.tableName, M(t, "id"), {
771
+ actorId: t.actorId === e.subjectId ? null : t.actorId,
772
+ targetId: t.targetId === e.subjectId ? null : t.targetId,
716
773
  detail: {}
717
774
  });
718
- for (let e of n[l.tableName] ?? []) await t.update(l.tableName, M(e, "id"), {
775
+ for (let e of u[n.tableName] ?? []) await c.update(n.tableName, M(e, "id"), {
719
776
  authorId: null,
720
777
  body: "",
721
778
  metadata: {}
722
779
  });
723
- for (let e of n[m.tableName] ?? []) await t.update(m.tableName, M(e, "id"), { metadata: {} });
724
- await t.update(c.tableName, H(e.tenantId, e.subjectId), {
780
+ for (let e of u[s.tableName] ?? []) await c.update(s.tableName, M(e, "id"), { metadata: {} });
781
+ await c.update(t.tableName, H(e.tenantId, e.subjectId), {
725
782
  subjectId: null,
726
783
  erased: !0,
727
784
  metadata: {}