2019-08-14 09:48:44 +00:00
|
|
|
/* eslint no-param-reassign: 0 */
|
2020-12-25 07:45:01 +00:00
|
|
|
|
|
|
|
import getUuid from 'widget/helpers/uuid';
|
|
|
|
import { MESSAGE_STATUS, MESSAGE_TYPE } from 'shared/constants/messages';
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
export default () => {
|
|
|
|
if (!Array.prototype.last) {
|
|
|
|
Object.assign(Array.prototype, {
|
|
|
|
last() {
|
|
|
|
return this[this.length - 1];
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2020-05-04 17:37:56 +00:00
|
|
|
|
|
|
|
export const getTypingUsersText = (users = []) => {
|
|
|
|
const count = users.length;
|
|
|
|
if (count === 1) {
|
|
|
|
const [user] = users;
|
|
|
|
return `${user.name} is typing`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count === 2) {
|
|
|
|
const [first, second] = users;
|
|
|
|
return `${first.name} and ${second.name} are typing`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [user] = users;
|
|
|
|
const rest = users.length - 1;
|
|
|
|
return `${user.name} and ${rest} others are typing`;
|
|
|
|
};
|
2020-12-25 07:45:01 +00:00
|
|
|
|
|
|
|
export const createPendingMessage = data => {
|
|
|
|
const timestamp = Math.floor(new Date().getTime() / 1000);
|
|
|
|
const tempMessageId = getUuid();
|
2021-01-06 12:26:29 +00:00
|
|
|
const { message, file } = data;
|
|
|
|
const tempAttachments = [{ id: tempMessageId }];
|
2020-12-25 07:45:01 +00:00
|
|
|
const pendingMessage = {
|
|
|
|
...data,
|
2021-01-06 12:26:29 +00:00
|
|
|
content: message || null,
|
2020-12-25 07:45:01 +00:00
|
|
|
id: tempMessageId,
|
|
|
|
echo_id: tempMessageId,
|
|
|
|
status: MESSAGE_STATUS.PROGRESS,
|
|
|
|
created_at: timestamp,
|
|
|
|
message_type: MESSAGE_TYPE.OUTGOING,
|
|
|
|
conversation_id: data.conversationId,
|
2021-01-06 12:26:29 +00:00
|
|
|
attachments: file ? tempAttachments : null,
|
2020-12-25 07:45:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return pendingMessage;
|
|
|
|
};
|