]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comment.service.ts
Add client hooks
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.service.ts
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'
7 import {
8 VideoComment as VideoCommentServerModel,
9 VideoCommentCreate,
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'
17
18 @Injectable()
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.'
22
23 constructor (
24 private authHttp: HttpClient,
25 private restExtractor: RestExtractor,
26 private restService: RestService
27 ) {}
28
29 addCommentThread (videoId: number | string, comment: VideoCommentCreate) {
30 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
31 const normalizedComment = objectLineFeedToHtml(comment, 'text')
32
33 return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
34 .pipe(
35 map(data => this.extractVideoComment(data.comment)),
36 catchError(err => this.restExtractor.handleError(err))
37 )
38 }
39
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')
43
44 return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
45 .pipe(
46 map(data => this.extractVideoComment(data.comment)),
47 catchError(err => this.restExtractor.handleError(err))
48 )
49 }
50
51 getVideoCommentThreads (parameters: {
52 videoId: number | string,
53 componentPagination: ComponentPagination,
54 sort: VideoSortField
55 }): Observable<{ comments: VideoComment[], totalComments: number}> {
56 const { videoId, componentPagination, sort } = parameters
57
58 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
59
60 let params = new HttpParams()
61 params = this.restService.addRestGetParams(params, pagination, sort)
62
63 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
64 return this.authHttp
65 .get(url, { params })
66 .pipe(
67 map(this.extractVideoComments),
68 catchError(err => this.restExtractor.handleError(err))
69 )
70 }
71
72 getVideoThreadComments (parameters: {
73 videoId: number | string,
74 threadId: number
75 }): Observable<VideoCommentThreadTree> {
76 const { videoId, threadId } = parameters
77 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}`
78
79 return this.authHttp
80 .get(url)
81 .pipe(
82 map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree)),
83 catchError(err => this.restExtractor.handleError(err))
84 )
85 }
86
87 deleteVideoComment (videoId: number | string, commentId: number) {
88 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}`
89
90 return this.authHttp
91 .delete(url)
92 .pipe(
93 map(this.restExtractor.extractDataBool),
94 catchError(err => this.restExtractor.handleError(err))
95 )
96 }
97
98 getVideoCommentsFeeds (videoUUID?: string) {
99 const feeds = [
100 {
101 format: FeedFormat.RSS,
102 label: 'rss 2.0',
103 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
104 },
105 {
106 format: FeedFormat.ATOM,
107 label: 'atom 1.0',
108 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
109 },
110 {
111 format: FeedFormat.JSON,
112 label: 'json 1.0',
113 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
114 }
115 ]
116
117 if (videoUUID !== undefined) {
118 for (const feed of feeds) {
119 feed.url += '?videoId=' + videoUUID
120 }
121 }
122
123 return feeds
124 }
125
126 private extractVideoComment (videoComment: VideoCommentServerModel) {
127 return new VideoComment(videoComment)
128 }
129
130 private extractVideoComments (result: ResultList<VideoCommentServerModel>) {
131 const videoCommentsJson = result.data
132 const totalComments = result.total
133 const comments: VideoComment[] = []
134
135 for (const videoCommentJson of videoCommentsJson) {
136 comments.push(new VideoComment(videoCommentJson))
137 }
138
139 return { comments, totalComments }
140 }
141
142 private extractVideoCommentTree (tree: VideoCommentThreadTree) {
143 if (!tree) return tree
144
145 tree.comment = new VideoComment(tree.comment)
146 tree.children.forEach(c => this.extractVideoCommentTree(c))
147
148 return tree
149 }
150 }