]>
Commit | Line | Data |
---|---|---|
1 | import { catchError, map } from 'rxjs/operators' | |
2 | import { HttpClient, HttpParams } from '@angular/common/http' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { objectLineFeedToHtml } from '@app/shared/misc/utils' | |
5 | import { Observable } from 'rxjs' | |
6 | import { ResultList, FeedFormat } from '../../../../../../shared/models' | |
7 | import { | |
8 | VideoComment as VideoCommentServerModel, | |
9 | VideoCommentCreate, | |
10 | VideoCommentThreadTree | |
11 | } from '../../../../../../shared/models/videos/video-comment.model' | |
12 | import { environment } from '../../../../environments/environment' | |
13 | import { RestExtractor, RestService } from '../../../shared/rest' | |
14 | import { ComponentPagination } from '../../../shared/rest/component-pagination.model' | |
15 | import { VideoSortField } from '../../../shared/video/sort-field.type' | |
16 | import { VideoComment } from './video-comment.model' | |
17 | ||
18 | @Injectable() | |
19 | export class VideoCommentService { | |
20 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | |
21 | private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/video-comments.' | |
22 | ||
23 | constructor ( | |
24 | private authHttp: HttpClient, | |
25 | private restExtractor: RestExtractor, | |
26 | private restService: RestService | |
27 | ) {} | |
28 | ||
29 | addCommentThread (videoId: number | string, comment: VideoCommentCreate) { | |
30 | const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads' | |
31 | const normalizedComment = objectLineFeedToHtml(comment, 'text') | |
32 | ||
33 | return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment) | |
34 | .pipe( | |
35 | map(data => this.extractVideoComment(data.comment)), | |
36 | catchError(err => this.restExtractor.handleError(err)) | |
37 | ) | |
38 | } | |
39 | ||
40 | addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) { | |
41 | const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId | |
42 | const normalizedComment = objectLineFeedToHtml(comment, 'text') | |
43 | ||
44 | return this.authHttp.post<{ comment: VideoCommentServerModel }>(url, normalizedComment) | |
45 | .pipe( | |
46 | map(data => this.extractVideoComment(data.comment)), | |
47 | catchError(err => this.restExtractor.handleError(err)) | |
48 | ) | |
49 | } | |
50 | ||
51 | getVideoCommentThreads ( | |
52 | videoId: number | string, | |
53 | componentPagination: ComponentPagination, | |
54 | sort: VideoSortField | |
55 | ): Observable<{ comments: VideoComment[], totalComments: number}> { | |
56 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
57 | ||
58 | let params = new HttpParams() | |
59 | params = this.restService.addRestGetParams(params, pagination, sort) | |
60 | ||
61 | const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads' | |
62 | return this.authHttp | |
63 | .get(url, { params }) | |
64 | .pipe( | |
65 | map(this.extractVideoComments), | |
66 | catchError(err => this.restExtractor.handleError(err)) | |
67 | ) | |
68 | } | |
69 | ||
70 | getVideoThreadComments (videoId: number | string, threadId: number): Observable<VideoCommentThreadTree> { | |
71 | const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}` | |
72 | ||
73 | return this.authHttp | |
74 | .get(url) | |
75 | .pipe( | |
76 | map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree)), | |
77 | catchError(err => this.restExtractor.handleError(err)) | |
78 | ) | |
79 | } | |
80 | ||
81 | deleteVideoComment (videoId: number | string, commentId: number) { | |
82 | const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comments/${commentId}` | |
83 | ||
84 | return this.authHttp | |
85 | .delete(url) | |
86 | .pipe( | |
87 | map(this.restExtractor.extractDataBool), | |
88 | catchError(err => this.restExtractor.handleError(err)) | |
89 | ) | |
90 | } | |
91 | ||
92 | getVideoCommentsFeeds (videoUUID?: string) { | |
93 | const feeds = [ | |
94 | { | |
95 | format: FeedFormat.RSS, | |
96 | label: 'rss 2.0', | |
97 | url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase() | |
98 | }, | |
99 | { | |
100 | format: FeedFormat.ATOM, | |
101 | label: 'atom 1.0', | |
102 | url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase() | |
103 | }, | |
104 | { | |
105 | format: FeedFormat.JSON, | |
106 | label: 'json 1.0', | |
107 | url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase() | |
108 | } | |
109 | ] | |
110 | ||
111 | if (videoUUID !== undefined) { | |
112 | for (const feed of feeds) { | |
113 | feed.url += '?videoId=' + videoUUID | |
114 | } | |
115 | } | |
116 | ||
117 | return feeds | |
118 | } | |
119 | ||
120 | private extractVideoComment (videoComment: VideoCommentServerModel) { | |
121 | return new VideoComment(videoComment) | |
122 | } | |
123 | ||
124 | private extractVideoComments (result: ResultList<VideoCommentServerModel>) { | |
125 | const videoCommentsJson = result.data | |
126 | const totalComments = result.total | |
127 | const comments: VideoComment[] = [] | |
128 | ||
129 | for (const videoCommentJson of videoCommentsJson) { | |
130 | comments.push(new VideoComment(videoCommentJson)) | |
131 | } | |
132 | ||
133 | return { comments, totalComments } | |
134 | } | |
135 | ||
136 | private extractVideoCommentTree (tree: VideoCommentThreadTree) { | |
137 | if (!tree) return tree | |
138 | ||
139 | tree.comment = new VideoComment(tree.comment) | |
140 | tree.children.forEach(c => this.extractVideoCommentTree(c)) | |
141 | ||
142 | return tree | |
143 | } | |
144 | } |