]>
Commit | Line | Data |
---|---|---|
3da38d6e | 1 | import * as debug from 'debug' |
1378c0d3 | 2 | import { Observable, Subject } from 'rxjs' |
2cc276f9 | 3 | import { filter, first, map } from 'rxjs/operators' |
afb7d2d5 | 4 | import { Injectable } from '@angular/core' |
3da38d6e C |
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 | ||
11 | const logger = debug('peertube:search:FindInBulkService') | |
12 | ||
13 | type BulkObservables <P extends number | string, R> = { | |
14 | notifier: Subject<P> | |
2cc276f9 | 15 | result: Observable<{ params: P[], response: R }> |
3da38d6e C |
16 | } |
17 | ||
18 | @Injectable() | |
19 | export class FindInBulkService { | |
20 | ||
21 | private getVideoInBulk: BulkObservables<string, ResultList<Video>> | |
22 | private getChannelInBulk: BulkObservables<string, ResultList<VideoChannel>> | |
23 | private getPlaylistInBulk: BulkObservables<string, ResultList<VideoPlaylist>> | |
24 | ||
25 | constructor ( | |
afb7d2d5 | 26 | private searchService: SearchService |
3da38d6e C |
27 | ) { |
28 | this.getVideoInBulk = this.buildBulkObservableObject(this.getVideosInBulk.bind(this)) | |
29 | this.getChannelInBulk = this.buildBulkObservableObject(this.getChannelsInBulk.bind(this)) | |
30 | this.getPlaylistInBulk = this.buildBulkObservableObject(this.getPlaylistsInBulk.bind(this)) | |
31 | } | |
32 | ||
33 | getVideo (uuid: string): Observable<Video> { | |
34 | logger('Schedule video fetch for uuid %s.', uuid) | |
35 | ||
36 | return this.getData({ | |
37 | observableObject: this.getVideoInBulk, | |
38 | finder: v => v.uuid === uuid, | |
39 | param: uuid | |
40 | }) | |
41 | } | |
42 | ||
43 | getChannel (handle: string): Observable<VideoChannel> { | |
44 | logger('Schedule channel fetch for handle %s.', handle) | |
45 | ||
46 | return this.getData({ | |
47 | observableObject: this.getChannelInBulk, | |
48 | finder: c => c.nameWithHost === handle || c.nameWithHostForced === handle, | |
49 | param: handle | |
50 | }) | |
51 | } | |
52 | ||
53 | getPlaylist (uuid: string): Observable<VideoPlaylist> { | |
54 | logger('Schedule playlist fetch for uuid %s.', uuid) | |
55 | ||
56 | return this.getData({ | |
57 | observableObject: this.getPlaylistInBulk, | |
58 | finder: p => p.uuid === uuid, | |
59 | param: uuid | |
60 | }) | |
61 | } | |
62 | ||
63 | private getData <P extends number | string, R> (options: { | |
64 | observableObject: BulkObservables<P, ResultList<R>> | |
65 | param: P | |
66 | finder: (d: R) => boolean | |
67 | }) { | |
68 | const { observableObject, param, finder } = options | |
69 | ||
70 | return new Observable<R>(obs => { | |
71 | observableObject.result | |
72 | .pipe( | |
2cc276f9 | 73 | filter(result => result.params.includes(param)), |
ff4de383 | 74 | first(), |
2cc276f9 | 75 | map(result => result.response.data), |
3da38d6e C |
76 | map(data => data.find(finder)) |
77 | ) | |
78 | .subscribe(result => { | |
ff4de383 C |
79 | if (!result) { |
80 | obs.error(new Error($localize`Element ${param} not found`)) | |
81 | } else { | |
82 | obs.next(result) | |
83 | obs.complete() | |
84 | } | |
3da38d6e C |
85 | }) |
86 | ||
87 | observableObject.notifier.next(param) | |
88 | }) | |
89 | } | |
90 | ||
91 | private getVideosInBulk (uuids: string[]) { | |
92 | logger('Fetching videos %s.', uuids.join(', ')) | |
93 | ||
94 | return this.searchService.searchVideos({ uuids }) | |
95 | } | |
96 | ||
97 | private getChannelsInBulk (handles: string[]) { | |
98 | logger('Fetching channels %s.', handles.join(', ')) | |
99 | ||
100 | return this.searchService.searchVideoChannels({ handles }) | |
101 | } | |
102 | ||
103 | private getPlaylistsInBulk (uuids: string[]) { | |
104 | logger('Fetching playlists %s.', uuids.join(', ')) | |
105 | ||
106 | return this.searchService.searchVideoPlaylists({ uuids }) | |
107 | } | |
108 | ||
2cc276f9 C |
109 | private buildBulkObservableObject <P extends number | string, R> (bulkGet: (params: P[]) => Observable<R>) { |
110 | const notifier = new Subject<P>() | |
3da38d6e C |
111 | |
112 | return { | |
113 | notifier, | |
114 | ||
115 | result: buildBulkObservable({ | |
116 | time: 500, | |
117 | bulkGet, | |
3da38d6e C |
118 | notifierObservable: notifier.asObservable() |
119 | }) | |
120 | } | |
121 | } | |
122 | } |