Add new follow, mention and user registered notifs
[oweals/peertube.git] / server / models / account / user-notification-setting.ts
1 import {
2   AfterDestroy,
3   AfterUpdate,
4   AllowNull,
5   BelongsTo,
6   Column,
7   CreatedAt,
8   Default,
9   ForeignKey,
10   Is,
11   Model,
12   Table,
13   UpdatedAt
14 } from 'sequelize-typescript'
15 import { throwIfNotValid } from '../utils'
16 import { UserModel } from './user'
17 import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications'
18 import { UserNotificationSetting, UserNotificationSettingValue } from '../../../shared/models/users/user-notification-setting.model'
19 import { clearCacheByUserId } from '../../lib/oauth-model'
20
21 @Table({
22   tableName: 'userNotificationSetting',
23   indexes: [
24     {
25       fields: [ 'userId' ],
26       unique: true
27     }
28   ]
29 })
30 export class UserNotificationSettingModel extends Model<UserNotificationSettingModel> {
31
32   @AllowNull(false)
33   @Default(null)
34   @Is(
35     'UserNotificationSettingNewVideoFromSubscription',
36     value => throwIfNotValid(value, isUserNotificationSettingValid, 'newVideoFromSubscription')
37   )
38   @Column
39   newVideoFromSubscription: UserNotificationSettingValue
40
41   @AllowNull(false)
42   @Default(null)
43   @Is(
44     'UserNotificationSettingNewCommentOnMyVideo',
45     value => throwIfNotValid(value, isUserNotificationSettingValid, 'newCommentOnMyVideo')
46   )
47   @Column
48   newCommentOnMyVideo: UserNotificationSettingValue
49
50   @AllowNull(false)
51   @Default(null)
52   @Is(
53     'UserNotificationSettingVideoAbuseAsModerator',
54     value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAbuseAsModerator')
55   )
56   @Column
57   videoAbuseAsModerator: UserNotificationSettingValue
58
59   @AllowNull(false)
60   @Default(null)
61   @Is(
62     'UserNotificationSettingBlacklistOnMyVideo',
63     value => throwIfNotValid(value, isUserNotificationSettingValid, 'blacklistOnMyVideo')
64   )
65   @Column
66   blacklistOnMyVideo: UserNotificationSettingValue
67
68   @AllowNull(false)
69   @Default(null)
70   @Is(
71     'UserNotificationSettingMyVideoPublished',
72     value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished')
73   )
74   @Column
75   myVideoPublished: UserNotificationSettingValue
76
77   @AllowNull(false)
78   @Default(null)
79   @Is(
80     'UserNotificationSettingMyVideoImportFinished',
81     value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished')
82   )
83   @Column
84   myVideoImportFinished: UserNotificationSettingValue
85
86   @AllowNull(false)
87   @Default(null)
88   @Is(
89     'UserNotificationSettingNewUserRegistration',
90     value => throwIfNotValid(value, isUserNotificationSettingValid, 'newUserRegistration')
91   )
92   @Column
93   newUserRegistration: UserNotificationSettingValue
94
95   @AllowNull(false)
96   @Default(null)
97   @Is(
98     'UserNotificationSettingNewFollow',
99     value => throwIfNotValid(value, isUserNotificationSettingValid, 'newFollow')
100   )
101   @Column
102   newFollow: UserNotificationSettingValue
103
104   @AllowNull(false)
105   @Default(null)
106   @Is(
107     'UserNotificationSettingCommentMention',
108     value => throwIfNotValid(value, isUserNotificationSettingValid, 'commentMention')
109   )
110   @Column
111   commentMention: UserNotificationSettingValue
112
113   @ForeignKey(() => UserModel)
114   @Column
115   userId: number
116
117   @BelongsTo(() => UserModel, {
118     foreignKey: {
119       allowNull: false
120     },
121     onDelete: 'cascade'
122   })
123   User: UserModel
124
125   @CreatedAt
126   createdAt: Date
127
128   @UpdatedAt
129   updatedAt: Date
130
131   @AfterUpdate
132   @AfterDestroy
133   static removeTokenCache (instance: UserNotificationSettingModel) {
134     return clearCacheByUserId(instance.userId)
135   }
136
137   toFormattedJSON (): UserNotificationSetting {
138     return {
139       newCommentOnMyVideo: this.newCommentOnMyVideo,
140       newVideoFromSubscription: this.newVideoFromSubscription,
141       videoAbuseAsModerator: this.videoAbuseAsModerator,
142       blacklistOnMyVideo: this.blacklistOnMyVideo,
143       myVideoPublished: this.myVideoPublished,
144       myVideoImportFinished: this.myVideoImportFinished,
145       newUserRegistration: this.newUserRegistration,
146       commentMention: this.commentMention,
147       newFollow: this.newFollow
148     }
149   }
150 }