aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/users/user-notification.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/users/user-notification.service.ts')
-rw-r--r--client/src/app/shared/users/user-notification.service.ts44
1 files changed, 10 insertions, 34 deletions
diff --git a/client/src/app/shared/users/user-notification.service.ts b/client/src/app/shared/users/user-notification.service.ts
index 2dfee8060..f8a30955d 100644
--- a/client/src/app/shared/users/user-notification.service.ts
+++ b/client/src/app/shared/users/user-notification.service.ts
@@ -1,30 +1,26 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { HttpClient, HttpParams } from '@angular/common/http' 2import { HttpClient, HttpParams } from '@angular/common/http'
3import { RestExtractor, RestService } from '@app/shared/rest' 3import { RestExtractor, RestService } from '../rest'
4import { catchError, map, tap } from 'rxjs/operators' 4import { catchError, map, tap } from 'rxjs/operators'
5import { environment } from '../../../environments/environment' 5import { environment } from '../../../environments/environment'
6import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '../../../../../shared' 6import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '../../../../../shared'
7import { UserNotification } from '@app/shared/users/user-notification.model' 7import { UserNotification } from './user-notification.model'
8import { Subject } from 'rxjs' 8import { AuthService } from '../../core'
9import * as io from 'socket.io-client' 9import { ComponentPagination } from '../rest/component-pagination.model'
10import { AuthService } from '@app/core' 10import { User } from '..'
11import { ComponentPagination } from '@app/shared/rest/component-pagination.model' 11import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service'
12import { User } from '@app/shared'
13 12
14@Injectable() 13@Injectable()
15export class UserNotificationService { 14export class UserNotificationService {
16 static BASE_NOTIFICATIONS_URL = environment.apiUrl + '/api/v1/users/me/notifications' 15 static BASE_NOTIFICATIONS_URL = environment.apiUrl + '/api/v1/users/me/notifications'
17 static BASE_NOTIFICATION_SETTINGS = environment.apiUrl + '/api/v1/users/me/notification-settings' 16 static BASE_NOTIFICATION_SETTINGS = environment.apiUrl + '/api/v1/users/me/notification-settings'
18 17
19 private notificationSubject = new Subject<{ type: 'new' | 'read' | 'read-all', notification?: UserNotification }>()
20
21 private socket: SocketIOClient.Socket
22
23 constructor ( 18 constructor (
24 private auth: AuthService, 19 private auth: AuthService,
25 private authHttp: HttpClient, 20 private authHttp: HttpClient,
26 private restExtractor: RestExtractor, 21 private restExtractor: RestExtractor,
27 private restService: RestService 22 private restService: RestService,
23 private userNotificationSocket: UserNotificationSocket
28 ) {} 24 ) {}
29 25
30 listMyNotifications (pagination: ComponentPagination, unread?: boolean, ignoreLoadingBar = false) { 26 listMyNotifications (pagination: ComponentPagination, unread?: boolean, ignoreLoadingBar = false) {
@@ -48,16 +44,6 @@ export class UserNotificationService {
48 .pipe(map(n => n.total)) 44 .pipe(map(n => n.total))
49 } 45 }
50 46
51 getMyNotificationsSocket () {
52 const socket = this.getSocket()
53
54 socket.on('new-notification', (n: UserNotificationServer) => {
55 this.notificationSubject.next({ type: 'new', notification: new UserNotification(n) })
56 })
57
58 return this.notificationSubject.asObservable()
59 }
60
61 markAsRead (notification: UserNotification) { 47 markAsRead (notification: UserNotification) {
62 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read' 48 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
63 49
@@ -67,7 +53,7 @@ export class UserNotificationService {
67 return this.authHttp.post(url, body, { headers }) 53 return this.authHttp.post(url, body, { headers })
68 .pipe( 54 .pipe(
69 map(this.restExtractor.extractDataBool), 55 map(this.restExtractor.extractDataBool),
70 tap(() => this.notificationSubject.next({ type: 'read' })), 56 tap(() => this.userNotificationSocket.dispatch('read')),
71 catchError(res => this.restExtractor.handleError(res)) 57 catchError(res => this.restExtractor.handleError(res))
72 ) 58 )
73 } 59 }
@@ -79,7 +65,7 @@ export class UserNotificationService {
79 return this.authHttp.post(url, {}, { headers }) 65 return this.authHttp.post(url, {}, { headers })
80 .pipe( 66 .pipe(
81 map(this.restExtractor.extractDataBool), 67 map(this.restExtractor.extractDataBool),
82 tap(() => this.notificationSubject.next({ type: 'read-all' })), 68 tap(() => this.userNotificationSocket.dispatch('read-all')),
83 catchError(res => this.restExtractor.handleError(res)) 69 catchError(res => this.restExtractor.handleError(res))
84 ) 70 )
85 } 71 }
@@ -94,16 +80,6 @@ export class UserNotificationService {
94 ) 80 )
95 } 81 }
96 82
97 private getSocket () {
98 if (this.socket) return this.socket
99
100 this.socket = io(environment.apiUrl + '/user-notifications', {
101 query: { accessToken: this.auth.getAccessToken() }
102 })
103
104 return this.socket
105 }
106
107 private formatNotification (notification: UserNotificationServer) { 83 private formatNotification (notification: UserNotificationServer) {
108 return new UserNotification(notification) 84 return new UserNotification(notification)
109 } 85 }