@oxy.so/federation 1.0.1 → 2.0.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.
@@ -36,6 +36,12 @@ const AP_CONTENT_TYPE = 'application/activity+json';
36
36
  /** Maximum decompressed response sizes accepted from untrusted federation hosts. */
37
37
  const ACTOR_BODY_MAX_BYTES = 1024 * 1024;
38
38
  const COLLECTION_BODY_MAX_BYTES = 64 * 1024;
39
+ /**
40
+ * Collection responses that DEFINITIVELY withhold the count: the owner hid the
41
+ * collection (401/403) or it no longer exists (404/410). Anything else non-2xx
42
+ * is a failed attempt, not an answer.
43
+ */
44
+ const COLLECTION_WITHHELD_STATUSES = new Set([401, 403, 404, 410]);
39
45
  const ERROR_BODY_MAX_BYTES = 4 * 1024;
40
46
  async function readBoundedResponseBody(res, maxBytes) {
41
47
  const contentLength = res.headers.get('content-length');
@@ -393,9 +399,11 @@ class ActorResolver {
393
399
  alsoKnownAs,
394
400
  networkAcct: networkIdentity?.federatedUsername,
395
401
  remoteCreatedAt: typeof actor.published === 'string' ? new Date(actor.published) : undefined,
396
- followersCount,
397
- followingCount,
398
- postsCount,
402
+ // Omitted, not written as `undefined`, when this refresh could not tell:
403
+ // an absent key is what tells the store to keep the value it has.
404
+ ...(followersCount !== undefined && { followersCount }),
405
+ ...(followingCount !== undefined && { followingCount }),
406
+ ...(postsCount !== undefined && { postsCount }),
399
407
  lastFetchedAt: new Date(),
400
408
  };
401
409
  const fedActor = await this.config.store.upsertActor(actorId, update);
@@ -427,9 +435,11 @@ class ActorResolver {
427
435
  // string away made both of those unrepresentable, so the stale text
428
436
  // survived every later refresh with nothing in the logs.
429
437
  bio: identityBio,
430
- followersCount,
431
- followingCount,
432
- postsCount,
438
+ // The identity bridge takes a number or nothing; an unknown count is
439
+ // sent as nothing rather than as a zero it would store.
440
+ followersCount: followersCount ?? undefined,
441
+ followingCount: followingCount ?? undefined,
442
+ postsCount: postsCount ?? undefined,
433
443
  oxyUserId: fedActor.oxyUserId ?? undefined,
434
444
  };
435
445
  const oxyId = await this.config.identity.resolveExternalUser(normalized, { forceAvatarRefresh });
@@ -507,19 +517,36 @@ class ActorResolver {
507
517
  this.config.logger.warn(`[FedSync] failed to tombstone gone actor ${actorUri}:`, err);
508
518
  }
509
519
  }
510
- /** Fetch the totalItems count from an ActivityPub collection URL. */
520
+ /**
521
+ * Read an ActivityPub collection's `totalItems`.
522
+ *
523
+ * Every failure used to come back as `0`, so a follower count the remote HID,
524
+ * or one a timeout kept us from reading, was stored and shown as a real
525
+ * "0 followers" — indistinguishable from an account nobody follows. A failure
526
+ * now says which kind it is (see {@link CollectionCount}):
527
+ *
528
+ * - `null` when the answer is definitive: no collection advertised, 401/403
529
+ * (the owner hid it), 404/410 (it is gone), or a readable collection with no
530
+ * usable `totalItems` (the server does not publish the count).
531
+ * - `undefined` when this attempt simply failed — a thrown fetch (timeout,
532
+ * network, SSRF refusal), any other non-2xx (429, 5xx), or a body that could
533
+ * not be read as a JSON object. The next refresh may well succeed, so the
534
+ * caller keeps whatever it last knew rather than forgetting it.
535
+ */
511
536
  async fetchCollectionCount(url) {
512
537
  if (!url)
513
- return 0;
538
+ return null;
514
539
  try {
515
540
  const res = await this.config.signedFetch(url, AP_CONTENT_TYPE);
516
- if (!res.ok)
517
- return 0;
541
+ if (!res.ok) {
542
+ return COLLECTION_WITHHELD_STATUSES.has(res.status) ? null : undefined;
543
+ }
518
544
  const col = await readBoundedJson(res, COLLECTION_BODY_MAX_BYTES);
519
- return typeof col.totalItems === 'number' ? col.totalItems : 0;
545
+ const total = col.totalItems;
546
+ return typeof total === 'number' && Number.isSafeInteger(total) && total >= 0 ? total : null;
520
547
  }
521
548
  catch {
522
- return 0;
549
+ return undefined;
523
550
  }
524
551
  }
525
552
  /**