]>
Commit | Line | Data |
---|---|---|
9df52d66 | 1 | import { SortMeta } from 'primeng/api' |
2f1548fd | 2 | import { catchError, map, tap } from 'rxjs/operators' |
9744bb2a | 3 | import { HttpClient, HttpContext, HttpParams } from '@angular/common/http' |
67ed6552 | 4 | import { Injectable } from '@angular/core' |
9df52d66 | 5 | import { AuthService, ComponentPaginationLight, PeerTubeSocket, RestExtractor, RestService } from '@app/core' |
9744bb2a | 6 | import { NGX_LOADING_BAR_IGNORED } from '@ngx-loading-bar/http-client' |
67ed6552 C |
7 | import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '@shared/models' |
8 | import { environment } from '../../../../environments/environment' | |
9a39392a | 9 | import { UserNotification } from './user-notification.model' |
2f1548fd C |
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 | ||
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 | 42 | .pipe( |
2f1548fd C |
43 | map(res => this.restExtractor.applyToResultListData(res, this.formatNotification.bind(this))), |
44 | catchError(err => this.restExtractor.handleError(err)) | |
45 | ) | |
46 | } | |
47 | ||
48 | countUnreadNotifications () { | |
654a188f | 49 | return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true }) |
2f1548fd C |
50 | .pipe(map(n => n.total)) |
51 | } | |
52 | ||
2f1548fd C |
53 | markAsRead (notification: UserNotification) { |
54 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read' | |
55 | ||
56 | const body = { ids: [ notification.id ] } | |
9744bb2a | 57 | const context = new HttpContext().set(NGX_LOADING_BAR_IGNORED, true) |
2f1548fd | 58 | |
9744bb2a | 59 | return this.authHttp.post(url, body, { context }) |
2f1548fd | 60 | .pipe( |
a5cf76af | 61 | tap(() => this.peertubeSocket.dispatchNotificationEvent('read')), |
2f1548fd C |
62 | catchError(res => this.restExtractor.handleError(res)) |
63 | ) | |
64 | } | |
65 | ||
66 | markAllAsRead () { | |
67 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all' | |
9744bb2a | 68 | const context = new HttpContext().set(NGX_LOADING_BAR_IGNORED, true) |
2f1548fd | 69 | |
9744bb2a | 70 | return this.authHttp.post(url, {}, { context }) |
2f1548fd | 71 | .pipe( |
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) | |
e8bffe96 | 81 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
2f1548fd C |
82 | } |
83 | ||
2f1548fd | 84 | private formatNotification (notification: UserNotificationServer) { |
d573926e | 85 | return new UserNotification(notification, this.auth.getUser()) |
2f1548fd C |
86 | } |
87 | } |