Skip to content

Commit

Permalink
Added eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Jul 29, 2024
1 parent e7a4873 commit baa1e8f
Show file tree
Hide file tree
Showing 12 changed files with 730 additions and 167 deletions.
11 changes: 11 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-check

// See https://typescript-eslint.io/getting-started/

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
);
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
"test:code": "c8 --src src --all --reporter text --reporter cobertura mocha -r tsx './src/**/*.test.ts'",
"test:all": "yarn test:types && yarn test:code",
"test": "docker compose -f docker-compose.testing.yml up --abort-on-container-exit --exit-code-from activitypub",
"lint:code": "eslint *.js lib/ --ext .js --cache",
"lint:code": "eslint src/**/*.ts",
"lint": "yarn lint:code"
},
"files": [
"src"
],
"devDependencies": {
"@eslint/js": "9.7.0",
"@types/eslint__js": "8.42.3",
"@types/mocha": "10.0.7",
"@types/node": "20.12.12",
"@types/uuid": "10.0.0",
"c8": "10.1.2",
"eslint": "9.7.0",
"mocha": "10.5.2",
"tsx": "4.11.0",
"typescript": "5.4.5"
"typescript": "5.5.4",
"typescript-eslint": "7.17.0"
},
"dependencies": {
"@fedify/fedify": "0.12.1",
Expand Down
7 changes: 0 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ import { serve } from '@hono/node-server';
import {
Article,
Accept,
Object as ActivityPubObject,
Federation,
Follow,
KvKey,
KvStore,
MemoryKvStore,
Create,
Note,
Application,
Group,
Organization,
Service,
Update,
} from '@fedify/fedify';
import { federation } from '@fedify/fedify/x/hono';
Expand Down
41 changes: 22 additions & 19 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export async function handleFollow(
follow: Follow,
) {
console.log('Handling Follow');
if (!follow.id) {
if (!follow.id || !follow.objectId) {
return;
}
const parsed = (ctx as any).parseUri(follow.objectId);
const parsed = ctx.parseUri(follow.objectId);
if (parsed?.type !== 'actor') {
// TODO Log
return;
Expand Down Expand Up @@ -81,12 +81,15 @@ export async function handleAccept(
accept: Accept,
) {
console.log('Handling Accept');
const parsed = (ctx as any).parseUri(accept.objectId);
console.log(parsed);
if (false && parsed?.type !== 'follow') {
console.log('Not accepting a follow - exit');
if (!accept.objectId) {
return;
}
const parsed = ctx.parseUri(accept.objectId);
console.log(parsed);
// if (false && parsed?.type !== 'follow') {
// console.log('Not accepting a follow - exit');
// return;
// }
if (!accept.id) {
console.log('Accept missing id - exit');
return;
Expand All @@ -113,12 +116,18 @@ export async function handleCreate(
create: Create,
) {
console.log('Handling Create');
const parsed = (ctx as any).parseUri(create.objectId);
console.log(parsed);
if (false && parsed?.type !== 'article') {
console.log('Not accepting a follow - exit');

if (create.objectId === null) {
console.log('Create missing objectId - exit');
return;
}

const parsed = ctx.parseUri(create.objectId);
console.log(parsed);
// if (false && parsed?.type !== 'article') {
// console.log('Not accepting a follow - exit');
// return;
// }
if (!create.id) {
console.log('Accept missing id - exit');
return;
Expand Down Expand Up @@ -146,12 +155,11 @@ export async function inboxErrorHandler(

export async function followersDispatcher(
ctx: RequestContext<ContextData>,
handle: string,
) {
console.log('Followers Dispatcher');
const results = (await ctx.data.db.get<string[]>(['followers'])) || [];
console.log(results);
let items: Person[] = [];
const items: Person[] = [];
for (const result of results) {
try {
const thing = await lookupObject(result);
Expand All @@ -169,20 +177,18 @@ export async function followersDispatcher(

export async function followersCounter(
ctx: RequestContext<ContextData>,
handle: string,
) {
const results = (await ctx.data.db.get<string[]>(['followers'])) || [];
return results.length;
}

export async function followingDispatcher(
ctx: RequestContext<ContextData>,
handle: string,
) {
console.log('Following Dispatcher');
const results = (await ctx.data.db.get<string[]>(['following'])) || [];
console.log(results);
let items: Person[] = [];
const items: Person[] = [];
for (const result of results) {
try {
const thing = await lookupObject(result);
Expand All @@ -200,20 +206,18 @@ export async function followingDispatcher(

export async function followingCounter(
ctx: RequestContext<ContextData>,
handle: string,
) {
const results = (await ctx.data.db.get<string[]>(['following'])) || [];
return results.length;
}

export async function outboxDispatcher(
ctx: RequestContext<ContextData>,
handle: string,
) {
console.log('Outbox Dispatcher');
const results = (await ctx.data.db.get<string[]>(['outbox'])) || [];
console.log(results);
let items: Activity[] = [];
const items: Activity[] = [];
for (const result of results) {
try {
const thing = await ctx.data.globaldb.get([result]);
Expand All @@ -230,7 +234,6 @@ export async function outboxDispatcher(

export async function outboxCounter(
ctx: RequestContext<ContextData>,
handle: string,
) {
const results = (await ctx.data.db.get<string[]>(['outbox'])) || [];
return results.length;
Expand Down
23 changes: 3 additions & 20 deletions src/dispatchers.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
import assert from 'assert';
import {
actorDispatcher,
keypairDispatcher,
handleFollow,
inboxErrorHandler,
handleAccept,
handleCreate,
followersDispatcher,
followersCounter,
followingDispatcher,
followingCounter,
outboxDispatcher,
outboxCounter,
articleDispatcher,
noteDispatcher,
followDispatcher,
acceptDispatcher,
createDispatcher,
} from './dispatchers';
import { RequestContext } from '@fedify/fedify';
import { ContextData } from './app';
import { actorDispatcher } from './dispatchers';

describe('dispatchers', function () {
describe('actorDispatcher', function () {
it('returns null if the handle is not "index"', async function () {
const ctx = {} as RequestContext<any>;
const ctx = {} as RequestContext<ContextData>;
const handle = 'anything';

const actual = await actorDispatcher(ctx, handle);
Expand Down
21 changes: 14 additions & 7 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Update,
PUBLIC_COLLECTION
} from '@fedify/fedify';
import { Context, Next } from 'hono';
import { Context } from 'hono';
import ky from 'ky';
import { v4 as uuidv4 } from 'uuid';
import { addToList } from './kv-helpers';
Expand All @@ -31,6 +31,16 @@ type GhostSiteSettings = {
}
}

type Post = {
uuid: string;
excerpt: string;
title: string;
html: string;
feature_image: string;
published_at: string;
url: string;
}

async function getGhostSiteSettings(host: string): Promise<GhostSiteSettings> {
const settings = await ky
.get(`https://${host}/ghost/api/admin/site/`)
Expand All @@ -45,7 +55,7 @@ async function getGhostSiteSettings(host: string): Promise<GhostSiteSettings> {
};
}

async function postToArticle(ctx: RequestContext<ContextData>, post: any) {
async function postToArticle(ctx: RequestContext<ContextData>, post: Post) {
if (!post) {
return {
article: null,
Expand All @@ -61,7 +71,7 @@ async function postToArticle(ctx: RequestContext<ContextData>, post: any) {
name: post.title,
content: post.html,
image: toURL(post.feature_image),
published: post.published_at,
published: post.published_at as any, /* eslint-disable-line @typescript-eslint/no-explicit-any */
preview: preview,
url: toURL(post.url),
});
Expand Down Expand Up @@ -108,7 +118,6 @@ export async function followAction(

export async function postPublishedWebhook(
ctx: Context<{ Variables: HonoContextVariables }>,
next: Next,
) {
// TODO: Validate webhook with secret
const data = await ctx.req.json();
Expand Down Expand Up @@ -155,7 +164,6 @@ export async function postPublishedWebhook(

export async function siteChangedWebhook(
ctx: Context<{ Variables: HonoContextVariables }>,
next: Next,
) {
try {
// Retrieve site settings from Ghost
Expand Down Expand Up @@ -210,10 +218,9 @@ export async function siteChangedWebhook(

export async function inboxHandler(
ctx: Context<{ Variables: HonoContextVariables }>,
next: Next,
) {
const results = (await ctx.get('db').get<string[]>(['inbox'])) || [];
let items: unknown[] = [];
const items: unknown[] = [];
for (const result of results) {
try {
const thing = await ctx.get('globaldb').get([result]);
Expand Down
1 change: 0 additions & 1 deletion src/knex.kvstore.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from 'assert';
import { KnexKvStore } from './knex.kvstore';
import { Knex } from 'knex';
import { client } from './db';

after(async function () {
Expand Down
4 changes: 2 additions & 2 deletions src/knex.kvstore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KvKey, KvStore, KvStoreSetOptions } from '@fedify/fedify';
import { KvKey, KvStore } from '@fedify/fedify';
import Knex from 'knex';

export class KnexKvStore implements KvStore {
Expand Down Expand Up @@ -26,7 +26,7 @@ export class KnexKvStore implements KvStore {
return row.value;
}

async set(key: KvKey, value: unknown, options?: KvStoreSetOptions) {
async set(key: KvKey, value: unknown) {
if (typeof value === 'boolean') {
value = {
'@@BOOLEAN@@': value,
Expand Down
2 changes: 1 addition & 1 deletion src/kv-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function scopeKvStore(db: KvStore, scope: KvKey): KvStore {
get(key: KvKey) {
return db.get(scopeKvKey(scope, key));
},
set(key: KvKey, value: any) {
set(key: KvKey, value: unknown) {
return db.set(scopeKvKey(scope, key), value);
},
delete(key: KvKey) {
Expand Down
6 changes: 6 additions & 0 deletions src/kv-helpers.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ describe('Kv Helpers', function () {
const actual = await scopedStore.get(['key']);
const expected = { value: 'da value' };
assert.deepEqual(actual, expected);
break checkIsSet;
}

checkDeletes: {
await scopedStore.delete(['key']);
const actual = await scopedStore.get(['key']);
const expected = null;
assert.deepEqual(actual, expected);
break checkDeletes;
}
});
});
Expand All @@ -41,6 +43,7 @@ describe('Kv Helpers', function () {
const actual = await store.get(['not-existing']);
const expected = ['first'];
assert.deepEqual(actual, expected);
break checkNonExisting;
}

checkExisting: {
Expand All @@ -49,6 +52,7 @@ describe('Kv Helpers', function () {
const actual = await store.get(['existing']);
const expected = ['first', 'second'];
assert.deepEqual(actual, expected);
break checkExisting;
}
});
});
Expand All @@ -62,6 +66,7 @@ describe('Kv Helpers', function () {
const actual = await store.get(['not-existing']);
const expected: never[] = [];
assert.deepEqual(actual, expected);
break checkNonExisting;
}

checkExisting: {
Expand All @@ -70,6 +75,7 @@ describe('Kv Helpers', function () {
const actual = await store.get(['existing']);
const expected: never[] = [];
assert.deepEqual(actual, expected);
break checkExisting;
}
});
});
Expand Down
14 changes: 1 addition & 13 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,13 @@ export async function getUserData(ctx: RequestContext<ContextData>, handle: stri
),
};

const dataToStore: PersonData = {
id: data.id.href,
name: data.name,
summary: data.summary,
preferredUsername: data.preferredUsername,
icon: 'https://ghost.org/favicon.ico',
inbox: data.inbox.href,
outbox: data.outbox.href,
following: data.following.href,
followers: data.followers.href,
};

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

return data;
}

export async function getUserKeypair(ctx: Context<ContextData>, handle: string) {
const existing = await ctx.data.db.get<{ publicKey: any; privateKey: any }>([
const existing = await ctx.data.db.get<{ publicKey: JsonWebKey; privateKey: JsonWebKey }>([
'keypair',
handle,
]);
Expand Down
Loading

0 comments on commit baa1e8f

Please sign in to comment.