]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/users/user-notification.service.ts
Use raw sql for abuses
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / users / user-notification.service.ts
1 import { catchError, map, tap } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { ComponentPaginationLight, RestExtractor, RestService, User, UserNotificationSocket } from '@app/core'
5 import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '@shared/models'
6 import { environment } from '../../../../environments/environment'
7 import { UserNotification } from './user-notification.model'
8
9 @Injectable()
10 export class UserNotificationService {
11 static BASE_NOTIFICATIONS_URL = environment.apiUrl + '/api/v1/users/me/notifications'
12 static BASE_NOTIFICATION_SETTINGS = environment.apiUrl + '/api/v1/users/me/notification-settings'
13
14 constructor (
15 private authHttp: HttpClient,
16 private restExtractor: RestExtractor,
17 private restService: RestService,
18 private userNotificationSocket: UserNotificationSocket
19 ) {}
20
21 listMyNotifications (pagination: ComponentPaginationLight, unread?: boolean, ignoreLoadingBar = false) {
22 let params = new HttpParams()
23 params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination))
24
25 if (unread) params = params.append('unread', `${unread}`)
26
27 const headers = ignoreLoadingBar ? { ignoreLoadingBar: '' } : undefined
28
29 return this.authHttp.get<ResultList<UserNotification>>(UserNotificationService.BASE_NOTIFICATIONS_URL, { params, headers })
30 .pipe(
31 map(res => this.restExtractor.convertResultListDateToHuman(res)),
32 map(res => this.restExtractor.applyToResultListData(res, this.formatNotification.bind(this))),
33 catchError(err => this.restExtractor.handleError(err))
34 )
35 }
36
37 countUnreadNotifications () {
38 return this.listMyNotifications({ currentPage: 1, itemsPerPage: 0 }, true)
39 .pipe(map(n => n.total))
40 }
41
42 markAsRead (notification: UserNotification) {
43 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
44
45 const body = { ids: [ notification.id ] }
46 const headers = { ignoreLoadingBar: '' }
47
48 return this.authHttp.post(url, body, { headers })
49 .pipe(
50 map(this.restExtractor.extractDataBool),
51 tap(() => this.userNotificationSocket.dispatch('read')),
52 catchError(res => this.restExtractor.handleError(res))
53 )
54 }
55
56 markAllAsRead () {
57 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all'
58 const headers = { ignoreLoadingBar: '' }
59
60 return this.authHttp.post(url, {}, { headers })
61 .pipe(
62 map(this.restExtractor.extractDataBool),
63 tap(() => this.userNotificationSocket.dispatch('read-all')),
64 catchError(res => this.restExtractor.handleError(res))
65 )
66 }
67
68 updateNotificationSettings (user: User, settings: UserNotificationSetting) {
69 const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS
70
71 return this.authHttp.put(url, settings)
72 .pipe(
73 map(this.restExtractor.extractDataBool),
74 catchError(res => this.restExtractor.handleError(res))
75 )
76 }
77
78 private formatNotification (notification: UserNotificationServer) {
79 return new UserNotification(notification)
80 }
81 }