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