]> 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
Migrate to $localize
[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`,
45 abuseStateChange: $localize`One of your abuse reports has been accepted or rejected by moderators`
2f1548fd
C
46 }
47 this.notificationSettingKeys = Object.keys(this.labelNotifications) as (keyof UserNotificationSetting)[]
48
49 this.rightNotifications = {
4f32032f 50 abuseAsModerator: UserRight.MANAGE_ABUSES,
3487330d 51 videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST,
846751c9 52 newUserRegistration: UserRight.MANAGE_USERS,
e1b49ee5
C
53 newInstanceFollower: UserRight.MANAGE_SERVER_FOLLOW,
54 autoInstanceFollowing: UserRight.MANAGE_CONFIGURATION
2f1548fd 55 }
2f1548fd
C
56 }
57
58 ngOnInit () {
ba430d75
C
59 this.serverService.getConfig()
60 .subscribe(config => {
61 this.emailEnabled = config.email.enabled
62 })
63
2f1548fd
C
64 this.userInformationLoaded.subscribe(() => this.loadNotificationSettings())
65 }
66
67 hasUserRight (field: keyof UserNotificationSetting) {
68 const rightToHave = this.rightNotifications[field]
69 if (!rightToHave) return true // No rights needed
70
71 return this.user.hasRight(rightToHave)
72 }
73
74 updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) {
75 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL
76 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL
77
78 this.savePreferences()
79 }
80
81 updateWebSetting (field: keyof UserNotificationSetting, value: boolean) {
82 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB
83 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB
84
85 this.savePreferences()
86 }
87
88 private savePreferencesImpl () {
89 this.userNotificationService.updateNotificationSettings(this.user, this.user.notificationSettings)
90 .subscribe(
91 () => {
66357162 92 this.notifier.success($localize`Preferences saved`, undefined, 2000)
2f1548fd
C
93 },
94
95 err => this.notifier.error(err.message)
96 )
97 }
98
99 private loadNotificationSettings () {
100 for (const key of Object.keys(this.user.notificationSettings)) {
101 const value = this.user.notificationSettings[key]
102 this.emailNotifications[key] = value & UserNotificationSettingValue.EMAIL
103
104 this.webNotifications[key] = value & UserNotificationSettingValue.WEB
105 }
106 }
107}