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