]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comment.service.ts
fix comment and top-menu placement regressions
[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 { lineFeedToHtml } from '@app/shared/misc/utils'
5 import { Observable } from 'rxjs'
6 import { ResultList } 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
22 constructor (
23 private authHttp: HttpClient,
24 private restExtractor: RestExtractor,
25 private restService: RestService
26 ) {}
27
28 addCommentThread (videoId: number | string, comment: VideoCommentCreate) {
29 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
30 const normalizedComment = lineFeedToHtml(comment, 'text')
31
32 return this.authHttp.post(url, normalizedComment)
33 .pipe(
34 map(data => this.extractVideoComment(data['comment'])),
35 catchError(err => this.restExtractor.handleError(err))
36 )
37 }
38
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')
42
43 return this.authHttp.post(url, normalizedComment)
44 .pipe(
45 map(data => this.extractVideoComment(data[ 'comment' ])),
46 catchError(err => this.restExtractor.handleError(err))
47 )
48 }
49
50 getVideoCommentThreads (
51 videoId: number | string,
52 componentPagination: ComponentPagination,
53 sort: VideoSortField
54 ): Observable<{ comments: VideoComment[], totalComments: number}> {
55 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
56
57 let params = new HttpParams()
58 params = this.restService.addRestGetParams(params, pagination, sort)
59
60 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
61 return this.authHttp
62 .get(url, { params })
63 .pipe(
64 map(this.extractVideoComments),
65 catchError(err => this.restExtractor.handleError(err))
66 )
67 }
68
69 getVideoThreadComments (videoId: number | string, threadId: number): Observable<VideoCommentThreadTree> {
70 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}`
71
72 return this.authHttp
73 .get(url)
74 .pipe(
75 map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree)),
76 catchError(err => this.restExtractor.handleError(err))
77 )
78 }
79
80 deleteVideoComment (videoId: number | string, commentId: number) {
81 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}`
82
83 return this.authHttp
84 .delete(url)
85 .pipe(
86 map(this.restExtractor.extractDataBool),
87 catchError(err => this.restExtractor.handleError(err))
88 )
89 }
90
91 private extractVideoComment (videoComment: VideoCommentServerModel) {
92 return new VideoComment(videoComment)
93 }
94
95 private extractVideoComments (result: ResultList<VideoCommentServerModel>) {
96 const videoCommentsJson = result.data
97 const totalComments = result.total
98 const comments: VideoComment[] = []
99
100 for (const videoCommentJson of videoCommentsJson) {
101 comments.push(new VideoComment(videoCommentJson))
102 }
103
104 return { comments, totalComments }
105 }
106
107 private extractVideoCommentTree (tree: VideoCommentThreadTree) {
108 if (!tree) return tree
109
110 tree.comment = new VideoComment(tree.comment)
111 tree.children.forEach(c => this.extractVideoCommentTree(c))
112
113 return tree
114 }
115 }