]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/videos/recommendations/recent-videos-recommendation.service.ts
match margin of abstract-video-list and sub-menu's fixed margin
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / recommendations / recent-videos-recommendation.service.ts
index 0ee34b9cb28a5d254f6c37e68be582bcee2c557c..a1e65c27cddd99d93bd5cda764482b0fdffca53d 100644 (file)
@@ -1,10 +1,10 @@
-import { Inject, Injectable } from '@angular/core'
+import { Injectable } from '@angular/core'
 import { RecommendationService } from '@app/videos/recommendations/recommendations.service'
 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'
 
@@ -13,14 +13,12 @@ import { AdvancedSearch } from '@app/search/advanced-search.model'
  */
 @Injectable()
 export class RecentVideosRecommendationService implements RecommendationService {
-
   readonly pageSize = 5
 
   constructor (
     private videos: VideoService,
     private searchService: SearchService
-  ) {
-  }
+  ) { }
 
   getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
     return this.fetchPage(1, recommendation)
@@ -33,24 +31,26 @@ export class RecentVideosRecommendationService implements RecommendationService
   }
 
   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({ videoPagination: pagination, sort: '-createdAt' })
+                                    .pipe(map(v => v.data))
+
+    if (!recommendation.tags || recommendation.tags.length === 0) return defaultSubscription
+
+    const params = {
+      search: '',
+      componentPagination: pagination,
+      advancedSearch: new AdvancedSearch({ tagsOneOf: recommendation.tags.join(','), sort: '-createdAt' })
     }
-    return this.searchService.searchVideos('',
-      pagination,
-      new AdvancedSearch({ tagsOneOf: recommendation.tags.join(','), sort: '-createdAt' })
-    ).pipe(
-      map(v => v.videos)
-    )
+
+    return this.searchService.searchVideos(params)
+               .pipe(
+                 map(v => v.data),
+                 switchMap(videos => {
+                   if (videos.length <= 1) return defaultSubscription
+
+                   return of(videos)
+                 })
+               )
   }
 }