forked from misskey-dev/misskey
-
Notifications
You must be signed in to change notification settings - Fork 12
/
note.ts
187 lines (157 loc) · 4.45 KB
/
note.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import renderDocument from './document';
import renderHashtag from './hashtag';
import renderMention from './mention';
import renderEmoji from './emoji';
import config from '../../../config';
import DriveFile, { IDriveFile } from '../../../models/drive-file';
import Note, { INote } from '../../../models/note';
import User from '../../../models/user';
import toHtml from '../misc/get-note-html';
import Emoji, { IEmoji } from '../../../models/emoji';
export default async function renderNote(note: INote, dive = true): Promise<any> {
const promisedFiles: Promise<IDriveFile[]> = note.fileIds
? DriveFile.find({ _id: { $in: note.fileIds } })
: Promise.resolve([]);
let inReplyTo;
let inReplyToNote: INote;
if (note.replyId) {
inReplyToNote = await Note.findOne({
_id: note.replyId,
});
if (inReplyToNote !== null) {
const inReplyToUser = await User.findOne({
_id: inReplyToNote.userId,
});
if (inReplyToUser !== null) {
if (inReplyToNote.uri) {
inReplyTo = inReplyToNote.uri;
} else {
if (dive) {
inReplyTo = await renderNote(inReplyToNote, false);
} else {
inReplyTo = `${config.url}/notes/${inReplyToNote._id}`;
}
}
}
}
} else {
inReplyTo = null;
}
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}`;
}
}
const user = await User.findOne({
_id: note.userId
});
const attributedTo = `${config.url}/users/${user._id}`;
const mentions = note.mentionedRemoteUsers && note.mentionedRemoteUsers.length > 0
? note.mentionedRemoteUsers.map(x => x.uri)
: [];
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;
}
const mentionedUsers = note.mentions ? await User.find({
_id: {
$in: note.mentions
}
}) : [];
const hashtagTags = (note.tags || []).map(tag => renderHashtag(tag));
const mentionTags = mentionedUsers.map(u => renderMention(u));
const files = await promisedFiles;
let text = note.text;
if (note.poll != null) {
if (text == null) text = '';
const url = `${config.url}/notes/${note._id}`;
// TODO: i18n
text += `\n[リモートで結果を表示](${url})`;
}
let apText = text;
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}`);
apText += '\n----------------------------------------\n';
apText += cs.join('\n');
apText += '\n----------------------------------------\n';
apText += '番号を返信して投票';
}
if (quote) {
apText += `\n\nRE: ${quote}`;
}
const summary = note.cw === '' ? String.fromCharCode(0x200B) : note.cw;
const content = toHtml(Object.assign({}, note, {
text: apText
}));
const emojis = await getEmojis(note.emojis);
const apemojis = emojis.map(emoji => renderEmoji(emoji));
const tag = [
...hashtagTags,
...mentionTags,
...apemojis,
];
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
}
}))
} : {};
return {
id: `${config.url}/notes/${note._id}`,
type: 'Note',
attributedTo,
summary,
content,
_misskey_content: text,
_misskey_quote: quote,
published: note.createdAt.toISOString(),
to,
cc,
inReplyTo,
attachment: files.map(renderDocument),
sensitive: note.cw != null || files.some(file => file.metadata.isSensitive),
tag,
...asPoll
};
}
export async function getEmojis(names: string[]): Promise<IEmoji[]> {
if (names == null || names.length < 1) return [];
const emojis = await Promise.all(
names.map(name => Emoji.findOne({
name,
host: null
}))
);
return emojis.filter(emoji => emoji != null);
}