diff options
Diffstat (limited to 'client/src/app/core/notification')
3 files changed, 87 insertions, 45 deletions
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 @@ | |||
1 | export * from './notifier.service' | 1 | export * from './notifier.service' |
2 | export * from './user-notification-socket.service' | 2 | export * 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 @@ | |||
1 | import { Subject } from 'rxjs' | ||
2 | import { Injectable, NgZone } from '@angular/core' | ||
3 | import { LiveVideoEventPayload, LiveVideoEventType, UserNotification as UserNotificationServer } from '@shared/models' | ||
4 | import { environment } from '../../../environments/environment' | ||
5 | import { AuthService } from '../auth' | ||
6 | |||
7 | export type NotificationEvent = 'new' | 'read' | 'read-all' | ||
8 | |||
9 | @Injectable() | ||
10 | export 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 @@ | |||
1 | import { Subject } from 'rxjs' | ||
2 | import { Injectable, NgZone } from '@angular/core' | ||
3 | import { UserNotification as UserNotificationServer } from '@shared/models' | ||
4 | import { environment } from '../../../environments/environment' | ||
5 | import { AuthService } from '../auth' | ||
6 | |||
7 | export type NotificationEvent = 'new' | 'read' | 'read-all' | ||
8 | |||
9 | @Injectable() | ||
10 | export 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 | } | ||