feat(website): the event feed both ways, and Linked products
Publish my events: the Website's simple events as crosscode.event/1 (GET /api/feed/v1/events, updatedSince, removals in deleted for 90 days) and a signed event.changed to linked siblings. Show events from: each sibling's feed every 15 minutes and at once on its signed event.changed; read-only copies of only that sibling's events, one bad event skipped, the last good copy kept. Admin: Linked products. church.changed from Accounts refreshes the profile at once. Plan: the video hero, more blocks and more pages move to website@1.1.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import { audit } from '../lib/audit.js';
|
||||
import { youtubeId, spotifyEmbed } from '../site/content.js';
|
||||
import { mediaUrl } from '../site/snapshot.js';
|
||||
import { getSource, setSource, syncYoutube, uniqueSlug } from '../site/youtube.js';
|
||||
import { markRemoved, notifyLinks } from '../feed/feed.js';
|
||||
|
||||
type Who = { id: string; role: string };
|
||||
|
||||
@@ -136,7 +137,10 @@ export async function saveEvent(ctx: Ctx, id: string | null, body: unknown, by:
|
||||
const data = { title: e.title, startsAt: new Date(e.startsAt), endsAt: e.endsAt ? new Date(e.endsAt) : null, allDay: e.allDay, location: e.location, description: e.description, imageId: e.imageId, link: e.link, timezone: ctx.config.timezone, updatedBy: by.id };
|
||||
if (id) {
|
||||
if (!(await ctx.db.simpleEvent.findUnique({ where: { id } }))) throw new ApiError(404, 'NOT_FOUND', 'That event doesn\'t exist.');
|
||||
return ctx.db.simpleEvent.update({ where: { id }, data });
|
||||
const saved = await ctx.db.simpleEvent.update({ where: { id }, data });
|
||||
// A live event changed: the church's other sites fetch it now.
|
||||
if (saved.publishedAt) void notifyLinks(ctx, [id]).catch(() => {});
|
||||
return saved;
|
||||
}
|
||||
return ctx.db.simpleEvent.create({ data: { ...data, slug: await uniqueSlug(ctx, e.title, 'simpleEvent') } });
|
||||
}
|
||||
@@ -149,6 +153,9 @@ export async function setEventState(ctx: Ctx, id: string, change: { published?:
|
||||
...(change.cancelled !== undefined ? { cancelled: change.cancelled } : {}),
|
||||
updatedBy: by.id,
|
||||
} });
|
||||
// In or out of the feed (ROADMAP-v3 §4: an unpublished event goes in `deleted`), and siblings told.
|
||||
if (change.published !== undefined) await markRemoved(ctx, id, !change.published);
|
||||
if (row.publishedAt || change.published) void notifyLinks(ctx, [id]).catch(() => {});
|
||||
await audit(ctx, { actorId: by.id, actorRole: by.role, action: 'event_changed', targetType: 'simple_event', targetId: id, metadata: { title: row.title, ...change } });
|
||||
}
|
||||
|
||||
@@ -156,5 +163,9 @@ export async function deleteEvent(ctx: Ctx, id: string, by: Who) {
|
||||
const row = await ctx.db.simpleEvent.findUnique({ where: { id } });
|
||||
if (!row) throw new ApiError(404, 'NOT_FOUND', 'That event doesn\'t exist.');
|
||||
await ctx.db.simpleEvent.delete({ where: { id } });
|
||||
if (row.publishedAt) {
|
||||
await markRemoved(ctx, id, true);
|
||||
void notifyLinks(ctx, [id]).catch(() => {});
|
||||
}
|
||||
await audit(ctx, { actorId: by.id, actorRole: by.role, action: 'event_deleted', targetType: 'simple_event', targetId: id, metadata: { title: row.title } });
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ import * as content from './content.js';
|
||||
import * as people from './people.js';
|
||||
import * as happenings from './happenings.js';
|
||||
import { syncYoutube } from '../site/youtube.js';
|
||||
import { publishOn, syncLink } from '../feed/feed.js';
|
||||
import { connectTo, disconnectFrom } from '../links/connect.js';
|
||||
import { linkedProducts } from '../links/store.js';
|
||||
import { MAX_UPLOAD_BYTES, storage, upload } from './media.js';
|
||||
import { createPreviewLink, listPreviewLinks, revokePreviewLink } from './previewLinks.js';
|
||||
|
||||
@@ -118,6 +121,53 @@ export function adminRouter(ctx: Ctx) {
|
||||
}));
|
||||
r.delete('/events/:id', editor, handle(async (req) => { await happenings.deleteEvent(ctx, param(req, 'id'), who(req)); return { ok: true }; }));
|
||||
|
||||
// ─── Linked products and the event feed (§8, §9) ───
|
||||
r.get('/links', handle(async () => {
|
||||
const [listed, links, publishing] = await Promise.all([linkedProducts(ctx), ctx.db.productLink.findMany(), publishOn(ctx)]);
|
||||
const counts = new Map<string, number>();
|
||||
for (const l of links) counts.set(l.installId, await ctx.db.importedEvent.count({ where: { linkId: l.installId } }));
|
||||
const ids = new Set([...listed.map((l) => l.installId), ...links.map((l) => l.installId)]);
|
||||
return {
|
||||
publishMyEvents: publishing,
|
||||
products: [...ids].map((id) => {
|
||||
const l = listed.find((x) => x.installId === id);
|
||||
const link = links.find((x) => x.installId === id);
|
||||
return {
|
||||
installId: id, product: link?.product ?? l!.product, url: link?.url ?? l!.url, connected: !!link, listed: !!l,
|
||||
showEvents: link?.showEvents ?? null, events: counts.get(id) ?? 0,
|
||||
lastSyncAt: link?.lastSyncAt?.toISOString() ?? null, lastSyncError: link?.lastSyncError ?? null,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}));
|
||||
const linkAdmin = requireRole(ctx, 'admin');
|
||||
r.post('/links/:id/connect', linkAdmin, handle(async (req) => {
|
||||
const link = await connectTo(ctx, param(req, 'id'), who(req), req);
|
||||
if (link.product !== 'accounts') await syncLink(ctx, link);
|
||||
return { ok: true };
|
||||
}));
|
||||
r.delete('/links/:id', linkAdmin, handle(async (req) => {
|
||||
const link = await ctx.db.productLink.findUnique({ where: { installId: param(req, 'id') } });
|
||||
if (link?.product === 'accounts') throw new ApiError(400, 'ACCOUNTS', 'The Website signs in through Accounts, so that link stays.');
|
||||
return disconnectFrom(ctx, param(req, 'id'), who(req), req);
|
||||
}));
|
||||
r.put('/links/:id', editor, json, handle(async (req) => {
|
||||
const id = param(req, 'id');
|
||||
const link = await ctx.db.productLink.findUnique({ where: { installId: id } });
|
||||
if (!link) throw new ApiError(404, 'NOT_LINKED', "That product isn't connected.");
|
||||
const updated = await ctx.db.productLink.update({ where: { installId: id }, data: { showEvents: !!req.body?.showEvents } });
|
||||
return syncLink(ctx, updated);
|
||||
}));
|
||||
r.post('/links/:id/sync', editor, handle(async (req) => {
|
||||
const link = await ctx.db.productLink.findUnique({ where: { installId: param(req, 'id') } });
|
||||
if (!link) throw new ApiError(404, 'NOT_LINKED', "That product isn't connected.");
|
||||
return syncLink(ctx, link);
|
||||
}));
|
||||
r.put('/feed', editor, json, handle(async (req) => {
|
||||
await ctx.settings.set('feed_publish', req.body?.publish === false ? 'false' : 'true');
|
||||
return { publishMyEvents: await publishOn(ctx) };
|
||||
}));
|
||||
|
||||
// ─── People: who can use the admin, as what (Owner and Admin) ───
|
||||
const admin = requireRole(ctx, 'admin');
|
||||
r.get('/people', admin, handle(() => people.listPeople(ctx)));
|
||||
|
||||
Reference in New Issue
Block a user