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 { Op } 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'
20 [ScopeNames.WITH_ALL]: {
23 attributes: [ 'id', 'uuid', 'name' ],
24 model: () => VideoModel.unscoped(),
29 attributes: [ 'id', 'name' ],
30 model: () => VideoChannelModel.unscoped()
36 model: () => VideoCommentModel.unscoped(),
41 attributes: [ 'id', 'name' ],
42 model: () => AccountModel.unscoped()
46 attributes: [ 'id', 'uuid', 'name' ],
47 model: () => VideoModel.unscoped()
53 model: () => VideoAbuseModel.unscoped(),
58 attributes: [ 'id', 'uuid', 'name' ],
59 model: () => VideoModel.unscoped()
65 model: () => VideoBlacklistModel.unscoped(),
70 attributes: [ 'id', 'uuid', 'name' ],
71 model: () => VideoModel.unscoped()
79 tableName: 'userNotification',
85 fields: [ 'commentId' ]
89 export class UserNotificationModel extends Model<UserNotificationModel> {
93 @Is('UserNotificationType', value => throwIfNotValid(value, isUserNotificationTypeValid, 'type'))
95 type: UserNotificationType
99 @Is('UserNotificationRead', value => throwIfNotValid(value, isBooleanValid, 'read'))
109 @ForeignKey(() => UserModel)
113 @BelongsTo(() => UserModel, {
121 @ForeignKey(() => VideoModel)
125 @BelongsTo(() => VideoModel, {
133 @ForeignKey(() => VideoCommentModel)
137 @BelongsTo(() => VideoCommentModel, {
143 Comment: VideoCommentModel
145 @ForeignKey(() => VideoAbuseModel)
149 @BelongsTo(() => VideoAbuseModel, {
155 VideoAbuse: VideoAbuseModel
157 @ForeignKey(() => VideoBlacklistModel)
159 videoBlacklistId: number
161 @BelongsTo(() => VideoBlacklistModel, {
167 VideoBlacklist: VideoBlacklistModel
169 static listForApi (userId: number, start: number, count: number, sort: string) {
173 order: getSort(sort),
179 return UserNotificationModel.scope(ScopeNames.WITH_ALL)
180 .findAndCountAll(query)
181 .then(({ rows, count }) => {
189 static markAsRead (userId: number, notificationIds: number[]) {
194 [Op.any]: notificationIds
199 return UserNotificationModel.update({ read: true }, query)
202 toFormattedJSON (): UserNotification {
203 const video = this.Video ? {
205 uuid: this.Video.uuid,
206 name: this.Video.name,
208 id: this.Video.VideoChannel.id,
209 displayName: this.Video.VideoChannel.getDisplayName()
213 const comment = this.Comment ? {
216 id: this.Comment.Account.id,
217 displayName: this.Comment.Account.getDisplayName()
220 id: this.Comment.Video.id,
221 uuid: this.Comment.Video.uuid,
222 name: this.Comment.Video.name
226 const videoAbuse = this.VideoAbuse ? {
227 id: this.VideoAbuse.id,
229 id: this.VideoAbuse.Video.id,
230 uuid: this.VideoAbuse.Video.uuid,
231 name: this.VideoAbuse.Video.name
235 const videoBlacklist = this.VideoBlacklist ? {
236 id: this.VideoBlacklist.id,
238 id: this.VideoBlacklist.Video.id,
239 uuid: this.VideoBlacklist.Video.uuid,
240 name: this.VideoBlacklist.Video.name
252 createdAt: this.createdAt.toISOString(),
253 updatedAt: this.updatedAt.toISOString()