2018-04-01 10:18:36 +00:00
|
|
|
import renderDocument from './document';
|
|
|
|
import renderHashtag from './hashtag';
|
2018-04-02 04:15:53 +00:00
|
|
|
import config from '../../../config';
|
2018-04-01 19:15:27 +00:00
|
|
|
import DriveFile 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-05 10:19:00 +00:00
|
|
|
|
2018-04-07 21:55:26 +00:00
|
|
|
export default async (note: INote) => {
|
2018-04-07 17:30:37 +00:00
|
|
|
const promisedFiles = note.mediaIds
|
|
|
|
? DriveFile.find({ _id: { $in: note.mediaIds } })
|
2018-04-05 10:19:00 +00:00
|
|
|
: Promise.resolve([]);
|
2018-04-01 10:18:36 +00:00
|
|
|
|
|
|
|
let inReplyTo;
|
|
|
|
|
2018-04-07 17:30:37 +00:00
|
|
|
if (note.replyId) {
|
|
|
|
const inReplyToNote = await Note.findOne({
|
|
|
|
_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-07 17:30:37 +00:00
|
|
|
inReplyTo = inReplyToNote.uri || `${config.url}/notes/${inReplyToNote._id}`;
|
2018-04-01 10:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inReplyTo = null;
|
|
|
|
}
|
|
|
|
|
2018-04-07 21:55:26 +00:00
|
|
|
const user = await User.findOne({
|
|
|
|
_id: note.userId
|
|
|
|
});
|
|
|
|
|
2018-04-01 10:18:36 +00:00
|
|
|
const attributedTo = `${config.url}/@${user.username}`;
|
|
|
|
|
|
|
|
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-04-07 17:30:37 +00:00
|
|
|
content: note.textHtml,
|
|
|
|
published: note.createdAt.toISOString(),
|
2018-04-01 10:18:36 +00:00
|
|
|
to: 'https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
cc: `${attributedTo}/followers`,
|
|
|
|
inReplyTo,
|
|
|
|
attachment: (await promisedFiles).map(renderDocument),
|
2018-04-07 17:30:37 +00:00
|
|
|
tag: (note.tags || []).map(renderHashtag)
|
2018-04-01 10:18:36 +00:00
|
|
|
};
|
|
|
|
};
|