97 lines
2.5 KiB
JavaScript
97 lines
2.5 KiB
JavaScript
const bdd = require("./bdd.js").bdd;
|
|
const DEBUG = require("../utils/debug").DEBUG;
|
|
|
|
function createRoom(roomName, ownerId, notSeen) {
|
|
return new Promise((resolve, reject) => {
|
|
const query = `INSERT INTO app_room (room_name, owner_id, notSeen) VALUES ('${roomName}', '${ownerId}', '${notSeen}')`;
|
|
bdd.query(query, (err, results, fields) => {
|
|
if (err) reject(err);
|
|
resolve(results.insertId);
|
|
});
|
|
});
|
|
}
|
|
|
|
function registerMessageInRoom(messageId, roomId, isSeen) {
|
|
const query = `INSERT INTO app_room_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, notSeen, isDm) {
|
|
return new Promise((resolve, reject) => {
|
|
const query = `INSERT INTO app_thread
|
|
(room_id, thread_name, notSeen, isDm)
|
|
VALUES (
|
|
'${roomId}',
|
|
'${threadName}',
|
|
'${notSeen}',
|
|
'${isDm}',
|
|
)`;
|
|
bdd.query(query, (err, results, fields) => {
|
|
if (err) reject(err);
|
|
resolve(results.insertId);
|
|
});
|
|
});
|
|
}
|
|
|
|
function registerMessageInThread(messageId, threadId, isSeen) {
|
|
// todo check if it is still a thread or should be a room
|
|
const query = `INSERT IGNORE INTO app_room_messages
|
|
(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);
|
|
});
|
|
});
|
|
}
|
|
|
|
function findSpacesFromMessage(messageId) {
|
|
const query = ``;
|
|
}
|
|
|
|
module.exports = {
|
|
createRoom,
|
|
registerMessageInRoom,
|
|
createThread,
|
|
registerMessageInThread,
|
|
isRoomGroup,
|
|
findSpacesFromMessage
|
|
}; |