-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathapi.js
More file actions
218 lines (208 loc) · 5.66 KB
/
Copy pathapi.js
File metadata and controls
218 lines (208 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import snakeCase from 'lodash.snakecase';
import { ensureConfig, getConfig, snakeCaseObject } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
ensureConfig([
'LMS_BASE_URL',
], 'Posts API service');
export const getThreadsApiUrl = () => `${getConfig().LMS_BASE_URL}/api/discussion/v1/threads/`;
export const getCoursesApiUrl = () => `${getConfig().LMS_BASE_URL}/api/discussion/v1/courses/`;
/**
* Fetches all the threads in the given course and topic.
* @param {string} courseId
* @param {string} author
* @param {[string]} topicIds List of topics to limit threads to
* @param {number} page
* @param {number} pageSize
* @param {string} textSearch A search string to match.
* @param {ThreadOrdering} orderBy The results wil be sorted on this basis.
* @param {boolean} following If true, only threads followed by the current user will be returned.
* @param {boolean} flagged If true, only threads that have been reported will be returned.
* @param {string} threadType Can be 'discussion' or 'question'.
* @param {ThreadViewStatus} view Set to "unread" on "unanswered" to filter to only those statuses.
* @param {boolean} countFlagged If true, abuseFlaggedCount will be available.
* @param {number} cohort
* @returns {Promise<{}>}
*/
export const getThreads = async (courseId, {
topicIds,
page,
pageSize,
textSearch,
orderBy,
following,
view,
author,
flagged,
threadType,
countFlagged,
cohort,
} = {}) => {
const params = snakeCaseObject({
courseId,
page,
pageSize,
topicId: topicIds && topicIds.join(','),
textSearch,
threadType,
orderBy: snakeCase(orderBy),
following,
view,
requestedFields: 'profile_image',
author,
flagged,
countFlagged,
groupId: cohort,
});
const { data } = await getAuthenticatedHttpClient().get(getThreadsApiUrl(), { params });
return data;
};
/**
* Fetches a single thread.
* @param {string} threadId
* @returns {Promise<{}>}
*/
export const getThread = async (threadId, courseId) => {
const params = { requested_fields: 'profile_image', course_id: courseId };
const url = `${getThreadsApiUrl()}${threadId}/`;
const { data } = await getAuthenticatedHttpClient().get(url, { params });
return data;
};
/**
* Posts a new thread.
* @param {string} courseId
* @param {string} topicId
* @param {ThreadType} type The thread's type (either "question" or "discussion")
* @param {string} title
* @param {string} content
* @param {number} cohort
* @param {boolean} following Follow the thread after creating
* @param {boolean} anonymous Should the thread be anonymous to all users
* @param {boolean} anonymousToPeers Should the thread be anonymous to peers
* @param notifyAllLearners
* @param recaptchaToken
* @param {boolean} enableInContextSidebar
* @returns {Promise<{}>}
*/
export const postThread = async (
courseId,
topicId,
type,
title,
content,
{
following,
groupId,
anonymous,
anonymousToPeers,
notifyAllLearners,
recaptchaToken,
} = {},
enableInContextSidebar = false,
) => {
const postData = snakeCaseObject({
courseId,
topicId,
type,
title,
raw_body: content,
following,
anonymous,
anonymousToPeers,
groupId,
enableInContextSidebar,
notifyAllLearners,
captchaToken: recaptchaToken,
});
const { data } = await getAuthenticatedHttpClient()
.post(getThreadsApiUrl(), postData);
return data;
};
/**
* Updates an existing thread.
* @param {string} threadId
* @param {string} topicId
* @param {ThreadType} type The thread's type (either "question" or "discussion")
* @param {string} title
* @param {string} content
* @param {boolean} flagged
* @param {boolean} voted
* @param {boolean} read
* @param {boolean} following
* @param {boolean} closed
* @param {boolean} pinned
* @param {string} editReasonCode
* @param {string} closeReasonCode
* @returns {Promise<{}>}
*/
export const updateThread = async (threadId, {
flagged,
voted,
read,
topicId,
type,
title,
content,
following,
closed,
pinned,
editReasonCode,
closeReasonCode,
groupId,
} = {}) => {
const url = `${getThreadsApiUrl()}${threadId}/`;
const patchData = snakeCaseObject({
topicId,
abuse_flagged: flagged,
voted,
read,
type,
title,
raw_body: content,
following,
closed,
pinned,
editReasonCode,
closeReasonCode,
groupId,
});
const { data } = await getAuthenticatedHttpClient()
.patch(url, patchData, { headers: { 'Content-Type': 'application/merge-patch+json' } });
return data;
};
/**
* Deletes a thread.
* @param {string} threadId
*/
export const deleteThread = async (threadId) => {
const url = `${getThreadsApiUrl()}${threadId}/`;
await getAuthenticatedHttpClient()
.delete(url);
};
/**
* Upload a file.
* @param {Blob} blob The file body
* @param {string} filename
* @param {string} courseId
* @param {string} threadKey
* @returns {Promise<{ location: string }>}
*/
export const uploadFile = async (blob, filename, courseId, threadKey) => {
const uploadUrl = `${getCoursesApiUrl()}${courseId}/upload`;
const formData = new FormData();
formData.append('thread_key', threadKey);
formData.append('uploaded_file', blob, filename);
const { data } = await getAuthenticatedHttpClient().post(uploadUrl, formData);
if (data.developer_message) {
throw new Error(data.developer_message);
}
return data;
};
/**
* Post send Account Activation Email.
*/
export const sendEmailForAccountActivation = async () => {
const url = `${getConfig().LMS_BASE_URL}/api/send_account_activation_email`;
const { data } = await getAuthenticatedHttpClient()
.post(url);
return data;
};