]>
Commit | Line | Data |
---|---|---|
1 | import { FindOptions, ModelIndexesOptions, Op, WhereOptions } from 'sequelize' | |
2 | import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' | |
3 | import { UserNotificationIncludes, UserNotificationModelForApi } from '@server/types/models/user' | |
4 | import { UserNotification, UserNotificationType } from '../../../shared' | |
5 | import { isBooleanValid } from '../../helpers/custom-validators/misc' | |
6 | import { isUserNotificationTypeValid } from '../../helpers/custom-validators/user-notifications' | |
7 | import { AbuseModel } from '../abuse/abuse' | |
8 | import { VideoAbuseModel } from '../abuse/video-abuse' | |
9 | import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' | |
10 | import { ActorModel } from '../activitypub/actor' | |
11 | import { ActorFollowModel } from '../activitypub/actor-follow' | |
12 | import { AvatarModel } from '../avatar/avatar' | |
13 | import { ServerModel } from '../server/server' | |
14 | import { getSort, throwIfNotValid } from '../utils' | |
15 | import { VideoModel } from '../video/video' | |
16 | import { VideoBlacklistModel } from '../video/video-blacklist' | |
17 | import { VideoChannelModel } from '../video/video-channel' | |
18 | import { VideoCommentModel } from '../video/video-comment' | |
19 | import { VideoImportModel } from '../video/video-import' | |
20 | import { AccountModel } from './account' | |
21 | import { UserModel } from './user' | |
22 | ||
23 | enum ScopeNames { | |
24 | WITH_ALL = 'WITH_ALL' | |
25 | } | |
26 | ||
27 | function buildActorWithAvatarInclude () { | |
28 | return { | |
29 | attributes: [ 'preferredUsername' ], | |
30 | model: ActorModel.unscoped(), | |
31 | required: true, | |
32 | include: [ | |
33 | { | |
34 | attributes: [ 'filename' ], | |
35 | model: AvatarModel.unscoped(), | |
36 | required: false | |
37 | }, | |
38 | { | |
39 | attributes: [ 'host' ], | |
40 | model: ServerModel.unscoped(), | |
41 | required: false | |
42 | } | |
43 | ] | |
44 | } | |
45 | } | |
46 | ||
47 | function buildVideoInclude (required: boolean) { | |
48 | return { | |
49 | attributes: [ 'id', 'uuid', 'name' ], | |
50 | model: VideoModel.unscoped(), | |
51 | required | |
52 | } | |
53 | } | |
54 | ||
55 | function buildChannelInclude (required: boolean, withActor = false) { | |
56 | return { | |
57 | required, | |
58 | attributes: [ 'id', 'name' ], | |
59 | model: VideoChannelModel.unscoped(), | |
60 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] | |
61 | } | |
62 | } | |
63 | ||
64 | function buildAccountInclude (required: boolean, withActor = false) { | |
65 | return { | |
66 | required, | |
67 | attributes: [ 'id', 'name' ], | |
68 | model: AccountModel.unscoped(), | |
69 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] | |
70 | } | |
71 | } | |
72 | ||
73 | @Scopes(() => ({ | |
74 | [ScopeNames.WITH_ALL]: { | |
75 | include: [ | |
76 | Object.assign(buildVideoInclude(false), { | |
77 | include: [ buildChannelInclude(true, true) ] | |
78 | }), | |
79 | ||
80 | { | |
81 | attributes: [ 'id', 'originCommentId' ], | |
82 | model: VideoCommentModel.unscoped(), | |
83 | required: false, | |
84 | include: [ | |
85 | buildAccountInclude(true, true), | |
86 | buildVideoInclude(true) | |
87 | ] | |
88 | }, | |
89 | ||
90 | { | |
91 | attributes: [ 'id', 'state' ], | |
92 | model: AbuseModel.unscoped(), | |
93 | required: false, | |
94 | include: [ | |
95 | { | |
96 | attributes: [ 'id' ], | |
97 | model: VideoAbuseModel.unscoped(), | |
98 | required: false, | |
99 | include: [ buildVideoInclude(true) ] | |
100 | }, | |
101 | { | |
102 | attributes: [ 'id' ], | |
103 | model: VideoCommentAbuseModel.unscoped(), | |
104 | required: false, | |
105 | include: [ | |
106 | { | |
107 | attributes: [ 'id', 'originCommentId' ], | |
108 | model: VideoCommentModel, | |
109 | required: true, | |
110 | include: [ | |
111 | { | |
112 | attributes: [ 'id', 'name', 'uuid' ], | |
113 | model: VideoModel.unscoped(), | |
114 | required: true | |
115 | } | |
116 | ] | |
117 | } | |
118 | ] | |
119 | }, | |
120 | { | |
121 | model: AccountModel, | |
122 | as: 'FlaggedAccount', | |
123 | required: true, | |
124 | include: [ buildActorWithAvatarInclude() ] | |
125 | } | |
126 | ] | |
127 | }, | |
128 | ||
129 | { | |
130 | attributes: [ 'id' ], | |
131 | model: VideoBlacklistModel.unscoped(), | |
132 | required: false, | |
133 | include: [ buildVideoInclude(true) ] | |
134 | }, | |
135 | ||
136 | { | |
137 | attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ], | |
138 | model: VideoImportModel.unscoped(), | |
139 | required: false, | |
140 | include: [ buildVideoInclude(false) ] | |
141 | }, | |
142 | ||
143 | { | |
144 | attributes: [ 'id', 'state' ], | |
145 | model: ActorFollowModel.unscoped(), | |
146 | required: false, | |
147 | include: [ | |
148 | { | |
149 | attributes: [ 'preferredUsername' ], | |
150 | model: ActorModel.unscoped(), | |
151 | required: true, | |
152 | as: 'ActorFollower', | |
153 | include: [ | |
154 | { | |
155 | attributes: [ 'id', 'name' ], | |
156 | model: AccountModel.unscoped(), | |
157 | required: true | |
158 | }, | |
159 | { | |
160 | attributes: [ 'filename' ], | |
161 | model: AvatarModel.unscoped(), | |
162 | required: false | |
163 | }, | |
164 | { | |
165 | attributes: [ 'host' ], | |
166 | model: ServerModel.unscoped(), | |
167 | required: false | |
168 | } | |
169 | ] | |
170 | }, | |
171 | { | |
172 | attributes: [ 'preferredUsername', 'type' ], | |
173 | model: ActorModel.unscoped(), | |
174 | required: true, | |
175 | as: 'ActorFollowing', | |
176 | include: [ | |
177 | buildChannelInclude(false), | |
178 | buildAccountInclude(false), | |
179 | { | |
180 | attributes: [ 'host' ], | |
181 | model: ServerModel.unscoped(), | |
182 | required: false | |
183 | } | |
184 | ] | |
185 | } | |
186 | ] | |
187 | }, | |
188 | ||
189 | buildAccountInclude(false, true) | |
190 | ] | |
191 | } | |
192 | })) | |
193 | @Table({ | |
194 | tableName: 'userNotification', | |
195 | indexes: [ | |
196 | { | |
197 | fields: [ 'userId' ] | |
198 | }, | |
199 | { | |
200 | fields: [ 'videoId' ], | |
201 | where: { | |
202 | videoId: { | |
203 | [Op.ne]: null | |
204 | } | |
205 | } | |
206 | }, | |
207 | { | |
208 | fields: [ 'commentId' ], | |
209 | where: { | |
210 | commentId: { | |
211 | [Op.ne]: null | |
212 | } | |
213 | } | |
214 | }, | |
215 | { | |
216 | fields: [ 'abuseId' ], | |
217 | where: { | |
218 | abuseId: { | |
219 | [Op.ne]: null | |
220 | } | |
221 | } | |
222 | }, | |
223 | { | |
224 | fields: [ 'videoBlacklistId' ], | |
225 | where: { | |
226 | videoBlacklistId: { | |
227 | [Op.ne]: null | |
228 | } | |
229 | } | |
230 | }, | |
231 | { | |
232 | fields: [ 'videoImportId' ], | |
233 | where: { | |
234 | videoImportId: { | |
235 | [Op.ne]: null | |
236 | } | |
237 | } | |
238 | }, | |
239 | { | |
240 | fields: [ 'accountId' ], | |
241 | where: { | |
242 | accountId: { | |
243 | [Op.ne]: null | |
244 | } | |
245 | } | |
246 | }, | |
247 | { | |
248 | fields: [ 'actorFollowId' ], | |
249 | where: { | |
250 | actorFollowId: { | |
251 | [Op.ne]: null | |
252 | } | |
253 | } | |
254 | } | |
255 | ] as (ModelIndexesOptions & { where?: WhereOptions })[] | |
256 | }) | |
257 | export class UserNotificationModel extends Model<UserNotificationModel> { | |
258 | ||
259 | @AllowNull(false) | |
260 | @Default(null) | |
261 | @Is('UserNotificationType', value => throwIfNotValid(value, isUserNotificationTypeValid, 'type')) | |
262 | @Column | |
263 | type: UserNotificationType | |
264 | ||
265 | @AllowNull(false) | |
266 | @Default(false) | |
267 | @Is('UserNotificationRead', value => throwIfNotValid(value, isBooleanValid, 'read')) | |
268 | @Column | |
269 | read: boolean | |
270 | ||
271 | @CreatedAt | |
272 | createdAt: Date | |
273 | ||
274 | @UpdatedAt | |
275 | updatedAt: Date | |
276 | ||
277 | @ForeignKey(() => UserModel) | |
278 | @Column | |
279 | userId: number | |
280 | ||
281 | @BelongsTo(() => UserModel, { | |
282 | foreignKey: { | |
283 | allowNull: false | |
284 | }, | |
285 | onDelete: 'cascade' | |
286 | }) | |
287 | User: UserModel | |
288 | ||
289 | @ForeignKey(() => VideoModel) | |
290 | @Column | |
291 | videoId: number | |
292 | ||
293 | @BelongsTo(() => VideoModel, { | |
294 | foreignKey: { | |
295 | allowNull: true | |
296 | }, | |
297 | onDelete: 'cascade' | |
298 | }) | |
299 | Video: VideoModel | |
300 | ||
301 | @ForeignKey(() => VideoCommentModel) | |
302 | @Column | |
303 | commentId: number | |
304 | ||
305 | @BelongsTo(() => VideoCommentModel, { | |
306 | foreignKey: { | |
307 | allowNull: true | |
308 | }, | |
309 | onDelete: 'cascade' | |
310 | }) | |
311 | Comment: VideoCommentModel | |
312 | ||
313 | @ForeignKey(() => AbuseModel) | |
314 | @Column | |
315 | abuseId: number | |
316 | ||
317 | @BelongsTo(() => AbuseModel, { | |
318 | foreignKey: { | |
319 | allowNull: true | |
320 | }, | |
321 | onDelete: 'cascade' | |
322 | }) | |
323 | Abuse: AbuseModel | |
324 | ||
325 | @ForeignKey(() => VideoBlacklistModel) | |
326 | @Column | |
327 | videoBlacklistId: number | |
328 | ||
329 | @BelongsTo(() => VideoBlacklistModel, { | |
330 | foreignKey: { | |
331 | allowNull: true | |
332 | }, | |
333 | onDelete: 'cascade' | |
334 | }) | |
335 | VideoBlacklist: VideoBlacklistModel | |
336 | ||
337 | @ForeignKey(() => VideoImportModel) | |
338 | @Column | |
339 | videoImportId: number | |
340 | ||
341 | @BelongsTo(() => VideoImportModel, { | |
342 | foreignKey: { | |
343 | allowNull: true | |
344 | }, | |
345 | onDelete: 'cascade' | |
346 | }) | |
347 | VideoImport: VideoImportModel | |
348 | ||
349 | @ForeignKey(() => AccountModel) | |
350 | @Column | |
351 | accountId: number | |
352 | ||
353 | @BelongsTo(() => AccountModel, { | |
354 | foreignKey: { | |
355 | allowNull: true | |
356 | }, | |
357 | onDelete: 'cascade' | |
358 | }) | |
359 | Account: AccountModel | |
360 | ||
361 | @ForeignKey(() => ActorFollowModel) | |
362 | @Column | |
363 | actorFollowId: number | |
364 | ||
365 | @BelongsTo(() => ActorFollowModel, { | |
366 | foreignKey: { | |
367 | allowNull: true | |
368 | }, | |
369 | onDelete: 'cascade' | |
370 | }) | |
371 | ActorFollow: ActorFollowModel | |
372 | ||
373 | static listForApi (userId: number, start: number, count: number, sort: string, unread?: boolean) { | |
374 | const where = { userId } | |
375 | ||
376 | const query: FindOptions = { | |
377 | offset: start, | |
378 | limit: count, | |
379 | order: getSort(sort), | |
380 | where | |
381 | } | |
382 | ||
383 | if (unread !== undefined) query.where['read'] = !unread | |
384 | ||
385 | return Promise.all([ | |
386 | UserNotificationModel.count({ where }) | |
387 | .then(count => count || 0), | |
388 | ||
389 | count === 0 | |
390 | ? [] | |
391 | : UserNotificationModel.scope(ScopeNames.WITH_ALL).findAll(query) | |
392 | ]).then(([ total, data ]) => ({ total, data })) | |
393 | } | |
394 | ||
395 | static markAsRead (userId: number, notificationIds: number[]) { | |
396 | const query = { | |
397 | where: { | |
398 | userId, | |
399 | id: { | |
400 | [Op.in]: notificationIds | |
401 | } | |
402 | } | |
403 | } | |
404 | ||
405 | return UserNotificationModel.update({ read: true }, query) | |
406 | } | |
407 | ||
408 | static markAllAsRead (userId: number) { | |
409 | const query = { where: { userId } } | |
410 | ||
411 | return UserNotificationModel.update({ read: true }, query) | |
412 | } | |
413 | ||
414 | toFormattedJSON (this: UserNotificationModelForApi): UserNotification { | |
415 | const video = this.Video | |
416 | ? Object.assign(this.formatVideo(this.Video), { channel: this.formatActor(this.Video.VideoChannel) }) | |
417 | : undefined | |
418 | ||
419 | const videoImport = this.VideoImport ? { | |
420 | id: this.VideoImport.id, | |
421 | video: this.VideoImport.Video ? this.formatVideo(this.VideoImport.Video) : undefined, | |
422 | torrentName: this.VideoImport.torrentName, | |
423 | magnetUri: this.VideoImport.magnetUri, | |
424 | targetUrl: this.VideoImport.targetUrl | |
425 | } : undefined | |
426 | ||
427 | const comment = this.Comment ? { | |
428 | id: this.Comment.id, | |
429 | threadId: this.Comment.getThreadId(), | |
430 | account: this.formatActor(this.Comment.Account), | |
431 | video: this.formatVideo(this.Comment.Video) | |
432 | } : undefined | |
433 | ||
434 | const abuse = this.Abuse ? this.formatAbuse(this.Abuse) : undefined | |
435 | ||
436 | const videoBlacklist = this.VideoBlacklist ? { | |
437 | id: this.VideoBlacklist.id, | |
438 | video: this.formatVideo(this.VideoBlacklist.Video) | |
439 | } : undefined | |
440 | ||
441 | const account = this.Account ? this.formatActor(this.Account) : undefined | |
442 | ||
443 | const actorFollowingType = { | |
444 | Application: 'instance' as 'instance', | |
445 | Group: 'channel' as 'channel', | |
446 | Person: 'account' as 'account' | |
447 | } | |
448 | const actorFollow = this.ActorFollow ? { | |
449 | id: this.ActorFollow.id, | |
450 | state: this.ActorFollow.state, | |
451 | follower: { | |
452 | id: this.ActorFollow.ActorFollower.Account.id, | |
453 | displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), | |
454 | name: this.ActorFollow.ActorFollower.preferredUsername, | |
455 | avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getStaticPath() } : undefined, | |
456 | host: this.ActorFollow.ActorFollower.getHost() | |
457 | }, | |
458 | following: { | |
459 | type: actorFollowingType[this.ActorFollow.ActorFollowing.type], | |
460 | displayName: (this.ActorFollow.ActorFollowing.VideoChannel || this.ActorFollow.ActorFollowing.Account).getDisplayName(), | |
461 | name: this.ActorFollow.ActorFollowing.preferredUsername, | |
462 | host: this.ActorFollow.ActorFollowing.getHost() | |
463 | } | |
464 | } : undefined | |
465 | ||
466 | return { | |
467 | id: this.id, | |
468 | type: this.type, | |
469 | read: this.read, | |
470 | video, | |
471 | videoImport, | |
472 | comment, | |
473 | abuse, | |
474 | videoBlacklist, | |
475 | account, | |
476 | actorFollow, | |
477 | createdAt: this.createdAt.toISOString(), | |
478 | updatedAt: this.updatedAt.toISOString() | |
479 | } | |
480 | } | |
481 | ||
482 | formatVideo (this: UserNotificationModelForApi, video: UserNotificationIncludes.VideoInclude) { | |
483 | return { | |
484 | id: video.id, | |
485 | uuid: video.uuid, | |
486 | name: video.name | |
487 | } | |
488 | } | |
489 | ||
490 | formatAbuse (this: UserNotificationModelForApi, abuse: UserNotificationIncludes.AbuseInclude) { | |
491 | const commentAbuse = abuse.VideoCommentAbuse?.VideoComment ? { | |
492 | threadId: abuse.VideoCommentAbuse.VideoComment.getThreadId(), | |
493 | ||
494 | video: { | |
495 | id: abuse.VideoCommentAbuse.VideoComment.Video.id, | |
496 | name: abuse.VideoCommentAbuse.VideoComment.Video.name, | |
497 | uuid: abuse.VideoCommentAbuse.VideoComment.Video.uuid | |
498 | } | |
499 | } : undefined | |
500 | ||
501 | const videoAbuse = abuse.VideoAbuse?.Video ? this.formatVideo(abuse.VideoAbuse.Video) : undefined | |
502 | ||
503 | const accountAbuse = (!commentAbuse && !videoAbuse) ? this.formatActor(abuse.FlaggedAccount) : undefined | |
504 | ||
505 | return { | |
506 | id: abuse.id, | |
507 | state: abuse.state, | |
508 | video: videoAbuse, | |
509 | comment: commentAbuse, | |
510 | account: accountAbuse | |
511 | } | |
512 | } | |
513 | ||
514 | formatActor ( | |
515 | this: UserNotificationModelForApi, | |
516 | accountOrChannel: UserNotificationIncludes.AccountIncludeActor | UserNotificationIncludes.VideoChannelIncludeActor | |
517 | ) { | |
518 | const avatar = accountOrChannel.Actor.Avatar | |
519 | ? { path: accountOrChannel.Actor.Avatar.getStaticPath() } | |
520 | : undefined | |
521 | ||
522 | return { | |
523 | id: accountOrChannel.id, | |
524 | displayName: accountOrChannel.getDisplayName(), | |
525 | name: accountOrChannel.Actor.preferredUsername, | |
526 | host: accountOrChannel.Actor.getHost(), | |
527 | avatar | |
528 | } | |
529 | } | |
530 | } |