diff options
author | Jorropo <jorropo.pgm@gmail.com> | 2018-09-04 11:01:54 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-09-04 11:01:54 +0200 |
commit | b0c36821d1dcf362f14c99ca3741e7d03aea0a04 (patch) | |
tree | ba2e84ae6f1559f9e3027feab0422e2bf5ad04a8 /client/src/app/videos/recommendations | |
parent | 5cf84858d49f4231cc4efec5e3132f17f65f6cf6 (diff) | |
download | PeerTube-b0c36821d1dcf362f14c99ca3741e7d03aea0a04.tar.gz PeerTube-b0c36821d1dcf362f14c99ca3741e7d03aea0a04.tar.zst PeerTube-b0c36821d1dcf362f14c99ca3741e7d03aea0a04.zip |
Add video recomandation by tags (#1001)
* Recommendation by tags (thx bradsk88)
Thx bradsk88 for the help.
* Prefer jest-preset-angular to skip need for babel config
* Fix jest
Diffstat (limited to 'client/src/app/videos/recommendations')
5 files changed, 43 insertions, 23 deletions
diff --git a/client/src/app/videos/recommendations/recent-videos-recommendation.service.spec.ts b/client/src/app/videos/recommendations/recent-videos-recommendation.service.spec.ts index f9055b82c..698b2e27b 100644 --- a/client/src/app/videos/recommendations/recent-videos-recommendation.service.spec.ts +++ b/client/src/app/videos/recommendations/recent-videos-recommendation.service.spec.ts | |||
@@ -21,7 +21,7 @@ describe('"Recent Videos" Recommender', () => { | |||
21 | { uuid: 'uuid2' } | 21 | { uuid: 'uuid2' } |
22 | ] | 22 | ] |
23 | getVideosMock.mockReturnValueOnce(of({ videos: vids })) | 23 | getVideosMock.mockReturnValueOnce(of({ videos: vids })) |
24 | const result = await service.getRecommendations('uuid1').toPromise() | 24 | const result = await service.getRecommendations({ uuid: 'uuid1' }).toPromise() |
25 | const uuids = result.map(v => v.uuid) | 25 | const uuids = result.map(v => v.uuid) |
26 | expect(uuids).toEqual(['uuid2']) | 26 | expect(uuids).toEqual(['uuid2']) |
27 | done() | 27 | done() |
@@ -36,7 +36,7 @@ describe('"Recent Videos" Recommender', () => { | |||
36 | { uuid: 'uuid7' } | 36 | { uuid: 'uuid7' } |
37 | ] | 37 | ] |
38 | getVideosMock.mockReturnValueOnce(of({ videos: vids })) | 38 | getVideosMock.mockReturnValueOnce(of({ videos: vids })) |
39 | const result = await service.getRecommendations('uuid1').toPromise() | 39 | const result = await service.getRecommendations({ uuid: 'uuid1' }).toPromise() |
40 | expect(result.length).toEqual(5) | 40 | expect(result.length).toEqual(5) |
41 | done() | 41 | done() |
42 | }) | 42 | }) |
@@ -51,12 +51,12 @@ describe('"Recent Videos" Recommender', () => { | |||
51 | ] | 51 | ] |
52 | getVideosMock | 52 | getVideosMock |
53 | .mockReturnValueOnce(of({ videos: vids })) | 53 | .mockReturnValueOnce(of({ videos: vids })) |
54 | const result = await service.getRecommendations('uuid1').toPromise() | 54 | const result = await service.getRecommendations({ uuid: 'uuid1' }).toPromise() |
55 | expect(result.length).toEqual(5) | 55 | expect(result.length).toEqual(5) |
56 | done() | 56 | done() |
57 | }) | 57 | }) |
58 | it('should fetch an extra result in case the given UUID is in the list', async (done) => { | 58 | it('should fetch an extra result in case the given UUID is in the list', async (done) => { |
59 | await service.getRecommendations('uuid1').toPromise() | 59 | await service.getRecommendations({ uuid: 'uuid1' }).toPromise() |
60 | let expectedSize = service.pageSize + 1 | 60 | let expectedSize = service.pageSize + 1 |
61 | let params = { currentPage: jasmine.anything(), itemsPerPage: expectedSize } | 61 | let params = { currentPage: jasmine.anything(), itemsPerPage: expectedSize } |
62 | expect(getVideosMock).toHaveBeenCalledWith(params, jasmine.anything()) | 62 | expect(getVideosMock).toHaveBeenCalledWith(params, jasmine.anything()) |
diff --git a/client/src/app/videos/recommendations/recent-videos-recommendation.service.ts b/client/src/app/videos/recommendations/recent-videos-recommendation.service.ts index 708d67699..4723f7fd0 100644 --- a/client/src/app/videos/recommendations/recent-videos-recommendation.service.ts +++ b/client/src/app/videos/recommendations/recent-videos-recommendation.service.ts | |||
@@ -1,9 +1,12 @@ | |||
1 | import { Inject, Injectable } from '@angular/core' | 1 | import { Inject, Injectable } from '@angular/core' |
2 | import { RecommendationService } from '@app/videos/recommendations/recommendations.service' | 2 | import { RecommendationService } from '@app/videos/recommendations/recommendations.service' |
3 | import { Video } from '@app/shared/video/video.model' | 3 | import { Video } from '@app/shared/video/video.model' |
4 | import { VideoService, VideosProvider } from '@app/shared/video/video.service' | 4 | import { RecommendationInfo } from '@app/shared/video/recommendation-info.model' |
5 | import { VideoService } from '@app/shared/video/video.service' | ||
5 | import { map } from 'rxjs/operators' | 6 | import { map } from 'rxjs/operators' |
6 | import { Observable } from 'rxjs' | 7 | import { Observable } from 'rxjs' |
8 | import { SearchService } from '@app/search/search.service' | ||
9 | import { AdvancedSearch } from '@app/search/advanced-search.model' | ||
7 | 10 | ||
8 | /** | 11 | /** |
9 | * Provides "recommendations" by providing the most recently uploaded videos. | 12 | * Provides "recommendations" by providing the most recently uploaded videos. |
@@ -14,26 +17,40 @@ export class RecentVideosRecommendationService implements RecommendationService | |||
14 | readonly pageSize = 5 | 17 | readonly pageSize = 5 |
15 | 18 | ||
16 | constructor ( | 19 | constructor ( |
17 | @Inject(VideoService) private videos: VideosProvider | 20 | private videos: VideoService, |
21 | private searchService: SearchService | ||
18 | ) { | 22 | ) { |
19 | } | 23 | } |
20 | 24 | ||
21 | getRecommendations (uuid: string): Observable<Video[]> { | 25 | getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> { |
22 | return this.fetchPage(1) | 26 | return this.fetchPage(1, recommendation) |
23 | .pipe( | 27 | .pipe( |
24 | map(vids => { | 28 | map(vids => { |
25 | const otherVideos = vids.filter(v => v.uuid !== uuid) | 29 | const otherVideos = vids.filter(v => v.uuid !== recommendation.uuid) |
26 | return otherVideos.slice(0, this.pageSize) | 30 | return otherVideos.slice(0, this.pageSize) |
27 | }) | 31 | }) |
28 | ) | 32 | ) |
29 | } | 33 | } |
30 | 34 | ||
31 | private fetchPage (page: number): Observable<Video[]> { | 35 | private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> { |
32 | let pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 } | 36 | let pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 } |
33 | return this.videos.getVideos(pagination, '-createdAt') | 37 | if (!recommendation.tags) { |
34 | .pipe( | 38 | return this.videos.getVideos(pagination, '-createdAt') |
35 | map(v => v.videos) | 39 | .pipe( |
36 | ) | 40 | map(v => v.videos) |
41 | ) | ||
42 | } | ||
43 | if (recommendation.tags.length === 0) { | ||
44 | return this.videos.getVideos(pagination, '-createdAt') | ||
45 | .pipe( | ||
46 | map(v => v.videos) | ||
47 | ) | ||
48 | } | ||
49 | return this.searchService.searchVideos('', | ||
50 | pagination, | ||
51 | new AdvancedSearch({ tagsOneOf: recommendation.tags.join(','), sort: '-createdAt' }) | ||
52 | ).pipe( | ||
53 | map(v => v.videos) | ||
54 | ) | ||
37 | } | 55 | } |
38 | |||
39 | } | 56 | } |
diff --git a/client/src/app/videos/recommendations/recommendations.service.ts b/client/src/app/videos/recommendations/recommendations.service.ts index 44cbda9b7..114a808b5 100644 --- a/client/src/app/videos/recommendations/recommendations.service.ts +++ b/client/src/app/videos/recommendations/recommendations.service.ts | |||
@@ -1,8 +1,9 @@ | |||
1 | import { Video } from '@app/shared/video/video.model' | 1 | import { Video } from '@app/shared/video/video.model' |
2 | import { RecommendationInfo } from '@app/shared/video/recommendation-info.model' | ||
2 | import { Observable } from 'rxjs' | 3 | import { Observable } from 'rxjs' |
3 | 4 | ||
4 | export type UUID = string | 5 | export type UUID = string |
5 | 6 | ||
6 | export interface RecommendationService { | 7 | export interface RecommendationService { |
7 | getRecommendations (uuid: UUID): Observable<Video[]> | 8 | getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> |
8 | } | 9 | } |
diff --git a/client/src/app/videos/recommendations/recommended-videos.component.ts b/client/src/app/videos/recommendations/recommended-videos.component.ts index aa4dd0ee2..c6c1d9e5d 100644 --- a/client/src/app/videos/recommendations/recommended-videos.component.ts +++ b/client/src/app/videos/recommendations/recommended-videos.component.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import { Component, Input, OnChanges } from '@angular/core' | 1 | import { Component, Input, OnChanges } from '@angular/core' |
2 | import { Observable } from 'rxjs' | 2 | import { Observable } from 'rxjs' |
3 | import { Video } from '@app/shared/video/video.model' | 3 | import { Video } from '@app/shared/video/video.model' |
4 | import { RecommendationInfo } from '@app/shared/video/recommendation-info.model' | ||
4 | import { RecommendedVideosStore } from '@app/videos/recommendations/recommended-videos.store' | 5 | import { RecommendedVideosStore } from '@app/videos/recommendations/recommended-videos.store' |
5 | import { User } from '@app/shared' | 6 | import { User } from '@app/shared' |
6 | 7 | ||
@@ -9,7 +10,7 @@ import { User } from '@app/shared' | |||
9 | templateUrl: './recommended-videos.component.html' | 10 | templateUrl: './recommended-videos.component.html' |
10 | }) | 11 | }) |
11 | export class RecommendedVideosComponent implements OnChanges { | 12 | export class RecommendedVideosComponent implements OnChanges { |
12 | @Input() inputVideo: Video | 13 | @Input() inputRecommendation: RecommendationInfo |
13 | @Input() user: User | 14 | @Input() user: User |
14 | 15 | ||
15 | readonly hasVideos$: Observable<boolean> | 16 | readonly hasVideos$: Observable<boolean> |
@@ -23,8 +24,8 @@ export class RecommendedVideosComponent implements OnChanges { | |||
23 | } | 24 | } |
24 | 25 | ||
25 | public ngOnChanges (): void { | 26 | public ngOnChanges (): void { |
26 | if (this.inputVideo) { | 27 | if (this.inputRecommendation) { |
27 | this.store.requestNewRecommendations(this.inputVideo.uuid) | 28 | this.store.requestNewRecommendations(this.inputRecommendation) |
28 | } | 29 | } |
29 | } | 30 | } |
30 | 31 | ||
diff --git a/client/src/app/videos/recommendations/recommended-videos.store.ts b/client/src/app/videos/recommendations/recommended-videos.store.ts index 689adeb1f..eb5c9867f 100644 --- a/client/src/app/videos/recommendations/recommended-videos.store.ts +++ b/client/src/app/videos/recommendations/recommended-videos.store.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import { Inject, Injectable } from '@angular/core' | 1 | import { Inject, Injectable } from '@angular/core' |
2 | import { Observable, ReplaySubject } from 'rxjs' | 2 | import { Observable, ReplaySubject } from 'rxjs' |
3 | import { Video } from '@app/shared/video/video.model' | 3 | import { Video } from '@app/shared/video/video.model' |
4 | import { RecommendationInfo } from '@app/shared/video/recommendation-info.model' | ||
4 | import { RecentVideosRecommendationService } from '@app/videos/recommendations/recent-videos-recommendation.service' | 5 | import { RecentVideosRecommendationService } from '@app/videos/recommendations/recent-videos-recommendation.service' |
5 | import { RecommendationService, UUID } from '@app/videos/recommendations/recommendations.service' | 6 | import { RecommendationService, UUID } from '@app/videos/recommendations/recommendations.service' |
6 | import { map, switchMap, take } from 'rxjs/operators' | 7 | import { map, switchMap, take } from 'rxjs/operators' |
@@ -12,13 +13,13 @@ import { map, switchMap, take } from 'rxjs/operators' | |||
12 | export class RecommendedVideosStore { | 13 | export class RecommendedVideosStore { |
13 | public readonly recommendations$: Observable<Video[]> | 14 | public readonly recommendations$: Observable<Video[]> |
14 | public readonly hasRecommendations$: Observable<boolean> | 15 | public readonly hasRecommendations$: Observable<boolean> |
15 | private readonly requestsForLoad$$ = new ReplaySubject<UUID>(1) | 16 | private readonly requestsForLoad$$ = new ReplaySubject<RecommendationInfo>(1) |
16 | 17 | ||
17 | constructor ( | 18 | constructor ( |
18 | @Inject(RecentVideosRecommendationService) private recommendations: RecommendationService | 19 | @Inject(RecentVideosRecommendationService) private recommendations: RecommendationService |
19 | ) { | 20 | ) { |
20 | this.recommendations$ = this.requestsForLoad$$.pipe( | 21 | this.recommendations$ = this.requestsForLoad$$.pipe( |
21 | switchMap(requestedUUID => recommendations.getRecommendations(requestedUUID) | 22 | switchMap(requestedRecommendation => recommendations.getRecommendations(requestedRecommendation) |
22 | .pipe(take(1)) | 23 | .pipe(take(1)) |
23 | )) | 24 | )) |
24 | this.hasRecommendations$ = this.recommendations$.pipe( | 25 | this.hasRecommendations$ = this.recommendations$.pipe( |
@@ -26,7 +27,7 @@ export class RecommendedVideosStore { | |||
26 | ) | 27 | ) |
27 | } | 28 | } |
28 | 29 | ||
29 | requestNewRecommendations (videoUUID: string) { | 30 | requestNewRecommendations (recommend: RecommendationInfo) { |
30 | this.requestsForLoad$$.next(videoUUID) | 31 | this.requestsForLoad$$.next(recommend) |
31 | } | 32 | } |
32 | } | 33 | } |