feat: chat in real time
gitea/salix/pipeline/head There was a failure building this commit
Details
gitea/salix/pipeline/head There was a failure building this commit
Details
This commit is contained in:
parent
8dfc588a7e
commit
d85717ca04
|
@ -36,7 +36,7 @@ module.exports = Self => {
|
|||
dated: new Date(),
|
||||
checkUserStatus: 0,
|
||||
message: message,
|
||||
status: 0,
|
||||
status: 'pending',
|
||||
attempts: 0
|
||||
});
|
||||
|
||||
|
|
|
@ -27,11 +27,17 @@ module.exports = Self => {
|
|||
Self.sendCheckingPresence = async(ctx, recipientId, message, options) => {
|
||||
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);
|
||||
|
@ -49,12 +55,37 @@ module.exports = Self => {
|
|||
dated: new Date(),
|
||||
checkUserStatus: 1,
|
||||
message: message,
|
||||
status: 0,
|
||||
status: 'sending',
|
||||
attempts: 0
|
||||
});
|
||||
console.log(chat);
|
||||
await models.Chat.sendQueued(chat.id);
|
||||
}, myOptions);
|
||||
|
||||
try {
|
||||
await Self.sendCheckingUserStatus(chat);
|
||||
await updateChat(chat, 'sent', myOptions);
|
||||
} catch (error) {
|
||||
await updateChat(chat, 'error', error, myOptions);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -3,11 +3,6 @@ module.exports = Self => {
|
|||
Self.remoteMethodCtx('sendQueued', {
|
||||
description: 'Send a RocketChat message',
|
||||
accessType: 'WRITE',
|
||||
accepts: [{
|
||||
arg: 'id',
|
||||
type: 'number',
|
||||
description: 'The message id'
|
||||
}],
|
||||
returns: {
|
||||
type: 'object',
|
||||
root: true
|
||||
|
@ -18,20 +13,25 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.sendQueued = async id => {
|
||||
Self.sendQueued = async() => {
|
||||
const models = Self.app.models;
|
||||
const maxAttempts = 3;
|
||||
const sentStatus = 1;
|
||||
const errorStatus = 2;
|
||||
let filter = {
|
||||
status: {neq: sentStatus},
|
||||
attempts: {lt: maxAttempts}
|
||||
};
|
||||
const sentStatus = 'sent';
|
||||
const sendingStatus = 'sending';
|
||||
const errorStatus = 'error';
|
||||
|
||||
// NOTA: Igual se deberia transaccionar por si coincidiera que en el momento que se esta enviando directamente
|
||||
// tambien se esta ejecutando la cola que no lo envie dos veces.
|
||||
const chats = await models.Chat.find({
|
||||
where: id ? {id} : filter
|
||||
where: {
|
||||
status: {
|
||||
neq: {
|
||||
and: [
|
||||
sentStatus,
|
||||
sendingStatus
|
||||
]
|
||||
}
|
||||
},
|
||||
attempts: {lt: maxAttempts}
|
||||
}
|
||||
});
|
||||
|
||||
for (let chat of chats) {
|
||||
|
@ -137,6 +137,7 @@ module.exports = Self => {
|
|||
* @param {string} error - The error
|
||||
* @return {Promise} - The request promise
|
||||
*/
|
||||
|
||||
async function updateChat(chat, status, error) {
|
||||
return chat.updateAttributes({
|
||||
status: status,
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
ALTER TABLE `vn`.`chat` ADD statusNew enum('pending','sent','error','sending') DEFAULT 'pending' NOT NULL;
|
||||
|
||||
UPDATE `vn`.`chat`
|
||||
SET statusNew = 'pending'
|
||||
WHERE status = 0;
|
||||
|
||||
UPDATE `vn`.`chat`
|
||||
SET statusNew = 'sent'
|
||||
WHERE status = 1;
|
||||
|
||||
UPDATE `vn`.`chat`
|
||||
SET statusNew = 'error'
|
||||
WHERE status = 2;
|
||||
|
||||
ALTER TABLE `vn`.`chat` CHANGE status status__ tinyint(1) DEFAULT NULL NULL;
|
||||
ALTER TABLE `vn`.`chat` CHANGE statusNew status enum('pending','sent','error','sending') CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci DEFAULT 'pending' NOT NULL;
|
Loading…
Reference in New Issue