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