]>
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 | Video, | |
13 | VideoComment as VideoCommentServerModel, | |
14 | VideoCommentAdmin, | |
15 | VideoCommentCreate, | |
16 | VideoCommentThreadTree as VideoCommentThreadTreeServerModel | |
17 | } from '@shared/models' | |
18 | import { environment } from '../../../environments/environment' | |
19 | import { VideoCommentThreadTree } from './video-comment-thread-tree.model' | |
20 | import { VideoComment } from './video-comment.model' | |
21 | ||
22 | @Injectable() | |
23 | export class VideoCommentService { | |
24 | static BASE_FEEDS_URL = environment.apiUrl + '/feeds/video-comments.' | |
25 | ||
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' | |
36 | const normalizedComment = objectLineFeedToHtml(comment, 'text') | |
37 | ||
38 | return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment) | |
39 | .pipe( | |
40 | map(data => this.extractVideoComment(data.comment)), | |
41 | catchError(err => this.restExtractor.handleError(err)) | |
42 | ) | |
43 | } | |
44 | ||
45 | addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) { | |
46 | const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId | |
47 | const normalizedComment = objectLineFeedToHtml(comment, 'text') | |
48 | ||
49 | return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment) | |
50 | .pipe( | |
51 | map(data => this.extractVideoComment(data.comment)), | |
52 | catchError(err => this.restExtractor.handleError(err)) | |
53 | ) | |
54 | } | |
55 | ||
56 | getAdminVideoComments (options: { | |
57 | pagination: RestPagination | |
58 | sort: SortMeta | |
59 | search?: string | |
60 | }): Observable<ResultList<VideoCommentAdmin>> { | |
61 | const { pagination, sort, search } = options | |
62 | const url = VideoCommentService.BASE_VIDEO_URL + 'comments' | |
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 | ||
77 | getVideoCommentThreads (parameters: { | |
78 | videoId: number | string | |
79 | componentPagination: ComponentPaginationLight | |
80 | sort: string | |
81 | }): Observable<ThreadsResultList<VideoComment>> { | |
82 | const { videoId, componentPagination, sort } = parameters | |
83 | ||
84 | const pagination = this.restService.componentToRestPagination(componentPagination) | |
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' | |
90 | return this.authHttp.get<ThreadsResultList<VideoComment>>(url, { params }) | |
91 | .pipe( | |
92 | map(result => this.extractVideoComments(result)), | |
93 | catchError(err => this.restExtractor.handleError(err)) | |
94 | ) | |
95 | } | |
96 | ||
97 | getVideoThreadComments (parameters: { | |
98 | videoId: number | string | |
99 | threadId: number | |
100 | }): Observable<VideoCommentThreadTree> { | |
101 | const { videoId, threadId } = parameters | |
102 | const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}` | |
103 | ||
104 | return this.authHttp | |
105 | .get<VideoCommentThreadTreeServerModel>(url) | |
106 | .pipe( | |
107 | map(tree => this.extractVideoCommentTree(tree)), | |
108 | catchError(err => this.restExtractor.handleError(err)) | |
109 | ) | |
110 | } | |
111 | ||
112 | deleteVideoComment (videoId: number | string, commentId: number) { | |
113 | const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}` | |
114 | ||
115 | return this.authHttp | |
116 | .delete(url) | |
117 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
118 | } | |
119 | ||
120 | deleteVideoComments (comments: { videoId: number | string, commentId: number }[]) { | |
121 | return from(comments) | |
122 | .pipe( | |
123 | concatMap(c => this.deleteVideoComment(c.videoId, c.commentId)), | |
124 | toArray() | |
125 | ) | |
126 | } | |
127 | ||
128 | getVideoCommentsFeeds (video: Pick<Video, 'uuid'>) { | |
129 | const feeds = [ | |
130 | { | |
131 | format: FeedFormat.RSS, | |
132 | label: 'rss 2.0', | |
133 | url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase() | |
134 | }, | |
135 | { | |
136 | format: FeedFormat.ATOM, | |
137 | label: 'atom 1.0', | |
138 | url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase() | |
139 | }, | |
140 | { | |
141 | format: FeedFormat.JSON, | |
142 | label: 'json 1.0', | |
143 | url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase() | |
144 | } | |
145 | ] | |
146 | ||
147 | if (video !== undefined) { | |
148 | for (const feed of feeds) { | |
149 | feed.url += '?videoId=' + video.uuid | |
150 | } | |
151 | } | |
152 | ||
153 | return feeds | |
154 | } | |
155 | ||
156 | private extractVideoComment (videoComment: VideoCommentServerModel) { | |
157 | return new VideoComment(videoComment) | |
158 | } | |
159 | ||
160 | private extractVideoComments (result: ThreadsResultList<VideoCommentServerModel>) { | |
161 | const videoCommentsJson = result.data | |
162 | const totalComments = result.total | |
163 | const comments: VideoComment[] = [] | |
164 | ||
165 | for (const videoCommentJson of videoCommentsJson) { | |
166 | comments.push(new VideoComment(videoCommentJson)) | |
167 | } | |
168 | ||
169 | return { data: comments, total: totalComments, totalNotDeletedComments: result.totalNotDeletedComments } | |
170 | } | |
171 | ||
172 | private extractVideoCommentTree (serverTree: VideoCommentThreadTreeServerModel): VideoCommentThreadTree { | |
173 | if (!serverTree) return null | |
174 | ||
175 | const tree = { | |
176 | comment: new VideoComment(serverTree.comment), | |
177 | children: serverTree.children.map(c => this.extractVideoCommentTree(c)) | |
178 | } | |
179 | ||
180 | const hasDisplayedChildren = tree.children.length === 0 | |
181 | ? !tree.comment.isDeleted | |
182 | : tree.children.some(c => c.hasDisplayedChildren) | |
183 | ||
184 | return Object.assign(tree, { hasDisplayedChildren }) | |
185 | } | |
186 | ||
187 | private buildParamsFromSearch (search: string, params: HttpParams) { | |
188 | const filters = this.restService.parseQueryStringFilter(search, { | |
189 | isLocal: { | |
190 | prefix: 'local:', | |
191 | isBoolean: true | |
192 | }, | |
193 | ||
194 | searchAccount: { prefix: 'account:' }, | |
195 | searchVideo: { prefix: 'video:' } | |
196 | }) | |
197 | ||
198 | return this.restService.addObjectParams(params, filters) | |
199 | } | |
200 | } |