]>
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`, | |
40 | newFollow: $localize`You or your channel(s) has a new follower`, | |
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`, | |
47 | newPluginVersion: $localize`One of your plugin/theme has a new available version` | |
2f1548fd | 48 | } |
dc4e0ddb C |
49 | this.notificationSettingGroups = [ |
50 | { | |
51 | label: $localize`Social`, | |
52 | keys: [ | |
53 | 'newVideoFromSubscription', | |
54 | 'newFollow', | |
55 | 'commentMention' | |
56 | ] | |
57 | }, | |
58 | ||
59 | { | |
60 | label: $localize`Your videos`, | |
61 | keys: [ | |
62 | 'newCommentOnMyVideo', | |
63 | 'blacklistOnMyVideo', | |
64 | 'myVideoPublished', | |
65 | 'myVideoImportFinished' | |
66 | ] | |
67 | }, | |
68 | ||
69 | { | |
70 | label: $localize`Moderation`, | |
71 | keys: [ | |
72 | 'abuseStateChange', | |
73 | 'abuseNewMessage', | |
74 | 'abuseAsModerator', | |
75 | 'videoAutoBlacklistAsModerator' | |
76 | ] | |
77 | }, | |
78 | ||
79 | { | |
80 | label: $localize`Administration`, | |
81 | keys: [ | |
82 | 'newUserRegistration', | |
83 | 'newInstanceFollower', | |
84 | 'autoInstanceFollowing', | |
85 | 'newPeerTubeVersion', | |
86 | 'newPluginVersion' | |
87 | ] | |
88 | } | |
89 | ] | |
2f1548fd C |
90 | |
91 | this.rightNotifications = { | |
4f32032f | 92 | abuseAsModerator: UserRight.MANAGE_ABUSES, |
3487330d | 93 | videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST, |
846751c9 | 94 | newUserRegistration: UserRight.MANAGE_USERS, |
e1b49ee5 | 95 | newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW, |
32a18cbf C |
96 | autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION, |
97 | newPeerTubeVersion: UserRight.MANAGE_DEBUG, | |
98 | newPluginVersion: UserRight.MANAGE_DEBUG | |
2f1548fd | 99 | } |
2f1548fd C |
100 | } |
101 | ||
102 | ngOnInit () { | |
2989628b C |
103 | const serverConfig = this.serverService.getHTMLConfig() |
104 | this.emailEnabled = serverConfig.email.enabled | |
ba430d75 | 105 | |
2f1548fd C |
106 | this.userInformationLoaded.subscribe(() => this.loadNotificationSettings()) |
107 | } | |
108 | ||
109 | hasUserRight (field: keyof UserNotificationSetting) { | |
110 | const rightToHave = this.rightNotifications[field] | |
111 | if (!rightToHave) return true // No rights needed | |
112 | ||
113 | return this.user.hasRight(rightToHave) | |
114 | } | |
115 | ||
116 | updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) { | |
117 | if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL | |
118 | else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL | |
119 | ||
120 | this.savePreferences() | |
121 | } | |
122 | ||
123 | updateWebSetting (field: keyof UserNotificationSetting, value: boolean) { | |
124 | if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB | |
125 | else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB | |
126 | ||
127 | this.savePreferences() | |
128 | } | |
129 | ||
130 | private savePreferencesImpl () { | |
a5cf76af | 131 | this.userNotificationService.updateNotificationSettings(this.user.notificationSettings) |
1378c0d3 C |
132 | .subscribe({ |
133 | next: () => { | |
66357162 | 134 | this.notifier.success($localize`Preferences saved`, undefined, 2000) |
2f1548fd C |
135 | }, |
136 | ||
1378c0d3 C |
137 | error: err => this.notifier.error(err.message) |
138 | }) | |
2f1548fd C |
139 | } |
140 | ||
141 | private loadNotificationSettings () { | |
142 | for (const key of Object.keys(this.user.notificationSettings)) { | |
143 | const value = this.user.notificationSettings[key] | |
144 | this.emailNotifications[key] = value & UserNotificationSettingValue.EMAIL | |
145 | ||
146 | this.webNotifications[key] = value & UserNotificationSettingValue.WEB | |
147 | } | |
148 | } | |
149 | } |