aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/notification/peertube-socket.service.ts
blob: 0db86d8e7fd71e3217682f77a2bfe82f95db0465 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Subject } from 'rxjs'
import { io, Socket } from 'socket.io-client'
import { Injectable } from '@angular/core'
import { LiveVideoEventPayload, LiveVideoEventType, UserNotification as UserNotificationServer } from '@shared/models'
import { environment } from '../../../environments/environment'
import { AuthService } from '../auth'

export type NotificationEvent = 'new' | 'read' | 'read-all'

@Injectable()
export class PeerTubeSocket {
  private io: typeof io

  private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>()
  private liveVideosSubject = new Subject<{ type: LiveVideoEventType, payload: LiveVideoEventPayload }>()

  private notificationSocket: Socket
  private liveVideosSocket: Socket

  constructor (
    private auth: AuthService
  ) {}

  async getMyNotificationsSocket () {
    await this.initNotificationSocket()

    return this.notificationSubject.asObservable()
  }

  getLiveVideosObservable () {
    return this.liveVideosSubject.asObservable()
  }

  async subscribeToLiveVideosSocket (videoId: number) {
    await this.initLiveVideosSocket()

    this.liveVideosSocket.emit('subscribe', { videoId })
  }

  unsubscribeLiveVideos (videoId: number) {
    if (!this.liveVideosSocket) return

    this.liveVideosSocket.emit('unsubscribe', { videoId })
  }

  dispatchNotificationEvent (type: NotificationEvent, notification?: UserNotificationServer) {
    this.notificationSubject.next({ type, notification })
  }

  private async initNotificationSocket () {
    if (this.notificationSocket) return

    await this.importIOIfNeeded()

    this.notificationSocket = this.io(environment.apiUrl + '/user-notifications', {
      query: { accessToken: this.auth.getAccessToken() }
    })

    this.notificationSocket.on('new-notification', (n: UserNotificationServer) => {
      this.dispatchNotificationEvent('new', n)
    })
  }

  private async initLiveVideosSocket () {
    if (this.liveVideosSocket) return

    await this.importIOIfNeeded()

    this.liveVideosSocket = this.io(environment.apiUrl + '/live-videos')

    const types: LiveVideoEventType[] = [ 'views-change', 'state-change' ]

    for (const type of types) {
      this.liveVideosSocket.on(type, (payload: LiveVideoEventPayload) => {
        this.dispatchLiveVideoEvent(type, payload)
      })
    }
  }

  private async importIOIfNeeded () {
    if (this.io) return

    this.io = (await import('socket.io-client')).io
  }

  private dispatchLiveVideoEvent (type: LiveVideoEventType, payload: LiveVideoEventPayload) {
    this.liveVideosSubject.next({ type, payload })
  }
}