]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
2f1548fd 1import { catchError, map, tap } from 'rxjs/operators'
67ed6552
C
2import { HttpClient, HttpParams } from '@angular/common/http'
3import { Injectable } from '@angular/core'
4import { ComponentPaginationLight, RestExtractor, RestService, User, UserNotificationSocket } from '@app/core'
5import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '@shared/models'
6import { environment } from '../../../../environments/environment'
9a39392a 7import { UserNotification } from './user-notification.model'
2f1548fd
C
8
9@Injectable()
10export 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
2f1548fd 14 constructor (
2f1548fd
C
15 private authHttp: HttpClient,
16 private restExtractor: RestExtractor,
9a39392a
C
17 private restService: RestService,
18 private userNotificationSocket: UserNotificationSocket
2f1548fd
C
19 ) {}
20
440d39c5 21 listMyNotifications (pagination: ComponentPaginationLight, unread?: boolean, ignoreLoadingBar = false) {
2f1548fd
C
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
2f1548fd
C
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),
9a39392a 51 tap(() => this.userNotificationSocket.dispatch('read')),
2f1548fd
C
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),
9a39392a 63 tap(() => this.userNotificationSocket.dispatch('read-all')),
2f1548fd
C
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
2f1548fd
C
78 private formatNotification (notification: UserNotificationServer) {
79 return new UserNotification(notification)
80 }
81}