diff options
author | Chocobozzz <me@florianbigard.com> | 2020-09-25 10:04:21 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-11-09 15:33:04 +0100 |
commit | a5cf76afa378aae81af2a9b0ce548e5d2582f832 (patch) | |
tree | 58da320232bee7c9656774c5d6811e82bbf6c696 /client/src/app/core/notification/peertube-socket.service.ts | |
parent | de6310b2fcbb8a6b79c546b23dfa1920724faaa7 (diff) | |
download | PeerTube-a5cf76afa378aae81af2a9b0ce548e5d2582f832.tar.gz PeerTube-a5cf76afa378aae81af2a9b0ce548e5d2582f832.tar.zst PeerTube-a5cf76afa378aae81af2a9b0ce548e5d2582f832.zip |
Add watch messages if live has not started
Diffstat (limited to 'client/src/app/core/notification/peertube-socket.service.ts')
-rw-r--r-- | client/src/app/core/notification/peertube-socket.service.ts | 86 |
1 files changed, 86 insertions, 0 deletions
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 | } | ||