]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/users/user-notification.service.ts
ecc66ecdbb8221c3db89f513640ff5c1a6b23bbe
[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, UserNotificationSocket } 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 restExtractor: RestExtractor,
18 private restService: RestService,
19 private userNotificationSocket: UserNotificationSocket
20 ) {}
21
22 listMyNotifications (parameters: {
23 pagination: ComponentPaginationLight
24 ignoreLoadingBar?: boolean
25 unread?: boolean,
26 sort?: SortMeta
27 }) {
28 const { pagination, ignoreLoadingBar, unread, sort } = parameters
29
30 let params = new HttpParams()
31 params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination), sort)
32
33 if (unread) params = params.append('unread', `${unread}`)
34
35 const headers = ignoreLoadingBar ? { ignoreLoadingBar: '' } : undefined
36
37 return this.authHttp.get<ResultList<UserNotification>>(UserNotificationService.BASE_NOTIFICATIONS_URL, { params, headers })
38 .pipe(
39 map(res => this.restExtractor.convertResultListDateToHuman(res)),
40 map(res => this.restExtractor.applyToResultListData(res, this.formatNotification.bind(this))),
41 catchError(err => this.restExtractor.handleError(err))
42 )
43 }
44
45 countUnreadNotifications () {
46 return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true })
47 .pipe(map(n => n.total))
48 }
49
50 markAsRead (notification: UserNotification) {
51 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
52
53 const body = { ids: [ notification.id ] }
54 const headers = { ignoreLoadingBar: '' }
55
56 return this.authHttp.post(url, body, { headers })
57 .pipe(
58 map(this.restExtractor.extractDataBool),
59 tap(() => this.userNotificationSocket.dispatch('read')),
60 catchError(res => this.restExtractor.handleError(res))
61 )
62 }
63
64 markAllAsRead () {
65 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all'
66 const headers = { ignoreLoadingBar: '' }
67
68 return this.authHttp.post(url, {}, { headers })
69 .pipe(
70 map(this.restExtractor.extractDataBool),
71 tap(() => this.userNotificationSocket.dispatch('read-all')),
72 catchError(res => this.restExtractor.handleError(res))
73 )
74 }
75
76 updateNotificationSettings (user: User, settings: UserNotificationSetting) {
77 const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS
78
79 return this.authHttp.put(url, settings)
80 .pipe(
81 map(this.restExtractor.extractDataBool),
82 catchError(res => this.restExtractor.handleError(res))
83 )
84 }
85
86 private formatNotification (notification: UserNotificationServer) {
87 return new UserNotification(notification)
88 }
89 }