]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/notification/peertube-socket.service.ts
Fetch things in bulk for the homepage
[github/Chocobozzz/PeerTube.git] / client / src / app / core / notification / peertube-socket.service.ts
CommitLineData
a5cf76af
C
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'
4f926722 6import { io, Socket } from 'socket.io-client'
a5cf76af
C
7
8export type NotificationEvent = 'new' | 'read' | 'read-all'
9
10@Injectable()
11export 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 (
21 private auth: AuthService,
22 private ngZone: NgZone
23 ) {}
24
25 async getMyNotificationsSocket () {
26 await this.initNotificationSocket()
27
28 return this.notificationSubject.asObservable()
29 }
30
31 getLiveVideosObservable () {
32 return this.liveVideosSubject.asObservable()
33 }
34
35 async subscribeToLiveVideosSocket (videoId: number) {
36 await this.initLiveVideosSocket()
37
38 this.liveVideosSocket.emit('subscribe', { videoId })
39 }
40
41 async unsubscribeLiveVideos (videoId: number) {
42 if (!this.liveVideosSocket) return
43
44 this.liveVideosSocket.emit('unsubscribe', { videoId })
45 }
46
47 dispatchNotificationEvent (type: NotificationEvent, notification?: UserNotificationServer) {
48 this.notificationSubject.next({ type, notification })
49 }
50
51 private async initNotificationSocket () {
52 if (this.notificationSocket) return
53
54 await this.importIOIfNeeded()
55
15feebd9 56 // Prevent protractor issues https://github.com/angular/angular/issues/11853
a5cf76af
C
57 this.ngZone.runOutsideAngular(() => {
58 this.notificationSocket = this.io(environment.apiUrl + '/user-notifications', {
59 query: { accessToken: this.auth.getAccessToken() }
60 })
a5cf76af 61 })
15feebd9 62
5d43dae3
C
63 this.notificationSocket.on('new-notification', (n: UserNotificationServer) => {
64 this.ngZone.run(() => this.dispatchNotificationEvent('new', n))
65 })
a5cf76af
C
66 }
67
68 private async initLiveVideosSocket () {
69 if (this.liveVideosSocket) return
70
71 await this.importIOIfNeeded()
72
15feebd9 73 // Prevent protractor issues https://github.com/angular/angular/issues/11853
a5cf76af
C
74 this.ngZone.runOutsideAngular(() => {
75 this.liveVideosSocket = this.io(environment.apiUrl + '/live-videos')
a5cf76af 76 })
15feebd9 77
a800dbf3
C
78 const types: LiveVideoEventType[] = [ 'views-change', 'state-change' ]
79
80 for (const type of types) {
dc1f314e
C
81 this.liveVideosSocket.on(type, (payload: LiveVideoEventPayload) => {
82 this.ngZone.run(() => this.dispatchLiveVideoEvent(type, payload))
83 })
a800dbf3 84 }
a5cf76af
C
85 }
86
87 private async importIOIfNeeded () {
88 if (this.io) return
89
4f926722 90 this.io = (await import('socket.io-client')).io
a5cf76af
C
91 }
92
93 private dispatchLiveVideoEvent (type: LiveVideoEventType, payload: LiveVideoEventPayload) {
94 this.liveVideosSubject.next({ type, payload })
95 }
96}