advancements in tests and storing messages
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const { db, execQueryAsync, execQueryAsyncWithId } = require("./db.js");
|
||||
const { queryCcId, queryToId, queryFromId } = require("./utils/addressQueries.js");
|
||||
const DEBUG = require("../utils/debug").DEBUG;
|
||||
|
||||
async function registerMailbox(userId, pwd, xoauth, xoauth2, host, port, tls) {
|
||||
@@ -48,8 +49,6 @@ async function getRooms(mailboxId) {
|
||||
|
||||
async function getMessages(roomId) {
|
||||
// todo attachements name
|
||||
// todo html, text, textAsHtml
|
||||
// todo datetime
|
||||
const query = `
|
||||
SELECT
|
||||
msg.message_id AS id,
|
||||
@@ -61,30 +60,9 @@ async function getMessages(roomId) {
|
||||
message.idate AS date
|
||||
FROM app_room_message msg
|
||||
|
||||
LEFT JOIN (
|
||||
SELECT address_field.address_id, address_field.message_id
|
||||
FROM address_field
|
||||
INNER JOIN field_name
|
||||
WHERE
|
||||
field_name.field_id = address_field.field_id AND
|
||||
field_name.field_name = 'from'
|
||||
) fromT ON msg.message_id = fromT.message_id
|
||||
LEFT JOIN (
|
||||
SELECT address_field.address_id, address_field.message_id
|
||||
FROM address_field
|
||||
INNER JOIN field_name
|
||||
WHERE
|
||||
field_name.field_id = address_field.field_id AND
|
||||
field_name.field_name = 'to'
|
||||
) toT ON msg.message_id = toT.message_id
|
||||
LEFT JOIN (
|
||||
SELECT address_field.address_id, address_field.message_id
|
||||
FROM address_field
|
||||
INNER JOIN field_name
|
||||
WHERE
|
||||
field_name.field_id = address_field.field_id AND
|
||||
field_name.field_name = 'cc'
|
||||
) ccT ON msg.message_id = ccT.message_id
|
||||
${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
|
||||
|
||||
LEFT JOIN (
|
||||
SELECT header_field.message_id, header_field.value
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
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) {
|
||||
@@ -28,15 +29,18 @@ 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];
|
||||
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];
|
||||
@@ -69,14 +73,53 @@ async function isRoomGroup(roomId) {
|
||||
}
|
||||
|
||||
async function findRoomsFromMessage(messageId) {
|
||||
const query = `SELECT room_id FROM app_room_message WHERE message_id = '${messageId}'`;
|
||||
return await execQueryAsync(query);
|
||||
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, parentID) {
|
||||
// const query = `SELECT `;
|
||||
// return await execQueryAsync(query)
|
||||
// todo
|
||||
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 = {
|
||||
@@ -86,4 +129,5 @@ module.exports = {
|
||||
registerMessageInThread,
|
||||
isRoomGroup,
|
||||
findRoomsFromMessage,
|
||||
hasSameMembersAsParent,
|
||||
};
|
||||
|
||||
@@ -94,10 +94,10 @@ CREATE TABLE header_field (
|
||||
field_id INT NOT NULL,
|
||||
bodypart_id INT,
|
||||
part VARCHAR(128),
|
||||
value TEXT NOT NULL,
|
||||
UNIQUE KEY (message_id, field_id, bodypart_id),
|
||||
value TEXT,
|
||||
UNIQUE KEY (message_id, field_id, bodypart_id),-- todo multiple raws
|
||||
FOREIGN KEY (message_id) REFERENCES message(message_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (field_id) REFERENCES field_name(field_id), -- todo on delete behavior
|
||||
FOREIGN KEY (field_id) REFERENCES field_name(field_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (bodypart_id) REFERENCES bodypart(bodypart_id)
|
||||
);
|
||||
|
||||
|
||||
20
back/db/utils/addressQueries.js
Normal file
20
back/db/utils/addressQueries.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const queryAddress = (type) => `
|
||||
LEFT JOIN (
|
||||
SELECT address_field.address_id, address_field.message_id
|
||||
FROM address_field
|
||||
INNER JOIN field_name
|
||||
WHERE
|
||||
field_name.field_id = address_field.field_id AND
|
||||
field_name.field_name = '${type}'
|
||||
)
|
||||
`;
|
||||
|
||||
const queryFromId = queryAddress("from");
|
||||
const queryToId = queryAddress("to");
|
||||
const queryCcId = queryAddress("cc");
|
||||
|
||||
module.exports = {
|
||||
queryFromId,
|
||||
queryToId,
|
||||
queryCcId
|
||||
}
|
||||
Reference in New Issue
Block a user