diff options
author | Chocobozzz <me@florianbigard.com> | 2019-01-14 15:32:09 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-01-14 15:32:09 +0100 |
commit | 9a39392a7e6b3f180104856a4ea893e5baf86a02 (patch) | |
tree | 11c5f568d6e5a8e67fb925337c83efd57adf9111 /client/src/app/core/notification/user-notification-socket.service.ts | |
parent | f57ad0d22c527328b1412aa1c8c3f0d4819219ca (diff) | |
download | PeerTube-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/core/notification/user-notification-socket.service.ts')
-rw-r--r-- | client/src/app/core/notification/user-notification-socket.service.ts | 41 |
1 files changed, 41 insertions, 0 deletions
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 @@ | |||
1 | import { Injectable } from '@angular/core' | ||
2 | import { environment } from '../../../environments/environment' | ||
3 | import { UserNotification as UserNotificationServer } from '../../../../../shared' | ||
4 | import { Subject } from 'rxjs' | ||
5 | import * as io from 'socket.io-client' | ||
6 | import { AuthService } from '../auth' | ||
7 | |||
8 | export type NotificationEvent = 'new' | 'read' | 'read-all' | ||
9 | |||
10 | @Injectable() | ||
11 | export 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 | } | ||