]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-search/find-in-bulk.service.ts
Support ICU in TS components
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-search / find-in-bulk.service.ts
1 import * as debug from 'debug'
2 import { Observable, Subject } from 'rxjs'
3 import { filter, first, map } from 'rxjs/operators'
4 import { Injectable } from '@angular/core'
5 import { buildBulkObservable } from '@app/helpers'
6 import { ResultList } from '@shared/models/common'
7 import { Video, VideoChannel } from '../shared-main'
8 import { VideoPlaylist } from '../shared-video-playlist'
9 import { SearchService } from './search.service'
10 import { AdvancedSearch } from './advanced-search.model'
11
12 const logger = debug('peertube:search:FindInBulkService')
13
14 type BulkObservables <P extends number | string, R> = {
15 notifier: Subject<P>
16 result: Observable<{ params: P[], response: R }>
17 }
18
19 @Injectable()
20 export class FindInBulkService {
21
22 private advancedSearchForBulk: AdvancedSearch
23
24 private getVideoInBulk: BulkObservables<string, ResultList<Video>>
25 private getChannelInBulk: BulkObservables<string, ResultList<VideoChannel>>
26 private getPlaylistInBulk: BulkObservables<string, ResultList<VideoPlaylist>>
27
28 constructor (
29 private searchService: SearchService
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))
34
35 this.advancedSearchForBulk = new AdvancedSearch({ searchTarget: 'local' })
36 }
37
38 getVideo (uuid: string): Observable<Video> {
39 logger('Schedule video fetch for uuid %s.', uuid)
40
41 return this.getData({
42 observableObject: this.getVideoInBulk,
43 finder: v => v.uuid === uuid,
44 param: uuid
45 })
46 }
47
48 getChannel (handle: string): Observable<VideoChannel> {
49 logger('Schedule channel fetch for handle %s.', handle)
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> {
59 logger('Schedule playlist fetch for uuid %s.', uuid)
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(
78 filter(result => result.params.includes(param)),
79 first(),
80 map(result => result.response.data),
81 map(data => data.find(finder))
82 )
83 .subscribe(result => {
84 if (!result) {
85 obs.error(new Error($localize`Element ${param} not found`))
86 } else {
87 obs.next(result)
88 obs.complete()
89 }
90 })
91
92 observableObject.notifier.next(param)
93 })
94 }
95
96 private getVideosInBulk (uuids: string[]) {
97 logger('Fetching videos %s.', uuids.join(', '))
98
99 return this.searchService.searchVideos({
100 uuids,
101 componentPagination: { itemsPerPage: uuids.length, currentPage: 1 },
102 advancedSearch: this.advancedSearchForBulk
103 })
104 }
105
106 private getChannelsInBulk (handles: string[]) {
107 logger('Fetching channels %s.', handles.join(', '))
108
109 return this.searchService.searchVideoChannels({
110 handles,
111 componentPagination: { itemsPerPage: handles.length, currentPage: 1 },
112 advancedSearch: this.advancedSearchForBulk
113 })
114 }
115
116 private getPlaylistsInBulk (uuids: string[]) {
117 logger('Fetching playlists %s.', uuids.join(', '))
118
119 return this.searchService.searchVideoPlaylists({
120 uuids,
121 componentPagination: { itemsPerPage: uuids.length, currentPage: 1 },
122 advancedSearch: this.advancedSearchForBulk
123 })
124 }
125
126 private buildBulkObservableObject <P extends number | string, R> (bulkGet: (params: P[]) => Observable<R>) {
127 const notifier = new Subject<P>()
128
129 return {
130 notifier,
131
132 result: buildBulkObservable({
133 time: 500,
134 bulkGet,
135 notifierObservable: notifier.asObservable()
136 })
137 }
138 }
139 }