diff options
Diffstat (limited to 'client/src/app/shared')
-rw-r--r-- | client/src/app/shared/users/user-notification.service.ts | 42 | ||||
-rw-r--r-- | client/src/app/shared/users/user-notifications.component.ts | 3 |
2 files changed, 12 insertions, 33 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..67ed8f74e 100644 --- a/client/src/app/shared/users/user-notification.service.ts +++ b/client/src/app/shared/users/user-notification.service.ts | |||
@@ -1,30 +1,28 @@ | |||
1 | import { Injectable } from '@angular/core' | 1 | import { Injectable } from '@angular/core' |
2 | import { HttpClient, HttpParams } from '@angular/common/http' | 2 | import { HttpClient, HttpParams } from '@angular/common/http' |
3 | import { RestExtractor, RestService } from '@app/shared/rest' | 3 | import { RestExtractor, RestService } from '../rest' |
4 | import { catchError, map, tap } from 'rxjs/operators' | 4 | import { catchError, map, tap } from 'rxjs/operators' |
5 | import { environment } from '../../../environments/environment' | 5 | import { environment } from '../../../environments/environment' |
6 | import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '../../../../../shared' | 6 | import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '../../../../../shared' |
7 | import { UserNotification } from '@app/shared/users/user-notification.model' | 7 | import { UserNotification } from './user-notification.model' |
8 | import { Subject } from 'rxjs' | 8 | import { AuthService } from '../../core' |
9 | import * as io from 'socket.io-client' | 9 | import { ComponentPagination } from '../rest/component-pagination.model' |
10 | import { AuthService } from '@app/core' | 10 | import { User } from '..' |
11 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | 11 | import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service' |
12 | import { User } from '@app/shared' | ||
13 | 12 | ||
14 | @Injectable() | 13 | @Injectable() |
15 | export class UserNotificationService { | 14 | export 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 | 18 | private socket: SocketIOClient.Socket |
22 | 19 | ||
23 | constructor ( | 20 | constructor ( |
24 | private auth: AuthService, | 21 | private auth: AuthService, |
25 | private authHttp: HttpClient, | 22 | private authHttp: HttpClient, |
26 | private restExtractor: RestExtractor, | 23 | private restExtractor: RestExtractor, |
27 | private restService: RestService | 24 | private restService: RestService, |
25 | private userNotificationSocket: UserNotificationSocket | ||
28 | ) {} | 26 | ) {} |
29 | 27 | ||
30 | listMyNotifications (pagination: ComponentPagination, unread?: boolean, ignoreLoadingBar = false) { | 28 | listMyNotifications (pagination: ComponentPagination, unread?: boolean, ignoreLoadingBar = false) { |
@@ -48,16 +46,6 @@ export class UserNotificationService { | |||
48 | .pipe(map(n => n.total)) | 46 | .pipe(map(n => n.total)) |
49 | } | 47 | } |
50 | 48 | ||
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) { | 49 | markAsRead (notification: UserNotification) { |
62 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read' | 50 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read' |
63 | 51 | ||
@@ -67,7 +55,7 @@ export class UserNotificationService { | |||
67 | return this.authHttp.post(url, body, { headers }) | 55 | return this.authHttp.post(url, body, { headers }) |
68 | .pipe( | 56 | .pipe( |
69 | map(this.restExtractor.extractDataBool), | 57 | map(this.restExtractor.extractDataBool), |
70 | tap(() => this.notificationSubject.next({ type: 'read' })), | 58 | tap(() => this.userNotificationSocket.dispatch('read')), |
71 | catchError(res => this.restExtractor.handleError(res)) | 59 | catchError(res => this.restExtractor.handleError(res)) |
72 | ) | 60 | ) |
73 | } | 61 | } |
@@ -79,7 +67,7 @@ export class UserNotificationService { | |||
79 | return this.authHttp.post(url, {}, { headers }) | 67 | return this.authHttp.post(url, {}, { headers }) |
80 | .pipe( | 68 | .pipe( |
81 | map(this.restExtractor.extractDataBool), | 69 | map(this.restExtractor.extractDataBool), |
82 | tap(() => this.notificationSubject.next({ type: 'read-all' })), | 70 | tap(() => this.userNotificationSocket.dispatch('read-all')), |
83 | catchError(res => this.restExtractor.handleError(res)) | 71 | catchError(res => this.restExtractor.handleError(res)) |
84 | ) | 72 | ) |
85 | } | 73 | } |
@@ -94,16 +82,6 @@ export class UserNotificationService { | |||
94 | ) | 82 | ) |
95 | } | 83 | } |
96 | 84 | ||
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) { | 85 | private formatNotification (notification: UserNotificationServer) { |
108 | return new UserNotification(notification) | 86 | return new UserNotification(notification) |
109 | } | 87 | } |
diff --git a/client/src/app/shared/users/user-notifications.component.ts b/client/src/app/shared/users/user-notifications.component.ts index 50c495a9a..e3913ba56 100644 --- a/client/src/app/shared/users/user-notifications.component.ts +++ b/client/src/app/shared/users/user-notifications.component.ts | |||
@@ -13,6 +13,7 @@ import { UserNotification } from '@app/shared/users/user-notification.model' | |||
13 | export class UserNotificationsComponent implements OnInit { | 13 | export class UserNotificationsComponent implements OnInit { |
14 | @Input() ignoreLoadingBar = false | 14 | @Input() ignoreLoadingBar = false |
15 | @Input() infiniteScroll = true | 15 | @Input() infiniteScroll = true |
16 | @Input() itemsPerPage = 20 | ||
16 | 17 | ||
17 | notifications: UserNotification[] = [] | 18 | notifications: UserNotification[] = [] |
18 | 19 | ||
@@ -21,7 +22,7 @@ export class UserNotificationsComponent implements OnInit { | |||
21 | 22 | ||
22 | componentPagination: ComponentPagination = { | 23 | componentPagination: ComponentPagination = { |
23 | currentPage: 1, | 24 | currentPage: 1, |
24 | itemsPerPage: 20, | 25 | itemsPerPage: this.itemsPerPage, |
25 | totalItems: null | 26 | totalItems: null |
26 | } | 27 | } |
27 | 28 | ||