]>
Commit | Line | Data |
---|---|---|
a5cf76af | 1 | import { Subject } from 'rxjs' |
afb7d2d5 C |
2 | import { io, Socket } from 'socket.io-client' |
3 | import { Injectable } from '@angular/core' | |
a5cf76af C |
4 | import { LiveVideoEventPayload, LiveVideoEventType, UserNotification as UserNotificationServer } from '@shared/models' |
5 | import { environment } from '../../../environments/environment' | |
6 | import { AuthService } from '../auth' | |
7 | ||
8 | export type NotificationEvent = 'new' | 'read' | 'read-all' | |
9 | ||
10 | @Injectable() | |
11 | export class PeerTubeSocket { | |
4f926722 | 12 | private io: typeof io |
a5cf76af C |
13 | |
14 | private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>() | |
15 | private liveVideosSubject = new Subject<{ type: LiveVideoEventType, payload: LiveVideoEventPayload }>() | |
16 | ||
4f926722 C |
17 | private notificationSocket: Socket |
18 | private liveVideosSocket: Socket | |
a5cf76af C |
19 | |
20 | constructor ( | |
afb7d2d5 | 21 | private auth: AuthService |
a5cf76af C |
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 | ||
98ab5dc8 | 40 | unsubscribeLiveVideos (videoId: number) { |
a5cf76af C |
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 | ||
afb7d2d5 C |
55 | this.notificationSocket = this.io(environment.apiUrl + '/user-notifications', { |
56 | query: { accessToken: this.auth.getAccessToken() } | |
a5cf76af | 57 | }) |
15feebd9 | 58 | |
5d43dae3 | 59 | this.notificationSocket.on('new-notification', (n: UserNotificationServer) => { |
afb7d2d5 | 60 | this.dispatchNotificationEvent('new', n) |
5d43dae3 | 61 | }) |
a5cf76af C |
62 | } |
63 | ||
64 | private async initLiveVideosSocket () { | |
65 | if (this.liveVideosSocket) return | |
66 | ||
67 | await this.importIOIfNeeded() | |
68 | ||
afb7d2d5 | 69 | this.liveVideosSocket = this.io(environment.apiUrl + '/live-videos') |
15feebd9 | 70 | |
a800dbf3 C |
71 | const types: LiveVideoEventType[] = [ 'views-change', 'state-change' ] |
72 | ||
73 | for (const type of types) { | |
dc1f314e | 74 | this.liveVideosSocket.on(type, (payload: LiveVideoEventPayload) => { |
afb7d2d5 | 75 | this.dispatchLiveVideoEvent(type, payload) |
dc1f314e | 76 | }) |
a800dbf3 | 77 | } |
a5cf76af C |
78 | } |
79 | ||
80 | private async importIOIfNeeded () { | |
81 | if (this.io) return | |
82 | ||
4f926722 | 83 | this.io = (await import('socket.io-client')).io |
a5cf76af C |
84 | } |
85 | ||
86 | private dispatchLiveVideoEvent (type: LiveVideoEventType, payload: LiveVideoEventPayload) { | |
87 | this.liveVideosSubject.next({ type, payload }) | |
88 | } | |
89 | } |