]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/video-channel/video-channel.service.ts
Skip videos count on client if we don't use it
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-channel / video-channel.service.ts
index 3533a0e9cb28660cbea5e2286a316d4eb2c3e86e..adb4f48192e426c3dfb9c428e4cd1489d13d3dd6 100644 (file)
@@ -1,18 +1,17 @@
+import { catchError, map, tap } from 'rxjs/operators'
 import { Injectable } from '@angular/core'
-import 'rxjs/add/operator/catch'
-import 'rxjs/add/operator/map'
-import { Observable } from 'rxjs/Observable'
+import { Observable, ReplaySubject } from 'rxjs'
 import { RestExtractor } from '../rest/rest-extractor.service'
-import { RestService } from '../rest/rest.service'
-import { HttpClient } from '@angular/common/http'
+import { HttpClient, HttpParams } from '@angular/common/http'
 import { VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '../../../../../shared/models/videos'
 import { AccountService } from '../account/account.service'
 import { ResultList } from '../../../../../shared'
 import { VideoChannel } from './video-channel.model'
-import { ReplaySubject } from 'rxjs/ReplaySubject'
 import { environment } from '../../../environments/environment'
-import { UserService } from '@app/+admin/users/shared/user.service'
-import { User } from '@app/shared'
+import { Account } from '@app/shared/account/account.model'
+import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
+import { ComponentPaginationLight } from '@app/shared/rest/component-pagination.model'
+import { RestService } from '@app/shared/rest'
 
 @Injectable()
 export class VideoChannelService {
@@ -20,50 +19,75 @@ export class VideoChannelService {
 
   videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
 
+  static extractVideoChannels (result: ResultList<VideoChannelServer>) {
+    const videoChannels: VideoChannel[] = []
+
+    for (const videoChannelJSON of result.data) {
+      videoChannels.push(new VideoChannel(videoChannelJSON))
+    }
+
+    return { data: videoChannels, total: result.total }
+  }
+
   constructor (
     private authHttp: HttpClient,
-    private restExtractor: RestExtractor,
-    private restService: RestService
-  ) {}
+    private restService: RestService,
+    private restExtractor: RestExtractor
+  ) { }
 
-  getVideoChannel (videoChannelUUID: string) {
-    return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID)
-               .map(videoChannelHash => new VideoChannel(videoChannelHash))
-               .do(videoChannel => this.videoChannelLoaded.next(videoChannel))
-               .catch((res) => this.restExtractor.handleError(res))
+  getVideoChannel (videoChannelName: string) {
+    return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName)
+               .pipe(
+                 map(videoChannelHash => new VideoChannel(videoChannelHash)),
+                 tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
   }
 
-  listAccountVideoChannels (accountId: number): Observable<ResultList<VideoChannel>> {
-    return this.authHttp.get<ResultList<VideoChannelServer>>(AccountService.BASE_ACCOUNT_URL + accountId + '/video-channels')
-               .map(res => this.extractVideoChannels(res))
-               .catch((res) => this.restExtractor.handleError(res))
+  listAccountVideoChannels (account: Account, componentPagination?: ComponentPaginationLight): Observable<ResultList<VideoChannel>> {
+    const pagination = componentPagination
+      ? this.restService.componentPaginationToRestPagination(componentPagination)
+      : { start: 0, count: 20 }
+
+    let params = new HttpParams()
+    params = this.restService.addRestGetParams(params, pagination)
+
+    const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
+    return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
+               .pipe(
+                 map(res => VideoChannelService.extractVideoChannels(res)),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
   }
 
   createVideoChannel (videoChannel: VideoChannelCreate) {
     return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
-               .map(this.restExtractor.extractDataBool)
-               .catch(err => this.restExtractor.handleError(err))
+               .pipe(
+                 map(this.restExtractor.extractDataBool),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
   }
 
-  updateVideoChannel (videoChannelUUID: string, videoChannel: VideoChannelUpdate) {
-    return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID, videoChannel)
-               .map(this.restExtractor.extractDataBool)
-               .catch(err => this.restExtractor.handleError(err))
+  updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
+    return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
+               .pipe(
+                 map(this.restExtractor.extractDataBool),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
   }
 
-  removeVideoChannel (videoChannel: VideoChannel) {
-    return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid)
-               .map(this.restExtractor.extractDataBool)
-               .catch(err => this.restExtractor.handleError(err))
-  }
+  changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) {
+    const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick'
 
-  private extractVideoChannels (result: ResultList<VideoChannelServer>) {
-    const videoChannels: VideoChannel[] = []
-
-    for (const videoChannelJSON of result.data) {
-      videoChannels.push(new VideoChannel(videoChannelJSON))
-    }
+    return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
+  }
 
-    return { data: videoChannels, total: result.total }
+  removeVideoChannel (videoChannel: VideoChannel) {
+    return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
+               .pipe(
+                 map(this.restExtractor.extractDataBool),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
   }
 }