Chatwoot/app/javascript/dashboard/api/inbox/message.js
2021-08-02 16:07:30 +05:30

69 lines
1.4 KiB
JavaScript

/* eslint no-console: 0 */
/* global axios */
import ApiClient from '../ApiClient';
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;
};
class MessageApi extends ApiClient {
constructor() {
super('conversations', { accountScoped: true });
}
create({
conversationId,
message,
private: isPrivate,
contentAttributes,
echo_id: echoId,
file,
}) {
return axios({
method: 'post',
url: `${this.url}/${conversationId}/messages`,
data: buildCreatePayload({
message,
isPrivate,
contentAttributes,
echoId,
file,
}),
});
}
delete(conversationID, messageId) {
return axios.delete(`${this.url}/${conversationID}/messages/${messageId}`);
}
getPreviousMessages({ conversationId, before }) {
return axios.get(`${this.url}/${conversationId}/messages`, {
params: { before },
});
}
}
export default new MessageApi();