]>
Commit | Line | Data |
---|---|---|
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 | } |