]>
Commit | Line | Data |
---|---|---|
2f1548fd | 1 | import { catchError, map, tap } from 'rxjs/operators' |
67ed6552 C |
2 | import { HttpClient, HttpParams } from '@angular/common/http' |
3 | import { Injectable } from '@angular/core' | |
a5cf76af | 4 | import { ComponentPaginationLight, RestExtractor, RestService, User, PeerTubeSocket, AuthService } from '@app/core' |
67ed6552 C |
5 | import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '@shared/models' |
6 | import { environment } from '../../../../environments/environment' | |
9a39392a | 7 | import { UserNotification } from './user-notification.model' |
654a188f | 8 | import { SortMeta } from 'primeng/api' |
2f1548fd C |
9 | |
10 | @Injectable() | |
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' | |
14 | ||
2f1548fd | 15 | constructor ( |
2f1548fd | 16 | private authHttp: HttpClient, |
d573926e | 17 | private auth: AuthService, |
2f1548fd | 18 | private restExtractor: RestExtractor, |
9a39392a | 19 | private restService: RestService, |
a5cf76af | 20 | private peertubeSocket: PeerTubeSocket |
2f1548fd C |
21 | ) {} |
22 | ||
654a188f RK |
23 | listMyNotifications (parameters: { |
24 | pagination: ComponentPaginationLight | |
25 | ignoreLoadingBar?: boolean | |
26 | unread?: boolean, | |
27 | sort?: SortMeta | |
28 | }) { | |
29 | const { pagination, ignoreLoadingBar, unread, sort } = parameters | |
30 | ||
2f1548fd | 31 | let params = new HttpParams() |
654a188f | 32 | params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination), sort) |
2f1548fd C |
33 | |
34 | if (unread) params = params.append('unread', `${unread}`) | |
35 | ||
36 | const headers = ignoreLoadingBar ? { ignoreLoadingBar: '' } : undefined | |
37 | ||
38 | return this.authHttp.get<ResultList<UserNotification>>(UserNotificationService.BASE_NOTIFICATIONS_URL, { params, headers }) | |
39 | .pipe( | |
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)) | |
43 | ) | |
44 | } | |
45 | ||
46 | countUnreadNotifications () { | |
654a188f | 47 | return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true }) |
2f1548fd C |
48 | .pipe(map(n => n.total)) |
49 | } | |
50 | ||
2f1548fd C |
51 | markAsRead (notification: UserNotification) { |
52 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read' | |
53 | ||
54 | const body = { ids: [ notification.id ] } | |
55 | const headers = { ignoreLoadingBar: '' } | |
56 | ||
57 | return this.authHttp.post(url, body, { headers }) | |
58 | .pipe( | |
59 | map(this.restExtractor.extractDataBool), | |
a5cf76af | 60 | tap(() => this.peertubeSocket.dispatchNotificationEvent('read')), |
2f1548fd C |
61 | catchError(res => this.restExtractor.handleError(res)) |
62 | ) | |
63 | } | |
64 | ||
65 | markAllAsRead () { | |
66 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all' | |
67 | const headers = { ignoreLoadingBar: '' } | |
68 | ||
69 | return this.authHttp.post(url, {}, { headers }) | |
70 | .pipe( | |
71 | map(this.restExtractor.extractDataBool), | |
a5cf76af | 72 | tap(() => this.peertubeSocket.dispatchNotificationEvent('read-all')), |
2f1548fd C |
73 | catchError(res => this.restExtractor.handleError(res)) |
74 | ) | |
75 | } | |
76 | ||
a5cf76af | 77 | updateNotificationSettings (settings: UserNotificationSetting) { |
2f1548fd C |
78 | const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS |
79 | ||
80 | return this.authHttp.put(url, settings) | |
81 | .pipe( | |
82 | map(this.restExtractor.extractDataBool), | |
83 | catchError(res => this.restExtractor.handleError(res)) | |
84 | ) | |
85 | } | |
86 | ||
2f1548fd | 87 | private formatNotification (notification: UserNotificationServer) { |
d573926e | 88 | return new UserNotification(notification, this.auth.getUser()) |
2f1548fd C |
89 | } |
90 | } |