2018-04-01 10:18:36 +00:00
|
|
|
import renderDocument from './document';
|
|
|
|
import renderHashtag from './hashtag';
|
2018-06-12 20:11:55 +00:00
|
|
|
import renderMention from './mention';
|
2018-11-01 23:59:40 +00:00
|
|
|
import renderEmoji from './emoji';
|
2018-04-02 04:15:53 +00:00
|
|
|
import config from '../../../config';
|
2018-06-18 05:28:43 +00:00
|
|
|
import DriveFile, { IDriveFile } from '../../../models/drive-file';
|
2018-04-07 17:30:37 +00:00
|
|
|
import Note, { INote } from '../../../models/note';
|
2018-04-07 21:55:26 +00:00
|
|
|
import User from '../../../models/user';
|
2018-04-22 01:44:17 +00:00
|
|
|
import toHtml from '../misc/get-note-html';
|
2018-11-01 23:59:40 +00:00
|
|
|
import Emoji, { IEmoji } from '../../../models/emoji';
|
2018-04-05 10:19:00 +00:00
|
|
|
|
2018-06-18 05:28:43 +00:00
|
|
|
export default async function renderNote(note: INote, dive = true): Promise<any> {
|
2018-09-05 10:32:46 +00:00
|
|
|
const promisedFiles: Promise<IDriveFile[]> = note.fileIds
|
|
|
|
? DriveFile.find({ _id: { $in: note.fileIds } })
|
2018-04-05 10:19:00 +00:00
|
|
|
: Promise.resolve([]);
|
2018-04-01 10:18:36 +00:00
|
|
|
|
|
|
|
let inReplyTo;
|
2019-03-06 13:55:47 +00:00
|
|
|
let inReplyToNote: INote;
|
2018-04-01 10:18:36 +00:00
|
|
|
|
2018-04-07 17:30:37 +00:00
|
|
|
if (note.replyId) {
|
2019-03-06 13:55:47 +00:00
|
|
|
inReplyToNote = await Note.findOne({
|
2018-04-07 17:30:37 +00:00
|
|
|
_id: note.replyId,
|
2018-04-01 10:18:36 +00:00
|
|
|
});
|
|
|
|
|
2018-04-07 17:30:37 +00:00
|
|
|
if (inReplyToNote !== null) {
|
2018-04-01 10:18:36 +00:00
|
|
|
const inReplyToUser = await User.findOne({
|
2018-04-07 17:30:37 +00:00
|
|
|
_id: inReplyToNote.userId,
|
2018-04-01 10:18:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (inReplyToUser !== null) {
|
2018-04-09 19:38:02 +00:00
|
|
|
if (inReplyToNote.uri) {
|
|
|
|
inReplyTo = inReplyToNote.uri;
|
|
|
|
} else {
|
|
|
|
if (dive) {
|
|
|
|
inReplyTo = await renderNote(inReplyToNote, false);
|
|
|
|
} else {
|
|
|
|
inReplyTo = `${config.url}/notes/${inReplyToNote._id}`;
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 10:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inReplyTo = null;
|
|
|
|
}
|
|
|
|
|
2018-11-22 17:10:07 +00:00
|
|
|
let quote;
|
|
|
|
|
|
|
|
if (note.renoteId) {
|
|
|
|
const renote = await Note.findOne({
|
|
|
|
_id: note.renoteId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (renote) {
|
|
|
|
quote = renote.uri ? renote.uri : `${config.url}/notes/${renote._id}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 21:55:26 +00:00
|
|
|
const user = await User.findOne({
|
|
|
|
_id: note.userId
|
|
|
|
});
|
|
|
|
|
2018-04-08 06:15:22 +00:00
|
|
|
const attributedTo = `${config.url}/users/${user._id}`;
|
2018-04-01 10:18:36 +00:00
|
|
|
|
2018-06-12 20:11:55 +00:00
|
|
|
const mentions = note.mentionedRemoteUsers && note.mentionedRemoteUsers.length > 0
|
|
|
|
? note.mentionedRemoteUsers.map(x => x.uri)
|
|
|
|
: [];
|
|
|
|
|
2018-08-12 18:49:17 +00: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 20:11:55 +00:00
|
|
|
|
2018-06-18 23:03:00 +00:00
|
|
|
const mentionedUsers = note.mentions ? await User.find({
|
2018-06-17 07:45:01 +00:00
|
|
|
_id: {
|
|
|
|
$in: note.mentions
|
|
|
|
}
|
2018-06-18 23:03:00 +00:00
|
|
|
}) : [];
|
2018-06-17 07:45:01 +00:00
|
|
|
|
2018-06-17 08:15:03 +00:00
|
|
|
const hashtagTags = (note.tags || []).map(tag => renderHashtag(tag));
|
|
|
|
const mentionTags = mentionedUsers.map(u => renderMention(u));
|
2018-06-12 20:11:55 +00:00
|
|
|
|
2018-08-19 09:08:29 +00:00
|
|
|
const files = await promisedFiles;
|
|
|
|
|
2018-09-17 03:18:59 +00:00
|
|
|
let text = note.text;
|
|
|
|
|
2019-01-21 04:27:19 +00:00
|
|
|
let question: string;
|
2018-09-17 02:59:24 +00:00
|
|
|
if (note.poll != null) {
|
2018-09-17 03:18:59 +00:00
|
|
|
if (text == null) text = '';
|
2018-09-17 02:59:24 +00:00
|
|
|
const url = `${config.url}/notes/${note._id}`;
|
|
|
|
// TODO: i18n
|
2019-01-21 07:34:17 +00:00
|
|
|
text += `\n[リモートで結果を表示](${url})`;
|
2019-01-21 04:27:19 +00:00
|
|
|
|
|
|
|
question = `${config.url}/questions/${note._id}`;
|
2018-09-17 02:59:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 07:30:28 +00:00
|
|
|
let apText = text;
|
2019-01-21 04:27:19 +00:00
|
|
|
if (apText == null) apText = '';
|
|
|
|
|
|
|
|
// Provides choices as text for AP
|
|
|
|
if (note.poll != null) {
|
|
|
|
const cs = note.poll.choices.map(c => `${c.id}: ${c.text}`);
|
2019-01-21 07:34:17 +00:00
|
|
|
apText += '\n----------------------------------------\n';
|
2019-01-21 04:27:19 +00:00
|
|
|
apText += cs.join('\n');
|
2019-01-21 07:34:17 +00:00
|
|
|
apText += '\n----------------------------------------\n';
|
|
|
|
apText += '番号を返信して投票';
|
2019-01-21 04:27:19 +00:00
|
|
|
}
|
2018-12-06 07:30:28 +00:00
|
|
|
|
2018-12-18 20:07:54 +00:00
|
|
|
if (quote) {
|
|
|
|
apText += `\n\nRE: ${quote}`;
|
2018-09-17 02:59:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:15:10 +00:00
|
|
|
const summary = note.cw === '' ? String.fromCharCode(0x200B) : note.cw;
|
|
|
|
|
2018-12-06 07:30:28 +00:00
|
|
|
const content = toHtml(Object.assign({}, note, {
|
|
|
|
text: apText
|
|
|
|
}));
|
2018-11-01 23:59:40 +00:00
|
|
|
|
2018-11-05 10:20:35 +00:00
|
|
|
const emojis = await getEmojis(note.emojis);
|
2018-11-01 23:59:40 +00:00
|
|
|
const apemojis = emojis.map(emoji => renderEmoji(emoji));
|
|
|
|
|
|
|
|
const tag = [
|
|
|
|
...hashtagTags,
|
|
|
|
...mentionTags,
|
|
|
|
...apemojis,
|
|
|
|
];
|
|
|
|
|
2019-03-06 13:55:47 +00:00
|
|
|
const {
|
|
|
|
choices = [],
|
|
|
|
expiresAt = null,
|
|
|
|
multiple = false
|
|
|
|
} = note.poll || {};
|
|
|
|
|
|
|
|
const asPoll = note.poll ? {
|
|
|
|
type: 'Question',
|
|
|
|
content: toHtml(Object.assign({}, note, {
|
|
|
|
text: text
|
|
|
|
})),
|
|
|
|
_misskey_fallback_content: content,
|
|
|
|
[expiresAt && expiresAt < new Date() ? 'closed' : 'endTime']: expiresAt,
|
|
|
|
[multiple ? 'anyOf' : 'oneOf']: choices.map(({ text, votes }) => ({
|
|
|
|
type: 'Note',
|
|
|
|
name: text,
|
|
|
|
replies: {
|
|
|
|
type: 'Collection',
|
|
|
|
totalItems: votes
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
} : {};
|
|
|
|
|
2018-04-01 10:18:36 +00:00
|
|
|
return {
|
2018-04-07 17:30:37 +00:00
|
|
|
id: `${config.url}/notes/${note._id}`,
|
2018-04-01 10:18:36 +00:00
|
|
|
type: 'Note',
|
|
|
|
attributedTo,
|
2018-11-30 22:15:10 +00:00
|
|
|
summary,
|
2018-11-01 23:59:40 +00:00
|
|
|
content,
|
2018-09-16 18:36:58 +00:00
|
|
|
_misskey_content: text,
|
2018-11-22 17:10:07 +00:00
|
|
|
_misskey_quote: quote,
|
2019-01-21 04:27:19 +00:00
|
|
|
_misskey_question: question,
|
2018-04-07 17:30:37 +00:00
|
|
|
published: note.createdAt.toISOString(),
|
2018-08-12 18:49:17 +00:00
|
|
|
to,
|
2018-06-12 20:11:55 +00:00
|
|
|
cc,
|
2018-04-01 10:18:36 +00:00
|
|
|
inReplyTo,
|
2018-08-19 09:08:29 +00:00
|
|
|
attachment: files.map(renderDocument),
|
|
|
|
sensitive: files.some(file => file.metadata.isSensitive),
|
2019-03-06 13:55:47 +00:00
|
|
|
tag,
|
|
|
|
...asPoll
|
2018-04-01 10:18:36 +00:00
|
|
|
};
|
2018-04-09 19:38:02 +00:00
|
|
|
}
|
2018-11-01 23:59:40 +00:00
|
|
|
|
2018-12-06 01:02:04 +00:00
|
|
|
export async function getEmojis(names: string[]): Promise<IEmoji[]> {
|
2018-11-01 23:59:40 +00:00
|
|
|
if (names == null || names.length < 1) return [];
|
|
|
|
|
|
|
|
const emojis = await Promise.all(
|
2018-11-05 10:29:50 +00:00
|
|
|
names.map(name => Emoji.findOne({
|
|
|
|
name,
|
|
|
|
host: null
|
|
|
|
}))
|
2018-11-01 23:59:40 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return emojis.filter(emoji => emoji != null);
|
|
|
|
}
|