]> 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
Optimize HLS/WebTorrent videos only
[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'
67ed6552 6import { UserNotificationSetting, UserNotificationSettingValue, UserRight } from '@shared/models'
2f1548fd
C
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})
13export class MyAccountNotificationPreferencesComponent implements OnInit {
14 @Input() user: User = null
15 @Input() userInformationLoaded: Subject<any>
16
17 notificationSettingKeys: (keyof UserNotificationSetting)[] = []
18 emailNotifications: { [ id in keyof UserNotificationSetting ]: boolean } = {} as any
19 webNotifications: { [ id in keyof UserNotificationSetting ]: boolean } = {} as any
20 labelNotifications: { [ id in keyof UserNotificationSetting ]: string } = {} as any
21 rightNotifications: { [ id in keyof Partial<UserNotificationSetting> ]: UserRight } = {} as any
ba430d75 22 emailEnabled = false
2f1548fd
C
23
24 private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500)
25
26 constructor (
2f1548fd
C
27 private userNotificationService: UserNotificationService,
28 private serverService: ServerService,
29 private notifier: Notifier
30 ) {
31 this.labelNotifications = {
66357162
C
32 newVideoFromSubscription: $localize`New video from your subscriptions`,
33 newCommentOnMyVideo: $localize`New comment on your video`,
34 abuseAsModerator: $localize`New abuse`,
35 videoAutoBlacklistAsModerator: $localize`Video blocked automatically waiting 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`,
32a18cbf
C
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`
2f1548fd
C
48 }
49 this.notificationSettingKeys = Object.keys(this.labelNotifications) as (keyof UserNotificationSetting)[]
50
51 this.rightNotifications = {
4f32032f 52 abuseAsModerator: UserRight.MANAGE_ABUSES,
3487330d 53 videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST,
846751c9 54 newUserRegistration: UserRight.MANAGE_USERS,
e1b49ee5 55 newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW,
32a18cbf
C
56 autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION,
57 newPeerTubeVersion: UserRight.MANAGE_DEBUG,
58 newPluginVersion: UserRight.MANAGE_DEBUG
2f1548fd 59 }
2f1548fd
C
60 }
61
62 ngOnInit () {
2989628b
C
63 const serverConfig = this.serverService.getHTMLConfig()
64 this.emailEnabled = serverConfig.email.enabled
ba430d75 65
2f1548fd
C
66 this.userInformationLoaded.subscribe(() => this.loadNotificationSettings())
67 }
68
69 hasUserRight (field: keyof UserNotificationSetting) {
70 const rightToHave = this.rightNotifications[field]
71 if (!rightToHave) return true // No rights needed
72
73 return this.user.hasRight(rightToHave)
74 }
75
76 updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) {
77 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL
78 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL
79
80 this.savePreferences()
81 }
82
83 updateWebSetting (field: keyof UserNotificationSetting, value: boolean) {
84 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB
85 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB
86
87 this.savePreferences()
88 }
89
90 private savePreferencesImpl () {
a5cf76af 91 this.userNotificationService.updateNotificationSettings(this.user.notificationSettings)
1378c0d3
C
92 .subscribe({
93 next: () => {
66357162 94 this.notifier.success($localize`Preferences saved`, undefined, 2000)
2f1548fd
C
95 },
96
1378c0d3
C
97 error: err => this.notifier.error(err.message)
98 })
2f1548fd
C
99 }
100
101 private loadNotificationSettings () {
102 for (const key of Object.keys(this.user.notificationSettings)) {
103 const value = this.user.notificationSettings[key]
104 this.emailNotifications[key] = value & UserNotificationSettingValue.EMAIL
105
106 this.webNotifications[key] = value & UserNotificationSettingValue.WEB
107 }
108 }
109}