Tasks 7.1–7.3, 7.5 (+7.4 pacing; Retry-After-duration backoff noted as remaining). Resolves the activities.owner_id open question. Schema (design decision from the open question): - activities.owner_id nullable + check constraint enforcing exactly one of (owner_id, remote_actor_iri) — the follows pattern again. - remote_published_at carries the origin's publish time for the §8 feed sort; index on remote_actor_iri for the feed join. - Compiler-audited fallout: notification fan-out, the Note object dispatcher, and the activity detail loader explicitly skip/404 remote rows (their canonical page is the origin; feed links there). Every other surface joins users on owner_id and excludes remote rows structurally. Ingestion: - federation-ingest.server.ts: parseOutboxItem targets exactly our own outgoing Create(Note) shape (PropertyValue stats, first-paragraph name, audience from to/cc); unknown items skipped, never fatal. - ingestRemoteActivities: replay-safe via unique remote_origin_iri, conflict-streak early exit (outboxes are newest-first). - pollRemoteActor: signed fetch (Authorized Fetch via a local follower's key), outbox resolution via remote_actors cache with actor-doc fallback (which refreshes the cache), 1 req/5 s per-host pacing, last_polled_at stamping. Network + pacing injectable. Jobs: poll-remote-actor (per-actor; the §4 Accept listener already enqueues it as the first poll) + poll-remote-outboxes (5-min cron sweep over accepted remote follows not polled within the hour). Tests: parse-shape units; integration suite for ingestion provenance, audience→visibility mirroring, replay safety, the DB author invariant, poll flow (actor-doc → collection → page), signer requirement, and due-polling selection. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
104 lines
3.5 KiB
TypeScript
104 lines
3.5 KiB
TypeScript
import type { JobDefinition } from "@trails-cool/jobs";
|
|
import { and, eq, isNotNull } from "drizzle-orm";
|
|
import { getDb } from "../lib/db.ts";
|
|
import { activities, follows, users } from "@trails-cool/db/schema/journal";
|
|
import { createNotification } from "../lib/notifications.server.ts";
|
|
import { logger } from "../lib/logger.server.ts";
|
|
|
|
interface FanoutData {
|
|
activityId: string;
|
|
}
|
|
|
|
/**
|
|
* Fan out an `activity_published` notification to every accepted
|
|
* follower of the activity's owner. Idempotent at the DB level via the
|
|
* `(recipient_user_id, type, subject_id)` unique partial index — a
|
|
* retry after partial failure won't double-insert.
|
|
*/
|
|
export const notificationsFanoutJob: JobDefinition = {
|
|
name: "notifications-fanout",
|
|
retryLimit: 3,
|
|
expireInSeconds: 300,
|
|
async handler(job) {
|
|
// pg-boss v12: `job` may be an array (batch) per its docs; we
|
|
// process whichever shape we get.
|
|
const batch = Array.isArray(job) ? job : [job];
|
|
for (const item of batch) {
|
|
const data = item.data as FanoutData;
|
|
await fanout(data.activityId);
|
|
}
|
|
},
|
|
};
|
|
|
|
export async function fanout(activityId: string): Promise<void> {
|
|
const db = getDb();
|
|
|
|
// Load the activity + owner info needed for the payload snapshot.
|
|
const [row] = await db
|
|
.select({
|
|
id: activities.id,
|
|
name: activities.name,
|
|
visibility: activities.visibility,
|
|
ownerId: activities.ownerId,
|
|
ownerUsername: users.username,
|
|
ownerDisplayName: users.displayName,
|
|
})
|
|
.from(activities)
|
|
.innerJoin(users, eq(activities.ownerId, users.id))
|
|
.where(eq(activities.id, activityId));
|
|
|
|
if (!row) {
|
|
logger.warn({ activityId }, "fanout: activity not found, skipping");
|
|
return;
|
|
}
|
|
// Remote-ingested rows have no local owner and never fan out locally
|
|
// (the users innerJoin already excludes them; this narrows the type).
|
|
if (row.ownerId === null) return;
|
|
// Defense in depth — the create-side guard already filters this, but
|
|
// recheck here so the job doesn't mistakenly fan out a row that was
|
|
// later flipped to private/unlisted before the job ran.
|
|
if (row.visibility !== "public") {
|
|
logger.info({ activityId, visibility: row.visibility }, "fanout: skipping non-public activity");
|
|
return;
|
|
}
|
|
|
|
// Find every accepted *local* follower of the owner. Remote
|
|
// followers (follower_id NULL, follower_actor_iri set) get the
|
|
// activity via federation push delivery, not via notifications.
|
|
const recipients = await db
|
|
.select({ followerId: follows.followerId })
|
|
.from(follows)
|
|
.where(
|
|
and(
|
|
eq(follows.followedUserId, row.ownerId),
|
|
isNotNull(follows.acceptedAt),
|
|
isNotNull(follows.followerId),
|
|
),
|
|
);
|
|
|
|
let inserted = 0;
|
|
for (const r of recipients) {
|
|
// Don't notify the owner about their own activity if they happen
|
|
// to follow themselves (shouldn't happen — followUser refuses
|
|
// self-follow — but defense in depth).
|
|
if (r.followerId === row.ownerId || r.followerId === null) continue;
|
|
const created = await createNotification({
|
|
type: "activity_published",
|
|
recipientUserId: r.followerId,
|
|
actorUserId: row.ownerId,
|
|
subjectId: row.id,
|
|
payload: {
|
|
activityId: row.id,
|
|
activityName: row.name,
|
|
ownerUsername: row.ownerUsername,
|
|
ownerDisplayName: row.ownerDisplayName,
|
|
},
|
|
});
|
|
if (created) inserted += 1;
|
|
}
|
|
|
|
logger.info(
|
|
{ activityId, recipients: recipients.length, inserted },
|
|
"notifications-fanout completed",
|
|
);
|
|
}
|