]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comment.service.ts
Update client according to new model paths
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.service.ts
CommitLineData
db400f44 1import { catchError, map } from 'rxjs/operators'
4635f59d
C
2import { HttpClient, HttpParams } from '@angular/common/http'
3import { Injectable } from '@angular/core'
3d9eaae3 4import { lineFeedToHtml } from '@app/shared/misc/utils'
db400f44 5import { Observable } from 'rxjs'
4635f59d
C
6import { ResultList } from '../../../../../../shared/models'
7import {
3d9eaae3
C
8 VideoComment as VideoCommentServerModel,
9 VideoCommentCreate,
4635f59d
C
10 VideoCommentThreadTree
11} from '../../../../../../shared/models/videos/video-comment.model'
12import { environment } from '../../../../environments/environment'
13import { RestExtractor, RestService } from '../../../shared/rest'
14import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
7b87d2d5 15import { VideoSortField } from '../../../shared/video/sort-field.type'
4635f59d
C
16import { VideoComment } from './video-comment.model'
17
18@Injectable()
19export 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'
5de8a55a 30 const normalizedComment = lineFeedToHtml(comment, 'text')
4635f59d 31
5de8a55a 32 return this.authHttp.post(url, normalizedComment)
db400f44 33 .pipe(
e4f0e92e
C
34 map(data => this.extractVideoComment(data['comment'])),
35 catchError(err => this.restExtractor.handleError(err))
db400f44 36 )
4635f59d
C
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)
db400f44
C
44 .pipe(
45 map(data => this.extractVideoComment(data[ 'comment' ])),
e4f0e92e 46 catchError(err => this.restExtractor.handleError(err))
db400f44 47 )
4635f59d
C
48 }
49
50 getVideoCommentThreads (
51 videoId: number | string,
52 componentPagination: ComponentPagination,
7b87d2d5 53 sort: VideoSortField
4635f59d
C
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
db400f44
C
62 .get(url, { params })
63 .pipe(
64 map(this.extractVideoComments),
e4f0e92e 65 catchError(err => this.restExtractor.handleError(err))
db400f44 66 )
4635f59d
C
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
db400f44
C
73 .get(url)
74 .pipe(
75 map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree)),
e4f0e92e 76 catchError(err => this.restExtractor.handleError(err))
db400f44 77 )
4635f59d
C
78 }
79
4cb6d457
C
80 deleteVideoComment (videoId: number | string, commentId: number) {
81 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}`
82
83 return this.authHttp
db400f44
C
84 .delete(url)
85 .pipe(
86 map(this.restExtractor.extractDataBool),
e4f0e92e 87 catchError(err => this.restExtractor.handleError(err))
db400f44 88 )
4cb6d457
C
89 }
90
4635f59d
C
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
db400f44 98 const comments: VideoComment[] = []
4635f59d
C
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}