]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/user-notification-setting.ts
Create a dedicated table to track video thumbnails
[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
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 'UserNotificationSettingVideoAutoBlacklistAsModerator',
63 value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAutoBlacklistAsModerator')
64 )
65 @Column
66 videoAutoBlacklistAsModerator: UserNotificationSettingValue
67
68 @AllowNull(false)
69 @Default(null)
70 @Is(
71 'UserNotificationSettingBlacklistOnMyVideo',
72 value => throwIfNotValid(value, isUserNotificationSettingValid, 'blacklistOnMyVideo')
73 )
74 @Column
75 blacklistOnMyVideo: UserNotificationSettingValue
76
77 @AllowNull(false)
78 @Default(null)
79 @Is(
80 'UserNotificationSettingMyVideoPublished',
81 value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished')
82 )
83 @Column
84 myVideoPublished: UserNotificationSettingValue
85
86 @AllowNull(false)
87 @Default(null)
88 @Is(
89 'UserNotificationSettingMyVideoImportFinished',
90 value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished')
91 )
92 @Column
93 myVideoImportFinished: UserNotificationSettingValue
94
95 @AllowNull(false)
96 @Default(null)
97 @Is(
98 'UserNotificationSettingNewUserRegistration',
99 value => throwIfNotValid(value, isUserNotificationSettingValid, 'newUserRegistration')
100 )
101 @Column
102 newUserRegistration: UserNotificationSettingValue
103
104 @AllowNull(false)
105 @Default(null)
106 @Is(
107 'UserNotificationSettingNewInstanceFollower',
108 value => throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower')
109 )
110 @Column
111 newInstanceFollower: UserNotificationSettingValue
112
113 @AllowNull(false)
114 @Default(null)
115 @Is(
116 'UserNotificationSettingNewFollow',
117 value => throwIfNotValid(value, isUserNotificationSettingValid, 'newFollow')
118 )
119 @Column
120 newFollow: UserNotificationSettingValue
121
122 @AllowNull(false)
123 @Default(null)
124 @Is(
125 'UserNotificationSettingCommentMention',
126 value => throwIfNotValid(value, isUserNotificationSettingValid, 'commentMention')
127 )
128 @Column
129 commentMention: UserNotificationSettingValue
130
131 @ForeignKey(() => UserModel)
132 @Column
133 userId: number
134
135 @BelongsTo(() => UserModel, {
136 foreignKey: {
137 allowNull: false
138 },
139 onDelete: 'cascade'
140 })
141 User: UserModel
142
143 @CreatedAt
144 createdAt: Date
145
146 @UpdatedAt
147 updatedAt: Date
148
149 @AfterUpdate
150 @AfterDestroy
151 static removeTokenCache (instance: UserNotificationSettingModel) {
152 return clearCacheByUserId(instance.userId)
153 }
154
155 toFormattedJSON (): UserNotificationSetting {
156 return {
157 newCommentOnMyVideo: this.newCommentOnMyVideo,
158 newVideoFromSubscription: this.newVideoFromSubscription,
159 videoAbuseAsModerator: this.videoAbuseAsModerator,
160 videoAutoBlacklistAsModerator: this.videoAutoBlacklistAsModerator,
161 blacklistOnMyVideo: this.blacklistOnMyVideo,
162 myVideoPublished: this.myVideoPublished,
163 myVideoImportFinished: this.myVideoImportFinished,
164 newUserRegistration: this.newUserRegistration,
165 commentMention: this.commentMention,
166 newFollow: this.newFollow,
167 newInstanceFollower: this.newInstanceFollower
168 }
169 }
170 }