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
|
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
import rap from '@prezzemolo/rap';
import db from '../db/mongodb';
import { IUser, pack as packUser } from './user';
import { pack as packApp } from './app';
import { pack as packChannel } from './channel';
import PollVote, { deletePollVote } from './poll-vote';
import Reaction, { deleteNoteReaction } from './note-reaction';
import { pack as packFile } from './drive-file';
import NoteWatching, { deleteNoteWatching } from './note-watching';
import NoteReaction from './note-reaction';
import Favorite, { deleteFavorite } from './favorite';
import Notification, { deleteNotification } from './notification';
import Following from './following';
const Note = db.get<INote>('notes');
Note.createIndex('uri', { sparse: true, unique: true });
export default Note;
export function isValidText(text: string): boolean {
return text.length <= 1000 && text.trim() != '';
}
export function isValidCw(text: string): boolean {
return text.length <= 100;
}
export type INote = {
_id: mongo.ObjectID;
channelId: mongo.ObjectID;
createdAt: Date;
deletedAt: Date;
mediaIds: mongo.ObjectID[];
replyId: mongo.ObjectID;
renoteId: mongo.ObjectID;
poll: any; // todo
text: string;
tags: string[];
cw: string;
userId: mongo.ObjectID;
appId: mongo.ObjectID;
viaMobile: boolean;
renoteCount: number;
repliesCount: number;
reactionCounts: any;
mentions: mongo.ObjectID[];
/**
* public ... 公開
* home ... ホームタイムライン(ユーザーページのタイムライン含む)のみに流す
* followers ... フォロワーのみ
* specified ... visibleUserIds で指定したユーザーのみ
* private ... 自分のみ
*/
visibility: 'public' | 'home' | 'followers' | 'specified' | 'private';
visibleUserIds: mongo.ObjectID[];
geo: {
coordinates: number[];
altitude: number;
accuracy: number;
altitudeAccuracy: number;
heading: number;
speed: number;
};
uri: string;
// 非正規化
_reply?: {
userId: mongo.ObjectID;
};
_renote?: {
userId: mongo.ObjectID;
};
_user: {
host: string;
inbox?: string;
};
};
/**
* Noteを物理削除します
*/
export async function deleteNote(note: string | mongo.ObjectID | INote) {
let n: INote;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(note)) {
n = await Note.findOne({
_id: note
});
} else if (typeof note === 'string') {
n = await Note.findOne({
_id: new mongo.ObjectID(note)
});
} else {
n = note as INote;
}
console.log(n == null ? `Note: delete skipped ${note}` : `Note: deleting ${n._id}`);
if (n == null) return;
// このNoteへの返信をすべて削除
await Promise.all((
await Note.find({ replyId: n._id })
).map(x => deleteNote(x)));
// このNoteのRenoteをすべて削除
await Promise.all((
await Note.find({ renoteId: n._id })
).map(x => deleteNote(x)));
// この投稿に対するNoteWatchingをすべて削除
await Promise.all((
await NoteWatching.find({ noteId: n._id })
).map(x => deleteNoteWatching(x)));
// この投稿に対するNoteReactionをすべて削除
await Promise.all((
await NoteReaction.find({ noteId: n._id })
).map(x => deleteNoteReaction(x)));
// この投稿に対するPollVoteをすべて削除
await Promise.all((
await PollVote.find({ noteId: n._id })
).map(x => deletePollVote(x)));
// この投稿に対するFavoriteをすべて削除
await Promise.all((
await Favorite.find({ noteId: n._id })
).map(x => deleteFavorite(x)));
// この投稿に対するNotificationをすべて削除
await Promise.all((
await Notification.find({ noteId: n._id })
).map(x => deleteNotification(x)));
// このNoteを削除
await Note.remove({
_id: n._id
});
console.log(`Note: deleted ${n._id}`);
}
/**
* 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
}
) => {
const opts = options || {
detail: true,
};
// Me
const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(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 (mongo.ObjectID.prototype.isPrototypeOf(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) throw `invalid note arg ${note}`;
let hide = false;
// visibility が private かつ投稿者のIDが自分のIDではなかったら非表示
if (_note.visibility == 'private' && (meId == null || !meId.equals(_note.userId))) {
hide = true;
}
// visibility が specified かつ自分が指定されていなかったら非表示
if (_note.visibility == 'specified') {
if (meId == null) {
hide = true;
} else if (meId.equals(_note.userId)) {
hide = false;
} else {
// 指定されているかどうか
const specified = _note.visibleUserIds.test(id => id.equals(meId));
if (specified) {
hide = false;
} else {
hide = true;
}
}
}
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
if (_note.visibility == 'followers') {
if (meId == null) {
hide = true;
} else if (meId.equals(_note.userId)) {
hide = false;
} else {
// フォロワーかどうか
const following = await Following.findOne({
followeeId: _note.userId,
followerId: meId
});
if (following == null) {
hide = true;
} else {
hide = false;
}
}
}
const id = _note._id;
// Rename _id to id
_note.id = _note._id;
delete _note._id;
delete _note.mentions;
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 channel
if (_note.channelId) {
_note.channel = packChannel(_note.channelId);
}
// Populate media
if (_note.mediaIds) {
_note.media = Promise.all(_note.mediaIds.map(fileId =>
packFile(fileId)
));
}
// When requested a detailed note data
if (opts.detail) {
// Get previous note info
_note.prev = (async () => {
const prev = await Note.findOne({
userId: _note.userId,
_id: {
$lt: id
}
}, {
fields: {
_id: true
},
sort: {
_id: -1
}
});
return prev ? prev._id : null;
})();
// Get next note info
_note.next = (async () => {
const next = await Note.findOne({
userId: _note.userId,
_id: {
$gt: id
}
}, {
fields: {
_id: true
},
sort: {
_id: 1
}
});
return next ? next._id : null;
})();
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 => c.id == vote.choice)[0];
myChoice.isVoted = true;
}
return poll;
})(_note.poll);
}
// Fetch my reaction
if (meId) {
_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);
return _note;
};
|