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