feat(conversations): add message creation limit lock
This commit is contained in:
@@ -289,12 +289,14 @@ const actions = {
|
||||
|
||||
sendMessageWithData: async ({ commit }, pendingMessage) => {
|
||||
const { conversation_id: conversationId, id } = pendingMessage;
|
||||
const shouldRetryMessage =
|
||||
hasMessageFailedWithExternalError(pendingMessage);
|
||||
try {
|
||||
commit(types.ADD_MESSAGE, {
|
||||
...pendingMessage,
|
||||
status: MESSAGE_STATUS.PROGRESS,
|
||||
});
|
||||
const response = hasMessageFailedWithExternalError(pendingMessage)
|
||||
const response = shouldRetryMessage
|
||||
? await MessageApi.retry(conversationId, id)
|
||||
: await MessageApi.create(pendingMessage);
|
||||
commit(types.ADD_MESSAGE, {
|
||||
@@ -309,6 +311,31 @@ const actions = {
|
||||
const errorMessage = error.response
|
||||
? error.response.data.error
|
||||
: undefined;
|
||||
const errorResponse = error.response?.data || {};
|
||||
if (errorResponse.message_creation_locked) {
|
||||
if (shouldRetryMessage) {
|
||||
commit(types.ADD_MESSAGE, {
|
||||
...pendingMessage,
|
||||
meta: {
|
||||
...(pendingMessage.meta || {}),
|
||||
error: errorResponse.error,
|
||||
},
|
||||
status: MESSAGE_STATUS.FAILED,
|
||||
});
|
||||
} else {
|
||||
commit(types.DELETE_MESSAGE, pendingMessage);
|
||||
}
|
||||
commit(types.SET_CONVERSATION_MESSAGE_CREATION_LOCK, {
|
||||
conversationId,
|
||||
message_limit: errorResponse.message_limit,
|
||||
message_limit_reached: errorResponse.message_limit_reached,
|
||||
message_creation_locked: errorResponse.message_creation_locked,
|
||||
message_creation_lock_reason:
|
||||
errorResponse.message_creation_lock_reason,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
|
||||
commit(types.ADD_MESSAGE, {
|
||||
...pendingMessage,
|
||||
meta: {
|
||||
@@ -322,6 +349,12 @@ const actions = {
|
||||
|
||||
addMessage({ commit, rootGetters }, message) {
|
||||
commit(types.ADD_MESSAGE, message);
|
||||
if (message.conversation) {
|
||||
commit(types.SET_CONVERSATION_MESSAGE_CREATION_LOCK, {
|
||||
conversationId: message.conversation_id,
|
||||
...message.conversation,
|
||||
});
|
||||
}
|
||||
if (message.message_type === MESSAGE_TYPE.INCOMING) {
|
||||
commit(types.SET_CONVERSATION_CAN_REPLY, {
|
||||
conversationId: message.conversation_id,
|
||||
|
||||
@@ -29,6 +29,24 @@ const getConversationById = _state => conversationId => {
|
||||
return _state.allConversations.find(c => c.id === conversationId);
|
||||
};
|
||||
|
||||
const MESSAGE_CREATION_LOCK_FIELDS = [
|
||||
'message_limit',
|
||||
'message_limit_reached',
|
||||
'message_creation_locked',
|
||||
'message_creation_lock_reason',
|
||||
];
|
||||
|
||||
const updateConversationMessageCreationLock = (
|
||||
conversation,
|
||||
lockState = {}
|
||||
) => {
|
||||
MESSAGE_CREATION_LOCK_FIELDS.forEach(field => {
|
||||
if (Object.prototype.hasOwnProperty.call(lockState, field)) {
|
||||
conversation[field] = lockState[field];
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// mutations
|
||||
export const mutations = {
|
||||
[types.SET_ALL_CONVERSATION](_state, conversationList) {
|
||||
@@ -205,6 +223,20 @@ export const mutations = {
|
||||
});
|
||||
},
|
||||
|
||||
[types.DELETE_MESSAGE]({ allConversations }, message) {
|
||||
const { conversation_id: conversationId } = message;
|
||||
const [chat] = getSelectedChatConversation({
|
||||
allConversations,
|
||||
selectedChatId: conversationId,
|
||||
});
|
||||
if (!chat) return;
|
||||
|
||||
const pendingMessageIndex = findPendingMessageIndex(chat, message);
|
||||
if (pendingMessageIndex !== -1) {
|
||||
chat.messages.splice(pendingMessageIndex, 1);
|
||||
}
|
||||
},
|
||||
|
||||
[types.ADD_MESSAGE]({ allConversations, selectedChatId }, message) {
|
||||
const { conversation_id: conversationId } = message;
|
||||
const [chat] = getSelectedChatConversation({
|
||||
@@ -216,17 +248,29 @@ export const mutations = {
|
||||
const pendingMessageIndex = findPendingMessageIndex(chat, message);
|
||||
if (pendingMessageIndex !== -1) {
|
||||
chat.messages[pendingMessageIndex] = message;
|
||||
updateConversationMessageCreationLock(chat, message.conversation);
|
||||
} else {
|
||||
chat.messages.push(message);
|
||||
chat.timestamp = message.created_at;
|
||||
const { conversation: { unread_count: unreadCount = 0 } = {} } = message;
|
||||
chat.unread_count = unreadCount;
|
||||
updateConversationMessageCreationLock(chat, message.conversation);
|
||||
if (selectedChatId === conversationId) {
|
||||
emitter.emit(BUS_EVENTS.SCROLL_TO_MESSAGE);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
[types.SET_CONVERSATION_MESSAGE_CREATION_LOCK](
|
||||
_state,
|
||||
{ conversationId, ...lockState }
|
||||
) {
|
||||
const chat = getConversationById(_state)(conversationId);
|
||||
if (chat) {
|
||||
updateConversationMessageCreationLock(chat, lockState);
|
||||
}
|
||||
},
|
||||
|
||||
[types.ADD_CONVERSATION](_state, conversation) {
|
||||
const exists = _state.allConversations.some(c => c.id === conversation.id);
|
||||
if (!exists) {
|
||||
|
||||
Reference in New Issue
Block a user