]>
Commit | Line | Data |
---|---|---|
1 | import * as debug from 'debug' | |
2 | import { Observable, Subject } from 'rxjs' | |
3 | import { 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 | ||
11 | const logger = debug('peertube:search:FindInBulkService') | |
12 | ||
13 | type BulkObservables <P extends number | string, R> = { | |
14 | notifier: Subject<P> | |
15 | result: Observable<R> | |
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 ( | |
26 | private searchService: SearchService | |
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( | |
73 | first(), | |
74 | map(({ data }) => data), | |
75 | map(data => data.find(finder)) | |
76 | ) | |
77 | .subscribe(result => { | |
78 | if (!result) { | |
79 | obs.error(new Error($localize`Element ${param} not found`)) | |
80 | } else { | |
81 | obs.next(result) | |
82 | obs.complete() | |
83 | } | |
84 | }) | |
85 | ||
86 | observableObject.notifier.next(param) | |
87 | }) | |
88 | } | |
89 | ||
90 | private getVideosInBulk (uuids: string[]) { | |
91 | logger('Fetching videos %s.', uuids.join(', ')) | |
92 | ||
93 | return this.searchService.searchVideos({ uuids }) | |
94 | } | |
95 | ||
96 | private getChannelsInBulk (handles: string[]) { | |
97 | logger('Fetching channels %s.', handles.join(', ')) | |
98 | ||
99 | return this.searchService.searchVideoChannels({ handles }) | |
100 | } | |
101 | ||
102 | private getPlaylistsInBulk (uuids: string[]) { | |
103 | logger('Fetching playlists %s.', uuids.join(', ')) | |
104 | ||
105 | return this.searchService.searchVideoPlaylists({ uuids }) | |
106 | } | |
107 | ||
108 | private buildBulkObservableObject <T extends number | string, R> (bulkGet: (params: T[]) => Observable<R>) { | |
109 | const notifier = new Subject<T>() | |
110 | ||
111 | return { | |
112 | notifier, | |
113 | ||
114 | result: buildBulkObservable({ | |
115 | time: 500, | |
116 | bulkGet, | |
117 | notifierObservable: notifier.asObservable() | |
118 | }) | |
119 | } | |
120 | } | |
121 | } |