1 import { SortMeta } from 'primeng/api'
2 import { catchError, map, tap } from 'rxjs/operators'
3 import { HttpClient, HttpParams } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { AuthService, ComponentPaginationLight, PeerTubeSocket, RestExtractor, RestService } from '@app/core'
6 import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '@shared/models'
7 import { environment } from '../../../../environments/environment'
8 import { UserNotification } from './user-notification.model'
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'
16 private authHttp: HttpClient,
17 private auth: AuthService,
18 private restExtractor: RestExtractor,
19 private restService: RestService,
20 private peertubeSocket: PeerTubeSocket
23 listMyNotifications (parameters: {
24 pagination: ComponentPaginationLight
25 ignoreLoadingBar?: boolean
29 const { pagination, ignoreLoadingBar, unread, sort } = parameters
31 let params = new HttpParams()
32 params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination), sort)
34 if (unread) params = params.append('unread', `${unread}`)
36 const headers = ignoreLoadingBar ? { ignoreLoadingBar: '' } : undefined
38 return this.authHttp.get<ResultList<UserNotification>>(UserNotificationService.BASE_NOTIFICATIONS_URL, { params, headers })
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))
46 countUnreadNotifications () {
47 return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true })
48 .pipe(map(n => n.total))
51 markAsRead (notification: UserNotification) {
52 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
54 const body = { ids: [ notification.id ] }
55 const headers = { ignoreLoadingBar: '' }
57 return this.authHttp.post(url, body, { headers })
59 map(this.restExtractor.extractDataBool),
60 tap(() => this.peertubeSocket.dispatchNotificationEvent('read')),
61 catchError(res => this.restExtractor.handleError(res))
66 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all'
67 const headers = { ignoreLoadingBar: '' }
69 return this.authHttp.post(url, {}, { headers })
71 map(this.restExtractor.extractDataBool),
72 tap(() => this.peertubeSocket.dispatchNotificationEvent('read-all')),
73 catchError(res => this.restExtractor.handleError(res))
77 updateNotificationSettings (settings: UserNotificationSetting) {
78 const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS
80 return this.authHttp.put(url, settings)
82 map(this.restExtractor.extractDataBool),
83 catchError(res => this.restExtractor.handleError(res))
87 private formatNotification (notification: UserNotificationServer) {
88 return new UserNotification(notification, this.auth.getUser())