]>
Commit | Line | Data |
---|---|---|
1 | import { debounce } from 'lodash-es' | |
2 | import { Subject } from 'rxjs' | |
3 | import { Component, Input, OnInit } from '@angular/core' | |
4 | import { Notifier, ServerService, User } from '@app/core' | |
5 | import { UserNotificationService } from '@app/shared/shared-main' | |
6 | import { UserNotificationSetting, UserNotificationSettingValue, UserRight } from '@shared/models' | |
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 { | |
14 | @Input() user: User | |
15 | @Input() userInformationLoaded: Subject<any> | |
16 | ||
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 } = {} | |
22 | emailEnabled = false | |
23 | ||
24 | private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500) | |
25 | ||
26 | constructor ( | |
27 | private userNotificationService: UserNotificationService, | |
28 | private serverService: ServerService, | |
29 | private notifier: Notifier | |
30 | ) { | |
31 | this.labelNotifications = { | |
32 | newVideoFromSubscription: $localize`New video from your subscriptions`, | |
33 | newCommentOnMyVideo: $localize`New comment on your video`, | |
34 | abuseAsModerator: $localize`New abuse`, | |
35 | videoAutoBlacklistAsModerator: $localize`An automatically blocked video is awaiting review`, | |
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 one of your channels 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`, | |
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`, | |
48 | myVideoStudioEditionFinished: $localize`Video studio edition has finished` | |
49 | } | |
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', | |
66 | 'myVideoImportFinished', | |
67 | 'myVideoStudioEditionFinished' | |
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 | ] | |
92 | ||
93 | this.rightNotifications = { | |
94 | abuseAsModerator: UserRight.MANAGE_ABUSES, | |
95 | videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST, | |
96 | newUserRegistration: UserRight.MANAGE_USERS, | |
97 | newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW, | |
98 | autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION, | |
99 | newPeerTubeVersion: UserRight.MANAGE_DEBUG, | |
100 | newPluginVersion: UserRight.MANAGE_DEBUG | |
101 | } | |
102 | } | |
103 | ||
104 | ngOnInit () { | |
105 | const serverConfig = this.serverService.getHTMLConfig() | |
106 | this.emailEnabled = serverConfig.email.enabled | |
107 | ||
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 () { | |
133 | this.userNotificationService.updateNotificationSettings(this.user.notificationSettings) | |
134 | .subscribe({ | |
135 | next: () => { | |
136 | this.notifier.success($localize`Preferences saved`, undefined, 2000) | |
137 | }, | |
138 | ||
139 | error: err => this.notifier.error(err.message) | |
140 | }) | |
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 | } |