pause matrix outbound queue until target room id is set

This commit is contained in:
Ladd Hoffman 2024-05-03 13:19:20 -05:00
parent f7b1bfcb3b
commit 69c869a693
3 changed files with 25 additions and 24 deletions

View File

@ -2,16 +2,16 @@ const Promise = require('bluebird');
const { v4: uuidv4 } = require('uuid');
const { isEqual } = require('lodash');
const { registerDecider } = require('./validation-pools');
const { registerMatrixEventHandler, sendMatrixEvent, sendMatrixText } = require('../matrix-bot');
const { matrixPools, matrixUserToAuthorAddress, applicationData } = require('../util/db');
const { registerDecider } = require('../validation-pools');
const { registerMatrixEventHandler, sendMatrixEvent, sendMatrixText } = require('../../matrix-bot');
const { matrixPools, matrixUserToAuthorAddress, applicationData } = require('../../util/db');
const {
rollup, wallet, work2, dao,
} = require('../util/contracts');
const read = require('../util/forum/read');
const write = require('../util/forum/write');
const addPostWithRetry = require('../util/add-post-with-retry');
const callWithRetry = require('../util/call-contract-method-with-retry');
} = require('../../util/contracts');
const read = require('../../util/forum/read');
const write = require('../../util/forum/write');
const addPostWithRetry = require('../../util/add-post-with-retry');
const callWithRetry = require('../../util/call-contract-method-with-retry');
const {
ROLLUP_BATCH_SIZE,

View File

@ -23,7 +23,7 @@ const matrixClient = new MatrixClient(
);
let joinedRooms;
const { startOutboundQueue, sendMatrixEvent, sendMatrixText } = require('./outbound-queue');
const { initializeOutboundQueue, sendMatrixEvent, sendMatrixText } = require('./outbound-queue');
const start = async () => {
// Automatically join a room to which we are invited
@ -35,7 +35,7 @@ const start = async () => {
matrixClient.start().then(() => {
console.log('Matrix bot started!');
// Start the outbound queue
startOutboundQueue(matrixClient);
initializeOutboundQueue(matrixClient);
});
};

View File

@ -5,17 +5,7 @@ const { applicationData } = require('../util/db');
let matrixClient;
let targetRoomId;
const setTargetRoomId = async (roomId) => {
targetRoomId = roomId;
console.log('target room ID:', targetRoomId);
await applicationData.put('targetRoomId', targetRoomId);
};
const processOutboundQueue = async ({ type, ...args }) => {
if (!targetRoomId) {
console.log('targetRoomId not set, cannot deliver message');
return;
}
switch (type) {
case 'MatrixEvent': {
const { eventType, content, onSend } = args;
@ -34,10 +24,19 @@ const processOutboundQueue = async ({ type, ...args }) => {
};
const outboundQueue = fastq(processOutboundQueue, 1);
// Pause until matrixClient is set
// Pause outbound queue until matrixClient and targetRoomId are set
outboundQueue.pause();
const startOutboundQueue = async (matrixClient_) => {
const setTargetRoomId = async (roomId) => {
targetRoomId = roomId;
console.log('target room ID:', targetRoomId);
await applicationData.put('targetRoomId', targetRoomId);
if (matrixClient) {
outboundQueue.resume();
}
};
const initializeOutboundQueue = async (matrixClient_) => {
matrixClient = matrixClient_;
try {
targetRoomId = await applicationData.get('targetRoomId');
@ -46,7 +45,9 @@ const startOutboundQueue = async (matrixClient_) => {
// No target room set
console.warn('target room ID is not set -- will not be able to send messages until it is set. Use !target <bot-id>');
}
outboundQueue.resume();
if (targetRoomId) {
outboundQueue.resume();
}
};
const sendMatrixEvent = async (eventType, content) => new Promise((resolve) => {
@ -73,7 +74,7 @@ const sendMatrixText = async (text) => new Promise((resolve) => {
module.exports = {
setTargetRoomId,
outboundQueue,
startOutboundQueue,
initializeOutboundQueue,
sendMatrixEvent,
sendMatrixText,
};