From 37a44fc915eef2140e22ceb96aba6b6eb2509007 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 17 Jun 2021 16:02:38 +0200 Subject: Add ability to search playlists --- .../my-video-playlist-elements.component.ts | 2 +- .../src/app/+search/channel-lazy-load.resolver.ts | 35 ----- client/src/app/+search/search-routing.module.ts | 10 +- client/src/app/+search/search.component.html | 9 +- client/src/app/+search/search.component.ts | 146 ++++++++++++--------- client/src/app/+search/search.module.ts | 10 +- .../+search/shared/abstract-lazy-load.resolver.ts | 34 +++++ .../+search/shared/channel-lazy-load.resolver.ts | 24 ++++ client/src/app/+search/shared/index.ts | 4 + .../+search/shared/playlist-lazy-load.resolver.ts | 24 ++++ .../app/+search/shared/video-lazy-load.resolver.ts | 24 ++++ client/src/app/+search/video-lazy-load.resolver.ts | 35 ----- .../src/app/header/search-typeahead.component.html | 2 +- client/src/app/shared/shared-main/angular/index.ts | 1 + .../shared/shared-main/angular/link.component.html | 11 ++ .../shared/shared-main/angular/link.component.scss | 7 + .../shared/shared-main/angular/link.component.ts | 17 +++ .../app/shared/shared-main/shared-main.module.ts | 7 +- .../src/app/shared/shared-search/search.service.ts | 44 ++++++- .../video-miniature.component.html | 13 +- .../video-miniature.component.ts | 5 +- .../video-playlist-miniature.component.html | 15 ++- .../video-playlist-miniature.component.scss | 3 + .../video-playlist-miniature.component.ts | 47 ++++++- .../shared-video-playlist/video-playlist.model.ts | 31 ++--- client/src/sass/include/_mixins.scss | 1 + client/src/types/link.type.ts | 1 + 27 files changed, 373 insertions(+), 189 deletions(-) delete mode 100644 client/src/app/+search/channel-lazy-load.resolver.ts create mode 100644 client/src/app/+search/shared/abstract-lazy-load.resolver.ts create mode 100644 client/src/app/+search/shared/channel-lazy-load.resolver.ts create mode 100644 client/src/app/+search/shared/index.ts create mode 100644 client/src/app/+search/shared/playlist-lazy-load.resolver.ts create mode 100644 client/src/app/+search/shared/video-lazy-load.resolver.ts delete mode 100644 client/src/app/+search/video-lazy-load.resolver.ts create mode 100644 client/src/app/shared/shared-main/angular/link.component.html create mode 100644 client/src/app/shared/shared-main/angular/link.component.scss create mode 100644 client/src/app/shared/shared-main/angular/link.component.ts create mode 100644 client/src/types/link.type.ts (limited to 'client') diff --git a/client/src/app/+my-library/my-video-playlists/my-video-playlist-elements.component.ts b/client/src/app/+my-library/my-video-playlists/my-video-playlist-elements.component.ts index a8fdf6e29..86fe70710 100644 --- a/client/src/app/+my-library/my-video-playlists/my-video-playlist-elements.component.ts +++ b/client/src/app/+my-library/my-video-playlists/my-video-playlist-elements.component.ts @@ -187,7 +187,7 @@ export class MyVideoPlaylistElementsComponent implements OnInit, OnDestroy { // Reload playlist thumbnail if the first element changed const newFirst = this.findFirst() if (oldFirst && newFirst && oldFirst.id !== newFirst.id) { - this.playlist.refreshThumbnail() + this.loadPlaylistInfo() } } diff --git a/client/src/app/+search/channel-lazy-load.resolver.ts b/client/src/app/+search/channel-lazy-load.resolver.ts deleted file mode 100644 index d9f7ec901..000000000 --- a/client/src/app/+search/channel-lazy-load.resolver.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { map } from 'rxjs/operators' -import { Injectable } from '@angular/core' -import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router' -import { SearchService } from '@app/shared/shared-search' - -@Injectable() -export class ChannelLazyLoadResolver implements Resolve { - constructor ( - private router: Router, - private searchService: SearchService - ) { } - - resolve (route: ActivatedRouteSnapshot) { - const url = route.params.url - - if (!url) { - console.error('Could not find url param.', { params: route.params }) - return this.router.navigateByUrl('/404') - } - - return this.searchService.searchVideoChannels({ search: url }) - .pipe( - map(result => { - if (result.data.length !== 1) { - console.error('Cannot find result for this URL') - return this.router.navigateByUrl('/404') - } - - const channel = result.data[0] - - return this.router.navigateByUrl('/video-channels/' + channel.nameWithHost) - }) - ) - } -} diff --git a/client/src/app/+search/search-routing.module.ts b/client/src/app/+search/search-routing.module.ts index 0d778af0d..5d00aae13 100644 --- a/client/src/app/+search/search-routing.module.ts +++ b/client/src/app/+search/search-routing.module.ts @@ -1,8 +1,7 @@ import { NgModule } from '@angular/core' import { RouterModule, Routes } from '@angular/router' -import { ChannelLazyLoadResolver } from './channel-lazy-load.resolver' import { SearchComponent } from './search.component' -import { VideoLazyLoadResolver } from './video-lazy-load.resolver' +import { ChannelLazyLoadResolver, PlaylistLazyLoadResolver, VideoLazyLoadResolver } from './shared' const searchRoutes: Routes = [ { @@ -27,6 +26,13 @@ const searchRoutes: Routes = [ resolve: { data: ChannelLazyLoadResolver } + }, + { + path: 'lazy-load-playlist', + component: SearchComponent, + resolve: { + data: PlaylistLazyLoadResolver + } } ] diff --git a/client/src/app/+search/search.component.html b/client/src/app/+search/search.component.html index 130be75fc..b28abca6a 100644 --- a/client/src/app/+search/search.component.html +++ b/client/src/app/+search/search.component.html @@ -59,10 +59,17 @@
+ +
+ +
diff --git a/client/src/app/+search/search.component.ts b/client/src/app/+search/search.component.ts index a4ab7a5b1..fdf9b7cc0 100644 --- a/client/src/app/+search/search.component.ts +++ b/client/src/app/+search/search.component.ts @@ -1,11 +1,13 @@ import { forkJoin, of, Subscription } from 'rxjs' +import { LinkType } from 'src/types/link.type' import { Component, OnDestroy, OnInit } from '@angular/core' import { ActivatedRoute, Router } from '@angular/router' -import { AuthService, ComponentPagination, HooksService, MetaService, Notifier, ServerService, User, UserService } from '@app/core' +import { AuthService, HooksService, MetaService, Notifier, ServerService, User, UserService } from '@app/core' import { immutableAssign } from '@app/helpers' import { Video, VideoChannel } from '@app/shared/shared-main' import { AdvancedSearch, SearchService } from '@app/shared/shared-search' -import { MiniatureDisplayOptions, VideoLinkType } from '@app/shared/shared-video-miniature' +import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature' +import { VideoPlaylist } from '@app/shared/shared-video-playlist' import { HTMLServerConfig, SearchTargetType } from '@shared/models' @Component({ @@ -16,10 +18,9 @@ import { HTMLServerConfig, SearchTargetType } from '@shared/models' export class SearchComponent implements OnInit, OnDestroy { results: (Video | VideoChannel)[] = [] - pagination: ComponentPagination = { + pagination = { currentPage: 1, - itemsPerPage: 10, // Only for videos, use another variable for channels - totalItems: null + totalItems: null as number } advancedSearch: AdvancedSearch = new AdvancedSearch() isSearchFilterCollapsed = true @@ -45,6 +46,11 @@ export class SearchComponent implements OnInit, OnDestroy { private firstSearch = true private channelsPerPage = 2 + private playlistsPerPage = 2 + private videosPerPage = 10 + + private hasMoreResults = true + private isSearching = false private lastSearchTarget: SearchTargetType @@ -104,77 +110,62 @@ export class SearchComponent implements OnInit, OnDestroy { if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() } - isVideoChannel (d: VideoChannel | Video): d is VideoChannel { + isVideoChannel (d: VideoChannel | Video | VideoPlaylist): d is VideoChannel { return d instanceof VideoChannel } - isVideo (v: VideoChannel | Video): v is Video { + isVideo (v: VideoChannel | Video | VideoPlaylist): v is Video { return v instanceof Video } + isPlaylist (v: VideoChannel | Video | VideoPlaylist): v is VideoPlaylist { + return v instanceof VideoPlaylist + } + isUserLoggedIn () { return this.authService.isLoggedIn() } - getVideoLinkType (): VideoLinkType { - if (this.advancedSearch.searchTarget === 'search-index') { - const remoteUriConfig = this.serverConfig.search.remoteUri + search () { + this.isSearching = true - // Redirect on the external instance if not allowed to fetch remote data - if ((!this.isUserLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users) { - return 'external' + forkJoin([ + this.getVideoChannelObs(), + this.getVideoPlaylistObs(), + this.getVideosObs() + ]).subscribe(results => { + for (const result of results) { + this.results = this.results.concat(result.data) } - return 'lazy-load' - } + this.pagination.totalItems = results.reduce((p, r) => p += r.total, 0) + this.lastSearchTarget = this.advancedSearch.searchTarget - return 'internal' - } + this.hasMoreResults = this.results.length < this.pagination.totalItems + }, - search () { - forkJoin([ - this.getVideosObs(), - this.getVideoChannelObs() - ]).subscribe( - ([videosResult, videoChannelsResult]) => { - this.results = this.results - .concat(videoChannelsResult.data) - .concat(videosResult.data) - - this.pagination.totalItems = videosResult.total + videoChannelsResult.total - this.lastSearchTarget = this.advancedSearch.searchTarget - - // Focus on channels if there are no enough videos - if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) { - this.resetPagination() - this.firstSearch = false - - this.channelsPerPage = 10 - this.search() - } - - this.firstSearch = false - }, + err => { + if (this.advancedSearch.searchTarget !== 'search-index') { + this.notifier.error(err.message) + return + } - err => { - if (this.advancedSearch.searchTarget !== 'search-index') { - this.notifier.error(err.message) - return - } + this.notifier.error( + $localize`Search index is unavailable. Retrying with instance results instead.`, + $localize`Search error` + ) + this.advancedSearch.searchTarget = 'local' + this.search() + }, - this.notifier.error( - $localize`Search index is unavailable. Retrying with instance results instead.`, - $localize`Search error` - ) - this.advancedSearch.searchTarget = 'local' - this.search() - } - ) + () => { + this.isSearching = false + }) } onNearOfBottom () { // Last page - if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return + if (!this.hasMoreResults || this.isSearching) return this.pagination.currentPage += 1 this.search() @@ -190,18 +181,33 @@ export class SearchComponent implements OnInit, OnDestroy { return this.advancedSearch.size() } - // Add VideoChannel for typings, but the template already checks "video" argument is a video - removeVideoFromArray (video: Video | VideoChannel) { + // Add VideoChannel/VideoPlaylist for typings, but the template already checks "video" argument is a video + removeVideoFromArray (video: Video | VideoChannel | VideoPlaylist) { this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id) } + getLinkType (): LinkType { + if (this.advancedSearch.searchTarget === 'search-index') { + const remoteUriConfig = this.serverConfig.search.remoteUri + + // Redirect on the external instance if not allowed to fetch remote data + if ((!this.isUserLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users) { + return 'external' + } + + return 'lazy-load' + } + + return 'internal' + } + isExternalChannelUrl () { - return this.getVideoLinkType() === 'external' + return this.getLinkType() === 'external' } getExternalChannelUrl (channel: VideoChannel) { // Same algorithm than videos - if (this.getVideoLinkType() === 'external') { + if (this.getLinkType() === 'external') { return channel.url } @@ -210,7 +216,7 @@ export class SearchComponent implements OnInit, OnDestroy { } getInternalChannelUrl (channel: VideoChannel) { - const linkType = this.getVideoLinkType() + const linkType = this.getLinkType() if (linkType === 'internal') { return [ '/c', channel.nameWithHost ] @@ -256,7 +262,7 @@ export class SearchComponent implements OnInit, OnDestroy { private getVideosObs () { const params = { search: this.currentSearch, - componentPagination: this.pagination, + componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.videosPerPage }), advancedSearch: this.advancedSearch } @@ -287,6 +293,24 @@ export class SearchComponent implements OnInit, OnDestroy { ) } + private getVideoPlaylistObs () { + if (!this.currentSearch) return of({ data: [], total: 0 }) + + const params = { + search: this.currentSearch, + componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.playlistsPerPage }), + searchTarget: this.advancedSearch.searchTarget + } + + return this.hooks.wrapObsFun( + this.searchService.searchVideoPlaylists.bind(this.searchService), + params, + 'search', + 'filter:api.search.video-playlists.list.params', + 'filter:api.search.video-playlists.list.result' + ) + } + private getDefaultSearchTarget (): SearchTargetType { const searchIndexConfig = this.serverConfig.search.searchIndex diff --git a/client/src/app/+search/search.module.ts b/client/src/app/+search/search.module.ts index 390833abc..26f1523fd 100644 --- a/client/src/app/+search/search.module.ts +++ b/client/src/app/+search/search.module.ts @@ -5,12 +5,12 @@ import { SharedMainModule } from '@app/shared/shared-main' import { SharedSearchModule } from '@app/shared/shared-search' import { SharedUserSubscriptionModule } from '@app/shared/shared-user-subscription' import { SharedVideoMiniatureModule } from '@app/shared/shared-video-miniature' +import { SharedVideoPlaylistModule } from '@app/shared/shared-video-playlist' import { SearchService } from '../shared/shared-search/search.service' -import { ChannelLazyLoadResolver } from './channel-lazy-load.resolver' import { SearchFiltersComponent } from './search-filters.component' import { SearchRoutingModule } from './search-routing.module' import { SearchComponent } from './search.component' -import { VideoLazyLoadResolver } from './video-lazy-load.resolver' +import { ChannelLazyLoadResolver, PlaylistLazyLoadResolver, VideoLazyLoadResolver } from './shared' @NgModule({ imports: [ @@ -21,7 +21,8 @@ import { VideoLazyLoadResolver } from './video-lazy-load.resolver' SharedFormModule, SharedActorImageModule, SharedUserSubscriptionModule, - SharedVideoMiniatureModule + SharedVideoMiniatureModule, + SharedVideoPlaylistModule ], declarations: [ @@ -36,7 +37,8 @@ import { VideoLazyLoadResolver } from './video-lazy-load.resolver' providers: [ SearchService, VideoLazyLoadResolver, - ChannelLazyLoadResolver + ChannelLazyLoadResolver, + PlaylistLazyLoadResolver ] }) export class SearchModule { } diff --git a/client/src/app/+search/shared/abstract-lazy-load.resolver.ts b/client/src/app/+search/shared/abstract-lazy-load.resolver.ts new file mode 100644 index 000000000..31240f451 --- /dev/null +++ b/client/src/app/+search/shared/abstract-lazy-load.resolver.ts @@ -0,0 +1,34 @@ +import { Observable } from 'rxjs' +import { map } from 'rxjs/operators' +import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router' +import { ResultList } from '@shared/models/result-list.model' + +export abstract class AbstractLazyLoadResolver implements Resolve { + protected router: Router + + resolve (route: ActivatedRouteSnapshot) { + const url = route.params.url + + if (!url) { + console.error('Could not find url param.', { params: route.params }) + return this.router.navigateByUrl('/404') + } + + return this.finder(url) + .pipe( + map(result => { + if (result.data.length !== 1) { + console.error('Cannot find result for this URL') + return this.router.navigateByUrl('/404') + } + + const redirectUrl = this.buildUrl(result.data[0]) + + return this.router.navigateByUrl(redirectUrl) + }) + ) + } + + protected abstract finder (url: string): Observable> + protected abstract buildUrl (e: T): string +} diff --git a/client/src/app/+search/shared/channel-lazy-load.resolver.ts b/client/src/app/+search/shared/channel-lazy-load.resolver.ts new file mode 100644 index 000000000..5e010f795 --- /dev/null +++ b/client/src/app/+search/shared/channel-lazy-load.resolver.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core' +import { Router } from '@angular/router' +import { VideoChannel } from '@app/shared/shared-main' +import { SearchService } from '@app/shared/shared-search' +import { AbstractLazyLoadResolver } from './abstract-lazy-load.resolver' + +@Injectable() +export class ChannelLazyLoadResolver extends AbstractLazyLoadResolver { + + constructor ( + protected router: Router, + private searchService: SearchService + ) { + super() + } + + protected finder (url: string) { + return this.searchService.searchVideoChannels({ search: url }) + } + + protected buildUrl (channel: VideoChannel) { + return '/video-channels/' + channel.nameWithHost + } +} diff --git a/client/src/app/+search/shared/index.ts b/client/src/app/+search/shared/index.ts new file mode 100644 index 000000000..1e68989ae --- /dev/null +++ b/client/src/app/+search/shared/index.ts @@ -0,0 +1,4 @@ +export * from './abstract-lazy-load.resolver' +export * from './channel-lazy-load.resolver' +export * from './playlist-lazy-load.resolver' +export * from './video-lazy-load.resolver' diff --git a/client/src/app/+search/shared/playlist-lazy-load.resolver.ts b/client/src/app/+search/shared/playlist-lazy-load.resolver.ts new file mode 100644 index 000000000..14ae798df --- /dev/null +++ b/client/src/app/+search/shared/playlist-lazy-load.resolver.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core' +import { Router } from '@angular/router' +import { SearchService } from '@app/shared/shared-search' +import { VideoPlaylist } from '@app/shared/shared-video-playlist' +import { AbstractLazyLoadResolver } from './abstract-lazy-load.resolver' + +@Injectable() +export class PlaylistLazyLoadResolver extends AbstractLazyLoadResolver { + + constructor ( + protected router: Router, + private searchService: SearchService + ) { + super() + } + + protected finder (url: string) { + return this.searchService.searchVideoPlaylists({ search: url }) + } + + protected buildUrl (playlist: VideoPlaylist) { + return '/w/p/' + playlist.uuid + } +} diff --git a/client/src/app/+search/shared/video-lazy-load.resolver.ts b/client/src/app/+search/shared/video-lazy-load.resolver.ts new file mode 100644 index 000000000..12b5b2e82 --- /dev/null +++ b/client/src/app/+search/shared/video-lazy-load.resolver.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core' +import { Router } from '@angular/router' +import { Video } from '@app/shared/shared-main' +import { SearchService } from '@app/shared/shared-search' +import { AbstractLazyLoadResolver } from './abstract-lazy-load.resolver' + +@Injectable() +export class VideoLazyLoadResolver extends AbstractLazyLoadResolver