diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-27 16:11:53 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-27 16:11:53 +0100 |
commit | 4635f59d7c3fea4b97029f10886c62fdf38b2084 (patch) | |
tree | d97357a00042bbfb33c4177ee24c01171d28dfce /client/src/app/videos/+video-watch/comment/video-comment.service.ts | |
parent | ea44f375f5d3da06ca0aebfe871b9f924a26ec29 (diff) | |
download | PeerTube-4635f59d7c3fea4b97029f10886c62fdf38b2084.tar.gz PeerTube-4635f59d7c3fea4b97029f10886c62fdf38b2084.tar.zst PeerTube-4635f59d7c3fea4b97029f10886c62fdf38b2084.zip |
Add video comment components
Diffstat (limited to 'client/src/app/videos/+video-watch/comment/video-comment.service.ts')
-rw-r--r-- | client/src/app/videos/+video-watch/comment/video-comment.service.ts | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/client/src/app/videos/+video-watch/comment/video-comment.service.ts b/client/src/app/videos/+video-watch/comment/video-comment.service.ts new file mode 100644 index 000000000..2fe6cc3e9 --- /dev/null +++ b/client/src/app/videos/+video-watch/comment/video-comment.service.ts | |||
@@ -0,0 +1,93 @@ | |||
1 | import { HttpClient, HttpParams } from '@angular/common/http' | ||
2 | import { Injectable } from '@angular/core' | ||
3 | import 'rxjs/add/operator/catch' | ||
4 | import 'rxjs/add/operator/map' | ||
5 | import { Observable } from 'rxjs/Observable' | ||
6 | import { ResultList } from '../../../../../../shared/models' | ||
7 | import { | ||
8 | VideoComment as VideoCommentServerModel, VideoCommentCreate, | ||
9 | VideoCommentThreadTree | ||
10 | } from '../../../../../../shared/models/videos/video-comment.model' | ||
11 | import { environment } from '../../../../environments/environment' | ||
12 | import { RestExtractor, RestService } from '../../../shared/rest' | ||
13 | import { ComponentPagination } from '../../../shared/rest/component-pagination.model' | ||
14 | import { SortField } from '../../../shared/video/sort-field.type' | ||
15 | import { VideoComment } from './video-comment.model' | ||
16 | |||
17 | @Injectable() | ||
18 | export class VideoCommentService { | ||
19 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | ||
20 | |||
21 | constructor ( | ||
22 | private authHttp: HttpClient, | ||
23 | private restExtractor: RestExtractor, | ||
24 | private restService: RestService | ||
25 | ) {} | ||
26 | |||
27 | addCommentThread (videoId: number | string, comment: VideoCommentCreate) { | ||
28 | const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads' | ||
29 | |||
30 | return this.authHttp.post(url, comment) | ||
31 | .map(data => this.extractVideoComment(data['comment'])) | ||
32 | .catch(this.restExtractor.handleError) | ||
33 | } | ||
34 | |||
35 | addCommentReply (videoId: number | string, inReplyToCommentId: number, comment: VideoCommentCreate) { | ||
36 | const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comments/' + inReplyToCommentId | ||
37 | |||
38 | return this.authHttp.post(url, comment) | ||
39 | .map(data => this.extractVideoComment(data['comment'])) | ||
40 | .catch(this.restExtractor.handleError) | ||
41 | } | ||
42 | |||
43 | getVideoCommentThreads ( | ||
44 | videoId: number | string, | ||
45 | componentPagination: ComponentPagination, | ||
46 | sort: SortField | ||
47 | ): Observable<{ comments: VideoComment[], totalComments: number}> { | ||
48 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | ||
49 | |||
50 | let params = new HttpParams() | ||
51 | params = this.restService.addRestGetParams(params, pagination, sort) | ||
52 | |||
53 | const url = VideoCommentService.BASE_VIDEO_URL + videoId + '/comment-threads' | ||
54 | return this.authHttp | ||
55 | .get(url, { params }) | ||
56 | .map(this.extractVideoComments) | ||
57 | .catch((res) => this.restExtractor.handleError(res)) | ||
58 | } | ||
59 | |||
60 | getVideoThreadComments (videoId: number | string, threadId: number): Observable<VideoCommentThreadTree> { | ||
61 | const url = `${VideoCommentService.BASE_VIDEO_URL + videoId}/comment-threads/${threadId}` | ||
62 | |||
63 | return this.authHttp | ||
64 | .get(url) | ||
65 | .map(tree => this.extractVideoCommentTree(tree as VideoCommentThreadTree)) | ||
66 | .catch((res) => this.restExtractor.handleError(res)) | ||
67 | } | ||
68 | |||
69 | private extractVideoComment (videoComment: VideoCommentServerModel) { | ||
70 | return new VideoComment(videoComment) | ||
71 | } | ||
72 | |||
73 | private extractVideoComments (result: ResultList<VideoCommentServerModel>) { | ||
74 | const videoCommentsJson = result.data | ||
75 | const totalComments = result.total | ||
76 | const comments = [] | ||
77 | |||
78 | for (const videoCommentJson of videoCommentsJson) { | ||
79 | comments.push(new VideoComment(videoCommentJson)) | ||
80 | } | ||
81 | |||
82 | return { comments, totalComments } | ||
83 | } | ||
84 | |||
85 | private extractVideoCommentTree (tree: VideoCommentThreadTree) { | ||
86 | if (!tree) return tree | ||
87 | |||
88 | tree.comment = new VideoComment(tree.comment) | ||
89 | tree.children.forEach(c => this.extractVideoCommentTree(c)) | ||
90 | |||
91 | return tree | ||
92 | } | ||
93 | } | ||