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'
9 selector: 'my-account-notification-preferences',
10 templateUrl: './my-account-notification-preferences.component.html',
11 styleUrls: [ './my-account-notification-preferences.component.scss' ]
13 export class MyAccountNotificationPreferencesComponent implements OnInit {
14 @Input() user: User = null
15 @Input() userInformationLoaded: Subject<any>
17 notificationSettingKeys: (keyof UserNotificationSetting)[] = []
18 emailNotifications: { [ id in keyof UserNotificationSetting ]: boolean } = {} as any
19 webNotifications: { [ id in keyof UserNotificationSetting ]: boolean } = {} as any
20 labelNotifications: { [ id in keyof UserNotificationSetting ]: string } = {} as any
21 rightNotifications: { [ id in keyof Partial<UserNotificationSetting> ]: UserRight } = {} as any
24 private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500)
27 private userNotificationService: UserNotificationService,
28 private serverService: ServerService,
29 private notifier: Notifier
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`Video blocked automatically waiting 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 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`,
45 abuseStateChange: $localize`One of your abuse reports has been accepted or rejected by moderators`
47 this.notificationSettingKeys = Object.keys(this.labelNotifications) as (keyof UserNotificationSetting)[]
49 this.rightNotifications = {
50 abuseAsModerator: UserRight.MANAGE_ABUSES,
51 videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST,
52 newUserRegistration: UserRight.MANAGE_USERS,
53 newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW,
54 autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION
59 this.serverService.getConfig()
60 .subscribe(config => {
61 this.emailEnabled = config.email.enabled
64 this.userInformationLoaded.subscribe(() => this.loadNotificationSettings())
67 hasUserRight (field: keyof UserNotificationSetting) {
68 const rightToHave = this.rightNotifications[field]
69 if (!rightToHave) return true // No rights needed
71 return this.user.hasRight(rightToHave)
74 updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) {
75 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL
76 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL
78 this.savePreferences()
81 updateWebSetting (field: keyof UserNotificationSetting, value: boolean) {
82 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB
83 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB
85 this.savePreferences()
88 private savePreferencesImpl () {
89 this.userNotificationService.updateNotificationSettings(this.user.notificationSettings)
92 this.notifier.success($localize`Preferences saved`, undefined, 2000)
95 err => this.notifier.error(err.message)
99 private loadNotificationSettings () {
100 for (const key of Object.keys(this.user.notificationSettings)) {
101 const value = this.user.notificationSettings[key]
102 this.emailNotifications[key] = value & UserNotificationSettingValue.EMAIL
104 this.webNotifications[key] = value & UserNotificationSettingValue.WEB