]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/video/video.service.ts
Replace current state when changing page
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
index 1a0644c3d896294ad2fe9538ea5b072324a99b7d..ef8babd55a9ac9fc0ada06666967b4e76529cc59 100644 (file)
@@ -7,21 +7,25 @@ import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } fr
 import { ResultList } from '../../../../../shared/models/result-list.model'
 import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
 import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
+import { VideoFilter } from '../../../../../shared/models/videos/video-query.type'
+import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
 import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
 import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
+import { environment } from '../../../environments/environment'
+import { ComponentPagination } from '../rest/component-pagination.model'
 import { RestExtractor } from '../rest/rest-extractor.service'
 import { RestService } from '../rest/rest.service'
-import { Search } from '../header/search.model'
 import { UserService } from '../users/user.service'
-import { SortField } from './sort-field.type'
+import { VideoSortField } from './sort-field.type'
 import { VideoDetails } from './video-details.model'
 import { VideoEdit } from './video-edit.model'
-import { VideoPagination } from './video-pagination.model'
 import { Video } from './video.model'
+import { objectToFormData } from '@app/shared/misc/utils'
 
 @Injectable()
 export class VideoService {
-  private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
+  private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
+  private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
 
   constructor (
     private authHttp: HttpClient,
@@ -29,6 +33,10 @@ export class VideoService {
     private restService: RestService
   ) {}
 
+  getVideoViewUrl (uuid: string) {
+    return VideoService.BASE_VIDEO_URL + uuid + '/views'
+  }
+
   getVideo (uuid: string): Observable<VideoDetails> {
     return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
                         .map(videoHash => new VideoDetails(videoHash))
@@ -36,7 +44,7 @@ export class VideoService {
   }
 
   viewVideo (uuid: string): Observable<VideoDetails> {
-    return this.authHttp.post(VideoService.BASE_VIDEO_URL + uuid + '/views', {})
+    return this.authHttp.post(this.getVideoViewUrl(uuid), {})
       .map(this.restExtractor.extractDataBool)
       .catch(this.restExtractor.handleError)
   }
@@ -46,19 +54,26 @@ export class VideoService {
     const licence = video.licence || undefined
     const category = video.category || undefined
     const description = video.description || undefined
+    const support = video.support || undefined
 
     const body: VideoUpdate = {
       name: video.name,
       category,
       licence,
       language,
+      support,
       description,
       privacy: video.privacy,
       tags: video.tags,
-      nsfw: video.nsfw
+      nsfw: video.nsfw,
+      commentsEnabled: video.commentsEnabled,
+      thumbnailfile: video.thumbnailfile,
+      previewfile: video.previewfile
     }
 
-    return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, body)
+    const data = objectToFormData(body)
+
+    return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
                         .map(this.restExtractor.extractDataBool)
                         .catch(this.restExtractor.handleError)
   }
@@ -71,8 +86,8 @@ export class VideoService {
       .catch(this.restExtractor.handleError)
   }
 
-  getMyVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
-    const pagination = this.videoPaginationToRestPagination(videoPagination)
+  getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number}> {
+    const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
 
     let params = new HttpParams()
     params = this.restService.addRestGetParams(params, pagination, sort)
@@ -82,22 +97,74 @@ export class VideoService {
       .catch((res) => this.restExtractor.handleError(res))
   }
 
-  getVideos (videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
-    const pagination = this.videoPaginationToRestPagination(videoPagination)
+  getVideos (
+    videoPagination: ComponentPagination,
+    sort: VideoSortField,
+    filter?: VideoFilter
+  ): Observable<{ videos: Video[], totalVideos: number}> {
+    const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
 
     let params = new HttpParams()
     params = this.restService.addRestGetParams(params, pagination, sort)
 
+    if (filter) {
+      params = params.set('filter', filter)
+    }
+
     return this.authHttp
       .get(VideoService.BASE_VIDEO_URL, { params })
       .map(this.extractVideos)
       .catch((res) => this.restExtractor.handleError(res))
   }
 
-  searchVideos (search: string, videoPagination: VideoPagination, sort: SortField): Observable<{ videos: Video[], totalVideos: number}> {
+  buildBaseFeedUrls (params: HttpParams) {
+    const feeds = [
+      {
+        label: 'rss 2.0',
+        url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
+      },
+      {
+        label: 'atom 1.0',
+        url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
+      },
+      {
+        label: 'json 1.0',
+        url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
+      }
+    ]
+
+    if (params && params.keys().length !== 0) {
+      for (const feed of feeds) {
+        feed.url += '?' + params.toString()
+      }
+    }
+
+    return feeds
+  }
+
+  getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) {
+    let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
+
+    if (filter) params = params.set('filter', filter)
+
+    return this.buildBaseFeedUrls(params)
+  }
+
+  getAccountFeedUrls (accountId: number) {
+    let params = this.restService.addRestGetParams(new HttpParams())
+    params = params.set('accountId', accountId.toString())
+
+    return this.buildBaseFeedUrls(params)
+  }
+
+  searchVideos (
+    search: string,
+    videoPagination: ComponentPagination,
+    sort: VideoSortField
+  ): Observable<{ videos: Video[], totalVideos: number}> {
     const url = VideoService.BASE_VIDEO_URL + 'search'
 
-    const pagination = this.videoPaginationToRestPagination(videoPagination)
+    const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
 
     let params = new HttpParams()
     params = this.restService.addRestGetParams(params, pagination, sort)
@@ -118,7 +185,7 @@ export class VideoService {
 
   loadCompleteDescription (descriptionPath: string) {
     return this.authHttp
-      .get(API_URL + descriptionPath)
+      .get(environment.apiUrl + descriptionPath)
       .map(res => res['description'])
       .catch((res) => this.restExtractor.handleError(res))
   }
@@ -131,6 +198,10 @@ export class VideoService {
     return this.setVideoRate(id, 'dislike')
   }
 
+  unsetVideoLike (id: number) {
+    return this.setVideoRate(id, 'none')
+  }
+
   getUserVideoRating (id: number): Observable<UserVideoRate> {
     const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
 
@@ -139,13 +210,6 @@ export class VideoService {
       .catch(res => this.restExtractor.handleError(res))
   }
 
-  private videoPaginationToRestPagination (videoPagination: VideoPagination) {
-    const start: number = (videoPagination.currentPage - 1) * videoPagination.itemsPerPage
-    const count: number = videoPagination.itemsPerPage
-
-    return { start, count }
-  }
-
   private setVideoRate (id: number, rateType: VideoRateType) {
     const url = VideoService.BASE_VIDEO_URL + id + '/rate'
     const body: UserVideoRateUpdate = {