]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-settings/my-account-notification-preferences/my-account-notification-preferences.component.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-notification-preferences / my-account-notification-preferences.component.ts
CommitLineData
67ed6552
C
1import { debounce } from 'lodash-es'
2import { Subject } from 'rxjs'
2f1548fd 3import { Component, Input, OnInit } from '@angular/core'
67ed6552
C
4import { Notifier, ServerService, User } from '@app/core'
5import { UserNotificationService } from '@app/shared/shared-main'
f9735a9e 6import { objectKeysTyped } from '@shared/core-utils'
67ed6552 7import { UserNotificationSetting, UserNotificationSettingValue, UserRight } from '@shared/models'
2f1548fd
C
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})
14export class MyAccountNotificationPreferencesComponent implements OnInit {
dc4e0ddb 15 @Input() user: User
2f1548fd
C
16 @Input() userInformationLoaded: Subject<any>
17
dc4e0ddb
C
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 } = {}
ba430d75 23 emailEnabled = false
2f1548fd
C
24
25 private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500)
26
27 constructor (
2f1548fd
C
28 private userNotificationService: UserNotificationService,
29 private serverService: ServerService,
30 private notifier: Notifier
31 ) {
32 this.labelNotifications = {
66357162
C
33 newVideoFromSubscription: $localize`New video from your subscriptions`,
34 newCommentOnMyVideo: $localize`New comment on your video`,
35 abuseAsModerator: $localize`New abuse`,
dc4e0ddb 36 videoAutoBlacklistAsModerator: $localize`An automatically blocked video is awaiting review`,
66357162
C
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`,
eaa52952 41 newFollow: $localize`You or one of your channels has a new follower`,
66357162
C
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`,
32a18cbf
C
46 abuseStateChange: $localize`One of your abuse reports has been accepted or rejected by moderators`,
47 newPeerTubeVersion: $localize`A new PeerTube version is available`,
1808a1f8 48 newPluginVersion: $localize`One of your plugin/theme has a new available version`,
92e66e04 49 myVideoStudioEditionFinished: $localize`Video studio edition has finished`
2f1548fd 50 }
dc4e0ddb
C
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',
1808a1f8 67 'myVideoImportFinished',
92e66e04 68 'myVideoStudioEditionFinished'
dc4e0ddb
C
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 ]
2f1548fd
C
93
94 this.rightNotifications = {
4f32032f 95 abuseAsModerator: UserRight.MANAGE_ABUSES,
3487330d 96 videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST,
846751c9 97 newUserRegistration: UserRight.MANAGE_USERS,
e1b49ee5 98 newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW,
32a18cbf
C
99 autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION,
100 newPeerTubeVersion: UserRight.MANAGE_DEBUG,
101 newPluginVersion: UserRight.MANAGE_DEBUG
2f1548fd 102 }
2f1548fd
C
103 }
104
105 ngOnInit () {
2989628b
C
106 const serverConfig = this.serverService.getHTMLConfig()
107 this.emailEnabled = serverConfig.email.enabled
ba430d75 108
2f1548fd
C
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 () {
a5cf76af 134 this.userNotificationService.updateNotificationSettings(this.user.notificationSettings)
1378c0d3
C
135 .subscribe({
136 next: () => {
66357162 137 this.notifier.success($localize`Preferences saved`, undefined, 2000)
2f1548fd
C
138 },
139
1378c0d3
C
140 error: err => this.notifier.error(err.message)
141 })
2f1548fd
C
142 }
143
144 private loadNotificationSettings () {
f9735a9e 145 for (const key of objectKeysTyped(this.user.notificationSettings)) {
2f1548fd 146 const value = this.user.notificationSettings[key]
54909304 147 this.emailNotifications[key] = !!(value & UserNotificationSettingValue.EMAIL)
2f1548fd 148
54909304 149 this.webNotifications[key] = !!(value & UserNotificationSettingValue.WEB)
2f1548fd
C
150 }
151 }
152}