2018-04-01 19:18:36 +09:00
|
|
|
import renderDocument from './document';
|
|
|
|
import renderHashtag from './hashtag';
|
2018-06-13 05:11:55 +09:00
|
|
|
import renderMention from './mention';
|
2018-04-02 13:15:53 +09:00
|
|
|
import config from '../../../config';
|
2018-06-18 14:28:43 +09:00
|
|
|
import DriveFile, { IDriveFile } from '../../../models/drive-file';
|
2018-04-08 02:30:37 +09:00
|
|
|
import Note, { INote } from '../../../models/note';
|
2018-04-08 06:55:26 +09:00
|
|
|
import User from '../../../models/user';
|
2018-04-22 10:44:17 +09:00
|
|
|
import toHtml from '../misc/get-note-html';
|
2018-04-05 19:19:00 +09:00
|
|
|
|
2018-06-18 14:28:43 +09:00
|
|
|
export default async function renderNote(note: INote, dive = true): Promise<any> {
|
|
|
|
const promisedFiles: Promise<IDriveFile[]> = note.mediaIds
|
2018-04-08 02:30:37 +09:00
|
|
|
? DriveFile.find({ _id: { $in: note.mediaIds } })
|
2018-04-05 19:19:00 +09:00
|
|
|
: Promise.resolve([]);
|
2018-04-01 19:18:36 +09:00
|
|
|
|
|
|
|
let inReplyTo;
|
|
|
|
|
2018-04-08 02:30:37 +09:00
|
|
|
if (note.replyId) {
|
|
|
|
const inReplyToNote = await Note.findOne({
|
|
|
|
_id: note.replyId,
|
2018-04-01 19:18:36 +09:00
|
|
|
});
|
|
|
|
|
2018-04-08 02:30:37 +09:00
|
|
|
if (inReplyToNote !== null) {
|
2018-04-01 19:18:36 +09:00
|
|
|
const inReplyToUser = await User.findOne({
|
2018-04-08 02:30:37 +09:00
|
|
|
_id: inReplyToNote.userId,
|
2018-04-01 19:18:36 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (inReplyToUser !== null) {
|
2018-04-10 04:38:02 +09: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 19:18:36 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inReplyTo = null;
|
|
|
|
}
|
|
|
|
|
2018-04-08 06:55:26 +09:00
|
|
|
const user = await User.findOne({
|
|
|
|
_id: note.userId
|
|
|
|
});
|
|
|
|
|
2018-04-08 15:15:22 +09:00
|
|
|
const attributedTo = `${config.url}/users/${user._id}`;
|
2018-04-01 19:18:36 +09:00
|
|
|
|
2018-06-13 05:11:55 +09:00
|
|
|
const mentions = note.mentionedRemoteUsers && note.mentionedRemoteUsers.length > 0
|
|
|
|
? note.mentionedRemoteUsers.map(x => x.uri)
|
|
|
|
: [];
|
|
|
|
|
|
|
|
const cc = ['public', 'home', 'followers'].includes(note.visibility)
|
|
|
|
? [`${attributedTo}/followers`].concat(mentions)
|
|
|
|
: [];
|
|
|
|
|
2018-06-19 08:03:00 +09:00
|
|
|
const mentionedUsers = note.mentions ? await User.find({
|
2018-06-17 16:45:01 +09:00
|
|
|
_id: {
|
|
|
|
$in: note.mentions
|
|
|
|
}
|
2018-06-19 08:03:00 +09:00
|
|
|
}) : [];
|
2018-06-17 16:45:01 +09:00
|
|
|
|
2018-06-17 17:15:03 +09:00
|
|
|
const hashtagTags = (note.tags || []).map(tag => renderHashtag(tag));
|
|
|
|
const mentionTags = mentionedUsers.map(u => renderMention(u));
|
2018-06-17 16:45:01 +09:00
|
|
|
const tag = [
|
|
|
|
...hashtagTags,
|
|
|
|
...mentionTags,
|
|
|
|
];
|
2018-06-13 05:11:55 +09:00
|
|
|
|
2018-04-01 19:18:36 +09:00
|
|
|
return {
|
2018-04-08 02:30:37 +09:00
|
|
|
id: `${config.url}/notes/${note._id}`,
|
2018-04-01 19:18:36 +09:00
|
|
|
type: 'Note',
|
|
|
|
attributedTo,
|
2018-06-17 14:11:31 +09:00
|
|
|
summary: note.cw,
|
2018-04-22 10:44:17 +09:00
|
|
|
content: toHtml(note),
|
2018-04-08 02:30:37 +09:00
|
|
|
published: note.createdAt.toISOString(),
|
2018-04-01 19:18:36 +09:00
|
|
|
to: 'https://www.w3.org/ns/activitystreams#Public',
|
2018-06-13 05:11:55 +09:00
|
|
|
cc,
|
2018-04-01 19:18:36 +09:00
|
|
|
inReplyTo,
|
|
|
|
attachment: (await promisedFiles).map(renderDocument),
|
2018-06-13 05:11:55 +09:00
|
|
|
tag
|
2018-04-01 19:18:36 +09:00
|
|
|
};
|
2018-04-10 04:38:02 +09:00
|
|
|
}
|