const { db, execQueryAsync, execQueryAsyncWithId } = require("./db.js"); const DEBUG = require("../utils/debug").DEBUG; async function createRoom(roomName, ownerId) { const query = `INSERT INTO app_room (room_name, owner_id) VALUES (?, ?)`; const values = [roomName.substring(0, 255), ownerId]; return await execQueryAsyncWithId(query, values); // todo add members } async function registerMessageInRoom(messageId, roomId, isSeen) { const query = `INSERT INTO app_space_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(roomId, threadName, isDm) { const query = `INSERT INTO app_thread (room_id, thread_name, isDm) VALUES (?, ?, ?)`; const values = [roomId, threadName, 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 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 findSpacesFromMessage(messageId) { const query = `SELECT room_id, thread_id FROM app_space_message WHERE message_id = '${messageId}'`; return await execQueryAsync(query); } async function hasSameMembersAsParent(messageId, parentID) { // const query = `SELECT `; // return await execQueryAsync(query) // todo } module.exports = { createRoom, registerMessageInRoom, createThread, registerMessageInThread, isRoomGroup, findSpacesFromMessage, };