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'
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'
17 private authHttp: HttpClient,
18 private auth: AuthService,
19 private restExtractor: RestExtractor,
20 private restService: RestService,
21 private peertubeSocket: PeerTubeSocket
24 listMyNotifications (parameters: {
25 pagination: ComponentPaginationLight
26 ignoreLoadingBar?: boolean
30 const { pagination, ignoreLoadingBar, unread, sort } = parameters
32 let params = new HttpParams()
33 params = this.restService.addRestGetParams(params, this.restService.componentToRestPagination(pagination), sort)
35 if (unread) params = params.append('unread', `${unread}`)
37 const context = ignoreLoadingBar
38 ? new HttpContext().set(NGX_LOADING_BAR_IGNORED, true)
41 return this.authHttp.get<ResultList<UserNotification>>(UserNotificationService.BASE_NOTIFICATIONS_URL, { params, context })
43 map(res => this.restExtractor.convertResultListDateToHuman(res)),
44 map(res => this.restExtractor.applyToResultListData(res, this.formatNotification.bind(this))),
45 catchError(err => this.restExtractor.handleError(err))
49 countUnreadNotifications () {
50 return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true })
51 .pipe(map(n => n.total))
54 markAsRead (notification: UserNotification) {
55 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
57 const body = { ids: [ notification.id ] }
58 const context = new HttpContext().set(NGX_LOADING_BAR_IGNORED, true)
60 return this.authHttp.post(url, body, { context })
62 tap(() => this.peertubeSocket.dispatchNotificationEvent('read')),
63 catchError(res => this.restExtractor.handleError(res))
68 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all'
69 const context = new HttpContext().set(NGX_LOADING_BAR_IGNORED, true)
71 return this.authHttp.post(url, {}, { context })
73 tap(() => this.peertubeSocket.dispatchNotificationEvent('read-all')),
74 catchError(res => this.restExtractor.handleError(res))
78 updateNotificationSettings (settings: UserNotificationSetting) {
79 const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS
81 return this.authHttp.put(url, settings)
82 .pipe(catchError(res => this.restExtractor.handleError(res)))
85 private formatNotification (notification: UserNotificationServer) {
86 return new UserNotification(notification, this.auth.getUser())