]>
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.unscoped(), | |
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 { | |
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 | static removeNotificationsOf (options: { id: number, type: 'account' | 'server', forUserId?: number }) { | |
415 | const id = parseInt(options.id + '', 10) | |
416 | ||
417 | function buildAccountWhereQuery (base: string) { | |
418 | const whereSuffix = options.forUserId | |
419 | ? ` AND "userNotification"."userId" = ${options.forUserId}` | |
420 | : '' | |
421 | ||
422 | if (options.type === 'account') { | |
423 | return base + | |
424 | ` WHERE "account"."id" = ${id} ${whereSuffix}` | |
425 | } | |
426 | ||
427 | return base + | |
428 | ` WHERE "actor"."serverId" = ${id} ${whereSuffix}` | |
429 | } | |
430 | ||
431 | const queries = [ | |
432 | buildAccountWhereQuery( | |
433 | `SELECT "userNotification"."id" FROM "userNotification" ` + | |
434 | `INNER JOIN "account" ON "userNotification"."accountId" = "account"."id" ` + | |
435 | `INNER JOIN actor ON "actor"."id" = "account"."actorId" ` | |
436 | ), | |
437 | ||
438 | // Remove notifications from muted accounts that followed ours | |
439 | buildAccountWhereQuery( | |
440 | `SELECT "userNotification"."id" FROM "userNotification" ` + | |
441 | `INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` + | |
442 | `INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` + | |
443 | `INNER JOIN account ON account."actorId" = actor.id ` | |
444 | ), | |
445 | ||
446 | // Remove notifications from muted accounts that commented something | |
447 | buildAccountWhereQuery( | |
448 | `SELECT "userNotification"."id" FROM "userNotification" ` + | |
449 | `INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` + | |
450 | `INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` + | |
451 | `INNER JOIN account ON account."actorId" = actor.id ` | |
452 | ), | |
453 | ||
454 | buildAccountWhereQuery( | |
455 | `SELECT "userNotification"."id" FROM "userNotification" ` + | |
456 | `INNER JOIN "videoComment" ON "videoComment".id = "userNotification"."commentId" ` + | |
457 | `INNER JOIN account ON account.id = "videoComment"."accountId" ` + | |
458 | `INNER JOIN actor ON "actor"."id" = "account"."actorId" ` | |
459 | ) | |
460 | ] | |
461 | ||
462 | const query = `DELETE FROM "userNotification" WHERE id IN (${queries.join(' UNION ')})` | |
463 | ||
464 | return UserNotificationModel.sequelize.query(query) | |
465 | } | |
466 | ||
467 | toFormattedJSON (this: UserNotificationModelForApi): UserNotification { | |
468 | const video = this.Video | |
469 | ? Object.assign(this.formatVideo(this.Video), { channel: this.formatActor(this.Video.VideoChannel) }) | |
470 | : undefined | |
471 | ||
472 | const videoImport = this.VideoImport ? { | |
473 | id: this.VideoImport.id, | |
474 | video: this.VideoImport.Video ? this.formatVideo(this.VideoImport.Video) : undefined, | |
475 | torrentName: this.VideoImport.torrentName, | |
476 | magnetUri: this.VideoImport.magnetUri, | |
477 | targetUrl: this.VideoImport.targetUrl | |
478 | } : undefined | |
479 | ||
480 | const comment = this.Comment ? { | |
481 | id: this.Comment.id, | |
482 | threadId: this.Comment.getThreadId(), | |
483 | account: this.formatActor(this.Comment.Account), | |
484 | video: this.formatVideo(this.Comment.Video) | |
485 | } : undefined | |
486 | ||
487 | const abuse = this.Abuse ? this.formatAbuse(this.Abuse) : undefined | |
488 | ||
489 | const videoBlacklist = this.VideoBlacklist ? { | |
490 | id: this.VideoBlacklist.id, | |
491 | video: this.formatVideo(this.VideoBlacklist.Video) | |
492 | } : undefined | |
493 | ||
494 | const account = this.Account ? this.formatActor(this.Account) : undefined | |
495 | ||
496 | const actorFollowingType = { | |
497 | Application: 'instance' as 'instance', | |
498 | Group: 'channel' as 'channel', | |
499 | Person: 'account' as 'account' | |
500 | } | |
501 | const actorFollow = this.ActorFollow ? { | |
502 | id: this.ActorFollow.id, | |
503 | state: this.ActorFollow.state, | |
504 | follower: { | |
505 | id: this.ActorFollow.ActorFollower.Account.id, | |
506 | displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), | |
507 | name: this.ActorFollow.ActorFollower.preferredUsername, | |
508 | avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getStaticPath() } : undefined, | |
509 | host: this.ActorFollow.ActorFollower.getHost() | |
510 | }, | |
511 | following: { | |
512 | type: actorFollowingType[this.ActorFollow.ActorFollowing.type], | |
513 | displayName: (this.ActorFollow.ActorFollowing.VideoChannel || this.ActorFollow.ActorFollowing.Account).getDisplayName(), | |
514 | name: this.ActorFollow.ActorFollowing.preferredUsername, | |
515 | host: this.ActorFollow.ActorFollowing.getHost() | |
516 | } | |
517 | } : undefined | |
518 | ||
519 | return { | |
520 | id: this.id, | |
521 | type: this.type, | |
522 | read: this.read, | |
523 | video, | |
524 | videoImport, | |
525 | comment, | |
526 | abuse, | |
527 | videoBlacklist, | |
528 | account, | |
529 | actorFollow, | |
530 | createdAt: this.createdAt.toISOString(), | |
531 | updatedAt: this.updatedAt.toISOString() | |
532 | } | |
533 | } | |
534 | ||
535 | formatVideo (this: UserNotificationModelForApi, video: UserNotificationIncludes.VideoInclude) { | |
536 | return { | |
537 | id: video.id, | |
538 | uuid: video.uuid, | |
539 | name: video.name | |
540 | } | |
541 | } | |
542 | ||
543 | formatAbuse (this: UserNotificationModelForApi, abuse: UserNotificationIncludes.AbuseInclude) { | |
544 | const commentAbuse = abuse.VideoCommentAbuse?.VideoComment ? { | |
545 | threadId: abuse.VideoCommentAbuse.VideoComment.getThreadId(), | |
546 | ||
547 | video: { | |
548 | id: abuse.VideoCommentAbuse.VideoComment.Video.id, | |
549 | name: abuse.VideoCommentAbuse.VideoComment.Video.name, | |
550 | uuid: abuse.VideoCommentAbuse.VideoComment.Video.uuid | |
551 | } | |
552 | } : undefined | |
553 | ||
554 | const videoAbuse = abuse.VideoAbuse?.Video ? this.formatVideo(abuse.VideoAbuse.Video) : undefined | |
555 | ||
556 | const accountAbuse = (!commentAbuse && !videoAbuse) ? this.formatActor(abuse.FlaggedAccount) : undefined | |
557 | ||
558 | return { | |
559 | id: abuse.id, | |
560 | state: abuse.state, | |
561 | video: videoAbuse, | |
562 | comment: commentAbuse, | |
563 | account: accountAbuse | |
564 | } | |
565 | } | |
566 | ||
567 | formatActor ( | |
568 | this: UserNotificationModelForApi, | |
569 | accountOrChannel: UserNotificationIncludes.AccountIncludeActor | UserNotificationIncludes.VideoChannelIncludeActor | |
570 | ) { | |
571 | const avatar = accountOrChannel.Actor.Avatar | |
572 | ? { path: accountOrChannel.Actor.Avatar.getStaticPath() } | |
573 | : undefined | |
574 | ||
575 | return { | |
576 | id: accountOrChannel.id, | |
577 | displayName: accountOrChannel.getDisplayName(), | |
578 | name: accountOrChannel.Actor.preferredUsername, | |
579 | host: accountOrChannel.Actor.getHost(), | |
580 | avatar | |
581 | } | |
582 | } | |
583 | } |