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