Skip to content

Commit

Permalink
Created new Account for Site on initialisation (#275)
Browse files Browse the repository at this point in the history
ref https://linear.app/ghost/issue/AP-658

This is required as going forward actors will be based on Accounts
  • Loading branch information
allouis authored Jan 22, 2025
1 parent 6af343c commit 144bde6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { cors } from 'hono/cors';
import jwt from 'jsonwebtoken';
import jose from 'node-jose';
import { behindProxy } from 'x-forwarded-fetch';
import { AccountService } from './account/account.service';
import { client } from './db';
import {
acceptDispatcher,
Expand Down Expand Up @@ -611,7 +612,8 @@ app.use(async (ctx, next) => {
await next();
});

const siteService = new SiteService(client);
const accountService = new AccountService(client);
const siteService = new SiteService(client, accountService);
// This needs to go before the middleware which loads the site
// Because the site doesn't always exist - this is how it's created
app.get('/.ghost/activitypub/site', getSiteDataHandler(siteService));
Expand Down
14 changes: 12 additions & 2 deletions src/site/site.service.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { TABLE_SITES } from '../constants';
import { client as db } from '../db';

import { AccountService } from '../account/account.service';
import type { Account } from '../account/types';
import { type Site, SiteService } from './site.service';

describe('SiteService', () => {
let service: SiteService;
let accountService: AccountService;
let site: Site;

beforeEach(async () => {
Expand All @@ -15,21 +18,28 @@ describe('SiteService', () => {
await db(TABLE_SITES).truncate();
await db.raw('SET FOREIGN_KEY_CHECKS = 1');

accountService = Object.create(AccountService.prototype);
// Create the service
service = new SiteService(db);
service = new SiteService(db, accountService);
});

it('Can initialise a site multiple times and retrieve it', async () => {
const existingSite = await service.getSiteByHost('hostname.tld');

expect(existingSite).toBeNull();

const createInternalAccount = vi
.spyOn(accountService, 'createInternalAccount')
.mockResolvedValue({} as unknown as Account);

const site = await service.initialiseSiteForHost('hostname.tld');

expect(site.host).toBe('hostname.tld');
expect(site.webhook_secret).toBeDefined();
expect(site.id).toBeDefined();

expect(createInternalAccount.mock.calls).toHaveLength(1);

const siteRows = await db(TABLE_SITES).select('*');

expect(siteRows).toHaveLength(1);
Expand Down
11 changes: 10 additions & 1 deletion src/site/site.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto from 'node:crypto';
import type { Knex } from 'knex';
import type { AccountService } from '../account/account.service';

export type Site = {
id: number;
Expand All @@ -8,7 +9,10 @@ export type Site = {
};

export class SiteService {
constructor(private client: Knex) {}
constructor(
private client: Knex,
private accountService: AccountService,
) {}

private async createSite(host: string): Promise<void> {
const rows = await this.client
Expand Down Expand Up @@ -65,6 +69,11 @@ export class SiteService {
throw new Error(`Site initialisation failed for ${host}`);
}

const internalAccount = await this.accountService.createInternalAccount(
newSite,
'index',
);

return newSite;
}
}

0 comments on commit 144bde6

Please sign in to comment.