2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-console: 0 */
|
|
|
|
/* global axios */
|
2019-10-27 13:31:59 +00:00
|
|
|
import ApiClient from '../ApiClient';
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-08-02 10:37:30 +00:00
|
|
|
export const buildCreatePayload = ({
|
|
|
|
message,
|
|
|
|
isPrivate,
|
|
|
|
contentAttributes,
|
|
|
|
echoId,
|
|
|
|
file,
|
|
|
|
}) => {
|
|
|
|
let payload;
|
|
|
|
if (file) {
|
|
|
|
payload = new FormData();
|
|
|
|
payload.append('attachments[]', file, file.name);
|
|
|
|
if (message) {
|
|
|
|
payload.append('content', message);
|
|
|
|
}
|
|
|
|
payload.append('private', isPrivate);
|
|
|
|
payload.append('echo_id', echoId);
|
|
|
|
} else {
|
|
|
|
payload = {
|
|
|
|
content: message,
|
|
|
|
private: isPrivate,
|
|
|
|
echo_id: echoId,
|
|
|
|
content_attributes: contentAttributes,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return payload;
|
|
|
|
};
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
class MessageApi extends ApiClient {
|
|
|
|
constructor() {
|
2020-03-09 17:57:10 +00:00
|
|
|
super('conversations', { accountScoped: true });
|
2019-10-27 13:31:59 +00:00
|
|
|
}
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-12-25 07:45:01 +00:00
|
|
|
create({
|
|
|
|
conversationId,
|
|
|
|
message,
|
|
|
|
private: isPrivate,
|
|
|
|
contentAttributes,
|
|
|
|
echo_id: echoId,
|
2021-01-06 12:26:29 +00:00
|
|
|
file,
|
2020-12-25 07:45:01 +00:00
|
|
|
}) {
|
2020-03-22 10:24:36 +00:00
|
|
|
return axios({
|
|
|
|
method: 'post',
|
|
|
|
url: `${this.url}/${conversationId}/messages`,
|
2021-08-02 10:37:30 +00:00
|
|
|
data: buildCreatePayload({
|
|
|
|
message,
|
|
|
|
isPrivate,
|
|
|
|
contentAttributes,
|
|
|
|
echoId,
|
|
|
|
file,
|
|
|
|
}),
|
2020-03-22 10:24:36 +00:00
|
|
|
});
|
|
|
|
}
|
2021-01-06 12:26:29 +00:00
|
|
|
|
2021-06-29 05:49:08 +00:00
|
|
|
delete(conversationID, messageId) {
|
|
|
|
return axios.delete(`${this.url}/${conversationID}/messages/${messageId}`);
|
|
|
|
}
|
|
|
|
|
2021-01-06 12:26:29 +00:00
|
|
|
getPreviousMessages({ conversationId, before }) {
|
|
|
|
return axios.get(`${this.url}/${conversationId}/messages`, {
|
|
|
|
params: { before },
|
|
|
|
});
|
|
|
|
}
|
2019-10-27 13:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new MessageApi();
|