]> 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
Remove old migration files
[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 { UserNotificationSetting, UserNotificationSettingValue, UserRight } from '@shared/models'
7
8 @Component({
9 selector: 'my-account-notification-preferences',
10 templateUrl: './my-account-notification-preferences.component.html',
11 styleUrls: [ './my-account-notification-preferences.component.scss' ]
12 })
13 export class MyAccountNotificationPreferencesComponent implements OnInit {
14 @Input() user: User
15 @Input() userInformationLoaded: Subject<any>
16
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 } = {}
22 emailEnabled = false
23
24 private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500)
25
26 constructor (
27 private userNotificationService: UserNotificationService,
28 private serverService: ServerService,
29 private notifier: Notifier
30 ) {
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`
48 }
49 this.notificationSettingGroups = [
50 {
51 label: $localize`Social`,
52 keys: [
53 'newVideoFromSubscription',
54 'newFollow',
55 'commentMention'
56 ]
57 },
58
59 {
60 label: $localize`Your videos`,
61 keys: [
62 'newCommentOnMyVideo',
63 'blacklistOnMyVideo',
64 'myVideoPublished',
65 'myVideoImportFinished'
66 ]
67 },
68
69 {
70 label: $localize`Moderation`,
71 keys: [
72 'abuseStateChange',
73 'abuseNewMessage',
74 'abuseAsModerator',
75 'videoAutoBlacklistAsModerator'
76 ]
77 },
78
79 {
80 label: $localize`Administration`,
81 keys: [
82 'newUserRegistration',
83 'newInstanceFollower',
84 'autoInstanceFollowing',
85 'newPeerTubeVersion',
86 'newPluginVersion'
87 ]
88 }
89 ]
90
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
99 }
100 }
101
102 ngOnInit () {
103 const serverConfig = this.serverService.getHTMLConfig()
104 this.emailEnabled = serverConfig.email.enabled
105
106 this.userInformationLoaded.subscribe(() => this.loadNotificationSettings())
107 }
108
109 hasUserRight (field: keyof UserNotificationSetting) {
110 const rightToHave = this.rightNotifications[field]
111 if (!rightToHave) return true // No rights needed
112
113 return this.user.hasRight(rightToHave)
114 }
115
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
119
120 this.savePreferences()
121 }
122
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
126
127 this.savePreferences()
128 }
129
130 private savePreferencesImpl () {
131 this.userNotificationService.updateNotificationSettings(this.user.notificationSettings)
132 .subscribe({
133 next: () => {
134 this.notifier.success($localize`Preferences saved`, undefined, 2000)
135 },
136
137 error: err => this.notifier.error(err.message)
138 })
139 }
140
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
145
146 this.webNotifications[key] = value & UserNotificationSettingValue.WEB
147 }
148 }
149 }