]> 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
Add notification on new instance follower (server side)
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-notification-preferences / my-account-notification-preferences.component.ts
CommitLineData
2f1548fd
C
1import { Component, Input, OnInit } from '@angular/core'
2import { User } from '@app/shared'
3import { I18n } from '@ngx-translate/i18n-polyfill'
4import { Subject } from 'rxjs'
5import { UserNotificationSetting, UserNotificationSettingValue, UserRight } from '../../../../../../shared'
6import { Notifier, ServerService } from '@app/core'
7import { debounce } from 'lodash-es'
8import { UserNotificationService } from '@app/shared/users/user-notification.service'
9
10@Component({
11 selector: 'my-account-notification-preferences',
12 templateUrl: './my-account-notification-preferences.component.html',
13 styleUrls: [ './my-account-notification-preferences.component.scss' ]
14})
15export class MyAccountNotificationPreferencesComponent implements OnInit {
16 @Input() user: User = null
17 @Input() userInformationLoaded: Subject<any>
18
19 notificationSettingKeys: (keyof UserNotificationSetting)[] = []
20 emailNotifications: { [ id in keyof UserNotificationSetting ]: boolean } = {} as any
21 webNotifications: { [ id in keyof UserNotificationSetting ]: boolean } = {} as any
22 labelNotifications: { [ id in keyof UserNotificationSetting ]: string } = {} as any
23 rightNotifications: { [ id in keyof Partial<UserNotificationSetting> ]: UserRight } = {} as any
24 emailEnabled: boolean
25
26 private savePreferences = debounce(this.savePreferencesImpl.bind(this), 500)
27
28 constructor (
29 private i18n: I18n,
30 private userNotificationService: UserNotificationService,
31 private serverService: ServerService,
32 private notifier: Notifier
33 ) {
7ccddd7b 34
2f1548fd
C
35 this.labelNotifications = {
36 newVideoFromSubscription: this.i18n('New video from your subscriptions'),
37 newCommentOnMyVideo: this.i18n('New comment on your video'),
1e17071b 38 videoAbuseAsModerator: this.i18n('New video abuse'),
7ccddd7b 39 videoAutoBlacklistAsModerator: this.i18n('Video auto-blacklisted waiting review'),
2f1548fd
C
40 blacklistOnMyVideo: this.i18n('One of your video is blacklisted/unblacklisted'),
41 myVideoPublished: this.i18n('Video published (after transcoding/scheduled update)'),
42 myVideoImportFinished: this.i18n('Video import finished'),
43 newUserRegistration: this.i18n('A new user registered on your instance'),
44 newFollow: this.i18n('You or your channel(s) has a new follower'),
45 commentMention: this.i18n('Someone mentioned you in video comments')
46 }
47 this.notificationSettingKeys = Object.keys(this.labelNotifications) as (keyof UserNotificationSetting)[]
48
49 this.rightNotifications = {
50 videoAbuseAsModerator: UserRight.MANAGE_VIDEO_ABUSES,
7ccddd7b 51 videoAutoBlacklistAsModerator: UserRight.MANAGE_VIDEO_BLACKLIST,
2f1548fd
C
52 newUserRegistration: UserRight.MANAGE_USERS
53 }
54
55 this.emailEnabled = this.serverService.getConfig().email.enabled
56 }
57
58 ngOnInit () {
59 this.userInformationLoaded.subscribe(() => this.loadNotificationSettings())
60 }
61
62 hasUserRight (field: keyof UserNotificationSetting) {
63 const rightToHave = this.rightNotifications[field]
64 if (!rightToHave) return true // No rights needed
65
66 return this.user.hasRight(rightToHave)
67 }
68
69 updateEmailSetting (field: keyof UserNotificationSetting, value: boolean) {
70 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.EMAIL
71 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.EMAIL
72
73 this.savePreferences()
74 }
75
76 updateWebSetting (field: keyof UserNotificationSetting, value: boolean) {
77 if (value === true) this.user.notificationSettings[field] |= UserNotificationSettingValue.WEB
78 else this.user.notificationSettings[field] &= ~UserNotificationSettingValue.WEB
79
80 this.savePreferences()
81 }
82
83 private savePreferencesImpl () {
84 this.userNotificationService.updateNotificationSettings(this.user, this.user.notificationSettings)
85 .subscribe(
86 () => {
87 this.notifier.success(this.i18n('Preferences saved'), undefined, 2000)
88 },
89
90 err => this.notifier.error(err.message)
91 )
92 }
93
94 private loadNotificationSettings () {
95 for (const key of Object.keys(this.user.notificationSettings)) {
96 const value = this.user.notificationSettings[key]
97 this.emailNotifications[key] = value & UserNotificationSettingValue.EMAIL
98
99 this.webNotifications[key] = value & UserNotificationSettingValue.WEB
100 }
101 }
102}