Skip to content

Commit

Permalink
Compact LD Activity (#2534)
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 authored Apr 29, 2024
1 parent 37b0532 commit 0d0a465
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 86 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"jsdom": "19.0.0",
"json5": "2.2.3",
"json5-loader": "4.0.1",
"jsonld": "5.2.0",
"jsonld": "8.3.2",
"jsrsasign": "11.0.0",
"katex": "0.16.10",
"koa": "2.13.4",
Expand Down
121 changes: 69 additions & 52 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/queue/processors/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import DbResolver from '../../remote/activitypub/db-resolver';
import { resolvePerson } from '../../remote/activitypub/models/person';
import { LdSignature } from '../../remote/activitypub/misc/ld-signature';
import { StatusError } from '../../misc/fetch';
import { FIXED_CONTEXT } from '../../remote/activitypub/misc/contexts';

const logger = new Logger('inbox');

// ユーザーのinboxにアクティビティが届いた時の処理
export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
const signature = job.data.signature; // HTTP-signature
const activity = job.data.activity;
let activity = job.data.activity;

//#region Log
const info = Object.assign({}, activity) as any;
Expand Down Expand Up @@ -104,6 +105,11 @@ export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
return `skip: LD-Signatureの検証に失敗しました`;
}

const activity2 = JSON.parse(JSON.stringify(activity));
delete activity2.signature;
const compacted = await ldSignature.compact(activity2, FIXED_CONTEXT);
activity = compacted as any;

// もう一度actorチェック
if (authUser.user.uri !== activity.actor) {
return `skip: LD-Signature user(${authUser.user.uri}) !== activity.actor(${activity.actor})`;
Expand Down
33 changes: 33 additions & 0 deletions src/remote/activitypub/misc/contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,36 @@ export const CONTEXTS: Record<string, any> = {
"https://w3id.org/security/v1": security_v1,
"https://www.w3.org/ns/activitystreams": activitystreams,
};

export const FIXED_CONTEXT = [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{
Key: 'sec:Key',
// as non-standards
manuallyApprovesFollowers: 'as:manuallyApprovesFollowers',
sensitive: 'as:sensitive',
Hashtag: 'as:Hashtag',
quoteUrl: 'as:quoteUrl',
// Mastodon
toot: 'http://joinmastodon.org/ns#',
Emoji: 'toot:Emoji',
featured: 'toot:featured',
discoverable: 'toot:discoverable',
indexable: 'toot:indexable',
// schema
schema: 'http://schema.org#',
PropertyValue: 'schema:PropertyValue',
value: 'schema:value',
// Misskey
misskey: 'https://misskey-hub.net/ns#',
'_misskey_content': 'misskey:_misskey_content',
'_misskey_quote': 'misskey:_misskey_quote',
'_misskey_reaction': 'misskey:_misskey_reaction',
'_misskey_votes': 'misskey:_misskey_votes',
'_misskey_talk': 'misskey:_misskey_talk',
'isCat': 'misskey:isCat',
// vcard
vcard: 'http://www.w3.org/2006/vcard/ns#',
}
];
7 changes: 7 additions & 0 deletions src/remote/activitypub/misc/ld-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export class LdSignature {
});
}

public async compact(data: any, context: any) {
const customLoader = this.getLoader();
return await jsonld.compact(data, context, {
documentLoader: customLoader
});
}

private getLoader() {
return async (url: string): Promise<any> => {
if (!url.match('^https?\:\/\/')) throw `Invalid URL ${url}`;
Expand Down
34 changes: 2 additions & 32 deletions src/remote/activitypub/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LdSignature } from '../misc/ld-signature';
import { ILocalUser } from '../../../models/entities/user';
import { UserKeypairs } from '../../../models';
import { ensure } from '../../../prelude/ensure';
import { FIXED_CONTEXT } from '../misc/contexts';

export const renderActivity = (x: any): IActivity | null => {
if (x == null) return null;
Expand All @@ -14,38 +15,7 @@ export const renderActivity = (x: any): IActivity | null => {
}

return Object.assign({
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{
Key: 'sec:Key',
// as non-standards
manuallyApprovesFollowers: 'as:manuallyApprovesFollowers',
sensitive: 'as:sensitive',
Hashtag: 'as:Hashtag',
quoteUrl: 'as:quoteUrl',
// Mastodon
toot: 'http://joinmastodon.org/ns#',
Emoji: 'toot:Emoji',
featured: 'toot:featured',
discoverable: 'toot:discoverable',
indexable: 'toot:indexable',
// schema
schema: 'http://schema.org#',
PropertyValue: 'schema:PropertyValue',
value: 'schema:value',
// Misskey
misskey: `${config.url}/ns#`,
'_misskey_content': 'misskey:_misskey_content',
'_misskey_quote': 'misskey:_misskey_quote',
'_misskey_reaction': 'misskey:_misskey_reaction',
'_misskey_votes': 'misskey:_misskey_votes',
'_misskey_talk': 'misskey:_misskey_talk',
'isCat': 'misskey:isCat',
// vcard
vcard: 'http://www.w3.org/2006/vcard/ns#',
}
]
'@context': FIXED_CONTEXT
}, x);
};

Expand Down

0 comments on commit 0d0a465

Please sign in to comment.