1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { objectLineFeedToHtml } from '@app/shared/misc/utils'
5 import { Observable } from 'rxjs'
6 import { ResultList, FeedFormat } 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/'
21 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/video-comments.'
24 private authHttp: HttpClient,
25 private restExtractor: RestExtractor,
26 private restService: RestService
29 addCommentThread (videoId: number | string, comment: VideoCommentCreate) {
30 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
31 const normalizedComment = objectLineFeedToHtml(comment, 'text')
33 return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
35 map(data => this.extractVideoComment(data.comment)),
36 catchError(err => this.restExtractor.handleError(err))
40 addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) {
41 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId
42 const normalizedComment = objectLineFeedToHtml(comment, 'text')
44 return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
46 map(data => this.extractVideoComment(data.comment)),
47 catchError(err => this.restExtractor.handleError(err))
51 getVideoCommentThreads (parameters: {
52 videoId: number | string,
53 componentPagination: ComponentPagination,
55 }): Observable<{ comments: VideoComment[], totalComments: number}> {
56 const { videoId, componentPagination, sort } = parameters
58 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
60 let params = new HttpParams()
61 params = this.restService.addRestGetParams(params, pagination, sort)
63 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
67 map(this.extractVideoComments),
68 catchError(err => this.restExtractor.handleError(err))
72 getVideoThreadComments (parameters: {
73 videoId: number | string,
75 }): Observable<VideoCommentThreadTree> {
76 const { videoId, threadId } = parameters
77 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}`
82 map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree)),
83 catchError(err => this.restExtractor.handleError(err))
87 deleteVideoComment (videoId: number | string, commentId: number) {
88 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}`
93 map(this.restExtractor.extractDataBool),
94 catchError(err => this.restExtractor.handleError(err))
98 getVideoCommentsFeeds (videoUUID?: string) {
101 format: FeedFormat.RSS,
103 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
106 format: FeedFormat.ATOM,
108 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
111 format: FeedFormat.JSON,
113 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
117 if (videoUUID !== undefined) {
118 for (const feed of feeds) {
119 feed.url += '?videoId=' + videoUUID
126 private extractVideoComment (videoComment: VideoCommentServerModel) {
127 return new VideoComment(videoComment)
130 private extractVideoComments (result: ResultList<VideoCommentServerModel>) {
131 const videoCommentsJson = result.data
132 const totalComments = result.total
133 const comments: VideoComment[] = []
135 for (const videoCommentJson of videoCommentsJson) {
136 comments.push(new VideoComment(videoCommentJson))
139 return { comments, totalComments }
142 private extractVideoCommentTree (tree: VideoCommentThreadTree) {
143 if (!tree) return tree
145 tree.comment = new VideoComment(tree.comment)
146 tree.children.forEach(c => this.extractVideoCommentTree(c))