]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comment.service.ts
Simplify client syndications
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.service.ts
CommitLineData
4635f59d
C
1import { HttpClient, HttpParams } from '@angular/common/http'
2import { Injectable } from '@angular/core'
3d9eaae3
C
3import { lineFeedToHtml } from '@app/shared/misc/utils'
4import { MarkdownService } from '@app/videos/shared'
4635f59d
C
5import 'rxjs/add/operator/catch'
6import 'rxjs/add/operator/map'
7import { Observable } from 'rxjs/Observable'
8import { ResultList } from '../../../../../../shared/models'
9import {
3d9eaae3
C
10 VideoComment as VideoCommentServerModel,
11 VideoCommentCreate,
4635f59d
C
12 VideoCommentThreadTree
13} from '../../../../../../shared/models/videos/video-comment.model'
14import { environment } from '../../../../environments/environment'
15import { RestExtractor, RestService } from '../../../shared/rest'
16import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
17import { SortField } from '../../../shared/video/sort-field.type'
18import { VideoComment } from './video-comment.model'
19
20@Injectable()
21export class VideoCommentService {
22 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
23
24 constructor (
25 private authHttp: HttpClient,
26 private restExtractor: RestExtractor,
27 private restService: RestService
28 ) {}
29
30 addCommentThread (videoId: number | string, comment: VideoCommentCreate) {
31 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
5de8a55a 32 const normalizedComment = lineFeedToHtml(comment, 'text')
4635f59d 33
5de8a55a 34 return this.authHttp.post(url, normalizedComment)
4635f59d
C
35 .map(data => this.extractVideoComment(data['comment']))
36 .catch(this.restExtractor.handleError)
37 }
38
39 addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) {
40 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId
5de8a55a 41 const normalizedComment = lineFeedToHtml(comment, 'text')
4635f59d 42
5de8a55a 43 return this.authHttp.post(url, normalizedComment)
4635f59d
C
44 .map(data => this.extractVideoComment(data['comment']))
45 .catch(this.restExtractor.handleError)
46 }
47
48 getVideoCommentThreads (
49 videoId: number | string,
50 componentPagination: ComponentPagination,
51 sort: SortField
52 ): Observable<{ comments: VideoComment[], totalComments: number}> {
53 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
54
55 let params = new HttpParams()
56 params = this.restService.addRestGetParams(params, pagination, sort)
57
58 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
59 return this.authHttp
60 .get(url, { params })
61 .map(this.extractVideoComments)
62 .catch((res) => this.restExtractor.handleError(res))
63 }
64
65 getVideoThreadComments (videoId: number | string, threadId: number): Observable<VideoCommentThreadTree> {
66 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}`
67
68 return this.authHttp
69 .get(url)
70 .map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree))
71 .catch((res) => this.restExtractor.handleError(res))
72 }
73
4cb6d457
C
74 deleteVideoComment (videoId: number | string, commentId: number) {
75 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}`
76
77 return this.authHttp
78 .delete(url)
79 .map(this.restExtractor.extractDataBool)
80 .catch((res) => this.restExtractor.handleError(res))
81 }
82
4635f59d
C
83 private extractVideoComment (videoComment: VideoCommentServerModel) {
84 return new VideoComment(videoComment)
85 }
86
87 private extractVideoComments (result: ResultList<VideoCommentServerModel>) {
88 const videoCommentsJson = result.data
89 const totalComments = result.total
90 const comments = []
91
92 for (const videoCommentJson of videoCommentsJson) {
93 comments.push(new VideoComment(videoCommentJson))
94 }
95
96 return { comments, totalComments }
97 }
98
99 private extractVideoCommentTree (tree: VideoCommentThreadTree) {
100 if (!tree) return tree
101
102 tree.comment = new VideoComment(tree.comment)
103 tree.children.forEach(c => this.extractVideoCommentTree(c))
104
105 return tree
106 }
107}