refactor(chat): not use transactions
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2023-02-08 15:15:26 +01:00
parent 9ec2f731ae
commit f06fc92ae8
3 changed files with 12 additions and 42 deletions

View File

@ -42,9 +42,9 @@ module.exports = Self => {
try {
await Self.sendMessage(chat.senderFk, chat.recipient, chat.message);
await updateChat(chat, 'sent');
await Self.updateChat(chat, 'sent');
} catch (error) {
await updateChat(chat, 'error', error);
await Self.updateChat(chat, 'error', error);
}
return true;

View File

@ -24,24 +24,13 @@ module.exports = Self => {
}
});
Self.sendCheckingPresence = async(ctx, recipientId, message, options) => {
Self.sendCheckingPresence = async(ctx, recipientId, message) => {
if (!recipientId) return false;
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const sender = await models.Account.findById(userId);
const recipient = await models.Account.findById(recipientId, null, myOptions);
const sender = await models.Account.findById(userId, {fields: ['id']});
const recipient = await models.Account.findById(recipientId, null);
// Prevent sending messages to yourself
if (recipientId == userId) return false;
@ -57,35 +46,15 @@ module.exports = Self => {
message: message,
status: 'sending',
attempts: 0
}, myOptions);
});
try {
await Self.sendCheckingUserStatus(chat);
await updateChat(chat, 'sent', myOptions);
await Self.updateChat(chat, 'sent');
} catch (error) {
await updateChat(chat, 'error', error, myOptions);
await Self.updateChat(chat, 'error', error);
}
if (tx) await tx.commit();
return true;
};
/**
* Update status and attempts of a chat
*
* @param {object} chat - The chat
* @param {string} status - The new status
* @param {string} error - The error
* @param {object} options - Query options
* @return {Promise} - The request promise
*/
async function updateChat(chat, status, error, options) {
return chat.updateAttributes({
status: status,
attempts: ++chat.attempts,
error: error
}, options);
}
};

View File

@ -131,16 +131,17 @@ module.exports = Self => {
* @param {object} chat - The chat
* @param {string} status - The new status
* @param {string} error - The error
* @param {object} options - Query options
* @return {Promise} - The request promise
*/
*/
async function updateChat(chat, status, error) {
Self.updateChat = async(chat, status, error) => {
return chat.updateAttributes({
status: status,
attempts: ++chat.attempts,
error: error
});
}
};
/**
* Returns the current user status on Rocketchat