]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-comment/video-comment.service.ts
Implement video comment list in admin
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-comment / video-comment.service.ts
CommitLineData
67ed6552 1import { Observable } from 'rxjs'
db400f44 2import { catchError, map } from 'rxjs/operators'
4635f59d
C
3import { HttpClient, HttpParams } from '@angular/common/http'
4import { Injectable } from '@angular/core'
0f8d00e3 5import { ComponentPaginationLight, RestExtractor, RestPagination, RestService } from '@app/core'
67ed6552 6import { objectLineFeedToHtml } from '@app/helpers'
4635f59d 7import {
67ed6552
C
8 FeedFormat,
9 ResultList,
3d9eaae3 10 VideoComment as VideoCommentServerModel,
0f8d00e3 11 VideoCommentAdmin,
3d9eaae3 12 VideoCommentCreate,
be27ef3b 13 VideoCommentThreadTree as VideoCommentThreadTreeServerModel
67ed6552 14} from '@shared/models'
cfde28ba 15import { environment } from '../../../environments/environment'
67ed6552 16import { VideoCommentThreadTree } from './video-comment-thread-tree.model'
4635f59d 17import { VideoComment } from './video-comment.model'
0f8d00e3 18import { SortMeta } from 'primeng/api'
4635f59d
C
19
20@Injectable()
21export class VideoCommentService {
22 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
53877968 23 private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/video-comments.'
4635f59d
C
24
25 constructor (
26 private authHttp: HttpClient,
27 private restExtractor: RestExtractor,
28 private restService: RestService
29 ) {}
30
31 addCommentThread (videoId: number | string, comment: VideoCommentCreate) {
32 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
1506307f 33 const normalizedComment = objectLineFeedToHtml(comment, 'text')
4635f59d 34
c199c427 35 return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
db400f44 36 .pipe(
c199c427 37 map(data => this.extractVideoComment(data.comment)),
e4f0e92e 38 catchError(err => this.restExtractor.handleError(err))
db400f44 39 )
4635f59d
C
40 }
41
42 addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) {
43 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId
1506307f 44 const normalizedComment = objectLineFeedToHtml(comment, 'text')
4635f59d 45
c199c427 46 return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment)
db400f44 47 .pipe(
c199c427 48 map(data => this.extractVideoComment(data.comment)),
e4f0e92e 49 catchError(err => this.restExtractor.handleError(err))
db400f44 50 )
4635f59d
C
51 }
52
0f8d00e3
C
53 getAdminVideoComments (options: {
54 pagination: RestPagination,
55 sort: SortMeta,
56 search?: string
57 }): Observable<ResultList<VideoCommentAdmin>> {
58 const { pagination, sort, search } = options
59 const url = VideoCommentService.BASE_VIDEO_URL + '/comments'
60
61 let params = new HttpParams()
62 params = this.restService.addRestGetParams(params, pagination, sort)
63
64 if (search) {
65 params = this.buildParamsFromSearch(search, params)
66 }
67
68 return this.authHttp.get<ResultList<VideoCommentAdmin>>(url, { params })
69 .pipe(
70 catchError(res => this.restExtractor.handleError(res))
71 )
72 }
73
93cae479 74 getVideoCommentThreads (parameters: {
4635f59d 75 videoId: number | string,
440d39c5 76 componentPagination: ComponentPaginationLight,
67ed6552 77 sort: string
e8f902c0 78 }): Observable<ResultList<VideoComment>> {
93cae479
C
79 const { videoId, componentPagination, sort } = parameters
80
4635f59d
C
81 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
82
83 let params = new HttpParams()
84 params = this.restService.addRestGetParams(params, pagination, sort)
85
86 const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads'
e8f902c0 87 return this.authHttp.get<ResultList<VideoComment>>(url, { params })
db400f44 88 .pipe(
e8f902c0 89 map(result => this.extractVideoComments(result)),
e4f0e92e 90 catchError(err => this.restExtractor.handleError(err))
db400f44 91 )
4635f59d
C
92 }
93
93cae479
C
94 getVideoThreadComments (parameters: {
95 videoId: number | string,
96 threadId: number
97 }): Observable<VideoCommentThreadTree> {
98 const { videoId, threadId } = parameters
4635f59d
C
99 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}`
100
101 return this.authHttp
be27ef3b 102 .get<VideoCommentThreadTreeServerModel>(url)
db400f44 103 .pipe(
be27ef3b 104 map(tree => this.extractVideoCommentTree(tree)),
e4f0e92e 105 catchError(err => this.restExtractor.handleError(err))
db400f44 106 )
4635f59d
C
107 }
108
4cb6d457
C
109 deleteVideoComment (videoId: number | string, commentId: number) {
110 const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}`
111
112 return this.authHttp
db400f44
C
113 .delete(url)
114 .pipe(
115 map(this.restExtractor.extractDataBool),
e4f0e92e 116 catchError(err => this.restExtractor.handleError(err))
db400f44 117 )
4cb6d457
C
118 }
119
53877968
C
120 getVideoCommentsFeeds (videoUUID?: string) {
121 const feeds = [
122 {
123 format: FeedFormat.RSS,
124 label: 'rss 2.0',
125 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
126 },
127 {
128 format: FeedFormat.ATOM,
129 label: 'atom 1.0',
130 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
131 },
132 {
133 format: FeedFormat.JSON,
134 label: 'json 1.0',
135 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
136 }
137 ]
138
139 if (videoUUID !== undefined) {
140 for (const feed of feeds) {
141 feed.url += '?videoId=' + videoUUID
142 }
143 }
144
145 return feeds
146 }
147
4635f59d
C
148 private extractVideoComment (videoComment: VideoCommentServerModel) {
149 return new VideoComment(videoComment)
150 }
151
152 private extractVideoComments (result: ResultList<VideoCommentServerModel>) {
153 const videoCommentsJson = result.data
154 const totalComments = result.total
db400f44 155 const comments: VideoComment[] = []
4635f59d
C
156
157 for (const videoCommentJson of videoCommentsJson) {
158 comments.push(new VideoComment(videoCommentJson))
159 }
160
e8f902c0 161 return { data: comments, total: totalComments }
4635f59d
C
162 }
163
be27ef3b
C
164 private extractVideoCommentTree (tree: VideoCommentThreadTreeServerModel) {
165 if (!tree) return tree as VideoCommentThreadTree
4635f59d
C
166
167 tree.comment = new VideoComment(tree.comment)
168 tree.children.forEach(c => this.extractVideoCommentTree(c))
169
be27ef3b 170 return tree as VideoCommentThreadTree
4635f59d 171 }
0f8d00e3
C
172
173 private buildParamsFromSearch (search: string, params: HttpParams) {
174 const filters = this.restService.parseQueryStringFilter(search, {
175 state: {
176 prefix: 'local:',
177 isBoolean: true,
178 handler: v => {
179 if (v === 'true') return v
180 if (v === 'false') return v
181
182 return undefined
183 }
184 },
185
186 searchAccount: { prefix: 'account:' },
187 searchVideo: { prefix: 'video:' }
188 })
189
190 return this.restService.addObjectParams(params, filters)
191 }
4635f59d 192}