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