diff options
author | Chocobozzz <me@florianbigard.com> | 2019-03-07 17:06:00 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-03-18 11:17:59 +0100 |
commit | f0a3988066f72a28bb44520af072f18d91d77dde (patch) | |
tree | dfa3a92102557b567530c5dd014c90866621140a /client/src/app/shared/video-playlist/video-playlist.service.ts | |
parent | 830b4faff15fb9c81d88e8e69fcdf94aad32bef8 (diff) | |
download | PeerTube-f0a3988066f72a28bb44520af072f18d91d77dde.tar.gz PeerTube-f0a3988066f72a28bb44520af072f18d91d77dde.tar.zst PeerTube-f0a3988066f72a28bb44520af072f18d91d77dde.zip |
Add to playlist dropdown
Diffstat (limited to 'client/src/app/shared/video-playlist/video-playlist.service.ts')
-rw-r--r-- | client/src/app/shared/video-playlist/video-playlist.service.ts | 77 |
1 files changed, 67 insertions, 10 deletions
diff --git a/client/src/app/shared/video-playlist/video-playlist.service.ts b/client/src/app/shared/video-playlist/video-playlist.service.ts index 8b66e122c..f7b37f83a 100644 --- a/client/src/app/shared/video-playlist/video-playlist.service.ts +++ b/client/src/app/shared/video-playlist/video-playlist.service.ts | |||
@@ -1,9 +1,9 @@ | |||
1 | import { catchError, map, switchMap } from 'rxjs/operators' | 1 | import { bufferTime, catchError, filter, first, map, share, switchMap } from 'rxjs/operators' |
2 | import { Injectable } from '@angular/core' | 2 | import { Injectable } from '@angular/core' |
3 | import { Observable } from 'rxjs' | 3 | import { Observable, ReplaySubject, Subject } from 'rxjs' |
4 | import { RestExtractor } from '../rest/rest-extractor.service' | 4 | import { RestExtractor } from '../rest/rest-extractor.service' |
5 | import { HttpClient } from '@angular/common/http' | 5 | import { HttpClient, HttpParams } from '@angular/common/http' |
6 | import { ResultList } from '../../../../../shared' | 6 | import { ResultList, VideoPlaylistElementCreate, VideoPlaylistElementUpdate } from '../../../../../shared' |
7 | import { environment } from '../../../environments/environment' | 7 | import { environment } from '../../../environments/environment' |
8 | import { VideoPlaylist as VideoPlaylistServerModel } from '@shared/models/videos/playlist/video-playlist.model' | 8 | import { VideoPlaylist as VideoPlaylistServerModel } from '@shared/models/videos/playlist/video-playlist.model' |
9 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | 9 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
@@ -15,16 +15,31 @@ import { ServerService } from '@app/core' | |||
15 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' | 15 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' |
16 | import { AccountService } from '@app/shared/account/account.service' | 16 | import { AccountService } from '@app/shared/account/account.service' |
17 | import { Account } from '@app/shared/account/account.model' | 17 | import { Account } from '@app/shared/account/account.model' |
18 | import { RestService } from '@app/shared/rest' | ||
19 | import { VideoExistInPlaylist } from '@shared/models/videos/playlist/video-exist-in-playlist.model' | ||
18 | 20 | ||
19 | @Injectable() | 21 | @Injectable() |
20 | export class VideoPlaylistService { | 22 | export class VideoPlaylistService { |
21 | static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' | 23 | static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' |
24 | static MY_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/users/me/video-playlists/' | ||
25 | |||
26 | // Use a replay subject because we "next" a value before subscribing | ||
27 | private videoExistsInPlaylistSubject: Subject<number> = new ReplaySubject(1) | ||
28 | private readonly videoExistsInPlaylistObservable: Observable<VideoExistInPlaylist> | ||
22 | 29 | ||
23 | constructor ( | 30 | constructor ( |
24 | private authHttp: HttpClient, | 31 | private authHttp: HttpClient, |
25 | private serverService: ServerService, | 32 | private serverService: ServerService, |
26 | private restExtractor: RestExtractor | 33 | private restExtractor: RestExtractor, |
27 | ) { } | 34 | private restService: RestService |
35 | ) { | ||
36 | this.videoExistsInPlaylistObservable = this.videoExistsInPlaylistSubject.pipe( | ||
37 | bufferTime(500), | ||
38 | filter(videoIds => videoIds.length !== 0), | ||
39 | switchMap(videoIds => this.doVideosExistInPlaylist(videoIds)), | ||
40 | share() | ||
41 | ) | ||
42 | } | ||
28 | 43 | ||
29 | listChannelPlaylists (videoChannel: VideoChannel): Observable<ResultList<VideoPlaylist>> { | 44 | listChannelPlaylists (videoChannel: VideoChannel): Observable<ResultList<VideoPlaylist>> { |
30 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/video-playlists' | 45 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/video-playlists' |
@@ -36,10 +51,13 @@ export class VideoPlaylistService { | |||
36 | ) | 51 | ) |
37 | } | 52 | } |
38 | 53 | ||
39 | listAccountPlaylists (account: Account): Observable<ResultList<VideoPlaylist>> { | 54 | listAccountPlaylists (account: Account, sort: string): Observable<ResultList<VideoPlaylist>> { |
40 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-playlists' | 55 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-playlists' |
41 | 56 | ||
42 | return this.authHttp.get<ResultList<VideoPlaylist>>(url) | 57 | let params = new HttpParams() |
58 | params = this.restService.addRestGetParams(params, undefined, sort) | ||
59 | |||
60 | return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params }) | ||
43 | .pipe( | 61 | .pipe( |
44 | switchMap(res => this.extractPlaylists(res)), | 62 | switchMap(res => this.extractPlaylists(res)), |
45 | catchError(err => this.restExtractor.handleError(err)) | 63 | catchError(err => this.restExtractor.handleError(err)) |
@@ -59,9 +77,8 @@ export class VideoPlaylistService { | |||
59 | createVideoPlaylist (body: VideoPlaylistCreate) { | 77 | createVideoPlaylist (body: VideoPlaylistCreate) { |
60 | const data = objectToFormData(body) | 78 | const data = objectToFormData(body) |
61 | 79 | ||
62 | return this.authHttp.post(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL, data) | 80 | return this.authHttp.post<{ videoPlaylist: { id: number } }>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL, data) |
63 | .pipe( | 81 | .pipe( |
64 | map(this.restExtractor.extractDataBool), | ||
65 | catchError(err => this.restExtractor.handleError(err)) | 82 | catchError(err => this.restExtractor.handleError(err)) |
66 | ) | 83 | ) |
67 | } | 84 | } |
@@ -84,6 +101,36 @@ export class VideoPlaylistService { | |||
84 | ) | 101 | ) |
85 | } | 102 | } |
86 | 103 | ||
104 | addVideoInPlaylist (playlistId: number, body: VideoPlaylistElementCreate) { | ||
105 | return this.authHttp.post(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos', body) | ||
106 | .pipe( | ||
107 | map(this.restExtractor.extractDataBool), | ||
108 | catchError(err => this.restExtractor.handleError(err)) | ||
109 | ) | ||
110 | } | ||
111 | |||
112 | updateVideoOfPlaylist (playlistId: number, videoId: number, body: VideoPlaylistElementUpdate) { | ||
113 | return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + videoId, body) | ||
114 | .pipe( | ||
115 | map(this.restExtractor.extractDataBool), | ||
116 | catchError(err => this.restExtractor.handleError(err)) | ||
117 | ) | ||
118 | } | ||
119 | |||
120 | removeVideoFromPlaylist (playlistId: number, videoId: number) { | ||
121 | return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + videoId) | ||
122 | .pipe( | ||
123 | map(this.restExtractor.extractDataBool), | ||
124 | catchError(err => this.restExtractor.handleError(err)) | ||
125 | ) | ||
126 | } | ||
127 | |||
128 | doesVideoExistInPlaylist (videoId: number) { | ||
129 | this.videoExistsInPlaylistSubject.next(videoId) | ||
130 | |||
131 | return this.videoExistsInPlaylistObservable.pipe(first()) | ||
132 | } | ||
133 | |||
87 | extractPlaylists (result: ResultList<VideoPlaylistServerModel>) { | 134 | extractPlaylists (result: ResultList<VideoPlaylistServerModel>) { |
88 | return this.serverService.localeObservable | 135 | return this.serverService.localeObservable |
89 | .pipe( | 136 | .pipe( |
@@ -105,4 +152,14 @@ export class VideoPlaylistService { | |||
105 | return this.serverService.localeObservable | 152 | return this.serverService.localeObservable |
106 | .pipe(map(translations => new VideoPlaylist(playlist, translations))) | 153 | .pipe(map(translations => new VideoPlaylist(playlist, translations))) |
107 | } | 154 | } |
155 | |||
156 | private doVideosExistInPlaylist (videoIds: number[]): Observable<VideoExistInPlaylist> { | ||
157 | const url = VideoPlaylistService.MY_VIDEO_PLAYLIST_URL + 'videos-exist' | ||
158 | let params = new HttpParams() | ||
159 | |||
160 | params = this.restService.addObjectParams(params, { videoIds }) | ||
161 | |||
162 | return this.authHttp.get<VideoExistInPlaylist>(url, { params }) | ||
163 | .pipe(catchError(err => this.restExtractor.handleError(err))) | ||
164 | } | ||
108 | } | 165 | } |