1 import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2 import { UserNotification, UserNotificationType } from '../../../shared'
3 import { getSort, throwIfNotValid } from '../utils'
4 import { isBooleanValid } from '../../helpers/custom-validators/misc'
5 import { isUserNotificationTypeValid } from '../../helpers/custom-validators/user-notifications'
6 import { UserModel } from './user'
7 import { VideoModel } from '../video/video'
8 import { VideoCommentModel } from '../video/video-comment'
9 import { FindOptions, ModelIndexesOptions, Op, WhereOptions } from 'sequelize'
10 import { VideoChannelModel } from '../video/video-channel'
11 import { AccountModel } from './account'
12 import { VideoAbuseModel } from '../video/video-abuse'
13 import { VideoBlacklistModel } from '../video/video-blacklist'
14 import { VideoImportModel } from '../video/video-import'
15 import { ActorModel } from '../activitypub/actor'
16 import { ActorFollowModel } from '../activitypub/actor-follow'
17 import { AvatarModel } from '../avatar/avatar'
18 import { ServerModel } from '../server/server'
19 import { UserNotificationIncludes, UserNotificationModelForApi } from '@server/typings/models/user'
25 function buildActorWithAvatarInclude () {
27 attributes: [ 'preferredUsername' ],
28 model: ActorModel.unscoped(),
32 attributes: [ 'filename' ],
33 model: AvatarModel.unscoped(),
37 attributes: [ 'host' ],
38 model: ServerModel.unscoped(),
45 function buildVideoInclude (required: boolean) {
47 attributes: [ 'id', 'uuid', 'name' ],
48 model: VideoModel.unscoped(),
53 function buildChannelInclude (required: boolean, withActor = false) {
56 attributes: [ 'id', 'name' ],
57 model: VideoChannelModel.unscoped(),
58 include: withActor === true ? [ buildActorWithAvatarInclude() ] : []
62 function buildAccountInclude (required: boolean, withActor = false) {
65 attributes: [ 'id', 'name' ],
66 model: AccountModel.unscoped(),
67 include: withActor === true ? [ buildActorWithAvatarInclude() ] : []
72 [ScopeNames.WITH_ALL]: {
74 Object.assign(buildVideoInclude(false), {
75 include: [ buildChannelInclude(true, true) ]
79 attributes: [ 'id', 'originCommentId' ],
80 model: VideoCommentModel.unscoped(),
83 buildAccountInclude(true, true),
84 buildVideoInclude(true)
90 model: VideoAbuseModel.unscoped(),
92 include: [ buildVideoInclude(true) ]
97 model: VideoBlacklistModel.unscoped(),
99 include: [ buildVideoInclude(true) ]
103 attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ],
104 model: VideoImportModel.unscoped(),
106 include: [ buildVideoInclude(false) ]
110 attributes: [ 'id', 'state' ],
111 model: ActorFollowModel.unscoped(),
115 attributes: [ 'preferredUsername' ],
116 model: ActorModel.unscoped(),
121 attributes: [ 'id', 'name' ],
122 model: AccountModel.unscoped(),
126 attributes: [ 'filename' ],
127 model: AvatarModel.unscoped(),
131 attributes: [ 'host' ],
132 model: ServerModel.unscoped(),
138 attributes: [ 'preferredUsername' ],
139 model: ActorModel.unscoped(),
141 as: 'ActorFollowing',
143 buildChannelInclude(false),
144 buildAccountInclude(false)
150 buildAccountInclude(false, true)
155 tableName: 'userNotification',
161 fields: [ 'videoId' ],
169 fields: [ 'commentId' ],
177 fields: [ 'videoAbuseId' ],
185 fields: [ 'videoBlacklistId' ],
193 fields: [ 'videoImportId' ],
201 fields: [ 'accountId' ],
209 fields: [ 'actorFollowId' ],
216 ] as (ModelIndexesOptions & { where?: WhereOptions })[]
218 export class UserNotificationModel extends Model<UserNotificationModel> {
222 @Is('UserNotificationType', value => throwIfNotValid(value, isUserNotificationTypeValid, 'type'))
224 type: UserNotificationType
228 @Is('UserNotificationRead', value => throwIfNotValid(value, isBooleanValid, 'read'))
238 @ForeignKey(() => UserModel)
242 @BelongsTo(() => UserModel, {
250 @ForeignKey(() => VideoModel)
254 @BelongsTo(() => VideoModel, {
262 @ForeignKey(() => VideoCommentModel)
266 @BelongsTo(() => VideoCommentModel, {
272 Comment: VideoCommentModel
274 @ForeignKey(() => VideoAbuseModel)
278 @BelongsTo(() => VideoAbuseModel, {
284 VideoAbuse: VideoAbuseModel
286 @ForeignKey(() => VideoBlacklistModel)
288 videoBlacklistId: number
290 @BelongsTo(() => VideoBlacklistModel, {
296 VideoBlacklist: VideoBlacklistModel
298 @ForeignKey(() => VideoImportModel)
300 videoImportId: number
302 @BelongsTo(() => VideoImportModel, {
308 VideoImport: VideoImportModel
310 @ForeignKey(() => AccountModel)
314 @BelongsTo(() => AccountModel, {
320 Account: AccountModel
322 @ForeignKey(() => ActorFollowModel)
324 actorFollowId: number
326 @BelongsTo(() => ActorFollowModel, {
332 ActorFollow: ActorFollowModel
334 static listForApi (userId: number, start: number, count: number, sort: string, unread?: boolean) {
335 const query: FindOptions = {
338 order: getSort(sort),
344 if (unread !== undefined) query.where['read'] = !unread
346 return UserNotificationModel.scope(ScopeNames.WITH_ALL)
347 .findAndCountAll(query)
348 .then(({ rows, count }) => {
356 static markAsRead (userId: number, notificationIds: number[]) {
361 [Op.in]: notificationIds // FIXME: sequelize ANY seems broken
366 return UserNotificationModel.update({ read: true }, query)
369 static markAllAsRead (userId: number) {
370 const query = { where: { userId } }
372 return UserNotificationModel.update({ read: true }, query)
375 toFormattedJSON (this: UserNotificationModelForApi): UserNotification {
376 const video = this.Video
377 ? Object.assign(this.formatVideo(this.Video),{ channel: this.formatActor(this.Video.VideoChannel) })
380 const videoImport = this.VideoImport ? {
381 id: this.VideoImport.id,
382 video: this.VideoImport.Video ? this.formatVideo(this.VideoImport.Video) : undefined,
383 torrentName: this.VideoImport.torrentName,
384 magnetUri: this.VideoImport.magnetUri,
385 targetUrl: this.VideoImport.targetUrl
388 const comment = this.Comment ? {
390 threadId: this.Comment.getThreadId(),
391 account: this.formatActor(this.Comment.Account),
392 video: this.formatVideo(this.Comment.Video)
395 const videoAbuse = this.VideoAbuse ? {
396 id: this.VideoAbuse.id,
397 video: this.formatVideo(this.VideoAbuse.Video)
400 const videoBlacklist = this.VideoBlacklist ? {
401 id: this.VideoBlacklist.id,
402 video: this.formatVideo(this.VideoBlacklist.Video)
405 const account = this.Account ? this.formatActor(this.Account) : undefined
407 const actorFollow = this.ActorFollow ? {
408 id: this.ActorFollow.id,
409 state: this.ActorFollow.state,
411 id: this.ActorFollow.ActorFollower.Account.id,
412 displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(),
413 name: this.ActorFollow.ActorFollower.preferredUsername,
414 avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getStaticPath() } : undefined,
415 host: this.ActorFollow.ActorFollower.getHost()
418 type: this.ActorFollow.ActorFollowing.VideoChannel ? 'channel' as 'channel' : 'account' as 'account',
419 displayName: (this.ActorFollow.ActorFollowing.VideoChannel || this.ActorFollow.ActorFollowing.Account).getDisplayName(),
420 name: this.ActorFollow.ActorFollowing.preferredUsername
435 createdAt: this.createdAt.toISOString(),
436 updatedAt: this.updatedAt.toISOString()
440 formatVideo (this: UserNotificationModelForApi, video: UserNotificationIncludes.VideoInclude) {
449 this: UserNotificationModelForApi,
450 accountOrChannel: UserNotificationIncludes.AccountIncludeActor | UserNotificationIncludes.VideoChannelIncludeActor
452 const avatar = accountOrChannel.Actor.Avatar
453 ? { path: accountOrChannel.Actor.Avatar.getStaticPath() }
457 id: accountOrChannel.id,
458 displayName: accountOrChannel.getDisplayName(),
459 name: accountOrChannel.Actor.preferredUsername,
460 host: accountOrChannel.Actor.getHost(),