]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comment.service.ts
Better typings
[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'
53877968 6import { ResultList, FeedFormat } from '../../../../../../shared/models'
4635f59d 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/'
53877968 21 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/video-comments.'
4635f59d
C
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'
5de8a55a 31 const normalizedComment = lineFeedToHtml(comment, 'text')
4635f59d 32
c199c427 33 return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
db400f44 34 .pipe(
c199c427 35 map(data => this.extractVideoComment(data.comment)),
e4f0e92e 36 catchError(err => this.restExtractor.handleError(err))
db400f44 37 )
4635f59d
C
38 }
39
40 addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) {
41 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId
5de8a55a 42 const normalizedComment = lineFeedToHtml(comment, 'text')
4635f59d 43
c199c427 44 return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
db400f44 45 .pipe(
c199c427 46 map(data => this.extractVideoComment(data.comment)),
e4f0e92e 47 catchError(err => this.restExtractor.handleError(err))
db400f44 48 )
4635f59d
C
49 }
50
51 getVideoCommentThreads (
52 videoId: number | string,
53 componentPagination: ComponentPagination,
7b87d2d5 54 sort: VideoSortField
4635f59d
C
55 ): Observable<{ comments: VideoComment[], totalComments: number}> {
56 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
57
58 let params = new HttpParams()
59 params = this.restService.addRestGetParams(params, pagination, sort)
60
61 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
62 return this.authHttp
db400f44
C
63 .get(url, { params })
64 .pipe(
65 map(this.extractVideoComments),
e4f0e92e 66 catchError(err => this.restExtractor.handleError(err))
db400f44 67 )
4635f59d
C
68 }
69
70 getVideoThreadComments (videoId: number | string, threadId: number): Observable<VideoCommentThreadTree> {
71 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}`
72
73 return this.authHttp
db400f44
C
74 .get(url)
75 .pipe(
76 map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree)),
e4f0e92e 77 catchError(err => this.restExtractor.handleError(err))
db400f44 78 )
4635f59d
C
79 }
80
4cb6d457
C
81 deleteVideoComment (videoId: number | string, commentId: number) {
82 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}`
83
84 return this.authHttp
db400f44
C
85 .delete(url)
86 .pipe(
87 map(this.restExtractor.extractDataBool),
e4f0e92e 88 catchError(err => this.restExtractor.handleError(err))
db400f44 89 )
4cb6d457
C
90 }
91
53877968
C
92 getVideoCommentsFeeds (videoUUID?: string) {
93 const feeds = [
94 {
95 format: FeedFormat.RSS,
96 label: 'rss 2.0',
97 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
98 },
99 {
100 format: FeedFormat.ATOM,
101 label: 'atom 1.0',
102 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
103 },
104 {
105 format: FeedFormat.JSON,
106 label: 'json 1.0',
107 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
108 }
109 ]
110
111 if (videoUUID !== undefined) {
112 for (const feed of feeds) {
113 feed.url += '?videoId=' + videoUUID
114 }
115 }
116
117 return feeds
118 }
119
4635f59d
C
120 private extractVideoComment (videoComment: VideoCommentServerModel) {
121 return new VideoComment(videoComment)
122 }
123
124 private extractVideoComments (result: ResultList<VideoCommentServerModel>) {
125 const videoCommentsJson = result.data
126 const totalComments = result.total
db400f44 127 const comments: VideoComment[] = []
4635f59d
C
128
129 for (const videoCommentJson of videoCommentsJson) {
130 comments.push(new VideoComment(videoCommentJson))
131 }
132
133 return { comments, totalComments }
134 }
135
136 private extractVideoCommentTree (tree: VideoCommentThreadTree) {
137 if (!tree) return tree
138
139 tree.comment = new VideoComment(tree.comment)
140 tree.children.forEach(c => this.extractVideoCommentTree(c))
141
142 return tree
143 }
144}