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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
import * as mongo from 'mongodb';
import * as deepcopy from 'deepcopy';
import rap from '@prezzemolo/rap';
import db, { dbLogger } from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { length } from 'stringz';
import { IUser, pack as packUser } from './user';
import { pack as packApp } from './app';
import PollVote from './poll-vote';
import Reaction from './note-reaction';
import { packMany as packFileMany, IDriveFile } from './drive-file';
import Following from './following';
import Emoji from './emoji';
const Note = db.get<INote>('notes');
Note.createIndex('uri', { sparse: true, unique: true });
Note.createIndex('userId');
Note.createIndex('mentions');
Note.createIndex('visibleUserIds');
Note.createIndex('replyId');
Note.createIndex('tagsLower');
Note.createIndex('_user.host');
Note.createIndex('_files._id');
Note.createIndex('_files.contentType');
Note.createIndex({ createdAt: -1 });
Note.createIndex({ score: -1 }, { sparse: true });
export default Note;
export function isValidCw(text: string): boolean {
return length(text.trim()) <= 100;
}
export type INote = {
_id: mongo.ObjectID;
createdAt: Date;
deletedAt: Date;
fileIds: mongo.ObjectID[];
replyId: mongo.ObjectID;
renoteId: mongo.ObjectID;
poll: IPoll;
text: string;
tags: string[];
tagsLower: string[];
emojis: string[];
cw: string;
userId: mongo.ObjectID;
appId: mongo.ObjectID;
viaMobile: boolean;
localOnly: boolean;
renoteCount: number;
repliesCount: number;
reactionCounts: any;
mentions: mongo.ObjectID[];
mentionedRemoteUsers: {
uri: string;
username: string;
host: string;
}[];
/**
* public ... 公開
* home ... ホームタイムライン(ユーザーページのタイムライン含む)のみに流す
* followers ... フォロワーのみ
* specified ... visibleUserIds で指定したユーザーのみ
*/
visibility: 'public' | 'home' | 'followers' | 'specified';
visibleUserIds: mongo.ObjectID[];
geo: {
coordinates: number[];
altitude: number;
accuracy: number;
altitudeAccuracy: number;
heading: number;
speed: number;
};
uri: string;
/**
* 人気の投稿度合いを表すスコア
*/
score: number;
// 非正規化
_reply?: {
userId: mongo.ObjectID;
};
_renote?: {
userId: mongo.ObjectID;
};
_user: {
host: string;
inbox?: string;
};
_files?: IDriveFile[];
};
export type IPoll = {
choices: IChoice[]
};
export type IChoice = {
id: number;
text: string;
votes: number;
};
export const hideNote = async (packedNote: any, meId: mongo.ObjectID) => {
let hide = false;
// visibility が private かつ投稿者のIDが自分のIDではなかったら非表示(後方互換性のため)
if (packedNote.visibility == 'private' && (meId == null || !meId.equals(packedNote.userId))) {
hide = true;
}
// visibility が specified かつ自分が指定されていなかったら非表示
if (packedNote.visibility == 'specified') {
if (meId == null) {
hide = true;
} else if (meId.equals(packedNote.userId)) {
hide = false;
} else {
// 指定されているかどうか
const specified = packedNote.visibleUserIds.some((id: any) => meId.equals(id));
if (specified) {
hide = false;
} else {
hide = true;
}
}
}
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
if (packedNote.visibility == 'followers') {
if (meId == null) {
hide = true;
} else if (meId.equals(packedNote.userId)) {
hide = false;
} else if (packedNote.reply && meId.equals(packedNote.reply.userId)) {
// 自分の投稿に対するリプライ
hide = false;
} else if (packedNote.mentions && packedNote.mentions.some((id: any) => meId.equals(id))) {
// 自分へのメンション
hide = false;
} else {
// フォロワーかどうか
const following = await Following.findOne({
followeeId: packedNote.userId,
followerId: meId
});
if (following == null) {
hide = true;
} else {
hide = false;
}
}
}
if (hide) {
packedNote.fileIds = [];
packedNote.files = [];
packedNote.text = null;
packedNote.poll = null;
packedNote.cw = null;
packedNote.tags = [];
packedNote.geo = null;
packedNote.isHidden = true;
}
};
export const packMany = (
notes: (string | mongo.ObjectID | INote)[],
me?: string | mongo.ObjectID | IUser,
options?: {
detail?: boolean;
skipHide?: boolean;
}
) => {
return Promise.all(notes.map(n => pack(n, me, options)));
};
/**
* Pack a note for API response
*
* @param note target
* @param me? serializee
* @param options? serialize options
* @return response
*/
export const pack = async (
note: string | mongo.ObjectID | INote,
me?: string | mongo.ObjectID | IUser,
options?: {
detail?: boolean;
skipHide?: boolean;
}
) => {
const opts = Object.assign({
detail: true,
skipHide: false
}, options);
// Me
const meId: mongo.ObjectID = me
? isObjectId(me)
? me as mongo.ObjectID
: typeof me === 'string'
? new mongo.ObjectID(me)
: (me as IUser)._id
: null;
let _note: any;
// Populate the note if 'note' is ID
if (isObjectId(note)) {
_note = await Note.findOne({
_id: note
});
} else if (typeof note === 'string') {
_note = await Note.findOne({
_id: new mongo.ObjectID(note)
});
} else {
_note = deepcopy(note);
}
// (データベースの欠損などで)投稿がデータベース上に見つからなかったとき
if (_note == null) {
dbLogger.warn(`[DAMAGED DB] (missing) pkg: note :: ${note}`);
return null;
}
const id = _note._id;
// _note._userを消す前か、_note.userを解決した後でないとホストがわからない
if (_note._user) {
const host = _note._user.host;
// 互換性のため。(古いMisskeyではNoteにemojisが無い)
if (_note.emojis == null) {
_note.emojis = Emoji.find({
host: host
}, {
fields: { _id: false }
});
} else {
_note.emojis = Emoji.find({
name: { $in: _note.emojis },
host: host
}, {
fields: { _id: false }
});
}
}
// Rename _id to id
_note.id = _note._id;
delete _note._id;
delete _note.prev;
delete _note.next;
delete _note.tagsLower;
delete _note.score;
delete _note._user;
delete _note._reply;
delete _note._renote;
delete _note._files;
delete _note._replyIds;
delete _note.mentionedRemoteUsers;
if (_note.geo) delete _note.geo.type;
// Populate user
_note.user = packUser(_note.userId, meId);
// Populate app
if (_note.appId) {
_note.app = packApp(_note.appId);
}
// Populate files
_note.files = packFileMany(_note.fileIds || []);
// Some counts
_note.renoteCount = _note.renoteCount || 0;
_note.repliesCount = _note.repliesCount || 0;
_note.reactionCounts = _note.reactionCounts || {};
// 後方互換性のため
_note.mediaIds = _note.fileIds;
_note.media = _note.files;
// When requested a detailed note data
if (opts.detail) {
if (_note.replyId) {
// Populate reply to note
_note.reply = pack(_note.replyId, meId, {
detail: false
});
}
if (_note.renoteId) {
// Populate renote
_note.renote = pack(_note.renoteId, meId, {
detail: _note.text == null
});
}
// Poll
if (meId && _note.poll) {
_note.poll = (async poll => {
const vote = await PollVote
.findOne({
userId: meId,
noteId: id
});
if (vote != null) {
const myChoice = poll.choices
.filter((c: any) => c.id == vote.choice)[0];
myChoice.isVoted = true;
}
return poll;
})(_note.poll);
}
if (meId) {
// Fetch my reaction
_note.myReaction = (async () => {
const reaction = await Reaction
.findOne({
userId: meId,
noteId: id,
deletedAt: { $exists: false }
});
if (reaction) {
return reaction.reaction;
}
return null;
})();
}
}
// resolve promises in _note object
_note = await rap(_note);
//#region (データベースの欠損などで)参照しているデータがデータベース上に見つからなかったとき
if (_note.user == null) {
dbLogger.warn(`[DAMAGED DB] (missing) pkg: note -> user :: ${_note.id} (user ${_note.userId})`);
return null;
}
if (opts.detail) {
if (_note.replyId != null && _note.reply == null) {
dbLogger.warn(`[DAMAGED DB] (missing) pkg: note -> reply :: ${_note.id} (reply ${_note.replyId})`);
return null;
}
if (_note.renoteId != null && _note.renote == null) {
dbLogger.warn(`[DAMAGED DB] (missing) pkg: note -> renote :: ${_note.id} (renote ${_note.renoteId})`);
return null;
}
}
//#endregion
if (_note.user.isCat && _note.text) {
_note.text = (_note.text
// ja-JP
.replace(/な/g, 'にゃ').replace(/ナ/g, 'ニャ').replace(/ナ/g, 'ニャ')
// ko-KR
.replace(/[나-낳]/g, (match: string) => String.fromCharCode(
match.codePointAt(0) + '냐'.charCodeAt(0) - '나'.charCodeAt(0)
))
);
}
if (!opts.skipHide) {
await hideNote(_note, meId);
}
return _note;
};
|