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