@ours.network/messenger-server 2.1.0 → 2.1.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.
@@ -38,7 +38,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
38
38
  var define_MESSENGER_BUILD_INFO_default;
39
39
  var init_define_MESSENGER_BUILD_INFO = __esm({
40
40
  "<define:__MESSENGER_BUILD_INFO__>"() {
41
- define_MESSENGER_BUILD_INFO_default = { name: "@ours.network/messenger-server", version: "2.1.0", sha: "09708dfd76879f6280e19fc0c693d1e59a876541", dirty: false };
41
+ define_MESSENGER_BUILD_INFO_default = { name: "@ours.network/messenger-server", version: "2.1.1", sha: "6ceab869f2c19bf1935e4c82b089d66fb25e658a", dirty: false };
42
42
  }
43
43
  });
44
44
 
@@ -10,7 +10,7 @@ import {
10
10
  publicEngineError,
11
11
  publicInternalError,
12
12
  reportFailure
13
- } from "./chunk-3JYT7LXJ.js";
13
+ } from "./chunk-HTCOS6QG.js";
14
14
 
15
15
  // node_modules/bn.js/lib/bn.js
16
16
  var require_bn = __commonJS({
@@ -11904,6 +11904,7 @@ function statusPush(status) {
11904
11904
  var statusEventId = (status) => `status:${status.status_id}:${status.revision}`;
11905
11905
  var sendEvent = (status) => ({ op: "send", eventId: statusEventId(status), delivery: status.kind === "question" ? "until-resolved" : "inbox", ...statusPush(status) });
11906
11906
  var resolveEvents = (status) => status.kind === "question" && status.state === "active" && !status.answer ? [{ op: "resolve", eventId: statusEventId(status) }] : [];
11907
+ var seenResolves = (status, revision) => status.kind === "question" ? [] : Array.from({ length: Math.max(0, revision - (status.seen_notified ?? 0)) }, (_, index) => ({ op: "resolve", eventId: statusEventId({ status_id: status.status_id, revision: (status.seen_notified ?? 0) + index + 1 }) }));
11907
11908
  var withEvents = (status, events) => events.length ? { ...status, notification_events: [...status.notification_events ?? [], ...events] } : status;
11908
11909
  var content = (r) => JSON.stringify([r.kind, r.title, r.body, r.attachments, r.interaction ?? null, r.supersedes ?? null]);
11909
11910
  var AlreadyAnsweredError = class extends StatusError {
@@ -12178,11 +12179,26 @@ var MessengerStatusStore = class {
12178
12179
  const current = this.require(statusId);
12179
12180
  const awaiting = current.kind === "question" && current.state === "active" && !current.answer;
12180
12181
  if (awaiting || revision !== current.revision || (current.seen_revision ?? 0) >= revision) return current;
12181
- const seen = { ...current, seen_revision: revision };
12182
+ const seen = withEvents({ ...current, seen_revision: revision, seen_notified: revision }, seenResolves(current, revision));
12182
12183
  this.write(seen);
12183
12184
  this.changed();
12184
12185
  return seen;
12185
12186
  }
12187
+ /**
12188
+ * Statuses seen before seeing read their notification keep an unread row: resolve each once by its own event ID.
12189
+ * Idempotent; returns how many statuses it linked.
12190
+ */
12191
+ resolveSeenNotifications() {
12192
+ let linked = 0;
12193
+ for (const current of this.list()) {
12194
+ const seen = current.seen_revision ?? 0;
12195
+ if (current.kind === "question" || seen <= (current.seen_notified ?? 0)) continue;
12196
+ this.write(withEvents({ ...current, seen_notified: seen }, seenResolves(current, seen)));
12197
+ linked++;
12198
+ }
12199
+ if (linked) this.changed();
12200
+ return linked;
12201
+ }
12186
12202
  /** Remove handed-off notification events (after the outbox durably accepted them). */
12187
12203
  clearEvents(statusId, eventKeys) {
12188
12204
  const current = this.get(statusId);
@@ -12224,29 +12240,33 @@ function feedItem(status) {
12224
12240
  };
12225
12241
  }
12226
12242
  function publicStatus(status) {
12227
- const { version: _version, request_key: _key, seen_revision: _seen, notification_events: _events, ...visible } = status;
12243
+ const { version: _version, request_key: _key, seen_revision: _seen, seen_notified: _notified, notification_events: _events, ...visible } = status;
12228
12244
  return { ...visible, seen: statusSeen(status), awaits_owner: statusAwaitsOwner(status) };
12229
12245
  }
12230
12246
  function listFeed(store, scope, options = {}) {
12231
12247
  const limit = options.limit ?? 100;
12232
12248
  if (!Number.isSafeInteger(limit) || limit < 1 || limit > FEED_PAGE_MAX) throw new StatusError("invalid_request", `limit must be 1-${FEED_PAGE_MAX}`);
12233
- const all = store.list().filter((status) => scope.kind === "global" || status.author.identity_cid === scope.identityCid).map(feedItem).sort((a, b2) => a.created_at.localeCompare(b2.created_at) || a.status_id.localeCompare(b2.status_id));
12234
- let start2 = 0;
12249
+ const scoped = store.list().filter((status) => scope.kind === "global" || status.author.identity_cid === scope.identityCid).map(feedItem).sort((a, b2) => a.created_at.localeCompare(b2.created_at) || a.status_id.localeCompare(b2.status_id));
12250
+ let from = 0;
12235
12251
  if (options.after !== void 0) {
12236
- start2 = all.findIndex((item) => item.status_id === options.after) + 1;
12237
- if (!start2) throw new StatusError("invalid_request", "after must be a status ID in this scope");
12252
+ from = scoped.findIndex((item) => item.status_id === options.after) + 1;
12253
+ if (!from) throw new StatusError("invalid_request", "after must be a status ID in this scope");
12238
12254
  }
12239
- const page = all.slice(start2, start2 + limit);
12255
+ const listed = scoped.map((item, position) => ({ item, position })).filter(({ item }) => !options.eligible || !item.seen);
12256
+ const all = listed.map(({ item }) => item);
12257
+ const start2 = listed.findIndex(({ position }) => position >= from);
12258
+ const page = start2 < 0 ? [] : all.slice(start2, start2 + limit);
12240
12259
  const unseen = all.filter((item) => !item.seen);
12241
12260
  return {
12242
12261
  items: page,
12243
- next_after: start2 + page.length < all.length ? page.at(-1).status_id : null,
12262
+ next_after: start2 >= 0 && start2 + page.length < all.length ? page.at(-1).status_id : null,
12244
12263
  total: all.length,
12245
12264
  unseen: unseen.length,
12246
12265
  open_questions: all.filter((item) => item.awaits_owner).map((item) => item.status_id),
12247
12266
  opens_at: unseen[0]?.status_id ?? all.at(-1)?.status_id ?? null,
12248
12267
  caught_up: unseen.length === 0,
12249
- scope_version: createHash("sha256").update(JSON.stringify(all)).digest("hex").slice(0, 32)
12268
+ scope_version: createHash("sha256").update(JSON.stringify(all)).digest("hex").slice(0, 32),
12269
+ ...options.eligible ? { eligible: true } : {}
12250
12270
  };
12251
12271
  }
12252
12272
 
@@ -12346,6 +12366,11 @@ var StatusService = class {
12346
12366
  reason: "the server restarted before the delivery result was known; use Try again if the agent did not receive it"
12347
12367
  });
12348
12368
  }
12369
+ try {
12370
+ this.store.resolveSeenNotifications();
12371
+ } catch (error) {
12372
+ this.warn(`Seen status notifications not linked yet: ${error.message}`);
12373
+ }
12349
12374
  this.flushNotifications();
12350
12375
  await this.ingest();
12351
12376
  }
@@ -22816,14 +22841,16 @@ var ROUTES = {
22816
22841
  "GET /api/contacts/:contact/commands": async ({ client, deps, params }) => projectCommandResource(client, deps, params),
22817
22842
  // ---- external agents' statuses (canonical here; Fleet agents' statuses stay in Fleet) -------------------------
22818
22843
  "GET /api/statuses": async ({ deps, query }) => {
22819
- const extra = [...query.keys()].filter((key) => !["scope", "agent", "after", "limit"].includes(key) || query.getAll(key).length !== 1);
22844
+ const extra = [...query.keys()].filter((key) => !["scope", "agent", "after", "limit", "filter"].includes(key) || query.getAll(key).length !== 1);
22820
22845
  if (extra.length) throw bad(`unexpected query ${extra[0]}`);
22846
+ const filter = query.get("filter");
22847
+ if (filter !== null && filter !== "eligible") throw bad("filter must be eligible");
22821
22848
  try {
22822
22849
  const after = query.get("after") ?? void 0, limit = query.get("limit");
22823
22850
  return listFeed(
22824
22851
  statusService(deps).store,
22825
22852
  feedScope({ scope: query.get("scope") ?? void 0, agent: query.get("agent") ?? void 0 }),
22826
- { ...after === void 0 ? {} : { after }, ...limit === null ? {} : { limit: Number(limit) } }
22853
+ { ...after === void 0 ? {} : { after }, ...limit === null ? {} : { limit: Number(limit) }, ...filter === "eligible" ? { eligible: true } : {} }
22827
22854
  );
22828
22855
  } catch (error) {
22829
22856
  throw statusHttpError(error);
package/dist/cli.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  normalizeBasePath,
8
8
  operatorError,
9
9
  reportFailure
10
- } from "./chunks/chunk-3JYT7LXJ.js";
10
+ } from "./chunks/chunk-HTCOS6QG.js";
11
11
 
12
12
  // src/cli.ts
13
13
  init_define_MESSENGER_BUILD_INFO();
@@ -131,7 +131,7 @@ async function main() {
131
131
  }
132
132
  if (process.argv.length !== 3) throw new ConfigurationError("serve does not accept command-line options");
133
133
  const cfg = loadConfig();
134
- const { start } = await import("./chunks/server-KIFVGKZV.js");
134
+ const { start } = await import("./chunks/server-BQCRSPII.js");
135
135
  const handle = await start(cfg, BUILD_INFO);
136
136
  let closing = false;
137
137
  const shutdown = (signal) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ours.network/messenger-server",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Self-hosted Messenger REST/SSE backend for one identity on a shared Ours daemon.",
5
5
  "type": "module",
6
6
  "license": "FSL-1.1-Apache-2.0",