]>
Commit | Line | Data |
---|---|---|
2f1548fd C |
1 | import { Component, Input, OnInit } from '@angular/core' |
2 | import { User } from '@app/shared' | |
3 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
4 | import { Subject } from 'rxjs' | |
5 | import { UserNotificationSetting, UserNotificationSettingValue, UserRight } from '../../../../../../shared' | |
6 | import { Notifier, ServerService } from '@app/core' | |
7 | import { debounce } from 'lodash-es' | |
8 | import { UserNotificationService } from '@app/shared/users/user-notification.service' | |
9 | ||
10 | @Component({ | |
11 | selector: 'my-account-notification-preferences', | |
12 | templateUrl: './my-account-notification-preferences.component.html', | |
13 | styleUrls: [ './my-account-notification-preferences.component.scss' ] | |
14 | }) | |
15 | export class MyAccountNotificationPreferencesComponent implements OnInit { | |
16 | @Input() user: User = null | |
17 | @Input() userInformationLoaded: Subject<any> | |
18 | ||
19 | notificationSettingKeys: (keyof UserNotificationSetting)[] = [] | |
20 | emailNotifications: { [ id in keyof UserNotificationSetting ]: boolean } = {} as any | |
21 | webNotifications: { [ id in keyof UserNotificationSetting ]: boolean } = {} as any | |
22 | labelNotifications: { [ id in keyof UserNotificationSetting ]: string } = {} as any | |
23 | rightNotifications: { [ id in keyof Partial<UserNotificationSetting> ]: UserRight } = {} as any | |
ba430d75 | 24 | emailEnabled = false |
2f1548fd C |
25 | |
26 | private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500) | |
27 | ||
28 | constructor ( | |
29 | private i18n: I18n, | |
30 | private userNotificationService: UserNotificationService, | |
31 | private serverService: ServerService, | |
32 | private notifier: Notifier | |
33 | ) { | |
34 | this.labelNotifications = { | |
35 | newVideoFromSubscription: this.i18n('New video from your subscriptions'), | |
36 | newCommentOnMyVideo: this.i18n('New comment on your video'), | |
1e17071b | 37 | videoAbuseAsModerator: this.i18n('New video abuse'), |
7ccddd7b | 38 | videoAutoBlacklistAsModerator: this.i18n('Video auto-blacklisted waiting review'), |
2f1548fd C |
39 | blacklistOnMyVideo: this.i18n('One of your video is blacklisted/unblacklisted'), |
40 | myVideoPublished: this.i18n('Video published (after transcoding/scheduled update)'), | |
41 | myVideoImportFinished: this.i18n('Video import finished'), | |
42 | newUserRegistration: this.i18n('A new user registered on your instance'), | |
43 | newFollow: this.i18n('You or your channel(s) has a new follower'), | |
846751c9 | 44 | commentMention: this.i18n('Someone mentioned you in video comments'), |
e1b49ee5 C |
45 | newInstanceFollower: this.i18n('Your instance has a new follower'), |
46 | autoInstanceFollowing: this.i18n('Your instance auto followed another instance') | |
2f1548fd C |
47 | } |
48 | this.notificationSettingKeys = Object.keys(this.labelNotifications) as (keyof UserNotificationSetting)[] | |
49 | ||
50 | this.rightNotifications = { | |
51 | videoAbuseAsModerator: UserRight.MANAGE_VIDEO_ABUSES, | |
7ccddd7b | 52 | videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST, |
846751c9 | 53 | newUserRegistration: UserRight.MANAGE_USERS, |
e1b49ee5 C |
54 | newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW, |
55 | autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION | |
2f1548fd | 56 | } |
2f1548fd C |
57 | } |
58 | ||
59 | ngOnInit () { | |
ba430d75 C |
60 | this.serverService.getConfig() |
61 | .subscribe(config => { | |
62 | this.emailEnabled = config.email.enabled | |
63 | }) | |
64 | ||
2f1548fd C |
65 | this.userInformationLoaded.subscribe(() => this.loadNotificationSettings()) |
66 | } | |
67 | ||
68 | hasUserRight (field: keyof UserNotificationSetting) { | |
69 | const rightToHave = this.rightNotifications[field] | |
70 | if (!rightToHave) return true // No rights needed | |
71 | ||
72 | return this.user.hasRight(rightToHave) | |
73 | } | |
74 | ||
75 | updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) { | |
76 | if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL | |
77 | else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL | |
78 | ||
79 | this.savePreferences() | |
80 | } | |
81 | ||
82 | updateWebSetting (field: keyof UserNotificationSetting, value: boolean) { | |
83 | if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB | |
84 | else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB | |
85 | ||
86 | this.savePreferences() | |
87 | } | |
88 | ||
89 | private savePreferencesImpl () { | |
90 | this.userNotificationService.updateNotificationSettings(this.user, this.user.notificationSettings) | |
91 | .subscribe( | |
92 | () => { | |
93 | this.notifier.success(this.i18n('Preferences saved'), undefined, 2000) | |
94 | }, | |
95 | ||
96 | err => this.notifier.error(err.message) | |
97 | ) | |
98 | } | |
99 | ||
100 | private loadNotificationSettings () { | |
101 | for (const key of Object.keys(this.user.notificationSettings)) { | |
102 | const value = this.user.notificationSettings[key] | |
103 | this.emailNotifications[key] = value & UserNotificationSettingValue.EMAIL | |
104 | ||
105 | this.webNotifications[key] = value & UserNotificationSettingValue.WEB | |
106 | } | |
107 | } | |
108 | } |