]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { debounce } from 'lodash-es' |
2 | import { Subject } from 'rxjs' | |
2f1548fd | 3 | import { Component, Input, OnInit } from '@angular/core' |
67ed6552 C |
4 | import { Notifier, ServerService, User } from '@app/core' |
5 | import { UserNotificationService } from '@app/shared/shared-main' | |
67ed6552 | 6 | import { UserNotificationSetting, UserNotificationSettingValue, UserRight } from '@shared/models' |
2f1548fd C |
7 | |
8 | @Component({ | |
9 | selector: 'my-account-notification-preferences', | |
10 | templateUrl: './my-account-notification-preferences.component.html', | |
11 | styleUrls: [ './my-account-notification-preferences.component.scss' ] | |
12 | }) | |
13 | export class MyAccountNotificationPreferencesComponent implements OnInit { | |
dc4e0ddb | 14 | @Input() user: User |
2f1548fd C |
15 | @Input() userInformationLoaded: Subject<any> |
16 | ||
dc4e0ddb C |
17 | notificationSettingGroups: { label: string, keys: (keyof UserNotificationSetting)[] }[] = [] |
18 | emailNotifications: { [ id in keyof UserNotificationSetting ]?: boolean } = {} | |
19 | webNotifications: { [ id in keyof UserNotificationSetting ]?: boolean } = {} | |
20 | labelNotifications: { [ id in keyof UserNotificationSetting ]?: string } = {} | |
21 | rightNotifications: { [ id in keyof Partial<UserNotificationSetting> ]?: UserRight } = {} | |
ba430d75 | 22 | emailEnabled = false |
2f1548fd C |
23 | |
24 | private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500) | |
25 | ||
26 | constructor ( | |
2f1548fd C |
27 | private userNotificationService: UserNotificationService, |
28 | private serverService: ServerService, | |
29 | private notifier: Notifier | |
30 | ) { | |
31 | this.labelNotifications = { | |
66357162 C |
32 | newVideoFromSubscription: $localize`New video from your subscriptions`, |
33 | newCommentOnMyVideo: $localize`New comment on your video`, | |
34 | abuseAsModerator: $localize`New abuse`, | |
dc4e0ddb | 35 | videoAutoBlacklistAsModerator: $localize`An automatically blocked video is awaiting review`, |
66357162 C |
36 | blacklistOnMyVideo: $localize`One of your video is blocked/unblocked`, |
37 | myVideoPublished: $localize`Video published (after transcoding/scheduled update)`, | |
38 | myVideoImportFinished: $localize`Video import finished`, | |
39 | newUserRegistration: $localize`A new user registered on your instance`, | |
eaa52952 | 40 | newFollow: $localize`You or one of your channels has a new follower`, |
66357162 C |
41 | commentMention: $localize`Someone mentioned you in video comments`, |
42 | newInstanceFollower: $localize`Your instance has a new follower`, | |
43 | autoInstanceFollowing: $localize`Your instance automatically followed another instance`, | |
44 | abuseNewMessage: $localize`An abuse report received a new message`, | |
32a18cbf C |
45 | abuseStateChange: $localize`One of your abuse reports has been accepted or rejected by moderators`, |
46 | newPeerTubeVersion: $localize`A new PeerTube version is available`, | |
1808a1f8 | 47 | newPluginVersion: $localize`One of your plugin/theme has a new available version`, |
92e66e04 | 48 | myVideoStudioEditionFinished: $localize`Video studio edition has finished` |
2f1548fd | 49 | } |
dc4e0ddb C |
50 | this.notificationSettingGroups = [ |
51 | { | |
52 | label: $localize`Social`, | |
53 | keys: [ | |
54 | 'newVideoFromSubscription', | |
55 | 'newFollow', | |
56 | 'commentMention' | |
57 | ] | |
58 | }, | |
59 | ||
60 | { | |
61 | label: $localize`Your videos`, | |
62 | keys: [ | |
63 | 'newCommentOnMyVideo', | |
64 | 'blacklistOnMyVideo', | |
65 | 'myVideoPublished', | |
1808a1f8 | 66 | 'myVideoImportFinished', |
92e66e04 | 67 | 'myVideoStudioEditionFinished' |
dc4e0ddb C |
68 | ] |
69 | }, | |
70 | ||
71 | { | |
72 | label: $localize`Moderation`, | |
73 | keys: [ | |
74 | 'abuseStateChange', | |
75 | 'abuseNewMessage', | |
76 | 'abuseAsModerator', | |
77 | 'videoAutoBlacklistAsModerator' | |
78 | ] | |
79 | }, | |
80 | ||
81 | { | |
82 | label: $localize`Administration`, | |
83 | keys: [ | |
84 | 'newUserRegistration', | |
85 | 'newInstanceFollower', | |
86 | 'autoInstanceFollowing', | |
87 | 'newPeerTubeVersion', | |
88 | 'newPluginVersion' | |
89 | ] | |
90 | } | |
91 | ] | |
2f1548fd C |
92 | |
93 | this.rightNotifications = { | |
4f32032f | 94 | abuseAsModerator: UserRight.MANAGE_ABUSES, |
3487330d | 95 | videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST, |
846751c9 | 96 | newUserRegistration: UserRight.MANAGE_USERS, |
e1b49ee5 | 97 | newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW, |
32a18cbf C |
98 | autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION, |
99 | newPeerTubeVersion: UserRight.MANAGE_DEBUG, | |
100 | newPluginVersion: UserRight.MANAGE_DEBUG | |
2f1548fd | 101 | } |
2f1548fd C |
102 | } |
103 | ||
104 | ngOnInit () { | |
2989628b C |
105 | const serverConfig = this.serverService.getHTMLConfig() |
106 | this.emailEnabled = serverConfig.email.enabled | |
ba430d75 | 107 | |
2f1548fd C |
108 | this.userInformationLoaded.subscribe(() => this.loadNotificationSettings()) |
109 | } | |
110 | ||
111 | hasUserRight (field: keyof UserNotificationSetting) { | |
112 | const rightToHave = this.rightNotifications[field] | |
113 | if (!rightToHave) return true // No rights needed | |
114 | ||
115 | return this.user.hasRight(rightToHave) | |
116 | } | |
117 | ||
118 | updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) { | |
119 | if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL | |
120 | else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL | |
121 | ||
122 | this.savePreferences() | |
123 | } | |
124 | ||
125 | updateWebSetting (field: keyof UserNotificationSetting, value: boolean) { | |
126 | if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB | |
127 | else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB | |
128 | ||
129 | this.savePreferences() | |
130 | } | |
131 | ||
132 | private savePreferencesImpl () { | |
a5cf76af | 133 | this.userNotificationService.updateNotificationSettings(this.user.notificationSettings) |
1378c0d3 C |
134 | .subscribe({ |
135 | next: () => { | |
66357162 | 136 | this.notifier.success($localize`Preferences saved`, undefined, 2000) |
2f1548fd C |
137 | }, |
138 | ||
1378c0d3 C |
139 | error: err => this.notifier.error(err.message) |
140 | }) | |
2f1548fd C |
141 | } |
142 | ||
143 | private loadNotificationSettings () { | |
144 | for (const key of Object.keys(this.user.notificationSettings)) { | |
145 | const value = this.user.notificationSettings[key] | |
146 | this.emailNotifications[key] = value & UserNotificationSettingValue.EMAIL | |
147 | ||
148 | this.webNotifications[key] = value & UserNotificationSettingValue.WEB | |
149 | } | |
150 | } | |
151 | } |