130 lines
4.3 KiB
JavaScript
130 lines
4.3 KiB
JavaScript
const { transformEmojis } = require("../utils/string.js");
|
|
const { db, execQueryAsync, execQueryAsyncWithId, execQuery } = require("./db.js");
|
|
const { queryFromId, queryToId, queryCcId } = require("./utils/addressQueries.js");
|
|
|
|
async function createRoom(roomName, ownerId, messageId) {
|
|
if (!roomName) roomName = "No room name";
|
|
roomName = transformEmojis(roomName);
|
|
const query = `INSERT IGNORE 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, idate) {
|
|
const query = `INSERT IGNORE INTO app_room_message (message_id, room_id) VALUES (?, ?)`;
|
|
const values = [messageId, roomId];
|
|
await execQueryAsync(query, values);
|
|
|
|
updateLastUpdateRoom(roomId, idate);
|
|
|
|
if (!isSeen) {
|
|
incrementNotSeenRoom(roomId);
|
|
}
|
|
}
|
|
|
|
function updateLastUpdateRoom(roomId, idate) {
|
|
const query = `UPDATE app_room SET lastUpdate = ? WHERE room_id = ?`;
|
|
const values = [idate, roomId];
|
|
execQuery(query, values);
|
|
}
|
|
|
|
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
|
|
console.log("register message in thread")
|
|
}
|
|
|
|
function updateLastUpdateThread(threadId) {
|
|
// todo
|
|
// check for parent
|
|
}
|
|
|
|
function incrementNotSeenThread(threadId) {
|
|
// todo
|
|
// also increment root 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,
|
|
};
|