aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/core')
-rw-r--r--client/src/app/core/core.module.ts4
-rw-r--r--client/src/app/core/notification/index.ts2
-rw-r--r--client/src/app/core/notification/peertube-socket.service.ts86
-rw-r--r--client/src/app/core/notification/user-notification-socket.service.ts44
4 files changed, 89 insertions, 47 deletions
diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.ts
index 22896e2e9..6c0a2245d 100644
--- a/client/src/app/core/core.module.ts
+++ b/client/src/app/core/core.module.ts
@@ -4,7 +4,7 @@ import { ToastModule } from 'primeng/toast'
4import { CommonModule } from '@angular/common' 4import { CommonModule } from '@angular/common'
5import { NgModule, Optional, SkipSelf } from '@angular/core' 5import { NgModule, Optional, SkipSelf } from '@angular/core'
6import { BrowserAnimationsModule } from '@angular/platform-browser/animations' 6import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
7import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service' 7import { PeerTubeSocket } from '@app/core/notification/peertube-socket.service'
8import { HooksService } from '@app/core/plugins/hooks.service' 8import { HooksService } from '@app/core/plugins/hooks.service'
9import { PluginService } from '@app/core/plugins/plugin.service' 9import { PluginService } from '@app/core/plugins/plugin.service'
10import { UnloggedGuard } from '@app/core/routing/unlogged-guard.service' 10import { UnloggedGuard } from '@app/core/routing/unlogged-guard.service'
@@ -84,7 +84,7 @@ import { LocalStorageService, ScreenService, SessionStorageService } from './wra
84 RedirectService, 84 RedirectService,
85 Notifier, 85 Notifier,
86 MessageService, 86 MessageService,
87 UserNotificationSocket, 87 PeerTubeSocket,
88 ServerConfigResolver, 88 ServerConfigResolver,
89 CanDeactivateGuard 89 CanDeactivateGuard
90 ] 90 ]
diff --git a/client/src/app/core/notification/index.ts b/client/src/app/core/notification/index.ts
index 3e8d9ea65..cd9634c8e 100644
--- a/client/src/app/core/notification/index.ts
+++ b/client/src/app/core/notification/index.ts
@@ -1,2 +1,2 @@
1export * from './notifier.service' 1export * from './notifier.service'
2export * from './user-notification-socket.service' 2export * from './peertube-socket.service'
diff --git a/client/src/app/core/notification/peertube-socket.service.ts b/client/src/app/core/notification/peertube-socket.service.ts
new file mode 100644
index 000000000..8668c44a8
--- /dev/null
+++ b/client/src/app/core/notification/peertube-socket.service.ts
@@ -0,0 +1,86 @@
1import { Subject } from 'rxjs'
2import { Injectable, NgZone } from '@angular/core'
3import { LiveVideoEventPayload, LiveVideoEventType, UserNotification as UserNotificationServer } from '@shared/models'
4import { environment } from '../../../environments/environment'
5import { AuthService } from '../auth'
6
7export type NotificationEvent = 'new' | 'read' | 'read-all'
8
9@Injectable()
10export class PeerTubeSocket {
11 private io: typeof import ('socket.io-client')
12
13 private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>()
14 private liveVideosSubject = new Subject<{ type: LiveVideoEventType, payload: LiveVideoEventPayload }>()
15
16 private notificationSocket: SocketIOClient.Socket
17 private liveVideosSocket: SocketIOClient.Socket
18
19 constructor (
20 private auth: AuthService,
21 private ngZone: NgZone
22 ) {}
23
24 async getMyNotificationsSocket () {
25 await this.initNotificationSocket()
26
27 return this.notificationSubject.asObservable()
28 }
29
30 getLiveVideosObservable () {
31 return this.liveVideosSubject.asObservable()
32 }
33
34 async subscribeToLiveVideosSocket (videoId: number) {
35 await this.initLiveVideosSocket()
36
37 this.liveVideosSocket.emit('subscribe', { videoId })
38 }
39
40 async unsubscribeLiveVideos (videoId: number) {
41 if (!this.liveVideosSocket) return
42
43 this.liveVideosSocket.emit('unsubscribe', { videoId })
44 }
45
46 dispatchNotificationEvent (type: NotificationEvent, notification?: UserNotificationServer) {
47 this.notificationSubject.next({ type, notification })
48 }
49
50 private async initNotificationSocket () {
51 if (this.notificationSocket) return
52
53 await this.importIOIfNeeded()
54
55 this.ngZone.runOutsideAngular(() => {
56 this.notificationSocket = this.io(environment.apiUrl + '/user-notifications', {
57 query: { accessToken: this.auth.getAccessToken() }
58 })
59
60 this.notificationSocket.on('new-notification', (n: UserNotificationServer) => this.dispatchNotificationEvent('new', n))
61 })
62 }
63
64 private async initLiveVideosSocket () {
65 if (this.liveVideosSocket) return
66
67 await this.importIOIfNeeded()
68
69 this.ngZone.runOutsideAngular(() => {
70 this.liveVideosSocket = this.io(environment.apiUrl + '/live-videos')
71
72 const type: LiveVideoEventType = 'state-change'
73 this.liveVideosSocket.on(type, (payload: LiveVideoEventPayload) => this.dispatchLiveVideoEvent(type, payload))
74 })
75 }
76
77 private async importIOIfNeeded () {
78 if (this.io) return
79
80 this.io = (await import('socket.io-client') as any).default
81 }
82
83 private dispatchLiveVideoEvent (type: LiveVideoEventType, payload: LiveVideoEventPayload) {
84 this.liveVideosSubject.next({ type, payload })
85 }
86}
diff --git a/client/src/app/core/notification/user-notification-socket.service.ts b/client/src/app/core/notification/user-notification-socket.service.ts
deleted file mode 100644
index 37f0bc32c..000000000
--- a/client/src/app/core/notification/user-notification-socket.service.ts
+++ /dev/null
@@ -1,44 +0,0 @@
1import { Subject } from 'rxjs'
2import { Injectable, NgZone } from '@angular/core'
3import { UserNotification as UserNotificationServer } from '@shared/models'
4import { environment } from '../../../environments/environment'
5import { AuthService } from '../auth'
6
7export type NotificationEvent = 'new' | 'read' | 'read-all'
8
9@Injectable()
10export class UserNotificationSocket {
11 private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>()
12
13 private socket: SocketIOClient.Socket
14
15 constructor (
16 private auth: AuthService,
17 private ngZone: NgZone
18 ) {}
19
20 dispatch (type: NotificationEvent, notification?: UserNotificationServer) {
21 this.notificationSubject.next({ type, notification })
22 }
23
24 async getMyNotificationsSocket () {
25 await this.initSocket()
26
27 return this.notificationSubject.asObservable()
28 }
29
30 private async initSocket () {
31 if (this.socket) return
32
33 // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
34 const io: typeof import ('socket.io-client') = (await import('socket.io-client') as any).default
35
36 this.ngZone.runOutsideAngular(() => {
37 this.socket = io(environment.apiUrl + '/user-notifications', {
38 query: { accessToken: this.auth.getAccessToken() }
39 })
40
41 this.socket.on('new-notification', (n: UserNotificationServer) => this.dispatch('new', n))
42 })
43 }
44}