]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/users/user-notification.service.ts
Add ability to delete history element
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / users / user-notification.service.ts
CommitLineData
9df52d66 1import { SortMeta } from 'primeng/api'
2f1548fd 2import { catchError, map, tap } from 'rxjs/operators'
9744bb2a 3import { HttpClient, HttpContext, HttpParams } from '@angular/common/http'
67ed6552 4import { Injectable } from '@angular/core'
9df52d66 5import { AuthService, ComponentPaginationLight, PeerTubeSocket, RestExtractor, RestService } from '@app/core'
9744bb2a 6import { NGX_LOADING_BAR_IGNORED } from '@ngx-loading-bar/http-client'
67ed6552
C
7import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '@shared/models'
8import { environment } from '../../../../environments/environment'
9a39392a 9import { UserNotification } from './user-notification.model'
2f1548fd
C
10
11@Injectable()
12export 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
2f1548fd 16 constructor (
2f1548fd 17 private authHttp: HttpClient,
d573926e 18 private auth: AuthService,
2f1548fd 19 private restExtractor: RestExtractor,
9a39392a 20 private restService: RestService,
a5cf76af 21 private peertubeSocket: PeerTubeSocket
2f1548fd
C
22 ) {}
23
654a188f
RK
24 listMyNotifications (parameters: {
25 pagination: ComponentPaginationLight
26 ignoreLoadingBar?: boolean
9df52d66 27 unread?: boolean
654a188f
RK
28 sort?: SortMeta
29 }) {
30 const { pagination, ignoreLoadingBar, unread, sort } = parameters
31
2f1548fd 32 let params = new HttpParams()
4beda9e1 33 params = this.restService.addRestGetParams(params, this.restService.componentToRestPagination(pagination), sort)
2f1548fd
C
34
35 if (unread) params = params.append('unread', `${unread}`)
36
9744bb2a
C
37 const context = ignoreLoadingBar
38 ? new HttpContext().set(NGX_LOADING_BAR_IGNORED, true)
39 : undefined
2f1548fd 40
9744bb2a 41 return this.authHttp.get<ResultList<UserNotification>>(UserNotificationService.BASE_NOTIFICATIONS_URL, { params, context })
2f1548fd
C
42 .pipe(
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))
46 )
47 }
48
49 countUnreadNotifications () {
654a188f 50 return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true })
2f1548fd
C
51 .pipe(map(n => n.total))
52 }
53
2f1548fd
C
54 markAsRead (notification: UserNotification) {
55 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
56
57 const body = { ids: [ notification.id ] }
9744bb2a 58 const context = new HttpContext().set(NGX_LOADING_BAR_IGNORED, true)
2f1548fd 59
9744bb2a 60 return this.authHttp.post(url, body, { context })
2f1548fd
C
61 .pipe(
62 map(this.restExtractor.extractDataBool),
a5cf76af 63 tap(() => this.peertubeSocket.dispatchNotificationEvent('read')),
2f1548fd
C
64 catchError(res => this.restExtractor.handleError(res))
65 )
66 }
67
68 markAllAsRead () {
69 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all'
9744bb2a 70 const context = new HttpContext().set(NGX_LOADING_BAR_IGNORED, true)
2f1548fd 71
9744bb2a 72 return this.authHttp.post(url, {}, { context })
2f1548fd
C
73 .pipe(
74 map(this.restExtractor.extractDataBool),
a5cf76af 75 tap(() => this.peertubeSocket.dispatchNotificationEvent('read-all')),
2f1548fd
C
76 catchError(res => this.restExtractor.handleError(res))
77 )
78 }
79
a5cf76af 80 updateNotificationSettings (settings: UserNotificationSetting) {
2f1548fd
C
81 const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS
82
83 return this.authHttp.put(url, settings)
84 .pipe(
85 map(this.restExtractor.extractDataBool),
86 catchError(res => this.restExtractor.handleError(res))
87 )
88 }
89
2f1548fd 90 private formatNotification (notification: UserNotificationServer) {
d573926e 91 return new UserNotification(notification, this.auth.getUser())
2f1548fd
C
92 }
93}