aboutsummaryrefslogtreecommitdiffhomepage
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
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
-rw-r--r--client/src/app/core/auth/auth.service.ts4
-rw-r--r--client/src/app/core/core.module.ts4
-rw-r--r--client/src/app/core/notification/index.ts1
-rw-r--r--client/src/app/core/notification/user-notification-socket.service.ts41
-rw-r--r--client/src/app/menu/avatar-notification.component.html2
-rw-r--r--client/src/app/menu/avatar-notification.component.ts5
-rw-r--r--client/src/app/shared/users/user-notification.service.ts42
-rw-r--r--client/src/app/shared/users/user-notifications.component.ts3
8 files changed, 63 insertions, 39 deletions
diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts
index 79ea32ced..eaa822e0f 100644
--- a/client/src/app/core/auth/auth.service.ts
+++ b/client/src/app/core/auth/auth.service.ts
@@ -3,12 +3,12 @@ import { catchError, map, mergeMap, share, tap } from 'rxjs/operators'
3import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http' 3import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
4import { Injectable } from '@angular/core' 4import { Injectable } from '@angular/core'
5import { Router } from '@angular/router' 5import { Router } from '@angular/router'
6import { Notifier } from '@app/core/notification' 6import { Notifier } from '@app/core/notification/notifier.service'
7import { OAuthClientLocal, User as UserServerModel, UserRefreshToken } from '../../../../../shared' 7import { OAuthClientLocal, User as UserServerModel, UserRefreshToken } from '../../../../../shared'
8import { User } from '../../../../../shared/models/users' 8import { User } from '../../../../../shared/models/users'
9import { UserLogin } from '../../../../../shared/models/users/user-login.model' 9import { UserLogin } from '../../../../../shared/models/users/user-login.model'
10import { environment } from '../../../environments/environment' 10import { environment } from '../../../environments/environment'
11import { RestExtractor } from '../../shared/rest' 11import { RestExtractor } from '../../shared/rest/rest-extractor.service'
12import { AuthStatus } from './auth-status.model' 12import { AuthStatus } from './auth-status.model'
13import { AuthUser } from './auth-user.model' 13import { AuthUser } from './auth-user.model'
14import { objectToUrlEncoded } from '@app/shared/misc/utils' 14import { objectToUrlEncoded } from '@app/shared/misc/utils'
diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.ts
index 7c0d4ac8f..3bc0e2885 100644
--- a/client/src/app/core/core.module.ts
+++ b/client/src/app/core/core.module.ts
@@ -18,6 +18,7 @@ import { CheatSheetComponent } from './hotkeys'
18import { ToastModule } from 'primeng/toast' 18import { ToastModule } from 'primeng/toast'
19import { Notifier } from './notification' 19import { Notifier } from './notification'
20import { MessageService } from 'primeng/api' 20import { MessageService } from 'primeng/api'
21import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service'
21 22
22@NgModule({ 23@NgModule({
23 imports: [ 24 imports: [
@@ -60,7 +61,8 @@ import { MessageService } from 'primeng/api'
60 UserRightGuard, 61 UserRightGuard,
61 RedirectService, 62 RedirectService,
62 Notifier, 63 Notifier,
63 MessageService 64 MessageService,
65 UserNotificationSocket
64 ] 66 ]
65}) 67})
66export class CoreModule { 68export class CoreModule {
diff --git a/client/src/app/core/notification/index.ts b/client/src/app/core/notification/index.ts
index 8b0cfde5f..3e8d9ea65 100644
--- a/client/src/app/core/notification/index.ts
+++ b/client/src/app/core/notification/index.ts
@@ -1 +1,2 @@
1export * from './notifier.service' 1export * from './notifier.service'
2export * from './user-notification-socket.service'
diff --git a/client/src/app/core/notification/user-notification-socket.service.ts b/client/src/app/core/notification/user-notification-socket.service.ts
new file mode 100644
index 000000000..f367d9ae4
--- /dev/null
+++ b/client/src/app/core/notification/user-notification-socket.service.ts
@@ -0,0 +1,41 @@
1import { Injectable } from '@angular/core'
2import { environment } from '../../../environments/environment'
3import { UserNotification as UserNotificationServer } from '../../../../../shared'
4import { Subject } from 'rxjs'
5import * as io from 'socket.io-client'
6import { AuthService } from '../auth'
7
8export type NotificationEvent = 'new' | 'read' | 'read-all'
9
10@Injectable()
11export class UserNotificationSocket {
12 private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>()
13
14 private socket: SocketIOClient.Socket
15
16 constructor (
17 private auth: AuthService
18 ) {}
19
20 dispatch (type: NotificationEvent, notification?: UserNotificationServer) {
21 this.notificationSubject.next({ type, notification })
22 }
23
24 getMyNotificationsSocket () {
25 const socket = this.getSocket()
26
27 socket.on('new-notification', (n: UserNotificationServer) => this.dispatch('new', n))
28
29 return this.notificationSubject.asObservable()
30 }
31
32 private getSocket () {
33 if (this.socket) return this.socket
34
35 this.socket = io(environment.apiUrl + '/user-notifications', {
36 query: { accessToken: this.auth.getAccessToken() }
37 })
38
39 return this.socket
40 }
41}
diff --git a/client/src/app/menu/avatar-notification.component.html b/client/src/app/menu/avatar-notification.component.html
index 2f0b7c669..4ef3f0e89 100644
--- a/client/src/app/menu/avatar-notification.component.html
+++ b/client/src/app/menu/avatar-notification.component.html
@@ -17,7 +17,7 @@
17 ></a> 17 ></a>
18 </div> 18 </div>
19 19
20 <my-user-notifications [ignoreLoadingBar]="true" [infiniteScroll]="false"></my-user-notifications> 20 <my-user-notifications [ignoreLoadingBar]="true" [infiniteScroll]="false" itemsPerPage="10"></my-user-notifications>
21 21
22 <a class="all-notifications" routerLink="/my-account/notifications" i18n>See all your notifications</a> 22 <a class="all-notifications" routerLink="/my-account/notifications" i18n>See all your notifications</a>
23</ng-template> 23</ng-template>
diff --git a/client/src/app/menu/avatar-notification.component.ts b/client/src/app/menu/avatar-notification.component.ts
index 60e090726..f1af08096 100644
--- a/client/src/app/menu/avatar-notification.component.ts
+++ b/client/src/app/menu/avatar-notification.component.ts
@@ -2,7 +2,7 @@ import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'
2import { User } from '../shared/users/user.model' 2import { User } from '../shared/users/user.model'
3import { UserNotificationService } from '@app/shared/users/user-notification.service' 3import { UserNotificationService } from '@app/shared/users/user-notification.service'
4import { Subscription } from 'rxjs' 4import { Subscription } from 'rxjs'
5import { Notifier } from '@app/core' 5import { Notifier, UserNotificationSocket } from '@app/core'
6import { NgbPopover } from '@ng-bootstrap/ng-bootstrap' 6import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
7import { NavigationEnd, Router } from '@angular/router' 7import { NavigationEnd, Router } from '@angular/router'
8import { filter } from 'rxjs/operators' 8import { filter } from 'rxjs/operators'
@@ -23,6 +23,7 @@ export class AvatarNotificationComponent implements OnInit, OnDestroy {
23 23
24 constructor ( 24 constructor (
25 private userNotificationService: UserNotificationService, 25 private userNotificationService: UserNotificationService,
26 private userNotificationSocket: UserNotificationSocket,
26 private notifier: Notifier, 27 private notifier: Notifier,
27 private router: Router 28 private router: Router
28 ) {} 29 ) {}
@@ -53,7 +54,7 @@ export class AvatarNotificationComponent implements OnInit, OnDestroy {
53 } 54 }
54 55
55 private subscribeToNotifications () { 56 private subscribeToNotifications () {
56 this.notificationSub = this.userNotificationService.getMyNotificationsSocket() 57 this.notificationSub = this.userNotificationSocket.getMyNotificationsSocket()
57 .subscribe(data => { 58 .subscribe(data => {
58 if (data.type === 'new') return this.unreadNotifications++ 59 if (data.type === 'new') return this.unreadNotifications++
59 if (data.type === 'read') return this.unreadNotifications-- 60 if (data.type === 'read') return this.unreadNotifications--
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