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