]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-search/find-in-bulk.service.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-search / find-in-bulk.service.ts
CommitLineData
3da38d6e 1import * as debug from 'debug'
1378c0d3 2import { Observable, Subject } from 'rxjs'
2cc276f9 3import { filter, first, map } from 'rxjs/operators'
afb7d2d5 4import { Injectable } from '@angular/core'
3da38d6e
C
5import { buildBulkObservable } from '@app/helpers'
6import { ResultList } from '@shared/models/common'
7import { Video, VideoChannel } from '../shared-main'
8import { VideoPlaylist } from '../shared-video-playlist'
9import { SearchService } from './search.service'
8d9c10bc 10import { AdvancedSearch } from './advanced-search.model'
3da38d6e 11
42b40636 12const debugLogger = debug('peertube:search:FindInBulkService')
3da38d6e
C
13
14type BulkObservables <P extends number | string, R> = {
15 notifier: Subject<P>
2cc276f9 16 result: Observable<{ params: P[], response: R }>
3da38d6e
C
17}
18
19@Injectable()
20export class FindInBulkService {
21
8d9c10bc
C
22 private advancedSearchForBulk: AdvancedSearch
23
3da38d6e
C
24 private getVideoInBulk: BulkObservables<string, ResultList<Video>>
25 private getChannelInBulk: BulkObservables<string, ResultList<VideoChannel>>
26 private getPlaylistInBulk: BulkObservables<string, ResultList<VideoPlaylist>>
27
28 constructor (
afb7d2d5 29 private searchService: SearchService
3da38d6e
C
30 ) {
31 this.getVideoInBulk = this.buildBulkObservableObject(this.getVideosInBulk.bind(this))
32 this.getChannelInBulk = this.buildBulkObservableObject(this.getChannelsInBulk.bind(this))
33 this.getPlaylistInBulk = this.buildBulkObservableObject(this.getPlaylistsInBulk.bind(this))
8d9c10bc
C
34
35 this.advancedSearchForBulk = new AdvancedSearch({ searchTarget: 'local' })
3da38d6e
C
36 }
37
38 getVideo (uuid: string): Observable<Video> {
42b40636 39 debugLogger('Schedule video fetch for uuid %s.', uuid)
3da38d6e
C
40
41 return this.getData({
42 observableObject: this.getVideoInBulk,
d5ae943d 43 finder: v => v.uuid === uuid || v.shortUUID === uuid,
3da38d6e
C
44 param: uuid
45 })
46 }
47
48 getChannel (handle: string): Observable<VideoChannel> {
42b40636 49 debugLogger('Schedule channel fetch for handle %s.', handle)
3da38d6e
C
50
51 return this.getData({
52 observableObject: this.getChannelInBulk,
53 finder: c => c.nameWithHost === handle || c.nameWithHostForced === handle,
54 param: handle
55 })
56 }
57
58 getPlaylist (uuid: string): Observable<VideoPlaylist> {
42b40636 59 debugLogger('Schedule playlist fetch for uuid %s.', uuid)
3da38d6e
C
60
61 return this.getData({
62 observableObject: this.getPlaylistInBulk,
63 finder: p => p.uuid === uuid,
64 param: uuid
65 })
66 }
67
68 private getData <P extends number | string, R> (options: {
69 observableObject: BulkObservables<P, ResultList<R>>
70 param: P
71 finder: (d: R) => boolean
72 }) {
73 const { observableObject, param, finder } = options
74
75 return new Observable<R>(obs => {
76 observableObject.result
77 .pipe(
2cc276f9 78 filter(result => result.params.includes(param)),
ff4de383 79 first(),
2cc276f9 80 map(result => result.response.data),
3da38d6e
C
81 map(data => data.find(finder))
82 )
251ce26d
C
83 .subscribe({
84 next: result => {
85 if (!result) {
86 obs.error(new Error($localize`Element ${param} not found`))
87 return
88 }
89
ff4de383
C
90 obs.next(result)
91 obs.complete()
251ce26d
C
92 },
93
94 error: err => obs.error(err)
3da38d6e
C
95 })
96
97 observableObject.notifier.next(param)
98 })
99 }
100
101 private getVideosInBulk (uuids: string[]) {
42b40636 102 debugLogger('Fetching videos %s.', uuids.join(', '))
3da38d6e 103
8d9c10bc
C
104 return this.searchService.searchVideos({
105 uuids,
106 componentPagination: { itemsPerPage: uuids.length, currentPage: 1 },
107 advancedSearch: this.advancedSearchForBulk
108 })
3da38d6e
C
109 }
110
111 private getChannelsInBulk (handles: string[]) {
42b40636 112 debugLogger('Fetching channels %s.', handles.join(', '))
3da38d6e 113
8d9c10bc
C
114 return this.searchService.searchVideoChannels({
115 handles,
116 componentPagination: { itemsPerPage: handles.length, currentPage: 1 },
117 advancedSearch: this.advancedSearchForBulk
118 })
3da38d6e
C
119 }
120
121 private getPlaylistsInBulk (uuids: string[]) {
42b40636 122 debugLogger('Fetching playlists %s.', uuids.join(', '))
3da38d6e 123
8d9c10bc
C
124 return this.searchService.searchVideoPlaylists({
125 uuids,
126 componentPagination: { itemsPerPage: uuids.length, currentPage: 1 },
127 advancedSearch: this.advancedSearchForBulk
128 })
3da38d6e
C
129 }
130
2cc276f9
C
131 private buildBulkObservableObject <P extends number | string, R> (bulkGet: (params: P[]) => Observable<R>) {
132 const notifier = new Subject<P>()
3da38d6e
C
133
134 return {
135 notifier,
136
137 result: buildBulkObservable({
138 time: 500,
139 bulkGet,
3da38d6e
C
140 notifierObservable: notifier.asObservable()
141 })
142 }
143 }
144}