]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/user-notification-setting.ts
Type toFormattedJSON
[github/Chocobozzz/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 import { MNotificationSettingFormattable } from '@server/typings/models'
21
22 @Table({
23 tableName: 'userNotificationSetting',
24 indexes: [
25 {
26 fields: [ 'userId' ],
27 unique: true
28 }
29 ]
30 })
31 export class UserNotificationSettingModel extends Model<UserNotificationSettingModel> {
32
33 @AllowNull(false)
34 @Default(null)
35 @Is(
36 'UserNotificationSettingNewVideoFromSubscription',
37 value => throwIfNotValid(value, isUserNotificationSettingValid, 'newVideoFromSubscription')
38 )
39 @Column
40 newVideoFromSubscription: UserNotificationSettingValue
41
42 @AllowNull(false)
43 @Default(null)
44 @Is(
45 'UserNotificationSettingNewCommentOnMyVideo',
46 value => throwIfNotValid(value, isUserNotificationSettingValid, 'newCommentOnMyVideo')
47 )
48 @Column
49 newCommentOnMyVideo: UserNotificationSettingValue
50
51 @AllowNull(false)
52 @Default(null)
53 @Is(
54 'UserNotificationSettingVideoAbuseAsModerator',
55 value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAbuseAsModerator')
56 )
57 @Column
58 videoAbuseAsModerator: UserNotificationSettingValue
59
60 @AllowNull(false)
61 @Default(null)
62 @Is(
63 'UserNotificationSettingVideoAutoBlacklistAsModerator',
64 value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAutoBlacklistAsModerator')
65 )
66 @Column
67 videoAutoBlacklistAsModerator: UserNotificationSettingValue
68
69 @AllowNull(false)
70 @Default(null)
71 @Is(
72 'UserNotificationSettingBlacklistOnMyVideo',
73 value => throwIfNotValid(value, isUserNotificationSettingValid, 'blacklistOnMyVideo')
74 )
75 @Column
76 blacklistOnMyVideo: UserNotificationSettingValue
77
78 @AllowNull(false)
79 @Default(null)
80 @Is(
81 'UserNotificationSettingMyVideoPublished',
82 value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished')
83 )
84 @Column
85 myVideoPublished: UserNotificationSettingValue
86
87 @AllowNull(false)
88 @Default(null)
89 @Is(
90 'UserNotificationSettingMyVideoImportFinished',
91 value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished')
92 )
93 @Column
94 myVideoImportFinished: UserNotificationSettingValue
95
96 @AllowNull(false)
97 @Default(null)
98 @Is(
99 'UserNotificationSettingNewUserRegistration',
100 value => throwIfNotValid(value, isUserNotificationSettingValid, 'newUserRegistration')
101 )
102 @Column
103 newUserRegistration: UserNotificationSettingValue
104
105 @AllowNull(false)
106 @Default(null)
107 @Is(
108 'UserNotificationSettingNewInstanceFollower',
109 value => throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower')
110 )
111 @Column
112 newInstanceFollower: UserNotificationSettingValue
113
114 @AllowNull(false)
115 @Default(null)
116 @Is(
117 'UserNotificationSettingNewFollow',
118 value => throwIfNotValid(value, isUserNotificationSettingValid, 'newFollow')
119 )
120 @Column
121 newFollow: UserNotificationSettingValue
122
123 @AllowNull(false)
124 @Default(null)
125 @Is(
126 'UserNotificationSettingCommentMention',
127 value => throwIfNotValid(value, isUserNotificationSettingValid, 'commentMention')
128 )
129 @Column
130 commentMention: UserNotificationSettingValue
131
132 @ForeignKey(() => UserModel)
133 @Column
134 userId: number
135
136 @BelongsTo(() => UserModel, {
137 foreignKey: {
138 allowNull: false
139 },
140 onDelete: 'cascade'
141 })
142 User: UserModel
143
144 @CreatedAt
145 createdAt: Date
146
147 @UpdatedAt
148 updatedAt: Date
149
150 @AfterUpdate
151 @AfterDestroy
152 static removeTokenCache (instance: UserNotificationSettingModel) {
153 return clearCacheByUserId(instance.userId)
154 }
155
156 toFormattedJSON (this: MNotificationSettingFormattable): UserNotificationSetting {
157 return {
158 newCommentOnMyVideo: this.newCommentOnMyVideo,
159 newVideoFromSubscription: this.newVideoFromSubscription,
160 videoAbuseAsModerator: this.videoAbuseAsModerator,
161 videoAutoBlacklistAsModerator: this.videoAutoBlacklistAsModerator,
162 blacklistOnMyVideo: this.blacklistOnMyVideo,
163 myVideoPublished: this.myVideoPublished,
164 myVideoImportFinished: this.myVideoImportFinished,
165 newUserRegistration: this.newUserRegistration,
166 commentMention: this.commentMention,
167 newFollow: this.newFollow,
168 newInstanceFollower: this.newInstanceFollower
169 }
170 }
171 }