aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/users
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-01-14 15:32:09 +0100
committerChocobozzz <me@florianbigard.com>2019-01-14 15:32:09 +0100
commit9a39392a7e6b3f180104856a4ea893e5baf86a02 (patch)
tree11c5f568d6e5a8e67fb925337c83efd57adf9111 /client/src/app/shared/users
parentf57ad0d22c527328b1412aa1c8c3f0d4819219ca (diff)
downloadPeerTube-9a39392a7e6b3f180104856a4ea893e5baf86a02.tar.gz
PeerTube-9a39392a7e6b3f180104856a4ea893e5baf86a02.tar.zst
PeerTube-9a39392a7e6b3f180104856a4ea893e5baf86a02.zip
Fix notification socket
Should be in core module to share the same subject to all the app
Diffstat (limited to 'client/src/app/shared/users')
-rw-r--r--client/src/app/shared/users/user-notification.service.ts42
-rw-r--r--client/src/app/shared/users/user-notifications.component.ts3
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 @@
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 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'
13export class UserNotificationsComponent implements OnInit { 13export 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