]>
Commit | Line | Data |
---|---|---|
1 | import { bufferTime, catchError, filter, first, map, share, switchMap } from 'rxjs/operators' | |
2 | import { Injectable } from '@angular/core' | |
3 | import { Observable, ReplaySubject, Subject } from 'rxjs' | |
4 | import { RestExtractor } from '../rest/rest-extractor.service' | |
5 | import { HttpClient, HttpParams } from '@angular/common/http' | |
6 | import { ResultList, VideoPlaylistElementCreate, VideoPlaylistElementUpdate } from '../../../../../shared' | |
7 | import { environment } from '../../../environments/environment' | |
8 | import { VideoPlaylist as VideoPlaylistServerModel } from '@shared/models/videos/playlist/video-playlist.model' | |
9 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
10 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | |
11 | import { VideoPlaylistCreate } from '@shared/models/videos/playlist/video-playlist-create.model' | |
12 | import { VideoPlaylistUpdate } from '@shared/models/videos/playlist/video-playlist-update.model' | |
13 | import { objectToFormData } from '@app/shared/misc/utils' | |
14 | import { ServerService } from '@app/core' | |
15 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' | |
16 | import { AccountService } from '@app/shared/account/account.service' | |
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' | |
20 | import { VideoPlaylistReorder } from '@shared/models/videos/playlist/video-playlist-reorder.model' | |
21 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
22 | import { VideoPlaylistElement as ServerVideoPlaylistElement } from '@shared/models/videos/playlist/video-playlist-element.model' | |
23 | import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model' | |
24 | ||
25 | @Injectable() | |
26 | export class VideoPlaylistService { | |
27 | static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' | |
28 | static MY_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/users/me/video-playlists/' | |
29 | ||
30 | // Use a replay subject because we "next" a value before subscribing | |
31 | private videoExistsInPlaylistSubject: Subject<number> = new ReplaySubject(1) | |
32 | private readonly videoExistsInPlaylistObservable: Observable<VideoExistInPlaylist> | |
33 | ||
34 | constructor ( | |
35 | private authHttp: HttpClient, | |
36 | private serverService: ServerService, | |
37 | private restExtractor: RestExtractor, | |
38 | private restService: RestService | |
39 | ) { | |
40 | this.videoExistsInPlaylistObservable = this.videoExistsInPlaylistSubject.pipe( | |
41 | bufferTime(500), | |
42 | filter(videoIds => videoIds.length !== 0), | |
43 | switchMap(videoIds => this.doVideosExistInPlaylist(videoIds)), | |
44 | share() | |
45 | ) | |
46 | } | |
47 | ||
48 | listChannelPlaylists (videoChannel: VideoChannel, componentPagination: ComponentPagination): Observable<ResultList<VideoPlaylist>> { | |
49 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/video-playlists' | |
50 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
51 | ||
52 | let params = new HttpParams() | |
53 | params = this.restService.addRestGetParams(params, pagination) | |
54 | ||
55 | return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params }) | |
56 | .pipe( | |
57 | switchMap(res => this.extractPlaylists(res)), | |
58 | catchError(err => this.restExtractor.handleError(err)) | |
59 | ) | |
60 | } | |
61 | ||
62 | listAccountPlaylists (account: Account, componentPagination: ComponentPagination, sort: string): Observable<ResultList<VideoPlaylist>> { | |
63 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-playlists' | |
64 | const pagination = componentPagination | |
65 | ? this.restService.componentPaginationToRestPagination(componentPagination) | |
66 | : undefined | |
67 | ||
68 | let params = new HttpParams() | |
69 | params = this.restService.addRestGetParams(params, pagination, sort) | |
70 | ||
71 | return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params }) | |
72 | .pipe( | |
73 | switchMap(res => this.extractPlaylists(res)), | |
74 | catchError(err => this.restExtractor.handleError(err)) | |
75 | ) | |
76 | } | |
77 | ||
78 | getVideoPlaylist (id: string | number) { | |
79 | const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + id | |
80 | ||
81 | return this.authHttp.get<VideoPlaylist>(url) | |
82 | .pipe( | |
83 | switchMap(res => this.extractPlaylist(res)), | |
84 | catchError(err => this.restExtractor.handleError(err)) | |
85 | ) | |
86 | } | |
87 | ||
88 | createVideoPlaylist (body: VideoPlaylistCreate) { | |
89 | const data = objectToFormData(body) | |
90 | ||
91 | return this.authHttp.post<{ videoPlaylist: { id: number } }>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL, data) | |
92 | .pipe( | |
93 | catchError(err => this.restExtractor.handleError(err)) | |
94 | ) | |
95 | } | |
96 | ||
97 | updateVideoPlaylist (videoPlaylist: VideoPlaylist, body: VideoPlaylistUpdate) { | |
98 | const data = objectToFormData(body) | |
99 | ||
100 | return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id, data) | |
101 | .pipe( | |
102 | map(this.restExtractor.extractDataBool), | |
103 | catchError(err => this.restExtractor.handleError(err)) | |
104 | ) | |
105 | } | |
106 | ||
107 | removeVideoPlaylist (videoPlaylist: VideoPlaylist) { | |
108 | return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id) | |
109 | .pipe( | |
110 | map(this.restExtractor.extractDataBool), | |
111 | catchError(err => this.restExtractor.handleError(err)) | |
112 | ) | |
113 | } | |
114 | ||
115 | addVideoInPlaylist (playlistId: number, body: VideoPlaylistElementCreate) { | |
116 | const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos' | |
117 | ||
118 | return this.authHttp.post<{ videoPlaylistElement: { id: number } }>(url, body) | |
119 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
120 | } | |
121 | ||
122 | updateVideoOfPlaylist (playlistId: number, playlistElementId: number, body: VideoPlaylistElementUpdate) { | |
123 | return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId, body) | |
124 | .pipe( | |
125 | map(this.restExtractor.extractDataBool), | |
126 | catchError(err => this.restExtractor.handleError(err)) | |
127 | ) | |
128 | } | |
129 | ||
130 | removeVideoFromPlaylist (playlistId: number, playlistElementId: number) { | |
131 | return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId) | |
132 | .pipe( | |
133 | map(this.restExtractor.extractDataBool), | |
134 | catchError(err => this.restExtractor.handleError(err)) | |
135 | ) | |
136 | } | |
137 | ||
138 | reorderPlaylist (playlistId: number, oldPosition: number, newPosition: number) { | |
139 | const body: VideoPlaylistReorder = { | |
140 | startPosition: oldPosition, | |
141 | insertAfterPosition: newPosition | |
142 | } | |
143 | ||
144 | return this.authHttp.post(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/reorder', body) | |
145 | .pipe( | |
146 | map(this.restExtractor.extractDataBool), | |
147 | catchError(err => this.restExtractor.handleError(err)) | |
148 | ) | |
149 | } | |
150 | ||
151 | getPlaylistVideos ( | |
152 | videoPlaylistId: number | string, | |
153 | componentPagination: ComponentPagination | |
154 | ): Observable<ResultList<VideoPlaylistElement>> { | |
155 | const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylistId + '/videos' | |
156 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
157 | ||
158 | let params = new HttpParams() | |
159 | params = this.restService.addRestGetParams(params, pagination) | |
160 | ||
161 | return this.authHttp | |
162 | .get<ResultList<ServerVideoPlaylistElement>>(path, { params }) | |
163 | .pipe( | |
164 | switchMap(res => this.extractVideoPlaylistElements(res)), | |
165 | catchError(err => this.restExtractor.handleError(err)) | |
166 | ) | |
167 | } | |
168 | ||
169 | doesVideoExistInPlaylist (videoId: number) { | |
170 | this.videoExistsInPlaylistSubject.next(videoId) | |
171 | ||
172 | return this.videoExistsInPlaylistObservable.pipe(first()) | |
173 | } | |
174 | ||
175 | extractPlaylists (result: ResultList<VideoPlaylistServerModel>) { | |
176 | return this.serverService.getServerLocale() | |
177 | .pipe( | |
178 | map(translations => { | |
179 | const playlistsJSON = result.data | |
180 | const total = result.total | |
181 | const playlists: VideoPlaylist[] = [] | |
182 | ||
183 | for (const playlistJSON of playlistsJSON) { | |
184 | playlists.push(new VideoPlaylist(playlistJSON, translations)) | |
185 | } | |
186 | ||
187 | return { data: playlists, total } | |
188 | }) | |
189 | ) | |
190 | } | |
191 | ||
192 | extractPlaylist (playlist: VideoPlaylistServerModel) { | |
193 | return this.serverService.getServerLocale() | |
194 | .pipe(map(translations => new VideoPlaylist(playlist, translations))) | |
195 | } | |
196 | ||
197 | extractVideoPlaylistElements (result: ResultList<ServerVideoPlaylistElement>) { | |
198 | return this.serverService.getServerLocale() | |
199 | .pipe( | |
200 | map(translations => { | |
201 | const elementsJson = result.data | |
202 | const total = result.total | |
203 | const elements: VideoPlaylistElement[] = [] | |
204 | ||
205 | for (const elementJson of elementsJson) { | |
206 | elements.push(new VideoPlaylistElement(elementJson, translations)) | |
207 | } | |
208 | ||
209 | return { total, data: elements } | |
210 | }) | |
211 | ) | |
212 | } | |
213 | ||
214 | private doVideosExistInPlaylist (videoIds: number[]): Observable<VideoExistInPlaylist> { | |
215 | const url = VideoPlaylistService.MY_VIDEO_PLAYLIST_URL + 'videos-exist' | |
216 | let params = new HttpParams() | |
217 | ||
218 | params = this.restService.addObjectParams(params, { videoIds }) | |
219 | ||
220 | return this.authHttp.get<VideoExistInPlaylist>(url, { params }) | |
221 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
222 | } | |
223 | } |