diff --git a/back/methods/chat/send.js b/back/methods/chat/send.js index 21e25c1f0..5e3821677 100644 --- a/back/methods/chat/send.js +++ b/back/methods/chat/send.js @@ -30,16 +30,23 @@ module.exports = Self => { const recipient = to.replace('@', ''); if (sender.name != recipient) { - await models.Chat.create({ + const chat = await models.Chat.create({ senderFk: sender.id, recipient: to, dated: Date.vnNew(), checkUserStatus: 0, message: message, - status: 'pending', + status: 'sending', attempts: 0 }); + try { + await Self.sendMessage(chat.senderFk, chat.recipient, chat.message); + await updateChat(chat, 'sent'); + } catch (error) { + await updateChat(chat, 'error', error); + } + return true; } diff --git a/back/methods/chat/sendQueued.js b/back/methods/chat/sendQueued.js index dcd530873..ad8363627 100644 --- a/back/methods/chat/sendQueued.js +++ b/back/methods/chat/sendQueued.js @@ -15,22 +15,18 @@ module.exports = Self => { Self.sendQueued = async() => { const models = Self.app.models; - const maxAttempts = 3; - const sentStatus = 'sent'; - const sendingStatus = 'sending'; - const errorStatus = 'error'; const chats = await models.Chat.find({ where: { status: { neq: { and: [ - sentStatus, - sendingStatus + 'sent', + 'sending' ] } }, - attempts: {lt: maxAttempts} + attempts: {lt: 3} } }); @@ -38,16 +34,16 @@ module.exports = Self => { if (chat.checkUserStatus) { try { await Self.sendCheckingUserStatus(chat); - await updateChat(chat, sentStatus); + await updateChat(chat, 'sent'); } catch (error) { - await updateChat(chat, errorStatus, error); + await updateChat(chat, 'error', error); } } else { try { await Self.sendMessage(chat.senderFk, chat.recipient, chat.message); - await updateChat(chat, sentStatus); + await updateChat(chat, 'sent'); } catch (error) { - await updateChat(chat, errorStatus, error); + await updateChat(chat, 'error', error); } } }