]>
Commit | Line | Data |
---|---|---|
d95d1559 | 1 | import { FindOptions, ModelIndexesOptions, Op, WhereOptions } from 'sequelize' |
1735c825 | 2 | import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
d95d1559 | 3 | import { UserNotificationIncludes, UserNotificationModelForApi } from '@server/types/models/user' |
cef534ed | 4 | import { UserNotification, UserNotificationType } from '../../../shared' |
cef534ed C |
5 | import { isBooleanValid } from '../../helpers/custom-validators/misc' |
6 | import { isUserNotificationTypeValid } from '../../helpers/custom-validators/user-notifications' | |
d95d1559 C |
7 | import { AbuseModel } from '../abuse/abuse' |
8 | import { VideoAbuseModel } from '../abuse/video-abuse' | |
9 | import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' | |
f7cc67b4 C |
10 | import { ActorModel } from '../activitypub/actor' |
11 | import { ActorFollowModel } from '../activitypub/actor-follow' | |
457bb213 | 12 | import { AvatarModel } from '../avatar/avatar' |
38967f7b | 13 | import { ServerModel } from '../server/server' |
d95d1559 C |
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' | |
cef534ed C |
22 | |
23 | enum ScopeNames { | |
24 | WITH_ALL = 'WITH_ALL' | |
25 | } | |
26 | ||
457bb213 C |
27 | function buildActorWithAvatarInclude () { |
28 | return { | |
29 | attributes: [ 'preferredUsername' ], | |
3acc5084 | 30 | model: ActorModel.unscoped(), |
457bb213 C |
31 | required: true, |
32 | include: [ | |
33 | { | |
34 | attributes: [ 'filename' ], | |
3acc5084 | 35 | model: AvatarModel.unscoped(), |
457bb213 | 36 | required: false |
38967f7b C |
37 | }, |
38 | { | |
39 | attributes: [ 'host' ], | |
3acc5084 | 40 | model: ServerModel.unscoped(), |
38967f7b | 41 | required: false |
457bb213 C |
42 | } |
43 | ] | |
44 | } | |
45 | } | |
46 | ||
dc133480 C |
47 | function buildVideoInclude (required: boolean) { |
48 | return { | |
49 | attributes: [ 'id', 'uuid', 'name' ], | |
3acc5084 | 50 | model: VideoModel.unscoped(), |
dc133480 C |
51 | required |
52 | } | |
53 | } | |
54 | ||
457bb213 | 55 | function buildChannelInclude (required: boolean, withActor = false) { |
dc133480 | 56 | return { |
f7cc67b4 | 57 | required, |
dc133480 | 58 | attributes: [ 'id', 'name' ], |
3acc5084 | 59 | model: VideoChannelModel.unscoped(), |
457bb213 | 60 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] |
dc133480 C |
61 | } |
62 | } | |
63 | ||
457bb213 | 64 | function buildAccountInclude (required: boolean, withActor = false) { |
dc133480 | 65 | return { |
f7cc67b4 | 66 | required, |
dc133480 | 67 | attributes: [ 'id', 'name' ], |
3acc5084 | 68 | model: AccountModel.unscoped(), |
457bb213 | 69 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] |
dc133480 C |
70 | } |
71 | } | |
72 | ||
3acc5084 | 73 | @Scopes(() => ({ |
cef534ed C |
74 | [ScopeNames.WITH_ALL]: { |
75 | include: [ | |
dc133480 | 76 | Object.assign(buildVideoInclude(false), { |
457bb213 | 77 | include: [ buildChannelInclude(true, true) ] |
dc133480 | 78 | }), |
457bb213 | 79 | |
cef534ed | 80 | { |
dc133480 | 81 | attributes: [ 'id', 'originCommentId' ], |
3acc5084 | 82 | model: VideoCommentModel.unscoped(), |
cef534ed C |
83 | required: false, |
84 | include: [ | |
457bb213 | 85 | buildAccountInclude(true, true), |
dc133480 | 86 | buildVideoInclude(true) |
cef534ed C |
87 | ] |
88 | }, | |
457bb213 | 89 | |
cef534ed C |
90 | { |
91 | attributes: [ 'id' ], | |
d95d1559 | 92 | model: AbuseModel.unscoped(), |
cef534ed | 93 | required: false, |
d95d1559 C |
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 | { | |
310b5219 | 112 | attributes: [ 'id', 'name', 'uuid' ], |
d95d1559 C |
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 | ] | |
cef534ed | 127 | }, |
457bb213 | 128 | |
cef534ed C |
129 | { |
130 | attributes: [ 'id' ], | |
3acc5084 | 131 | model: VideoBlacklistModel.unscoped(), |
cef534ed | 132 | required: false, |
dc133480 C |
133 | include: [ buildVideoInclude(true) ] |
134 | }, | |
457bb213 | 135 | |
dc133480 C |
136 | { |
137 | attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ], | |
3acc5084 | 138 | model: VideoImportModel.unscoped(), |
dc133480 C |
139 | required: false, |
140 | include: [ buildVideoInclude(false) ] | |
f7cc67b4 | 141 | }, |
457bb213 | 142 | |
f7cc67b4 | 143 | { |
8ce1ba6e | 144 | attributes: [ 'id', 'state' ], |
3acc5084 | 145 | model: ActorFollowModel.unscoped(), |
f7cc67b4 C |
146 | required: false, |
147 | include: [ | |
148 | { | |
149 | attributes: [ 'preferredUsername' ], | |
3acc5084 | 150 | model: ActorModel.unscoped(), |
f7cc67b4 C |
151 | required: true, |
152 | as: 'ActorFollower', | |
457bb213 C |
153 | include: [ |
154 | { | |
155 | attributes: [ 'id', 'name' ], | |
3acc5084 | 156 | model: AccountModel.unscoped(), |
457bb213 C |
157 | required: true |
158 | }, | |
159 | { | |
160 | attributes: [ 'filename' ], | |
3acc5084 | 161 | model: AvatarModel.unscoped(), |
457bb213 | 162 | required: false |
38967f7b C |
163 | }, |
164 | { | |
165 | attributes: [ 'host' ], | |
3acc5084 | 166 | model: ServerModel.unscoped(), |
38967f7b | 167 | required: false |
457bb213 C |
168 | } |
169 | ] | |
f7cc67b4 C |
170 | }, |
171 | { | |
8424c402 | 172 | attributes: [ 'preferredUsername', 'type' ], |
3acc5084 | 173 | model: ActorModel.unscoped(), |
f7cc67b4 C |
174 | required: true, |
175 | as: 'ActorFollowing', | |
176 | include: [ | |
177 | buildChannelInclude(false), | |
8424c402 C |
178 | buildAccountInclude(false), |
179 | { | |
180 | attributes: [ 'host' ], | |
181 | model: ServerModel.unscoped(), | |
182 | required: false | |
183 | } | |
f7cc67b4 C |
184 | ] |
185 | } | |
186 | ] | |
457bb213 C |
187 | }, |
188 | ||
189 | buildAccountInclude(false, true) | |
3acc5084 | 190 | ] |
cef534ed | 191 | } |
3acc5084 | 192 | })) |
cef534ed C |
193 | @Table({ |
194 | tableName: 'userNotification', | |
195 | indexes: [ | |
196 | { | |
457bb213 | 197 | fields: [ 'userId' ] |
cef534ed C |
198 | }, |
199 | { | |
457bb213 C |
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 | { | |
d95d1559 | 216 | fields: [ 'abuseId' ], |
457bb213 | 217 | where: { |
d95d1559 | 218 | abuseId: { |
457bb213 C |
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 | } | |
cef534ed | 254 | } |
3acc5084 | 255 | ] as (ModelIndexesOptions & { where?: WhereOptions })[] |
cef534ed C |
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 | ||
d95d1559 | 313 | @ForeignKey(() => AbuseModel) |
cef534ed | 314 | @Column |
d95d1559 | 315 | abuseId: number |
cef534ed | 316 | |
d95d1559 | 317 | @BelongsTo(() => AbuseModel, { |
cef534ed C |
318 | foreignKey: { |
319 | allowNull: true | |
320 | }, | |
321 | onDelete: 'cascade' | |
322 | }) | |
d95d1559 | 323 | Abuse: AbuseModel |
cef534ed C |
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 | ||
dc133480 C |
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 | ||
f7cc67b4 C |
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 | ||
dc133480 | 373 | static listForApi (userId: number, start: number, count: number, sort: string, unread?: boolean) { |
119b16e5 C |
374 | const where = { userId } |
375 | ||
1735c825 | 376 | const query: FindOptions = { |
cef534ed C |
377 | offset: start, |
378 | limit: count, | |
379 | order: getSort(sort), | |
119b16e5 | 380 | where |
cef534ed C |
381 | } |
382 | ||
dc133480 C |
383 | if (unread !== undefined) query.where['read'] = !unread |
384 | ||
119b16e5 C |
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 })) | |
cef534ed C |
393 | } |
394 | ||
395 | static markAsRead (userId: number, notificationIds: number[]) { | |
396 | const query = { | |
397 | where: { | |
398 | userId, | |
399 | id: { | |
0374b6b5 | 400 | [Op.in]: notificationIds |
cef534ed C |
401 | } |
402 | } | |
403 | } | |
404 | ||
405 | return UserNotificationModel.update({ read: true }, query) | |
406 | } | |
407 | ||
2f1548fd C |
408 | static markAllAsRead (userId: number) { |
409 | const query = { where: { userId } } | |
410 | ||
411 | return UserNotificationModel.update({ read: true }, query) | |
412 | } | |
413 | ||
453e83ea | 414 | toFormattedJSON (this: UserNotificationModelForApi): UserNotification { |
457bb213 | 415 | const video = this.Video |
a1587156 | 416 | ? Object.assign(this.formatVideo(this.Video), { channel: this.formatActor(this.Video.VideoChannel) }) |
457bb213 | 417 | : undefined |
dc133480 C |
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 | |
cef534ed C |
425 | } : undefined |
426 | ||
427 | const comment = this.Comment ? { | |
428 | id: this.Comment.id, | |
dc133480 | 429 | threadId: this.Comment.getThreadId(), |
457bb213 | 430 | account: this.formatActor(this.Comment.Account), |
dc133480 | 431 | video: this.formatVideo(this.Comment.Video) |
cef534ed C |
432 | } : undefined |
433 | ||
d95d1559 | 434 | const abuse = this.Abuse ? this.formatAbuse(this.Abuse) : undefined |
cef534ed C |
435 | |
436 | const videoBlacklist = this.VideoBlacklist ? { | |
437 | id: this.VideoBlacklist.id, | |
dc133480 | 438 | video: this.formatVideo(this.VideoBlacklist.Video) |
cef534ed C |
439 | } : undefined |
440 | ||
457bb213 | 441 | const account = this.Account ? this.formatActor(this.Account) : undefined |
f7cc67b4 | 442 | |
8424c402 C |
443 | const actorFollowingType = { |
444 | Application: 'instance' as 'instance', | |
445 | Group: 'channel' as 'channel', | |
446 | Person: 'account' as 'account' | |
447 | } | |
f7cc67b4 C |
448 | const actorFollow = this.ActorFollow ? { |
449 | id: this.ActorFollow.id, | |
883993c8 | 450 | state: this.ActorFollow.state, |
f7cc67b4 | 451 | follower: { |
457bb213 | 452 | id: this.ActorFollow.ActorFollower.Account.id, |
f7cc67b4 | 453 | displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), |
457bb213 | 454 | name: this.ActorFollow.ActorFollower.preferredUsername, |
557b13ae | 455 | avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getStaticPath() } : undefined, |
38967f7b | 456 | host: this.ActorFollow.ActorFollower.getHost() |
f7cc67b4 C |
457 | }, |
458 | following: { | |
8424c402 | 459 | type: actorFollowingType[this.ActorFollow.ActorFollowing.type], |
f7cc67b4 | 460 | displayName: (this.ActorFollow.ActorFollowing.VideoChannel || this.ActorFollow.ActorFollowing.Account).getDisplayName(), |
8424c402 C |
461 | name: this.ActorFollow.ActorFollowing.preferredUsername, |
462 | host: this.ActorFollow.ActorFollowing.getHost() | |
f7cc67b4 C |
463 | } |
464 | } : undefined | |
465 | ||
cef534ed C |
466 | return { |
467 | id: this.id, | |
468 | type: this.type, | |
469 | read: this.read, | |
470 | video, | |
dc133480 | 471 | videoImport, |
cef534ed | 472 | comment, |
d95d1559 | 473 | abuse, |
cef534ed | 474 | videoBlacklist, |
f7cc67b4 C |
475 | account, |
476 | actorFollow, | |
cef534ed C |
477 | createdAt: this.createdAt.toISOString(), |
478 | updatedAt: this.updatedAt.toISOString() | |
479 | } | |
480 | } | |
dc133480 | 481 | |
453e83ea | 482 | formatVideo (this: UserNotificationModelForApi, video: UserNotificationIncludes.VideoInclude) { |
dc133480 C |
483 | return { |
484 | id: video.id, | |
485 | uuid: video.uuid, | |
486 | name: video.name | |
487 | } | |
488 | } | |
457bb213 | 489 | |
d95d1559 C |
490 | formatAbuse (this: UserNotificationModelForApi, abuse: UserNotificationIncludes.AbuseInclude) { |
491 | const commentAbuse = abuse.VideoCommentAbuse?.VideoComment ? { | |
492 | threadId: abuse.VideoCommentAbuse.VideoComment.getThreadId(), | |
493 | ||
494 | video: { | |
310b5219 C |
495 | id: abuse.VideoCommentAbuse.VideoComment.Video.id, |
496 | name: abuse.VideoCommentAbuse.VideoComment.Video.name, | |
d95d1559 C |
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 | video: videoAbuse, | |
508 | comment: commentAbuse, | |
509 | account: accountAbuse | |
510 | } | |
511 | } | |
512 | ||
453e83ea C |
513 | formatActor ( |
514 | this: UserNotificationModelForApi, | |
515 | accountOrChannel: UserNotificationIncludes.AccountIncludeActor | UserNotificationIncludes.VideoChannelIncludeActor | |
516 | ) { | |
457bb213 | 517 | const avatar = accountOrChannel.Actor.Avatar |
557b13ae | 518 | ? { path: accountOrChannel.Actor.Avatar.getStaticPath() } |
457bb213 C |
519 | : undefined |
520 | ||
521 | return { | |
522 | id: accountOrChannel.id, | |
523 | displayName: accountOrChannel.getDisplayName(), | |
524 | name: accountOrChannel.Actor.preferredUsername, | |
38967f7b | 525 | host: accountOrChannel.Actor.getHost(), |
457bb213 C |
526 | avatar |
527 | } | |
528 | } | |
cef534ed | 529 | } |