load message in front

This commit is contained in:
grimhilt
2023-03-25 13:06:59 +01:00
parent 926dc60920
commit 4d4ef54bcb
12 changed files with 246 additions and 131 deletions

View File

@@ -4,7 +4,7 @@ const { getMessages } = require("../db/api.js");
async function messages(body, res) {
const { roomId } = body;
getMessages(roomId).then((messages) => {
res.status(statusCode.OK).json(messages.data);
res.status(statusCode.OK).json(messages);
}).catch((err) => {
console.log(err)
res.status(statusCode.INTERNAL_SERVER_ERROR);

View File

@@ -12,9 +12,11 @@ async function registerMailbox(userId, pwd, xoauth, xoauth2, host, port, tls) {
async function getMailboxes() {
const query = `
SELECT app_account.account_id AS id, address.email
FROM app_account INNER JOIN address
WHERE address.address_id = app_account.user_id
SELECT
app_account.account_id AS id,
address.email
FROM app_account INNER JOIN address
WHERE address.address_id = app_account.user_id
`;
const values = [];
return await execQueryAsync(query, values);
@@ -46,31 +48,68 @@ async function getRooms(mailboxId) {
async function getMessages(roomId) {
// todo attachements name
// todo html, text, textAsHtml
// todo datetime
const query = `
SELECT
address_field.address_id,
bodypart.text,
header_field.value
FROM bodypart
INNER JOIN header_field
INNER JOIN address_field
INNER JOIN field_name
msg.message_id AS id,
GROUP_CONCAT(fromT.address_id) AS fromA,
GROUP_CONCAT(toT.address_id) AS toA,
GROUP_CONCAT(ccT.address_id) AS ccA,
subjectT.value AS subject,
content.text AS content,
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
(
header_field.field_id = field_name.field_id OR
address_field.field_id = field_name.field_id
) AND
(
field_name.field_name = 'html' OR
field_name.field_name = 'text' OR
field_name.field_name = 'textAsHtml' OR
field_name.field_name = 'to' OR
field_name.field_name = 'cc' OR
field_name.field_name = 'subject'
)
`;
// todo roomID
const values = [];
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
LEFT JOIN (
SELECT header_field.message_id, header_field.value
FROM header_field
INNER JOIN field_name
WHERE
field_name.field_id = header_field.field_id AND
field_name.field_name = 'subject'
) subjectT ON msg.message_id = subjectT.message_id
LEFT JOIN (
SELECT bodypart.text, header_field.message_id FROM bodypart
INNER JOIN header_field
INNER JOIN field_name
WHERE
field_name.field_id = header_field.field_id AND
field_name.field_name = 'html' AND
bodypart.bodypart_id = header_field.bodypart_id
) content ON msg.message_id = content.message_id
INNER JOIN message ON message.message_id = msg.message_id
WHERE msg.room_id = ?
GROUP BY msg.message_id;
`;
const values = [roomId];
return await execQueryAsync(query, values);
}

View File

@@ -14,10 +14,10 @@ async function registerMessage(timestamp, rfc822size, messageId) {
function registerMailbox_message(mailboxId, uid, messageId, modseq, seen, deleted) {
const query = `
INSERT IGNORE INTO mailbox_message
(mailbox_id, uid, message_id, modseq, seen, deleted) VALUES (1, 19, 10, '12450', 0, 0)
(mailbox_id, uid, message_id, modseq, seen, deleted) VALUES (?, ?, ?, ?, ?, ?)
`;
// todo
const values = [mailboxId, uid, messageId, modseq, seen, deleted];
console.log(values)
execQuery(query, values);
}

View File

@@ -9,7 +9,7 @@ async function createRoom(roomName, ownerId, messageId) {
}
async function registerMessageInRoom(messageId, roomId, isSeen) {
const query = `INSERT INTO app_space_message (message_id, room_id) VALUES (?, ?)`;
const query = `INSERT IGNORE INTO app_room_message (message_id, room_id) VALUES (?, ?)`;
const values = [messageId, roomId];
await execQueryAsync(query, values);
@@ -68,8 +68,8 @@ async function isRoomGroup(roomId) {
});
}
async function findSpacesFromMessage(messageId) {
const query = `SELECT room_id, thread_id FROM app_space_message WHERE message_id = '${messageId}'`;
async function findRoomsFromMessage(messageId) {
const query = `SELECT room_id FROM app_room_message WHERE message_id = '${messageId}'`;
return await execQueryAsync(query);
}
@@ -85,5 +85,5 @@ module.exports = {
createThread,
registerMessageInThread,
isRoomGroup,
findSpacesFromMessage,
findRoomsFromMessage,
};

View File

@@ -131,25 +131,24 @@ CREATE TABLE app_room (
-- 12
CREATE TABLE app_thread (
thread_id INT AUTO_INCREMENT,
room_id INT NOT NULL,
thread_name VARCHAR(255) NOT NULL,
notSeen INT NOT NULL DEFAULT 0,
lastUpdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
parent_room_id INT,
root_room_id INT,
isDm BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (thread_id),
FOREIGN KEY (room_id) REFERENCES app_room(room_id) ON DELETE CASCADE
PRIMARY KEY (room_id),
UNIQUE KEY (room_id, parent_room_id, root_room_id),
FOREIGN KEY (room_id) REFERENCES app_room(room_id) ON DELETE CASCADE,
FOREIGN KEY (parent_room_id) REFERENCES app_room(room_id) ON DELETE SET NULL,
FOREIGN KEY (root_room_id) REFERENCES app_room(room_id) ON DELETE SET NULL
);
-- 13
CREATE TABLE app_space_message (
CREATE TABLE app_room_message (
message_id INT NOT NULL,
room_id INT,
thread_id INT,
UNIQUE KEY (message_id, room_id, thread_id),
UNIQUE KEY (message_id, room_id),
FOREIGN KEY (message_id) REFERENCES message(message_id) ON DELETE CASCADE,
FOREIGN KEY (room_id) REFERENCES app_room(room_id) ON DELETE SET NULL,
FOREIGN KEY (thread_id) REFERENCES app_thread(thread_id) ON DELETE SET NULL
FOREIGN KEY (room_id) REFERENCES app_room(room_id) ON DELETE SET NULL
);
-- 14
@@ -159,11 +158,3 @@ CREATE TABLE app_room_member (
FOREIGN KEY (room_id) REFERENCES app_room(room_id) ON DELETE CASCADE,
FOREIGN KEY (member_id) REFERENCES address(address_id)
);
-- 14
CREATE TABLE app_thread_member (
thread_id INT NOT NULL,
member_id INT NOT NULL,
FOREIGN KEY (thread_id) REFERENCES app_thread(thread_id) ON DELETE CASCADE,
FOREIGN KEY (member_id) REFERENCES address(address_id)
);

View File

@@ -28,6 +28,7 @@ if (shouldReset) {
execQuery("SET FOREIGN_KEY_CHECKS=0");
results.map((table) => {
execQuery("DELETE FROM " + table.table_name);
// execQuery("DROP TABLE " + table.table_name);
});
});
return;

View File

@@ -19,6 +19,7 @@ const validate_mailbox = ajv.compile(schema_mailbox);
*/
router.get("/mailboxes", (req, res) => {
getMailboxes().then((data) => {
data[0].id = 1; // todo debug
res.status(statusCodes.OK).json(data)
});
});
@@ -49,9 +50,10 @@ router.get("/:roomId/messages", async (req, res) => {
* Register a new mailbox inside the app
*/
router.post("/mailbox", async (req, res) => {
console.log(req.body)
const valid = validate_mailbox(req.body);
if (!valid) {
res.status(statusCodes.NOT_ACCEPTABLE).send(validate_mailbox.errors)
res.status(statusCodes.NOT_ACCEPTABLE).send({ error: validate_mailbox.errors });
} else {
await addMailbox(req.body, res);
}