egirlskey/packages/backend/src/core/activitypub/ApRendererService.ts

714 lines
20 KiB
TypeScript
Raw Normal View History

2022-09-17 18:27:08 +00:00
import { createPublicKey } from 'node:crypto';
import { Inject, Injectable } from '@nestjs/common';
import { In, IsNull } from 'typeorm';
import { v4 as uuid } from 'uuid';
import * as mfm from 'mfm-js';
import { DI } from '@/di-symbols.js';
2022-09-20 20:33:11 +00:00
import type { Config } from '@/config.js';
2023-02-13 06:50:22 +00:00
import type { LocalUser, RemoteUser, User } from '@/models/entities/User.js';
2022-09-17 18:27:08 +00:00
import type { IMentionedRemoteUsers, Note } from '@/models/entities/Note.js';
import type { Blocking } from '@/models/entities/Blocking.js';
import type { Relay } from '@/models/entities/Relay.js';
import type { DriveFile } from '@/models/entities/DriveFile.js';
import type { NoteReaction } from '@/models/entities/NoteReaction.js';
import type { Emoji } from '@/models/entities/Emoji.js';
import type { Poll } from '@/models/entities/Poll.js';
import type { PollVote } from '@/models/entities/PollVote.js';
import { UserKeypairService } from '@/core/UserKeypairService.js';
2022-09-17 18:27:08 +00:00
import { MfmService } from '@/core/MfmService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
import type { UserKeypair } from '@/models/entities/UserKeypair.js';
2022-09-22 21:21:31 +00:00
import type { UsersRepository, UserProfilesRepository, NotesRepository, DriveFilesRepository, EmojisRepository, PollsRepository } from '@/models/index.js';
2023-01-25 02:23:57 +00:00
import { bindThis } from '@/decorators.js';
2022-09-17 18:27:08 +00:00
import { LdSignatureService } from './LdSignatureService.js';
import { ApMfmService } from './ApMfmService.js';
import type { IAccept, IActivity, IAdd, IAnnounce, IApDocument, IApEmoji, IApHashtag, IApImage, IApMention, IBlock, ICreate, IDelete, IFlag, IFollow, IKey, ILike, IObject, IPost, IQuestion, IReject, IRemove, ITombstone, IUndo, IUpdate } from './type.js';
2022-09-17 18:27:08 +00:00
import type { IIdentifier } from './models/identifier.js';
@Injectable()
export class ApRendererService {
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.userProfilesRepository)
private userProfilesRepository: UserProfilesRepository,
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
@Inject(DI.emojisRepository)
private emojisRepository: EmojisRepository,
@Inject(DI.pollsRepository)
private pollsRepository: PollsRepository,
private userEntityService: UserEntityService,
private driveFileEntityService: DriveFileEntityService,
private ldSignatureService: LdSignatureService,
private userKeypairService: UserKeypairService,
2022-09-17 18:27:08 +00:00
private apMfmService: ApMfmService,
private mfmService: MfmService,
) {
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderAccept(object: any, user: { id: User['id']; host: null }): IAccept {
2022-09-17 18:27:08 +00:00
return {
type: 'Accept',
actor: `${this.config.url}/users/${user.id}`,
object,
};
}
@bindThis
2023-02-13 06:50:22 +00:00
public renderAdd(user: LocalUser, target: any, object: any): IAdd {
2022-09-17 18:27:08 +00:00
return {
type: 'Add',
actor: `${this.config.url}/users/${user.id}`,
target,
object,
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderAnnounce(object: any, note: Note): IAnnounce {
2022-09-17 18:27:08 +00:00
const attributedTo = `${this.config.url}/users/${note.userId}`;
let to: string[] = [];
let cc: string[] = [];
if (note.visibility === 'public') {
to = ['https://www.w3.org/ns/activitystreams#Public'];
cc = [`${attributedTo}/followers`];
} else if (note.visibility === 'home') {
to = [`${attributedTo}/followers`];
cc = ['https://www.w3.org/ns/activitystreams#Public'];
} else if (note.visibility === 'followers') {
to = [`${attributedTo}/followers`];
cc = [];
2022-09-17 18:27:08 +00:00
} else {
2023-02-12 09:47:30 +00:00
throw new Error('renderAnnounce: cannot render non-public note');
2022-09-17 18:27:08 +00:00
}
return {
id: `${this.config.url}/notes/${note.id}/activity`,
actor: `${this.config.url}/users/${note.userId}`,
type: 'Announce',
published: note.createdAt.toISOString(),
to,
cc,
object,
};
}
/**
* Renders a block into its ActivityPub representation.
*
* @param block The block to be rendered. The blockee relation must be loaded.
*/
@bindThis
2023-02-12 09:47:30 +00:00
public renderBlock(block: Blocking): IBlock {
2022-09-17 18:27:08 +00:00
if (block.blockee?.uri == null) {
throw new Error('renderBlock: missing blockee uri');
}
2022-09-17 18:27:08 +00:00
return {
type: 'Block',
id: `${this.config.url}/blocks/${block.id}`,
actor: `${this.config.url}/users/${block.blockerId}`,
object: block.blockee.uri,
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderCreate(object: IObject, note: Note): ICreate {
2022-09-17 18:27:08 +00:00
const activity = {
id: `${this.config.url}/notes/${note.id}/activity`,
actor: `${this.config.url}/users/${note.userId}`,
type: 'Create',
published: note.createdAt.toISOString(),
object,
2023-02-12 09:47:30 +00:00
} as ICreate;
2022-09-17 18:27:08 +00:00
if (object.to) activity.to = object.to;
if (object.cc) activity.cc = object.cc;
2022-09-17 18:27:08 +00:00
return activity;
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderDelete(object: IObject | string, user: { id: User['id']; host: null }): IDelete {
2022-09-17 18:27:08 +00:00
return {
type: 'Delete',
actor: `${this.config.url}/users/${user.id}`,
object,
published: new Date().toISOString(),
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderDocument(file: DriveFile): IApDocument {
2022-09-17 18:27:08 +00:00
return {
type: 'Document',
mediaType: file.webpublicType ?? file.type,
2022-09-17 18:27:08 +00:00
url: this.driveFileEntityService.getPublicUrl(file),
name: file.comment,
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderEmoji(emoji: Emoji): IApEmoji {
2022-09-17 18:27:08 +00:00
return {
id: `${this.config.url}/emojis/${emoji.name}`,
type: 'Emoji',
name: `:${emoji.name}:`,
2023-02-12 09:47:30 +00:00
updated: emoji.updatedAt != null ? emoji.updatedAt.toISOString() : new Date().toISOString(),
2022-09-17 18:27:08 +00:00
icon: {
type: 'Image',
mediaType: emoji.type ?? 'image/png',
// || emoji.originalUrl してるのは後方互換性のためpublicUrlはstringなので??はだめ)
url: emoji.publicUrl || emoji.originalUrl,
2022-09-17 18:27:08 +00:00
},
};
}
// to anonymise reporters, the reporting actor must be a system user
@bindThis
2023-02-17 06:15:36 +00:00
public renderFlag(user: LocalUser, object: IObject | string, content: string): IFlag {
2022-09-17 18:27:08 +00:00
return {
type: 'Flag',
actor: `${this.config.url}/users/${user.id}`,
content,
object,
};
}
@bindThis
2023-02-13 06:50:22 +00:00
public renderFollowRelay(relay: Relay, relayActor: LocalUser): IFollow {
2023-02-12 09:47:30 +00:00
return {
2022-09-17 18:27:08 +00:00
id: `${this.config.url}/activities/follow-relay/${relay.id}`,
type: 'Follow',
actor: `${this.config.url}/users/${relayActor.id}`,
object: 'https://www.w3.org/ns/activitystreams#Public',
};
}
/**
* Convert (local|remote)(Follower|Followee)ID to URL
* @param id Follower|Followee ID
*/
@bindThis
2022-09-17 18:27:08 +00:00
public async renderFollowUser(id: User['id']) {
const user = await this.usersRepository.findOneByOrFail({ id: id });
return this.userEntityService.isLocalUser(user) ? `${this.config.url}/users/${user.id}` : user.uri;
}
@bindThis
2022-09-17 18:27:08 +00:00
public renderFollow(
follower: { id: User['id']; host: User['host']; uri: User['host'] },
followee: { id: User['id']; host: User['host']; uri: User['host'] },
requestId?: string,
2023-02-12 09:47:30 +00:00
): IFollow {
return {
2022-09-17 18:27:08 +00:00
id: requestId ?? `${this.config.url}/follows/${follower.id}/${followee.id}`,
type: 'Follow',
2023-02-12 09:47:30 +00:00
actor: this.userEntityService.isLocalUser(follower) ? `${this.config.url}/users/${follower.id}` : follower.uri!,
object: this.userEntityService.isLocalUser(followee) ? `${this.config.url}/users/${followee.id}` : followee.uri!,
};
2022-09-17 18:27:08 +00:00
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderHashtag(tag: string): IApHashtag {
2022-09-17 18:27:08 +00:00
return {
type: 'Hashtag',
href: `${this.config.url}/tags/${encodeURIComponent(tag)}`,
name: `#${tag}`,
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderImage(file: DriveFile): IApImage {
2022-09-17 18:27:08 +00:00
return {
type: 'Image',
url: this.driveFileEntityService.getPublicUrl(file),
sensitive: file.isSensitive,
name: file.comment,
};
}
@bindThis
2023-02-13 06:50:22 +00:00
public renderKey(user: LocalUser, key: UserKeypair, postfix?: string): IKey {
2022-09-17 18:27:08 +00:00
return {
id: `${this.config.url}/users/${user.id}${postfix ?? '/publickey'}`,
type: 'Key',
owner: `${this.config.url}/users/${user.id}`,
publicKeyPem: createPublicKey(key.publicKey).export({
type: 'spki',
format: 'pem',
}),
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public async renderLike(noteReaction: NoteReaction, note: { uri: string | null }): Promise<ILike> {
2022-09-17 18:27:08 +00:00
const reaction = noteReaction.reaction;
const object = {
type: 'Like',
id: `${this.config.url}/likes/${noteReaction.id}`,
actor: `${this.config.url}/users/${noteReaction.userId}`,
object: note.uri ? note.uri : `${this.config.url}/notes/${noteReaction.noteId}`,
content: reaction,
_misskey_reaction: reaction,
2023-02-12 09:47:30 +00:00
} as ILike;
2022-09-17 18:27:08 +00:00
if (reaction.startsWith(':')) {
2023-02-03 08:44:25 +00:00
const name = reaction.replaceAll(':', '');
2023-02-12 09:47:30 +00:00
// TODO: cache
2022-09-17 18:27:08 +00:00
const emoji = await this.emojisRepository.findOneBy({
name,
host: IsNull(),
});
if (emoji) object.tag = [this.renderEmoji(emoji)];
}
return object;
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderMention(mention: User): IApMention {
2022-09-17 18:27:08 +00:00
return {
type: 'Mention',
2023-02-13 06:50:22 +00:00
href: this.userEntityService.isRemoteUser(mention) ? mention.uri! : `${this.config.url}/users/${(mention as LocalUser).id}`,
name: this.userEntityService.isRemoteUser(mention) ? `@${mention.username}@${mention.host}` : `@${(mention as LocalUser).username}`,
2022-09-17 18:27:08 +00:00
};
}
@bindThis
public async renderNote(note: Note, dive = true): Promise<IPost> {
2022-09-17 18:27:08 +00:00
const getPromisedFiles = async (ids: string[]) => {
if (!ids || ids.length === 0) return [];
const items = await this.driveFilesRepository.findBy({ id: In(ids) });
return ids.map(id => items.find(item => item.id === id)).filter(item => item != null) as DriveFile[];
};
2022-09-17 18:27:08 +00:00
let inReplyTo;
let inReplyToNote: Note | null;
2022-09-17 18:27:08 +00:00
if (note.replyId) {
inReplyToNote = await this.notesRepository.findOneBy({ id: note.replyId });
2022-09-17 18:27:08 +00:00
if (inReplyToNote != null) {
const inReplyToUser = await this.usersRepository.findOneBy({ id: inReplyToNote.userId });
2022-09-17 18:27:08 +00:00
if (inReplyToUser != null) {
if (inReplyToNote.uri) {
inReplyTo = inReplyToNote.uri;
} else {
if (dive) {
inReplyTo = await this.renderNote(inReplyToNote, false);
} else {
inReplyTo = `${this.config.url}/notes/${inReplyToNote.id}`;
}
}
}
}
} else {
inReplyTo = null;
}
2022-09-17 18:27:08 +00:00
let quote;
2022-09-17 18:27:08 +00:00
if (note.renoteId) {
const renote = await this.notesRepository.findOneBy({ id: note.renoteId });
2022-09-17 18:27:08 +00:00
if (renote) {
quote = renote.uri ? renote.uri : `${this.config.url}/notes/${renote.id}`;
}
}
2022-09-17 18:27:08 +00:00
const attributedTo = `${this.config.url}/users/${note.userId}`;
2022-09-17 18:27:08 +00:00
const mentions = (JSON.parse(note.mentionedRemoteUsers) as IMentionedRemoteUsers).map(x => x.uri);
2022-09-17 18:27:08 +00:00
let to: string[] = [];
let cc: string[] = [];
2022-09-17 18:27:08 +00:00
if (note.visibility === 'public') {
to = ['https://www.w3.org/ns/activitystreams#Public'];
cc = [`${attributedTo}/followers`].concat(mentions);
} else if (note.visibility === 'home') {
to = [`${attributedTo}/followers`];
cc = ['https://www.w3.org/ns/activitystreams#Public'].concat(mentions);
} else if (note.visibility === 'followers') {
to = [`${attributedTo}/followers`];
cc = mentions;
} else {
to = mentions;
}
2022-09-17 18:27:08 +00:00
const mentionedUsers = note.mentions.length > 0 ? await this.usersRepository.findBy({
id: In(note.mentions),
}) : [];
2022-09-17 18:27:08 +00:00
const hashtagTags = (note.tags ?? []).map(tag => this.renderHashtag(tag));
const mentionTags = mentionedUsers.map(u => this.renderMention(u));
2022-09-17 18:27:08 +00:00
const files = await getPromisedFiles(note.fileIds);
2022-09-17 18:27:08 +00:00
const text = note.text ?? '';
let poll: Poll | null = null;
2022-09-17 18:27:08 +00:00
if (note.hasPoll) {
poll = await this.pollsRepository.findOneBy({ noteId: note.id });
}
2022-09-17 18:27:08 +00:00
let apText = text;
2022-09-17 18:27:08 +00:00
if (quote) {
apText += `\n\nRE: ${quote}`;
}
2022-09-17 18:27:08 +00:00
const summary = note.cw === '' ? String.fromCharCode(0x200B) : note.cw;
2022-09-17 18:27:08 +00:00
const content = this.apMfmService.getNoteHtml(Object.assign({}, note, {
text: apText,
}));
2022-09-18 18:11:50 +00:00
const emojis = await this.getEmojis(note.emojis);
2022-09-17 18:27:08 +00:00
const apemojis = emojis.map(emoji => this.renderEmoji(emoji));
2022-09-17 18:27:08 +00:00
const tag = [
...hashtagTags,
...mentionTags,
...apemojis,
];
2022-09-17 18:27:08 +00:00
const asPoll = poll ? {
type: 'Question',
content: this.apMfmService.getNoteHtml(Object.assign({}, note, {
text: text,
})),
[poll.expiresAt && poll.expiresAt < new Date() ? 'closed' : 'endTime']: poll.expiresAt,
[poll.multiple ? 'anyOf' : 'oneOf']: poll.choices.map((text, i) => ({
type: 'Note',
name: text,
replies: {
type: 'Collection',
totalItems: poll!.votes[i],
},
})),
2023-02-12 11:06:10 +00:00
} as const : {};
2022-09-17 18:27:08 +00:00
return {
id: `${this.config.url}/notes/${note.id}`,
type: 'Note',
attributedTo,
2022-09-24 22:44:42 +00:00
summary: summary ?? undefined,
content: content ?? undefined,
2022-09-17 18:27:08 +00:00
_misskey_content: text,
source: {
content: text,
mediaType: 'text/x.misskeymarkdown',
},
_misskey_quote: quote,
quoteUrl: quote,
published: note.createdAt.toISOString(),
to,
cc,
inReplyTo,
attachment: files.map(x => this.renderDocument(x)),
sensitive: note.cw != null || files.some(file => file.isSensitive),
tag,
...asPoll,
};
}
@bindThis
2023-02-13 06:50:22 +00:00
public async renderPerson(user: LocalUser) {
2022-09-17 18:27:08 +00:00
const id = `${this.config.url}/users/${user.id}`;
const isSystem = !!user.username.match(/\./);
const [avatar, banner, profile] = await Promise.all([
user.avatarId ? this.driveFilesRepository.findOneBy({ id: user.avatarId }) : Promise.resolve(undefined),
user.bannerId ? this.driveFilesRepository.findOneBy({ id: user.bannerId }) : Promise.resolve(undefined),
this.userProfilesRepository.findOneByOrFail({ userId: user.id }),
]);
const attachment: {
type: 'PropertyValue',
name: string,
value: string,
identifier?: IIdentifier,
}[] = [];
if (profile.fields) {
for (const field of profile.fields) {
attachment.push({
type: 'PropertyValue',
name: field.name,
value: (field.value != null && field.value.match(/^https?:/))
? `<a href="${new URL(field.value).href}" rel="me nofollow noopener" target="_blank">${new URL(field.value).href}</a>`
: field.value,
});
}
}
2022-09-18 18:11:50 +00:00
const emojis = await this.getEmojis(user.emojis);
2022-09-17 18:27:08 +00:00
const apemojis = emojis.map(emoji => this.renderEmoji(emoji));
const hashtagTags = (user.tags ?? []).map(tag => this.renderHashtag(tag));
const tag = [
...apemojis,
...hashtagTags,
];
const keypair = await this.userKeypairService.getUserKeypair(user.id);
2022-09-17 18:27:08 +00:00
const person = {
type: isSystem ? 'Application' : user.isBot ? 'Service' : 'Person',
id,
inbox: `${id}/inbox`,
outbox: `${id}/outbox`,
followers: `${id}/followers`,
following: `${id}/following`,
featured: `${id}/collections/featured`,
sharedInbox: `${this.config.url}/inbox`,
endpoints: { sharedInbox: `${this.config.url}/inbox` },
url: `${this.config.url}/@${user.username}`,
preferredUsername: user.username,
name: user.name,
summary: profile.description ? this.mfmService.toHtml(mfm.parse(profile.description)) : null,
icon: avatar ? this.renderImage(avatar) : null,
image: banner ? this.renderImage(banner) : null,
tag,
manuallyApprovesFollowers: user.isLocked,
discoverable: !!user.isExplorable,
publicKey: this.renderKey(user, keypair, '#main-key'),
isCat: user.isCat,
attachment: attachment.length ? attachment : undefined,
} as any;
if (profile.birthday) {
person['vcard:bday'] = profile.birthday;
}
if (profile.location) {
person['vcard:Address'] = profile.location;
}
return person;
}
@bindThis
2023-02-12 11:06:10 +00:00
public renderQuestion(user: { id: User['id'] }, note: Note, poll: Poll): IQuestion {
2023-02-12 09:47:30 +00:00
return {
2022-09-17 18:27:08 +00:00
type: 'Question',
id: `${this.config.url}/questions/${note.id}`,
actor: `${this.config.url}/users/${user.id}`,
content: note.text ?? '',
[poll.multiple ? 'anyOf' : 'oneOf']: poll.choices.map((text, i) => ({
name: text,
_misskey_votes: poll.votes[i],
replies: {
type: 'Collection',
totalItems: poll.votes[i],
},
})),
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderReject(object: any, user: { id: User['id'] }): IReject {
2022-09-17 18:27:08 +00:00
return {
type: 'Reject',
actor: `${this.config.url}/users/${user.id}`,
object,
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderRemove(user: { id: User['id'] }, target: any, object: any): IRemove {
2022-09-17 18:27:08 +00:00
return {
type: 'Remove',
actor: `${this.config.url}/users/${user.id}`,
target,
object,
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderTombstone(id: string): ITombstone {
2022-09-17 18:27:08 +00:00
return {
id,
type: 'Tombstone',
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderUndo(object: any, user: { id: User['id'] }): IUndo {
2022-09-17 18:27:08 +00:00
const id = typeof object.id === 'string' && object.id.startsWith(this.config.url) ? `${object.id}/undo` : undefined;
return {
type: 'Undo',
...(id ? { id } : {}),
actor: `${this.config.url}/users/${user.id}`,
object,
published: new Date().toISOString(),
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public renderUpdate(object: any, user: { id: User['id'] }): IUpdate {
return {
2022-09-17 18:27:08 +00:00
id: `${this.config.url}/users/${user.id}#updates/${new Date().getTime()}`,
actor: `${this.config.url}/users/${user.id}`,
type: 'Update',
to: ['https://www.w3.org/ns/activitystreams#Public'],
object,
published: new Date().toISOString(),
2023-02-12 09:47:30 +00:00
};
2022-09-17 18:27:08 +00:00
}
@bindThis
2023-02-13 06:50:22 +00:00
public renderVote(user: { id: User['id'] }, vote: PollVote, note: Note, poll: Poll, pollOwner: RemoteUser): ICreate {
2022-09-17 18:27:08 +00:00
return {
id: `${this.config.url}/users/${user.id}#votes/${vote.id}/activity`,
actor: `${this.config.url}/users/${user.id}`,
type: 'Create',
to: [pollOwner.uri],
published: new Date().toISOString(),
object: {
id: `${this.config.url}/users/${user.id}#votes/${vote.id}`,
type: 'Note',
attributedTo: `${this.config.url}/users/${user.id}`,
to: [pollOwner.uri],
inReplyTo: note.uri,
name: poll.choices[vote.choice],
},
};
}
@bindThis
2023-02-12 09:47:30 +00:00
public addContext<T extends IObject>(x: T): T & { '@context': any; id: string; } {
2022-09-17 18:27:08 +00:00
if (typeof x === 'object' && x.id == null) {
x.id = `${this.config.url}/${uuid()}`;
}
2022-09-17 18:27:08 +00:00
return Object.assign({
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{
// 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',
// 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',
'isCat': 'misskey:isCat',
// vcard
vcard: 'http://www.w3.org/2006/vcard/ns#',
},
],
2023-02-12 09:47:30 +00:00
}, x as T & { id: string; });
2022-09-17 18:27:08 +00:00
}
@bindThis
2022-09-17 18:27:08 +00:00
public async attachLdSignature(activity: any, user: { id: User['id']; host: null; }): Promise<IActivity> {
const keypair = await this.userKeypairService.getUserKeypair(user.id);
2022-09-17 18:27:08 +00:00
const ldSignature = this.ldSignatureService.use();
ldSignature.debug = false;
activity = await ldSignature.signRsaSignature2017(activity, keypair.privateKey, `${this.config.url}/users/${user.id}#main-key`);
2022-09-17 18:27:08 +00:00
return activity;
}
2022-09-17 18:27:08 +00:00
/**
* Render OrderedCollectionPage
* @param id URL of self
* @param totalItems Number of total items
* @param orderedItems Items
* @param partOf URL of base
* @param prev URL of prev page (optional)
* @param next URL of next page (optional)
*/
@bindThis
2022-09-17 18:27:08 +00:00
public renderOrderedCollectionPage(id: string, totalItems: any, orderedItems: any, partOf: string, prev?: string, next?: string) {
const page = {
id,
partOf,
type: 'OrderedCollectionPage',
totalItems,
orderedItems,
} as any;
if (prev) page.prev = prev;
if (next) page.next = next;
return page;
}
/**
* Render OrderedCollection
* @param id URL of self
* @param totalItems Total number of items
* @param first URL of first page (optional)
* @param last URL of last page (optional)
* @param orderedItems attached objects (optional)
*/
@bindThis
public renderOrderedCollection(id: string | null, totalItems: any, first?: string, last?: string, orderedItems?: IObject[]) {
2022-09-17 18:27:08 +00:00
const page: any = {
id,
type: 'OrderedCollection',
totalItems,
};
2022-09-17 18:27:08 +00:00
if (first) page.first = first;
if (last) page.last = last;
if (orderedItems) page.orderedItems = orderedItems;
2022-09-17 18:27:08 +00:00
return page;
}
@bindThis
2022-09-18 18:11:50 +00:00
private async getEmojis(names: string[]): Promise<Emoji[]> {
2022-09-17 18:27:08 +00:00
if (names == null || names.length === 0) return [];
const emojis = await Promise.all(
names.map(name => this.emojisRepository.findOneBy({
name,
host: IsNull(),
})),
);
return emojis.filter(emoji => emoji != null) as Emoji[];
}
}