Skip to content

Commit

Permalink
Publish to PUBLIC_COLLECTION
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Jul 25, 2024
1 parent bce8f19 commit 640e568
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ACTOR_DEFAULT_HANDLE = 'index';
export const ACTOR_DEFAULT_NAME = 'Local Ghost site';
export const ACTOR_DEFAULT_ICON = 'https://ghost.org/favicon.ico';
export const ACTOR_DEFAULT_SUMMARY = 'This is a summary';
30 changes: 25 additions & 5 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Create,
Note,
Update,
PUBLIC_COLLECTION
} from '@fedify/fedify';
import { Context, Next } from 'hono';
import ky from 'ky';
Expand All @@ -15,6 +16,12 @@ import { addToList } from './kv-helpers';
import { toURL } from './toURL';
import { ContextData, HonoContextVariables, fedify } from './app';
import type { PersonData } from './user';
import {
ACTOR_DEFAULT_HANDLE,
ACTOR_DEFAULT_ICON,
ACTOR_DEFAULT_NAME,
ACTOR_DEFAULT_SUMMARY
} from './constants';

type GhostSiteSettings = {
site: {
Expand All @@ -24,6 +31,20 @@ type GhostSiteSettings = {
}
}

async function getGhostSiteSettings(host: string): Promise<GhostSiteSettings> {
const settings = await ky
.get(`https://${host}/ghost/api/admin/site/`)
.json<Partial<GhostSiteSettings>>();

return {
site: {
description: settings?.site?.description || ACTOR_DEFAULT_SUMMARY,
title: settings?.site?.title || ACTOR_DEFAULT_NAME,
icon: settings?.site?.icon || ACTOR_DEFAULT_ICON
}
};
}

async function postToArticle(ctx: RequestContext<ContextData>, post: any) {
if (!post) {
return {
Expand Down Expand Up @@ -138,14 +159,12 @@ export async function siteChangedWebhook(
) {
try {
// Retrieve site settings from Ghost
const host = ctx.req.header('host');
const host = ctx.req.header('host') || '';

const settings = await ky
.get(`https://${host}/ghost/api/admin/site/`)
.json<GhostSiteSettings>();
const settings = await getGhostSiteSettings(host);

// Update the database
const handle = 'index';
const handle = ACTOR_DEFAULT_HANDLE;
const db = ctx.get('db');

const current = await db.get<PersonData>(['handle', handle]);
Expand All @@ -169,6 +188,7 @@ export async function siteChangedWebhook(
const update = new Update({
id: apCtx.getObjectUri(Update, { id: uuidv4() }),
actor: actor?.id,
to: PUBLIC_COLLECTION,
object: actor
});

Expand Down

0 comments on commit 640e568

Please sign in to comment.