misskey-awawa/src/remote/activitypub/renderer/index.ts

62 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-08-25 04:11:54 +00:00
import config from '../../../config';
import { v4 as uuid } from 'uuid';
import { IActivity } from '../type';
import { LdSignature } from '../misc/ld-signature';
import { ILocalUser } from '../../../models/entities/user';
import { UserKeypairs } from '../../../models';
import { ensure } from '../../../prelude/ensure';
2018-08-25 04:11:54 +00:00
export const renderActivity = (x: any): IActivity | null => {
2018-11-16 18:25:48 +00:00
if (x == null) return null;
2018-08-25 04:11:54 +00:00
if (x !== null && typeof x === 'object' && x.id == null) {
x.id = `${config.url}/${uuid()}`;
2018-08-25 04:11:54 +00:00
}
return Object.assign({
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
2018-08-25 04:11:54 +00:00
]
}, x);
};
export const attachLdSignature = async (activity: any, user: ILocalUser): Promise<IActivity | null> => {
if (activity == null) return null;
const keypair = await UserKeypairs.findOne({
userId: user.id
}).then(ensure);
const obj = {
// 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',
// 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',
};
activity['@context'].push(obj);
const ldSignature = new LdSignature();
ldSignature.debug = false;
activity = await ldSignature.signRsaSignature2017(activity, keypair.privateKey, `${config.url}/users/${user.id}#main-key`);
return activity;
};