From 3da38d6e9f8d600476be276666ac7223aa5f172c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 2 Aug 2021 15:29:09 +0200 Subject: Fetch things in bulk for the homepage --- .../shared/shared-search/find-in-bulk.service.ts | 118 +++++++++++++++++++++ client/src/app/shared/shared-search/index.ts | 1 + .../src/app/shared/shared-search/search.service.ts | 18 ++-- .../shared/shared-search/shared-search.module.ts | 2 + 4 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 client/src/app/shared/shared-search/find-in-bulk.service.ts (limited to 'client/src/app/shared/shared-search') diff --git a/client/src/app/shared/shared-search/find-in-bulk.service.ts b/client/src/app/shared/shared-search/find-in-bulk.service.ts new file mode 100644 index 000000000..0383d8648 --- /dev/null +++ b/client/src/app/shared/shared-search/find-in-bulk.service.ts @@ -0,0 +1,118 @@ +import * as debug from 'debug' +import { Observable, Subject } from 'rxjs' +import { map } from 'rxjs/operators' +import { Injectable, NgZone } from '@angular/core' +import { buildBulkObservable } from '@app/helpers' +import { ResultList } from '@shared/models/common' +import { Video, VideoChannel } from '../shared-main' +import { VideoPlaylist } from '../shared-video-playlist' +import { SearchService } from './search.service' + +const logger = debug('peertube:search:FindInBulkService') + +type BulkObservables

= { + notifier: Subject

+ result: Observable +} + +@Injectable() +export class FindInBulkService { + + private getVideoInBulk: BulkObservables> + private getChannelInBulk: BulkObservables> + private getPlaylistInBulk: BulkObservables> + + constructor ( + private searchService: SearchService, + private ngZone: NgZone + ) { + this.getVideoInBulk = this.buildBulkObservableObject(this.getVideosInBulk.bind(this)) + this.getChannelInBulk = this.buildBulkObservableObject(this.getChannelsInBulk.bind(this)) + this.getPlaylistInBulk = this.buildBulkObservableObject(this.getPlaylistsInBulk.bind(this)) + } + + getVideo (uuid: string): Observable

(options: { + observableObject: BulkObservables> + param: P + finder: (d: R) => boolean + }) { + const { observableObject, param, finder } = options + + return new Observable(obs => { + observableObject.result + .pipe( + map(({ data }) => data), + map(data => data.find(finder)) + ) + .subscribe(result => { + obs.next(result) + obs.complete() + }) + + observableObject.notifier.next(param) + }) + } + + private getVideosInBulk (uuids: string[]) { + logger('Fetching videos %s.', uuids.join(', ')) + + return this.searchService.searchVideos({ uuids }) + } + + private getChannelsInBulk (handles: string[]) { + logger('Fetching channels %s.', handles.join(', ')) + + return this.searchService.searchVideoChannels({ handles }) + } + + private getPlaylistsInBulk (uuids: string[]) { + logger('Fetching playlists %s.', uuids.join(', ')) + + return this.searchService.searchVideoPlaylists({ uuids }) + } + + private buildBulkObservableObject (bulkGet: (params: T[]) => Observable) { + const notifier = new Subject() + + return { + notifier, + + result: buildBulkObservable({ + time: 500, + bulkGet, + ngZone: this.ngZone, + notifierObservable: notifier.asObservable() + }) + } + } +} diff --git a/client/src/app/shared/shared-search/index.ts b/client/src/app/shared/shared-search/index.ts index f687f6767..0235893c4 100644 --- a/client/src/app/shared/shared-search/index.ts +++ b/client/src/app/shared/shared-search/index.ts @@ -1,3 +1,4 @@ export * from './advanced-search.model' +export * from './find-in-bulk.service' export * from './search.service' export * from './shared-search.module' diff --git a/client/src/app/shared/shared-search/search.service.ts b/client/src/app/shared/shared-search/search.service.ts index a1603da98..fdfab0e0e 100644 --- a/client/src/app/shared/shared-search/search.service.ts +++ b/client/src/app/shared/shared-search/search.service.ts @@ -32,11 +32,12 @@ export class SearchService { } searchVideos (parameters: { - search: string + search?: string componentPagination?: ComponentPaginationLight advancedSearch?: AdvancedSearch + uuids?: string[] }): Observable> { - const { search, componentPagination, advancedSearch } = parameters + const { search, uuids, componentPagination, advancedSearch } = parameters const url = SearchService.BASE_SEARCH_URL + 'videos' let pagination: RestPagination @@ -49,6 +50,7 @@ export class SearchService { params = this.restService.addRestGetParams(params, pagination) if (search) params = params.append('search', search) + if (uuids) params = this.restService.addArrayParams(params, 'uuids', uuids) if (advancedSearch) { const advancedSearchObject = advancedSearch.toVideosAPIObject() @@ -64,11 +66,12 @@ export class SearchService { } searchVideoChannels (parameters: { - search: string + search?: string advancedSearch?: AdvancedSearch componentPagination?: ComponentPaginationLight + handles?: string[] }): Observable> { - const { search, advancedSearch, componentPagination } = parameters + const { search, advancedSearch, componentPagination, handles } = parameters const url = SearchService.BASE_SEARCH_URL + 'video-channels' @@ -81,6 +84,7 @@ export class SearchService { params = this.restService.addRestGetParams(params, pagination) if (search) params = params.append('search', search) + if (handles) params = this.restService.addArrayParams(params, 'handles', handles) if (advancedSearch) { const advancedSearchObject = advancedSearch.toChannelAPIObject() @@ -96,11 +100,12 @@ export class SearchService { } searchVideoPlaylists (parameters: { - search: string + search?: string advancedSearch?: AdvancedSearch componentPagination?: ComponentPaginationLight + uuids?: string[] }): Observable> { - const { search, advancedSearch, componentPagination } = parameters + const { search, advancedSearch, componentPagination, uuids } = parameters const url = SearchService.BASE_SEARCH_URL + 'video-playlists' @@ -113,6 +118,7 @@ export class SearchService { params = this.restService.addRestGetParams(params, pagination) if (search) params = params.append('search', search) + if (uuids) params = this.restService.addArrayParams(params, 'uuids', uuids) if (advancedSearch) { const advancedSearchObject = advancedSearch.toPlaylistAPIObject() diff --git a/client/src/app/shared/shared-search/shared-search.module.ts b/client/src/app/shared/shared-search/shared-search.module.ts index be4ef5e3f..8b5492400 100644 --- a/client/src/app/shared/shared-search/shared-search.module.ts +++ b/client/src/app/shared/shared-search/shared-search.module.ts @@ -1,6 +1,7 @@ import { NgModule } from '@angular/core' import { SharedMainModule } from '../shared-main' import { SharedVideoPlaylistModule } from '../shared-video-playlist' +import { FindInBulkService } from './find-in-bulk.service' import { SearchService } from './search.service' @NgModule({ @@ -16,6 +17,7 @@ import { SearchService } from './search.service' ], providers: [ + FindInBulkService, SearchService ] }) -- cgit v1.2.3