]>
Commit | Line | Data |
---|---|---|
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.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 () { | |
50 | return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true }) | |
51 | .pipe(map(n => n.total)) | |
52 | } | |
53 | ||
54 | markAsRead (notification: UserNotification) { | |
55 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read' | |
56 | ||
57 | const body = { ids: [ notification.id ] } | |
58 | const context = new HttpContext().set(NGX_LOADING_BAR_IGNORED, true) | |
59 | ||
60 | return this.authHttp.post(url, body, { context }) | |
61 | .pipe( | |
62 | tap(() => this.peertubeSocket.dispatchNotificationEvent('read')), | |
63 | catchError(res => this.restExtractor.handleError(res)) | |
64 | ) | |
65 | } | |
66 | ||
67 | markAllAsRead () { | |
68 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all' | |
69 | const context = new HttpContext().set(NGX_LOADING_BAR_IGNORED, true) | |
70 | ||
71 | return this.authHttp.post(url, {}, { context }) | |
72 | .pipe( | |
73 | tap(() => this.peertubeSocket.dispatchNotificationEvent('read-all')), | |
74 | catchError(res => this.restExtractor.handleError(res)) | |
75 | ) | |
76 | } | |
77 | ||
78 | updateNotificationSettings (settings: UserNotificationSetting) { | |
79 | const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS | |
80 | ||
81 | return this.authHttp.put(url, settings) | |
82 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
83 | } | |
84 | ||
85 | private formatNotification (notification: UserNotificationServer) { | |
86 | return new UserNotification(notification, this.auth.getUser()) | |
87 | } | |
88 | } |