]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/users/user-notification.service.ts
Merge branch 'release/4.2.0' into develop
[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 61 .pipe(
a5cf76af 62 tap(() => this.peertubeSocket.dispatchNotificationEvent('read')),
2f1548fd
C
63 catchError(res => this.restExtractor.handleError(res))
64 )
65 }
66
67 markAllAsRead () {
68 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all'
9744bb2a 69 const context = new HttpContext().set(NGX_LOADING_BAR_IGNORED, true)
2f1548fd 70
9744bb2a 71 return this.authHttp.post(url, {}, { context })
2f1548fd 72 .pipe(
a5cf76af 73 tap(() => this.peertubeSocket.dispatchNotificationEvent('read-all')),
2f1548fd
C
74 catchError(res => this.restExtractor.handleError(res))
75 )
76 }
77
a5cf76af 78 updateNotificationSettings (settings: UserNotificationSetting) {
2f1548fd
C
79 const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS
80
81 return this.authHttp.put(url, settings)
e8bffe96 82 .pipe(catchError(res => this.restExtractor.handleError(res)))
2f1548fd
C
83 }
84
2f1548fd 85 private formatNotification (notification: UserNotificationServer) {
d573926e 86 return new UserNotification(notification, this.auth.getUser())
2f1548fd
C
87 }
88}