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 {
15 @Input() userInformationLoaded: Subject<any>
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 } = {}
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`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 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`,
46 newPeerTubeVersion: $localize`A new PeerTube version is available`,
47 newPluginVersion: $localize`One of your plugin/theme has a new available version`
49 this.notificationSettingGroups = [
51 label: $localize`Social`,
53 'newVideoFromSubscription',
60 label: $localize`Your videos`,
62 'newCommentOnMyVideo',
65 'myVideoImportFinished'
70 label: $localize`Moderation`,
75 'videoAutoBlacklistAsModerator'
80 label: $localize`Administration`,
82 'newUserRegistration',
83 'newInstanceFollower',
84 'autoInstanceFollowing',
91 this.rightNotifications = {
92 abuseAsModerator: UserRight.MANAGE_ABUSES,
93 videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST,
94 newUserRegistration: UserRight.MANAGE_USERS,
95 newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW,
96 autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION,
97 newPeerTubeVersion: UserRight.MANAGE_DEBUG,
98 newPluginVersion: UserRight.MANAGE_DEBUG
103 const serverConfig = this.serverService.getHTMLConfig()
104 this.emailEnabled = serverConfig.email.enabled
106 this.userInformationLoaded.subscribe(() => this.loadNotificationSettings())
109 hasUserRight (field: keyof UserNotificationSetting) {
110 const rightToHave = this.rightNotifications[field]
111 if (!rightToHave) return true // No rights needed
113 return this.user.hasRight(rightToHave)
116 updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) {
117 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL
118 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL
120 this.savePreferences()
123 updateWebSetting (field: keyof UserNotificationSetting, value: boolean) {
124 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB
125 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB
127 this.savePreferences()
130 private savePreferencesImpl () {
131 this.userNotificationService.updateNotificationSettings(this.user.notificationSettings)
134 this.notifier.success($localize`Preferences saved`, undefined, 2000)
137 error: err => this.notifier.error(err.message)
141 private loadNotificationSettings () {
142 for (const key of Object.keys(this.user.notificationSettings)) {
143 const value = this.user.notificationSettings[key]
144 this.emailNotifications[key] = value & UserNotificationSettingValue.EMAIL
146 this.webNotifications[key] = value & UserNotificationSettingValue.WEB