1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { lineFeedToHtml } from '@app/shared/misc/utils'
5 import { Observable } from 'rxjs'
6 import { ResultList } from '../../../../../../shared/models'
8 VideoComment as VideoCommentServerModel,
10 VideoCommentThreadTree
11 } from '../../../../../../shared/models/videos/video-comment.model'
12 import { environment } from '../../../../environments/environment'
13 import { RestExtractor, RestService } from '../../../shared/rest'
14 import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
15 import { VideoSortField } from '../../../shared/video/sort-field.type'
16 import { VideoComment } from './video-comment.model'
19 export class VideoCommentService {
20 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
23 private authHttp: HttpClient,
24 private restExtractor: RestExtractor,
25 private restService: RestService
28 addCommentThread (videoId: number | string, comment: VideoCommentCreate) {
29 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
30 const normalizedComment = lineFeedToHtml(comment, 'text')
32 return this.authHttp.post(url, normalizedComment)
34 map(data => this.extractVideoComment(data['comment'])),
35 catchError(this.restExtractor.handleError)
39 addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) {
40 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId
41 const normalizedComment = lineFeedToHtml(comment, 'text')
43 return this.authHttp.post(url, normalizedComment)
45 map(data => this.extractVideoComment(data[ 'comment' ])),
46 catchError(this.restExtractor.handleError)
50 getVideoCommentThreads (
51 videoId: number | string,
52 componentPagination: ComponentPagination,
54 ): Observable<{ comments: VideoComment[], totalComments: number}> {
55 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
57 let params = new HttpParams()
58 params = this.restService.addRestGetParams(params, pagination, sort)
60 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
64 map(this.extractVideoComments),
65 catchError((res) => this.restExtractor.handleError(res))
69 getVideoThreadComments (videoId: number | string, threadId: number): Observable<VideoCommentThreadTree> {
70 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}`
75 map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree)),
76 catchError((res) => this.restExtractor.handleError(res))
80 deleteVideoComment (videoId: number | string, commentId: number) {
81 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}`
86 map(this.restExtractor.extractDataBool),
87 catchError((res) => this.restExtractor.handleError(res))
91 private extractVideoComment (videoComment: VideoCommentServerModel) {
92 return new VideoComment(videoComment)
95 private extractVideoComments (result: ResultList<VideoCommentServerModel>) {
96 const videoCommentsJson = result.data
97 const totalComments = result.total
98 const comments: VideoComment[] = []
100 for (const videoCommentJson of videoCommentsJson) {
101 comments.push(new VideoComment(videoCommentJson))
104 return { comments, totalComments }
107 private extractVideoCommentTree (tree: VideoCommentThreadTree) {
108 if (!tree) return tree
110 tree.comment = new VideoComment(tree.comment)
111 tree.children.forEach(c => this.extractVideoCommentTree(c))