]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-account/my-account-settings/my-account-notification-preferences/my-account-notification-preferences.component.ts
2adc276a91f3f8219a3c946a6b7d394b219e9b03
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-notification-preferences / my-account-notification-preferences.component.ts
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'
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
16 @Input() userInformationLoaded: Subject<any>
17
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 } = {}
23 emailEnabled = false
24
25 private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500)
26
27 constructor (
28 private userNotificationService: UserNotificationService,
29 private serverService: ServerService,
30 private notifier: Notifier
31 ) {
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`
50 }
51 this.notificationSettingGroups = [
52 {
53 label: $localize`Social`,
54 keys: [
55 'newVideoFromSubscription',
56 'newFollow',
57 'commentMention'
58 ]
59 },
60
61 {
62 label: $localize`Your videos`,
63 keys: [
64 'newCommentOnMyVideo',
65 'blacklistOnMyVideo',
66 'myVideoPublished',
67 'myVideoImportFinished',
68 'myVideoStudioEditionFinished'
69 ]
70 },
71
72 {
73 label: $localize`Moderation`,
74 keys: [
75 'abuseStateChange',
76 'abuseNewMessage',
77 'abuseAsModerator',
78 'videoAutoBlacklistAsModerator'
79 ]
80 },
81
82 {
83 label: $localize`Administration`,
84 keys: [
85 'newUserRegistration',
86 'newInstanceFollower',
87 'autoInstanceFollowing',
88 'newPeerTubeVersion',
89 'newPluginVersion'
90 ]
91 }
92 ]
93
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
102 }
103 }
104
105 ngOnInit () {
106 const serverConfig = this.serverService.getHTMLConfig()
107 this.emailEnabled = serverConfig.email.enabled
108
109 this.userInformationLoaded.subscribe(() => this.loadNotificationSettings())
110 }
111
112 hasUserRight (field: keyof UserNotificationSetting) {
113 const rightToHave = this.rightNotifications[field]
114 if (!rightToHave) return true // No rights needed
115
116 return this.user.hasRight(rightToHave)
117 }
118
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
122
123 this.savePreferences()
124 }
125
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
129
130 this.savePreferences()
131 }
132
133 private savePreferencesImpl () {
134 this.userNotificationService.updateNotificationSettings(this.user.notificationSettings)
135 .subscribe({
136 next: () => {
137 this.notifier.success($localize`Preferences saved`, undefined, 2000)
138 },
139
140 error: err => this.notifier.error(err.message)
141 })
142 }
143
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)
148
149 this.webNotifications[key] = !!(value & UserNotificationSettingValue.WEB)
150 }
151 }
152 }