]>
Commit | Line | Data |
---|---|---|
1 | import { pick } from '@shared/core-utils' | |
2 | import { HttpStatusCode, ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models' | |
3 | import { unwrapBody } from '../requests' | |
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | |
5 | ||
6 | export class CommentsCommand extends AbstractCommand { | |
7 | ||
8 | private lastVideoId: number | string | |
9 | private lastThreadId: number | |
10 | private lastReplyId: number | |
11 | ||
12 | listForAdmin (options: OverrideCommandOptions & { | |
13 | start?: number | |
14 | count?: number | |
15 | sort?: string | |
16 | isLocal?: boolean | |
17 | onLocalVideo?: boolean | |
18 | search?: string | |
19 | searchAccount?: string | |
20 | searchVideo?: string | |
21 | } = {}) { | |
22 | const { sort = '-createdAt' } = options | |
23 | const path = '/api/v1/videos/comments' | |
24 | ||
25 | const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'onLocalVideo', 'search', 'searchAccount', 'searchVideo' ]) } | |
26 | ||
27 | return this.getRequestBody<ResultList<VideoComment>>({ | |
28 | ...options, | |
29 | ||
30 | path, | |
31 | query, | |
32 | implicitToken: true, | |
33 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
34 | }) | |
35 | } | |
36 | ||
37 | listThreads (options: OverrideCommandOptions & { | |
38 | videoId: number | string | |
39 | start?: number | |
40 | count?: number | |
41 | sort?: string | |
42 | }) { | |
43 | const { start, count, sort, videoId } = options | |
44 | const path = '/api/v1/videos/' + videoId + '/comment-threads' | |
45 | ||
46 | return this.getRequestBody<VideoCommentThreads>({ | |
47 | ...options, | |
48 | ||
49 | path, | |
50 | query: { start, count, sort }, | |
51 | implicitToken: false, | |
52 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
53 | }) | |
54 | } | |
55 | ||
56 | getThread (options: OverrideCommandOptions & { | |
57 | videoId: number | string | |
58 | threadId: number | |
59 | }) { | |
60 | const { videoId, threadId } = options | |
61 | const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId | |
62 | ||
63 | return this.getRequestBody<VideoCommentThreadTree>({ | |
64 | ...options, | |
65 | ||
66 | path, | |
67 | implicitToken: false, | |
68 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
69 | }) | |
70 | } | |
71 | ||
72 | async createThread (options: OverrideCommandOptions & { | |
73 | videoId: number | string | |
74 | text: string | |
75 | }) { | |
76 | const { videoId, text } = options | |
77 | const path = '/api/v1/videos/' + videoId + '/comment-threads' | |
78 | ||
79 | const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({ | |
80 | ...options, | |
81 | ||
82 | path, | |
83 | fields: { text }, | |
84 | implicitToken: true, | |
85 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
86 | })) | |
87 | ||
88 | this.lastThreadId = body.comment?.id | |
89 | this.lastVideoId = videoId | |
90 | ||
91 | return body.comment | |
92 | } | |
93 | ||
94 | async addReply (options: OverrideCommandOptions & { | |
95 | videoId: number | string | |
96 | toCommentId: number | |
97 | text: string | |
98 | }) { | |
99 | const { videoId, toCommentId, text } = options | |
100 | const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId | |
101 | ||
102 | const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({ | |
103 | ...options, | |
104 | ||
105 | path, | |
106 | fields: { text }, | |
107 | implicitToken: true, | |
108 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
109 | })) | |
110 | ||
111 | this.lastReplyId = body.comment?.id | |
112 | ||
113 | return body.comment | |
114 | } | |
115 | ||
116 | async addReplyToLastReply (options: OverrideCommandOptions & { | |
117 | text: string | |
118 | }) { | |
119 | return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastReplyId }) | |
120 | } | |
121 | ||
122 | async addReplyToLastThread (options: OverrideCommandOptions & { | |
123 | text: string | |
124 | }) { | |
125 | return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastThreadId }) | |
126 | } | |
127 | ||
128 | async findCommentId (options: OverrideCommandOptions & { | |
129 | videoId: number | string | |
130 | text: string | |
131 | }) { | |
132 | const { videoId, text } = options | |
133 | const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' }) | |
134 | ||
135 | return data.find(c => c.text === text).id | |
136 | } | |
137 | ||
138 | delete (options: OverrideCommandOptions & { | |
139 | videoId: number | string | |
140 | commentId: number | |
141 | }) { | |
142 | const { videoId, commentId } = options | |
143 | const path = '/api/v1/videos/' + videoId + '/comments/' + commentId | |
144 | ||
145 | return this.deleteRequest({ | |
146 | ...options, | |
147 | ||
148 | path, | |
149 | implicitToken: true, | |
150 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
151 | }) | |
152 | } | |
153 | } |