import { Video } from '@app/shared/video/video.model'
import { RecommendationInfo } from '@app/shared/video/recommendation-info.model'
import { VideoService } from '@app/shared/video/video.service'
-import { map } from 'rxjs/operators'
-import { Observable } from 'rxjs'
+import { map, switchMap } from 'rxjs/operators'
+import { Observable, of } from 'rxjs'
import { SearchService } from '@app/search/search.service'
import { AdvancedSearch } from '@app/search/advanced-search.model'
*/
@Injectable()
export class RecentVideosRecommendationService implements RecommendationService {
-
readonly pageSize = 5
constructor (
}
private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
- let pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
- if (!recommendation.tags) {
- return this.videos.getVideos(pagination, '-createdAt')
- .pipe(
- map(v => v.videos)
- )
- }
- if (recommendation.tags.length === 0) {
- return this.videos.getVideos(pagination, '-createdAt')
- .pipe(
- map(v => v.videos)
- )
- }
+ const pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
+ const defaultSubscription = this.videos.getVideos(pagination, '-createdAt')
+ .pipe(map(v => v.videos))
+
+ if (!recommendation.tags || recommendation.tags.length === 0) return defaultSubscription
+
return this.searchService.searchVideos('',
pagination,
new AdvancedSearch({ tagsOneOf: recommendation.tags.join(','), sort: '-createdAt' })
).pipe(
- map(v => v.videos)
+ map(v => v.videos),
+ switchMap(videos => {
+ if (videos.length <= 1) return defaultSubscription
+
+ return of(videos)
+ })
)
}
}
-<div class="other-videos">
+<div *ngIf="hasVideos$ | async" class="other-videos">
<div i18n class="title-page title-page-single">
Other videos
</div>
- <ng-container *ngIf="hasVideos$ | async">
- <div *ngFor="let video of (videos$ | async)">
- <my-video-miniature [video]="video" [user]="user"></my-video-miniature>
- </div>
- </ng-container>
+ <div *ngFor="let video of (videos$ | async)">
+ <my-video-miniature [video]="video" [user]="user"></my-video-miniature>
+ </div>
</div>