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 { objectKeysTyped } from '@shared/core-utils'
7 import { UserNotificationSetting, UserNotificationSettingValue, UserRight } from '@shared/models'
10 selector: 'my-account-notification-preferences',
11 templateUrl: './my-account-notification-preferences.component.html',
12 styleUrls: [ './my-account-notification-preferences.component.scss' ]
14 export class MyAccountNotificationPreferencesComponent implements OnInit {
16 @Input() userInformationLoaded: Subject<any>
18 notificationSettingGroups: { label: string, keys: (keyof UserNotificationSetting)[] }[] = []
19 emailNotifications: { [ id in keyof UserNotificationSetting ]?: boolean } = {}
20 webNotifications: { [ id in keyof UserNotificationSetting ]?: boolean } = {}
21 labelNotifications: { [ id in keyof UserNotificationSetting ]?: string } = {}
22 rightNotifications: { [ id in keyof Partial<UserNotificationSetting> ]?: UserRight } = {}
25 private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500)
28 private userNotificationService: UserNotificationService,
29 private serverService: ServerService,
30 private notifier: Notifier
32 this.labelNotifications = {
33 newVideoFromSubscription: $localize`New video from your subscriptions`,
34 newCommentOnMyVideo: $localize`New comment on your video`,
35 abuseAsModerator: $localize`New abuse`,
36 videoAutoBlacklistAsModerator: $localize`An automatically blocked video is awaiting review`,
37 blacklistOnMyVideo: $localize`One of your video is blocked/unblocked`,
38 myVideoPublished: $localize`Video published (after transcoding/scheduled update)`,
39 myVideoImportFinished: $localize`Video import finished`,
40 newUserRegistration: $localize`A new user registered on your instance`,
41 newFollow: $localize`You or one of your channels has a new follower`,
42 commentMention: $localize`Someone mentioned you in video comments`,
43 newInstanceFollower: $localize`Your instance has a new follower`,
44 autoInstanceFollowing: $localize`Your instance automatically followed another instance`,
45 abuseNewMessage: $localize`An abuse report received a new message`,
46 abuseStateChange: $localize`One of your abuse reports has been accepted or rejected by moderators`,
47 newPeerTubeVersion: $localize`A new PeerTube version is available`,
48 newPluginVersion: $localize`One of your plugin/theme has a new available version`,
49 myVideoStudioEditionFinished: $localize`Video studio edition has finished`
51 this.notificationSettingGroups = [
53 label: $localize`Social`,
55 'newVideoFromSubscription',
62 label: $localize`Your videos`,
64 'newCommentOnMyVideo',
67 'myVideoImportFinished',
68 'myVideoStudioEditionFinished'
73 label: $localize`Moderation`,
78 'videoAutoBlacklistAsModerator'
83 label: $localize`Administration`,
85 'newUserRegistration',
86 'newInstanceFollower',
87 'autoInstanceFollowing',
94 this.rightNotifications = {
95 abuseAsModerator: UserRight.MANAGE_ABUSES,
96 videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST,
97 newUserRegistration: UserRight.MANAGE_USERS,
98 newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW,
99 autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION,
100 newPeerTubeVersion: UserRight.MANAGE_DEBUG,
101 newPluginVersion: UserRight.MANAGE_DEBUG
106 const serverConfig = this.serverService.getHTMLConfig()
107 this.emailEnabled = serverConfig.email.enabled
109 this.userInformationLoaded.subscribe(() => this.loadNotificationSettings())
112 hasUserRight (field: keyof UserNotificationSetting) {
113 const rightToHave = this.rightNotifications[field]
114 if (!rightToHave) return true // No rights needed
116 return this.user.hasRight(rightToHave)
119 updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) {
120 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL
121 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL
123 this.savePreferences()
126 updateWebSetting (field: keyof UserNotificationSetting, value: boolean) {
127 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB
128 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB
130 this.savePreferences()
133 private savePreferencesImpl () {
134 this.userNotificationService.updateNotificationSettings(this.user.notificationSettings)
137 this.notifier.success($localize`Preferences saved`, undefined, 2000)
140 error: err => this.notifier.error(err.message)
144 private loadNotificationSettings () {
145 for (const key of objectKeysTyped(this.user.notificationSettings)) {
146 const value = this.user.notificationSettings[key]
147 this.emailNotifications[key] = !!(value & UserNotificationSettingValue.EMAIL)
149 this.webNotifications[key] = !!(value & UserNotificationSettingValue.WEB)