]>
Commit | Line | Data |
---|---|---|
cef534ed C |
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 | ||
dc133480 C |
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 | ||
cef534ed C |
86 | @ForeignKey(() => UserModel) |
87 | @Column | |
88 | userId: number | |
89 | ||
90 | @BelongsTo(() => UserModel, { | |
91 | foreignKey: { | |
92 | allowNull: false | |
93 | }, | |
94 | onDelete: 'cascade' | |
95 | }) | |
96 | User: UserModel | |
97 | ||
98 | @CreatedAt | |
99 | createdAt: Date | |
100 | ||
101 | @UpdatedAt | |
102 | updatedAt: Date | |
103 | ||
104 | @AfterUpdate | |
105 | @AfterDestroy | |
106 | static removeTokenCache (instance: UserNotificationSettingModel) { | |
107 | return clearCacheByUserId(instance.userId) | |
108 | } | |
109 | ||
110 | toFormattedJSON (): UserNotificationSetting { | |
111 | return { | |
112 | newCommentOnMyVideo: this.newCommentOnMyVideo, | |
113 | newVideoFromSubscription: this.newVideoFromSubscription, | |
114 | videoAbuseAsModerator: this.videoAbuseAsModerator, | |
dc133480 C |
115 | blacklistOnMyVideo: this.blacklistOnMyVideo, |
116 | myVideoPublished: this.myVideoPublished, | |
117 | myVideoImportFinished: this.myVideoImportFinished | |
cef534ed C |
118 | } |
119 | } | |
120 | } |