]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/videos/+video-watch/comment/video-comment.service.ts
Better typings
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.service.ts
index 470af12305cc3604091b07e68caadf04bb589ece..921447d5be264e3369273e9d6cd03b94464ef3c7 100644 (file)
@@ -1,11 +1,9 @@
+import { catchError, map } from 'rxjs/operators'
 import { HttpClient, HttpParams } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { lineFeedToHtml } from '@app/shared/misc/utils'
-import { MarkdownService } from '@app/videos/shared'
-import 'rxjs/add/operator/catch'
-import 'rxjs/add/operator/map'
-import { Observable } from 'rxjs/Observable'
-import { ResultList } from '../../../../../../shared/models'
+import { Observable } from 'rxjs'
+import { ResultList, FeedFormat } from '../../../../../../shared/models'
 import {
   VideoComment as VideoCommentServerModel,
   VideoCommentCreate,
@@ -14,12 +12,13 @@ import {
 import { environment } from '../../../../environments/environment'
 import { RestExtractor, RestService } from '../../../shared/rest'
 import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
-import { SortField } from '../../../shared/video/sort-field.type'
+import { VideoSortField } from '../../../shared/video/sort-field.type'
 import { VideoComment } from './video-comment.model'
 
 @Injectable()
 export class VideoCommentService {
   private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
+  private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/video-comments.'
 
   constructor (
     private authHttp: HttpClient,
@@ -31,24 +30,28 @@ export class VideoCommentService {
     const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
     const normalizedComment = lineFeedToHtml(comment, 'text')
 
-    return this.authHttp.post(url, normalizedComment)
-      .map(data => this.extractVideoComment(data['comment']))
-      .catch(this.restExtractor.handleError)
+    return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
+               .pipe(
+                  map(data => this.extractVideoComment(data.comment)),
+                  catchError(err => this.restExtractor.handleError(err))
+               )
   }
 
   addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) {
     const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId
     const normalizedComment = lineFeedToHtml(comment, 'text')
 
-    return this.authHttp.post(url, normalizedComment)
-      .map(data => this.extractVideoComment(data['comment']))
-      .catch(this.restExtractor.handleError)
+    return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
+               .pipe(
+                 map(data => this.extractVideoComment(data.comment)),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
   }
 
   getVideoCommentThreads (
     videoId: number | string,
     componentPagination: ComponentPagination,
-    sort: SortField
+    sort: VideoSortField
   ): Observable<{ comments: VideoComment[], totalComments: number}> {
     const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
 
@@ -57,27 +60,61 @@ export class VideoCommentService {
 
     const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
     return this.authHttp
-      .get(url, { params })
-      .map(this.extractVideoComments)
-      .catch((res) => this.restExtractor.handleError(res))
+               .get(url, { params })
+               .pipe(
+                 map(this.extractVideoComments),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
   }
 
   getVideoThreadComments (videoId: number | string, threadId: number): Observable<VideoCommentThreadTree> {
     const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}`
 
     return this.authHttp
-      .get(url)
-      .map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree))
-      .catch((res) => this.restExtractor.handleError(res))
+               .get(url)
+               .pipe(
+                 map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree)),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
   }
 
   deleteVideoComment (videoId: number | string, commentId: number) {
     const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}`
 
     return this.authHttp
-      .delete(url)
-      .map(this.restExtractor.extractDataBool)
-      .catch((res) => this.restExtractor.handleError(res))
+               .delete(url)
+               .pipe(
+                 map(this.restExtractor.extractDataBool),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
+  }
+
+  getVideoCommentsFeeds (videoUUID?: string) {
+    const feeds = [
+      {
+        format: FeedFormat.RSS,
+        label: 'rss 2.0',
+        url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
+      },
+      {
+        format: FeedFormat.ATOM,
+        label: 'atom 1.0',
+        url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
+      },
+      {
+        format: FeedFormat.JSON,
+        label: 'json 1.0',
+        url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
+      }
+    ]
+
+    if (videoUUID !== undefined) {
+      for (const feed of feeds) {
+        feed.url += '?videoId=' + videoUUID
+      }
+    }
+
+    return feeds
   }
 
   private extractVideoComment (videoComment: VideoCommentServerModel) {
@@ -87,7 +124,7 @@ export class VideoCommentService {
   private extractVideoComments (result: ResultList<VideoCommentServerModel>) {
     const videoCommentsJson = result.data
     const totalComments = result.total
-    const comments = []
+    const comments: VideoComment[] = []
 
     for (const videoCommentJson of videoCommentsJson) {
       comments.push(new VideoComment(videoCommentJson))