Skip to content

Commit

Permalink
WIP Refactored updateSiteActor
Browse files Browse the repository at this point in the history
  • Loading branch information
allouis committed Dec 16, 2024
1 parent 9de4b3e commit 45dfed6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 59 deletions.
60 changes: 2 additions & 58 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import {
Article,
Create,
Follow,
type KvStore,
Like,
Mention,
Note,
PUBLIC_COLLECTION,
type RequestContext,
Undo,
Update,
isActor,
} from '@fedify/fedify';
import { Temporal } from '@js-temporal/polyfill';
Expand All @@ -23,15 +21,14 @@ import {
buildActivity,
prepareNoteContent,
} from './helpers/activitypub/activity';
import { getSiteSettings } from './helpers/ghost';
import { toURL } from './helpers/uri';
import { type PersonData, getUserData } from './helpers/user';
import { getUserData } from './helpers/user';
import { addToList, removeFromList } from './kv-helpers';
import { lookupActor, lookupObject } from './lookup-helpers';

import type { Logger } from '@logtape/logtape';
import z from 'zod';
import { getSite } from './db';
import { updateSiteActor } from './helpers/activitypub/actor';

const PostSchema = z.object({
uuid: z.string().uuid(),
Expand Down Expand Up @@ -588,59 +585,6 @@ export async function getSiteDataHandler(
});
}

async function updateSiteActor(
db: KvStore,
globaldb: KvStore,
apCtx: RequestContext<ContextData>,
logger: Logger,
host: string,
) {
const settings = await getSiteSettings(host);
const handle = ACTOR_DEFAULT_HANDLE;

const current = await db.get<PersonData>(['handle', handle]);

if (
current &&
current.icon === settings.site.icon &&
current.name === settings.site.title &&
current.summary === settings.site.description
) {
logger.info('No site settings changed, not updating site actor');
return false;
}

// Update the database if the site settings have changed
const updated = {
...current,
icon: settings.site.icon,
name: settings.site.title,
summary: settings.site.description,
};

await db.set(['handle', handle], updated);

logger.info('Site settings changed, will notify followers');

const actor = await apCtx.getActor(handle);

const update = new Update({
id: apCtx.getObjectUri(Update, { id: uuidv4() }),
actor: actor?.id,
to: PUBLIC_COLLECTION,
object: actor?.id,
cc: apCtx.getFollowersUri('index'),
});

await globaldb.set([update.id!.href], await update.toJsonLd());

await apCtx.sendActivity({ handle }, 'followers', update, {
preferSharedInbox: true,
});

return true;
}

export async function siteChangedWebhook(
ctx: Context<{ Variables: HonoContextVariables }>,
) {
Expand Down
67 changes: 66 additions & 1 deletion src/helpers/activitypub/actor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { type Actor, type KvStore, PropertyValue } from '@fedify/fedify';
import {
type Actor,
type KvStore,
PUBLIC_COLLECTION,
PropertyValue,
type RequestContext,
Update,
} from '@fedify/fedify';
import type { Logger } from '@logtape/logtape';
import { v4 as uuidv4 } from 'uuid';
import type { ContextData } from '../../app';
import { ACTOR_DEFAULT_HANDLE } from '../../constants';
import { getSiteSettings } from '../ghost';
import type { PersonData } from '../user';

interface Attachment {
name: string;
Expand Down Expand Up @@ -63,3 +76,55 @@ export async function isFollowing(
export function isHandle(handle: string): boolean {
return /^@([\w-]+)@([\w-]+\.[\w.-]+)$/.test(handle);
}
export async function updateSiteActor(
db: KvStore,
globaldb: KvStore,
apCtx: RequestContext<ContextData>,
logger: Logger,
host: string,
) {
const settings = await getSiteSettings(host);
const handle = ACTOR_DEFAULT_HANDLE;

const current = await db.get<PersonData>(['handle', handle]);

if (
current &&
current.icon === settings.site.icon &&
current.name === settings.site.title &&
current.summary === settings.site.description
) {
logger.info('No site settings changed, not updating site actor');
return false;
}

// Update the database if the site settings have changed
const updated = {
...current,
icon: settings.site.icon,
name: settings.site.title,
summary: settings.site.description,
};

await db.set(['handle', handle], updated);

logger.info('Site settings changed, will notify followers');

const actor = await apCtx.getActor(handle);

const update = new Update({
id: apCtx.getObjectUri(Update, { id: uuidv4() }),
actor: actor?.id,
to: PUBLIC_COLLECTION,
object: actor?.id,
cc: apCtx.getFollowersUri('index'),
});

await globaldb.set([update.id!.href], await update.toJsonLd());

await apCtx.sendActivity({ handle }, 'followers', update, {
preferSharedInbox: true,
});

return true;
}

0 comments on commit 45dfed6

Please sign in to comment.