]>
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 | ||
21 | @Injectable() | |
22 | export class VideoPlaylistService { | |
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> | |
29 | ||
30 | constructor ( | |
31 | private authHttp: HttpClient, | |
32 | private serverService: ServerService, | |
33 | private restExtractor: RestExtractor, | |
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 | } | |
43 | ||
44 | listChannelPlaylists (videoChannel: VideoChannel): Observable<ResultList<VideoPlaylist>> { | |
45 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/video-playlists' | |
46 | ||
47 | return this.authHttp.get<ResultList<VideoPlaylist>>(url) | |
48 | .pipe( | |
49 | switchMap(res => this.extractPlaylists(res)), | |
50 | catchError(err => this.restExtractor.handleError(err)) | |
51 | ) | |
52 | } | |
53 | ||
54 | listAccountPlaylists (account: Account, sort: string): Observable<ResultList<VideoPlaylist>> { | |
55 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-playlists' | |
56 | ||
57 | let params = new HttpParams() | |
58 | params = this.restService.addRestGetParams(params, undefined, sort) | |
59 | ||
60 | return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params }) | |
61 | .pipe( | |
62 | switchMap(res => this.extractPlaylists(res)), | |
63 | catchError(err => this.restExtractor.handleError(err)) | |
64 | ) | |
65 | } | |
66 | ||
67 | getVideoPlaylist (id: string | number) { | |
68 | const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + id | |
69 | ||
70 | return this.authHttp.get<VideoPlaylist>(url) | |
71 | .pipe( | |
72 | switchMap(res => this.extractPlaylist(res)), | |
73 | catchError(err => this.restExtractor.handleError(err)) | |
74 | ) | |
75 | } | |
76 | ||
77 | createVideoPlaylist (body: VideoPlaylistCreate) { | |
78 | const data = objectToFormData(body) | |
79 | ||
80 | return this.authHttp.post<{ videoPlaylist: { id: number } }>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL, data) | |
81 | .pipe( | |
82 | catchError(err => this.restExtractor.handleError(err)) | |
83 | ) | |
84 | } | |
85 | ||
86 | updateVideoPlaylist (videoPlaylist: VideoPlaylist, body: VideoPlaylistUpdate) { | |
87 | const data = objectToFormData(body) | |
88 | ||
89 | return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id, data) | |
90 | .pipe( | |
91 | map(this.restExtractor.extractDataBool), | |
92 | catchError(err => this.restExtractor.handleError(err)) | |
93 | ) | |
94 | } | |
95 | ||
96 | removeVideoPlaylist (videoPlaylist: VideoPlaylist) { | |
97 | return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id) | |
98 | .pipe( | |
99 | map(this.restExtractor.extractDataBool), | |
100 | catchError(err => this.restExtractor.handleError(err)) | |
101 | ) | |
102 | } | |
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 | ||
134 | extractPlaylists (result: ResultList<VideoPlaylistServerModel>) { | |
135 | return this.serverService.localeObservable | |
136 | .pipe( | |
137 | map(translations => { | |
138 | const playlistsJSON = result.data | |
139 | const total = result.total | |
140 | const playlists: VideoPlaylist[] = [] | |
141 | ||
142 | for (const playlistJSON of playlistsJSON) { | |
143 | playlists.push(new VideoPlaylist(playlistJSON, translations)) | |
144 | } | |
145 | ||
146 | return { data: playlists, total } | |
147 | }) | |
148 | ) | |
149 | } | |
150 | ||
151 | extractPlaylist (playlist: VideoPlaylistServerModel) { | |
152 | return this.serverService.localeObservable | |
153 | .pipe(map(translations => new VideoPlaylist(playlist, translations))) | |
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 | } | |
165 | } |