const { db, execQueryAsync, execQueryAsyncWithId } = require("./db.js"); const { queryFromId, queryToId, queryCcId } = require("./utils/addressQueries.js"); const DEBUG = require("../utils/debug").DEBUG; async function createRoom(roomName, ownerId, messageId) { const query = `INSERT INTO app_room (room_name, owner_id, message_id) VALUES (?, ?, ?)`; const values = [roomName.substring(0, 255), ownerId, messageId]; return await execQueryAsyncWithId(query, values); // todo add members } async function registerMessageInRoom(messageId, roomId, isSeen) { const query = `INSERT IGNORE INTO app_room_message (message_id, room_id) VALUES (?, ?)`; const values = [messageId, roomId]; await execQueryAsync(query, values); updateLastUpdateRoom(roomId); if (!isSeen) { incrementNotSeenRoom(roomId); } } function updateLastUpdateRoom(roomId) { // todo } function incrementNotSeenRoom(roomId) { // todo } async function createThread(threadName, ownerId, messageId, parentRoomId, isDm) { const rootRoomId = -1; // todo const threadId = await createRoom(threadName, ownerId, messageId); const query = `INSERT INTO app_thread (room_id, parent_room_id, root_room_id, isDm) VALUES (?, ?, ?, ?)`; const values = [threadId, parentRoomId, rootRoomId, isDm]; return await execQueryAsync(query, values); // todo add members } async function registerMessageInThread(messageId, threadId, isSeen) { // todo check if it is still a thread or should be a room // todo isdm const query = `INSERT IGNORE INTO app_space_message (message_id, thread_id) VALUES (?, ?)`; const values = [messageId, threadId]; await execQueryAsync(query, values); updateLastUpdateThread(threadId); if (!isSeen) { incrementNotSeenThread(threadId); } } function updateLastUpdateRoom(threadId) { // todo // check for parent } function incrementNotSeenThread(threadId) { // todo // also increment parent room } async function isRoomGroup(roomId) { return new Promise((resolve, reject) => { const query = `SELECT isGroup FROM app_room WHERE room_id = '${roomId}'`; db.query(query, (err, results, fields) => { if (err) reject(err); resolve(results[0].isGroup); }); }); } async function findRoomsFromMessage(messageId) { const query = `SELECT room_id FROM app_room_message WHERE message_id = ? ORDER BY room_id`; const values = [messageId]; return await execQueryAsync(query, values); } async function hasSameMembersAsParent(messageId, messageID) { const query1 = ` SELECT GROUP_CONCAT(fromT.address_id) AS fromA, GROUP_CONCAT(toT.address_id) AS toA, GROUP_CONCAT(ccT.address_id) AS ccA FROM message msg ${queryFromId} fromT ON msg.message_id = fromT.message_id ${queryToId} toT ON msg.message_id = toT.message_id ${queryCcId} ccT ON msg.message_id = ccT.message_id WHERE msg.message_id = ? `; const values1 = [messageId]; let addressesMsg1 = await execQueryAsync(query1, values1); const query2 = ` SELECT GROUP_CONCAT(fromT.address_id) AS fromA, GROUP_CONCAT(toT.address_id) AS toA, GROUP_CONCAT(ccT.address_id) AS ccA FROM message msg ${queryFromId} fromT ON msg.message_id = fromT.message_id ${queryToId} toT ON msg.message_id = toT.message_id ${queryCcId} ccT ON msg.message_id = ccT.message_id WHERE msg.messageID = ? `; const values2 = [messageID]; let addressesMsg2 = await execQueryAsync(query2, values2); addressesMsg1 = addressesMsg1[0]?.fromA ?.split(",") .concat(addressesMsg1[0]?.toA?.split(",")) .concat(addressesMsg1[0]?.ccA?.split(",")); addressesMsg2 = addressesMsg2[0]?.fromA ?.split(",") .concat(addressesMsg2[0]?.toA?.split(",")) .concat(addressesMsg2[0]?.ccA?.split(",")); return ( addressesMsg1.length == addressesMsg2.length && addressesMsg1.reduce((a, b) => a && addressesMsg2.includes(b), true) ); } module.exports = { createRoom, registerMessageInRoom, createThread, registerMessageInThread, isRoomGroup, findRoomsFromMessage, hasSameMembersAsParent, };