mail/back/sql/saveMessageApp.js
2023-03-13 19:12:57 +01:00

102 lines
2.7 KiB
JavaScript

const { bdd, execQueryAsync, execQueryAsyncWithId } = require("./bdd.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
}
function registerMessageInRoom(messageId, roomId, isSeen) {
const query = `INSERT INTO app_space_message (message_id, room_id) VALUES ('${messageId}', '${roomId}')`;
bdd.query(query, (err, results, fields) => {
if (err) throw err;
});
updateLastUpdateRoom(roomId);
if (!isSeen) {
incrementNotSeenRoom(roomId);
}
}
function updateLastUpdateRoom(roomId) {
// todo
}
function incrementNotSeenRoom(roomId) {
// todo
}
function createThread(roomId, threadName, isDm) {
return new Promise((resolve, reject) => {
const query = `INSERT INTO app_thread
(room_id, thread_name, isDm)
VALUES (
'${roomId}',
'${threadName}',
'${isDm}',
)`;
bdd.query(query, (err, results, fields) => {
if (err) reject(err);
resolve(results.insertId);
});
});
// todo add members
}
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 ('${messageId}', '${threadId}')`;
bdd.query(query, (err, results, fields) => {
if (err) throw err;
});
updateLastUpdateThread(threadId);
if (!isSeen) {
incrementNotSeenThread(threadId);
}
}
function updateLastUpdateRoom(threadId) {
// todo
// check for parent
}
function incrementNotSeenThread(threadId) {
// todo
// also increment parent room
}
function isRoomGroup(roomId) {
return new Promise((resolve, reject) => {
const query = `SELECT isGroup FROM app_room WHERE room_id = '${roomId}'`;
bdd.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
};