34febc4577462aa5d62e7a42b370c0e6689422df
[oweals/peertube.git] /
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
24   emailEnabled: boolean
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
35     this.labelNotifications = {
36       newVideoFromSubscription: this.i18n('New video from your subscriptions'),
37       newCommentOnMyVideo: this.i18n('New comment on your video'),
38       videoAbuseAsModerator: this.i18n('New video abuse'),
39       videoAutoBlacklistAsModerator: this.i18n('Video auto-blacklisted waiting review'),
40       blacklistOnMyVideo: this.i18n('One of your video is blacklisted/unblacklisted'),
41       myVideoPublished: this.i18n('Video published (after transcoding/scheduled update)'),
42       myVideoImportFinished: this.i18n('Video import finished'),
43       newUserRegistration: this.i18n('A new user registered on your instance'),
44       newFollow: this.i18n('You or your channel(s) has a new follower'),
45       commentMention: this.i18n('Someone mentioned you in video comments'),
46       newInstanceFollower: this.i18n('Your instance has a new follower')
47     }
48     this.notificationSettingKeys = Object.keys(this.labelNotifications) as (keyof UserNotificationSetting)[]
49
50     this.rightNotifications = {
51       videoAbuseAsModerator: UserRight.MANAGE_VIDEO_ABUSES,
52       videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST,
53       newUserRegistration: UserRight.MANAGE_USERS,
54       newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW
55     }
56
57     this.emailEnabled = this.serverService.getConfig().email.enabled
58   }
59
60   ngOnInit () {
61     this.userInformationLoaded.subscribe(() => this.loadNotificationSettings())
62   }
63
64   hasUserRight (field: keyof UserNotificationSetting) {
65     const rightToHave = this.rightNotifications[field]
66     if (!rightToHave) return true // No rights needed
67
68     return this.user.hasRight(rightToHave)
69   }
70
71   updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) {
72     if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL
73     else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL
74
75     this.savePreferences()
76   }
77
78   updateWebSetting (field: keyof UserNotificationSetting, value: boolean) {
79     if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB
80     else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB
81
82     this.savePreferences()
83   }
84
85   private savePreferencesImpl () {
86     this.userNotificationService.updateNotificationSettings(this.user, this.user.notificationSettings)
87       .subscribe(
88         () => {
89           this.notifier.success(this.i18n('Preferences saved'), undefined, 2000)
90         },
91
92         err => this.notifier.error(err.message)
93       )
94   }
95
96   private loadNotificationSettings () {
97     for (const key of Object.keys(this.user.notificationSettings)) {
98       const value = this.user.notificationSettings[key]
99       this.emailNotifications[key] = value & UserNotificationSettingValue.EMAIL
100
101       this.webNotifications[key] = value & UserNotificationSettingValue.WEB
102     }
103   }
104 }