]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/users/user-notification.service.ts
Add watch messages if live has not started
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / users / user-notification.service.ts
1 import { catchError, map, tap } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { ComponentPaginationLight, RestExtractor, RestService, User, PeerTubeSocket, AuthService } from '@app/core'
5 import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '@shared/models'
6 import { environment } from '../../../../environments/environment'
7 import { UserNotification } from './user-notification.model'
8 import { SortMeta } from 'primeng/api'
9
10 @Injectable()
11 export class UserNotificationService {
12 static BASE_NOTIFICATIONS_URL = environment.apiUrl + '/api/v1/users/me/notifications'
13 static BASE_NOTIFICATION_SETTINGS = environment.apiUrl + '/api/v1/users/me/notification-settings'
14
15 constructor (
16 private authHttp: HttpClient,
17 private auth: AuthService,
18 private restExtractor: RestExtractor,
19 private restService: RestService,
20 private peertubeSocket: PeerTubeSocket
21 ) {}
22
23 listMyNotifications (parameters: {
24 pagination: ComponentPaginationLight
25 ignoreLoadingBar?: boolean
26 unread?: boolean,
27 sort?: SortMeta
28 }) {
29 const { pagination, ignoreLoadingBar, unread, sort } = parameters
30
31 let params = new HttpParams()
32 params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination), sort)
33
34 if (unread) params = params.append('unread', `${unread}`)
35
36 const headers = ignoreLoadingBar ? { ignoreLoadingBar: '' } : undefined
37
38 return this.authHttp.get<ResultList<UserNotification>>(UserNotificationService.BASE_NOTIFICATIONS_URL, { params, headers })
39 .pipe(
40 map(res => this.restExtractor.convertResultListDateToHuman(res)),
41 map(res => this.restExtractor.applyToResultListData(res, this.formatNotification.bind(this))),
42 catchError(err => this.restExtractor.handleError(err))
43 )
44 }
45
46 countUnreadNotifications () {
47 return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true })
48 .pipe(map(n => n.total))
49 }
50
51 markAsRead (notification: UserNotification) {
52 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
53
54 const body = { ids: [ notification.id ] }
55 const headers = { ignoreLoadingBar: '' }
56
57 return this.authHttp.post(url, body, { headers })
58 .pipe(
59 map(this.restExtractor.extractDataBool),
60 tap(() => this.peertubeSocket.dispatchNotificationEvent('read')),
61 catchError(res => this.restExtractor.handleError(res))
62 )
63 }
64
65 markAllAsRead () {
66 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all'
67 const headers = { ignoreLoadingBar: '' }
68
69 return this.authHttp.post(url, {}, { headers })
70 .pipe(
71 map(this.restExtractor.extractDataBool),
72 tap(() => this.peertubeSocket.dispatchNotificationEvent('read-all')),
73 catchError(res => this.restExtractor.handleError(res))
74 )
75 }
76
77 updateNotificationSettings (settings: UserNotificationSetting) {
78 const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS
79
80 return this.authHttp.put(url, settings)
81 .pipe(
82 map(this.restExtractor.extractDataBool),
83 catchError(res => this.restExtractor.handleError(res))
84 )
85 }
86
87 private formatNotification (notification: UserNotificationServer) {
88 return new UserNotification(notification, this.auth.getUser())
89 }
90 }