@segmentstream/ontology 0.153.0 → 0.153.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.153.1
4
+
5
+ An edit that replaces a message's files discards the chat's stored attachments it no longer holds, as a deleted message's are, so a channel that keeps files no longer keeps a replaced file until the chats' retention.
6
+
3
7
  ## 0.153.0
4
8
 
5
9
  An email channel with `record: "all"` takes the mailbox's chats into the inbox. A verified sender's chat that has no member joins the inbox without members when the ontology's `inbox.channels` lists the channel, which may now name an email channel. Mail that failed DMARC never joins. Mail staff send from the mailbox is recorded in each recipient's chat, including a chat the agent has none of yet, when `recordQuery`, if any, finds it.
@@ -265,6 +265,26 @@ export function sqlChatMessageStore(sql) {
265
265
  `AND "message_id" = ${params.add(change.messageId)} ` +
266
266
  `AND "purged_at" IS NULL${guard.map((condition) => ` AND ${condition}`).join("")} ` +
267
267
  `RETURNING "id"`, params.values)).pipe(Effect.map((updated) => ({ applied: updated.length > 0 })));
268
+ /**
269
+ * Discards a chat's stored attachments, by their ids, that a message no longer holds: a channel
270
+ * that keeps files would otherwise keep them for as long as the chat's messages.
271
+ */
272
+ const discard = (ids, channel, chatId) => Effect.gen(function* () {
273
+ if (ids.length === 0) {
274
+ return;
275
+ }
276
+ const [storage] = yield* rows(`SELECT to_regclass('"${ATTACHMENTS_TABLE}"') IS NOT NULL AS "present"`);
277
+ if (storage?.present === true || storage?.present === "t") {
278
+ yield* rows(`UPDATE "${ATTACHMENTS_TABLE}" SET "state" = 'discarded', "expires_at" = now() ` +
279
+ `WHERE "id"::text = ANY($1::text[]) AND "conversation" = $2 ` +
280
+ `AND "state" IN ('received', 'ready')`, [ids, `${channel}:${chatId}`]);
281
+ }
282
+ });
283
+ /** The attachment ids a message's `files` names. */
284
+ const fileIds = (files) => jsonArray(files).flatMap((file) => {
285
+ const id = file.id;
286
+ return typeof id === "string" ? [id] : [];
287
+ });
268
288
  const revised = `"revision" = ${NEXT_REVISION}`;
269
289
  const sent = (change) => inChat(change.channel, change.chatId, Effect.gen(function* () {
270
290
  const [row] = yield* rows(`SELECT "id"::text AS "id", "status" FROM ${TABLE} ` +
@@ -349,18 +369,7 @@ export function sqlChatMessageStore(sql) {
349
369
  if (deleted === undefined) {
350
370
  return { applied: false };
351
371
  }
352
- const files = jsonArray(deleted.files).flatMap((file) => {
353
- const id = file.id;
354
- return typeof id === "string" ? [id] : [];
355
- });
356
- const [storage] = files.length === 0
357
- ? []
358
- : yield* rows(`SELECT to_regclass('"${ATTACHMENTS_TABLE}"') IS NOT NULL AS "present"`);
359
- if (storage?.present === true || storage?.present === "t") {
360
- yield* rows(`UPDATE "${ATTACHMENTS_TABLE}" SET "state" = 'discarded', "expires_at" = now() ` +
361
- `WHERE "id"::text = ANY($1::text[]) AND "conversation" = $2 ` +
362
- `AND "state" IN ('received', 'ready')`, [files, `${input.channel}:${input.chatId}`]);
363
- }
372
+ yield* discard(fileIds(deleted.files), input.channel, input.chatId);
364
373
  return { applied: true };
365
374
  }));
366
375
  const change = (input) => {
@@ -378,20 +387,45 @@ export function sqlChatMessageStore(sql) {
378
387
  : [
379
388
  `coalesce("meta", 'null'::jsonb) = ${params.add(JSON.stringify(input.expected ?? null), "jsonb")}`,
380
389
  ];
381
- // An edit older than the one applied, a late or retried delivery, changes nothing.
382
- return update(input, params, [
390
+ const assignments = [
383
391
  `"text" = ${edited}`,
384
392
  `"files" = ${files}`,
385
393
  `"meta" = ${meta}`,
386
394
  `"edited_at" = ${at}`,
387
395
  revised,
388
- ], [
396
+ ];
397
+ // An edit older than the one applied, a late or retried delivery, changes nothing.
398
+ const guard = [
389
399
  `"deleted_at" IS NULL`,
390
400
  `("text" IS DISTINCT FROM ${edited} OR "files" IS DISTINCT FROM ${files} ` +
391
401
  `OR "meta" IS DISTINCT FROM ${meta})`,
392
402
  `("edited_at" IS NULL OR "edited_at" <= ${at})`,
393
403
  ...guarded,
394
- ]);
404
+ ];
405
+ if (input.files === undefined) {
406
+ return update(input, params, assignments, guard);
407
+ }
408
+ const replaced = input.files;
409
+ const target = [
410
+ `"channel" = ${params.add(input.channel)}`,
411
+ `"chat_id" = ${params.add(input.chatId)}`,
412
+ `"message_id" = ${params.add(input.messageId)}`,
413
+ `"purged_at" IS NULL`,
414
+ ];
415
+ // The files an edit replaced are discarded, as a deleted message's are.
416
+ return inChat(input.channel, input.chatId, Effect.gen(function* () {
417
+ const [applied] = yield* rows(`WITH "before" AS (SELECT "id" AS "before_id", "files" AS "before_files" ` +
418
+ `FROM ${TABLE} WHERE ${target.join(" AND ")} FOR UPDATE) ` +
419
+ `UPDATE ${TABLE} SET ${assignments.join(", ")} FROM "before" ` +
420
+ `WHERE ${TABLE}."id" = "before"."before_id" AND ${guard.join(" AND ")} ` +
421
+ `RETURNING "before"."before_files"::text AS "files"`, params.values);
422
+ if (applied === undefined) {
423
+ return { applied: false };
424
+ }
425
+ const kept = new Set(replaced.map((file) => file.id));
426
+ yield* discard(fileIds(applied.files).filter((id) => !kept.has(id)), input.channel, input.chatId);
427
+ return { applied: true };
428
+ }));
395
429
  }
396
430
  case "delete":
397
431
  return remove(input);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.153.0",
3
+ "version": "0.153.1",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",