yumechi-no-kuni/src/remote/activitypub/renderer/note.ts

137 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-04-01 05:18:36 -05:00
import renderDocument from './document';
import renderHashtag from './hashtag';
2018-06-12 15:11:55 -05:00
import renderMention from './mention';
import renderEmoji from './emoji';
2018-04-01 23:15:53 -05:00
import config from '../../../config';
2018-06-18 00:28:43 -05:00
import DriveFile, { IDriveFile } from '../../../models/drive-file';
2018-04-07 12:30:37 -05:00
import Note, { INote } from '../../../models/note';
2018-04-07 16:55:26 -05:00
import User from '../../../models/user';
2018-04-21 20:44:17 -05:00
import toHtml from '../misc/get-note-html';
import Emoji, { IEmoji } from '../../../models/emoji';
2018-04-05 05:19:00 -05:00
2018-06-18 00:28:43 -05:00
export default async function renderNote(note: INote, dive = true): Promise<any> {
2018-09-05 05:32:46 -05:00
const promisedFiles: Promise<IDriveFile[]> = note.fileIds
? DriveFile.find({ _id: { $in: note.fileIds } })
2018-04-05 05:19:00 -05:00
: Promise.resolve([]);
2018-04-01 05:18:36 -05:00
let inReplyTo;
2018-04-07 12:30:37 -05:00
if (note.replyId) {
const inReplyToNote = await Note.findOne({
_id: note.replyId,
2018-04-01 05:18:36 -05:00
});
2018-04-07 12:30:37 -05:00
if (inReplyToNote !== null) {
2018-04-01 05:18:36 -05:00
const inReplyToUser = await User.findOne({
2018-04-07 12:30:37 -05:00
_id: inReplyToNote.userId,
2018-04-01 05:18:36 -05:00
});
if (inReplyToUser !== null) {
if (inReplyToNote.uri) {
inReplyTo = inReplyToNote.uri;
} else {
if (dive) {
inReplyTo = await renderNote(inReplyToNote, false);
} else {
inReplyTo = `${config.url}/notes/${inReplyToNote._id}`;
}
}
2018-04-01 05:18:36 -05:00
}
}
} else {
inReplyTo = null;
}
2018-04-07 16:55:26 -05:00
const user = await User.findOne({
_id: note.userId
});
2018-04-08 01:15:22 -05:00
const attributedTo = `${config.url}/users/${user._id}`;
2018-04-01 05:18:36 -05:00
2018-06-12 15:11:55 -05:00
const mentions = note.mentionedRemoteUsers && note.mentionedRemoteUsers.length > 0
? note.mentionedRemoteUsers.map(x => x.uri)
: [];
2018-08-12 13:49:17 -05:00
let to: string[] = [];
let cc: string[] = [];
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;
}
2018-06-12 15:11:55 -05:00
2018-06-18 18:03:00 -05:00
const mentionedUsers = note.mentions ? await User.find({
2018-06-17 02:45:01 -05:00
_id: {
$in: note.mentions
}
2018-06-18 18:03:00 -05:00
}) : [];
2018-06-17 02:45:01 -05:00
2018-06-17 03:15:03 -05:00
const hashtagTags = (note.tags || []).map(tag => renderHashtag(tag));
const mentionTags = mentionedUsers.map(u => renderMention(u));
2018-06-12 15:11:55 -05:00
2018-08-19 04:08:29 -05:00
const files = await promisedFiles;
2018-09-16 22:18:59 -05:00
let text = note.text;
2018-09-16 21:59:24 -05:00
if (note.poll != null) {
2018-09-16 22:18:59 -05:00
if (text == null) text = '';
2018-09-16 21:59:24 -05:00
const url = `${config.url}/notes/${note._id}`;
// TODO: i18n
2018-09-16 22:18:59 -05:00
text += `\n\n[投票を見る](${url})`;
2018-09-16 21:59:24 -05:00
}
if (note.renoteId != null) {
2018-09-16 22:18:59 -05:00
if (text == null) text = '';
2018-09-16 21:59:24 -05:00
const url = `${config.url}/notes/${note.renoteId}`;
2018-09-16 22:18:59 -05:00
text += `\n\nRE: ${url}`;
2018-09-16 21:59:24 -05:00
}
const content = toHtml(Object.assign({}, note, { text }));
const emojis = await getEmojis(note.emojis);
const apemojis = emojis.map(emoji => renderEmoji(emoji));
const tag = [
...hashtagTags,
...mentionTags,
...apemojis,
];
2018-04-01 05:18:36 -05:00
return {
2018-04-07 12:30:37 -05:00
id: `${config.url}/notes/${note._id}`,
2018-04-01 05:18:36 -05:00
type: 'Note',
attributedTo,
2018-06-17 00:11:31 -05:00
summary: note.cw,
content,
2018-09-16 13:36:58 -05:00
_misskey_content: text,
2018-04-07 12:30:37 -05:00
published: note.createdAt.toISOString(),
2018-08-12 13:49:17 -05:00
to,
2018-06-12 15:11:55 -05:00
cc,
2018-04-01 05:18:36 -05:00
inReplyTo,
2018-08-19 04:08:29 -05:00
attachment: files.map(renderDocument),
sensitive: files.some(file => file.metadata.isSensitive),
2018-06-12 15:11:55 -05:00
tag
2018-04-01 05:18:36 -05:00
};
}
async function getEmojis(names: string[]): Promise<IEmoji[]> {
if (names == null || names.length < 1) return [];
const emojis = await Promise.all(
2018-11-05 04:29:50 -06:00
names.map(name => Emoji.findOne({
name,
host: null
}))
);
return emojis.filter(emoji => emoji != null);
}