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