start to load messages from rooms
This commit is contained in:
16
back/controllers/messages.js
Normal file
16
back/controllers/messages.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const statusCode = require("../utils/statusCodes").statusCodes;
|
||||
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);
|
||||
}).catch((err) => {
|
||||
console.log(err)
|
||||
res.status(statusCode.INTERNAL_SERVER_ERROR);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
messages,
|
||||
};
|
||||
@@ -4,7 +4,6 @@ const { getRooms } = require("../db/api.js");
|
||||
async function rooms(body, res) {
|
||||
const { mailboxId, offset, limit } = body;
|
||||
getRooms(mailboxId).then((rooms) => {
|
||||
console.log(rooms)
|
||||
res.status(statusCode.OK).json(rooms);
|
||||
}).catch((err) => {
|
||||
console.log(err)
|
||||
|
||||
@@ -44,8 +44,39 @@ async function getRooms(mailboxId) {
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
async function getMessages(roomId) {
|
||||
// todo attachements name
|
||||
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
|
||||
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 = [];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
registerMailbox,
|
||||
getMailboxes,
|
||||
getRooms
|
||||
getRooms,
|
||||
getMessages
|
||||
};
|
||||
|
||||
@@ -16,6 +16,7 @@ function registerMailbox_message(mailboxId, uid, messageId, modseq, seen, delete
|
||||
INSERT IGNORE INTO mailbox_message
|
||||
(mailbox_id, uid, message_id, modseq, seen, deleted) VALUES (1, 19, 10, '12450', 0, 0)
|
||||
`;
|
||||
// todo
|
||||
const values = [mailboxId, uid, messageId, modseq, seen, deleted];
|
||||
execQuery(query, values);
|
||||
}
|
||||
@@ -30,7 +31,7 @@ function registerBodypart(messageId, part, bodypartId, bytes, nbLines) {
|
||||
}
|
||||
|
||||
async function saveBodypart(bytes, hash, text, data) {
|
||||
const query = `INSERT IGNORE INTO bodypart (bytes, hash, text, data) VALUES (?, ?, ?,)`;
|
||||
const query = `INSERT IGNORE INTO bodypart (bytes, hash, text, data) VALUES (?, ?, ?, ?)`;
|
||||
const values = [bytes, hash, text, data];
|
||||
return await execQueryAsyncWithId(query, values);
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ async function saveFromParsedData(parsed, messageId) {
|
||||
}),
|
||||
);
|
||||
} else if (["subject", "inReplyTo"].includes(key)) {
|
||||
// todo : "references"
|
||||
// todo : "references" (array)
|
||||
promises.push(
|
||||
getFieldId(key).then(async (fieldId) => {
|
||||
await saveHeader_fields(messageId, fieldId, undefined, undefined, parsed[key]);
|
||||
@@ -92,22 +92,22 @@ async function saveFromParsedData(parsed, messageId) {
|
||||
} else if (["html", "text", "textAsHtml"].includes(key)) {
|
||||
const hash = "0";
|
||||
const size = "0";
|
||||
// saveBodypart(size, hash, parsed[key], "").then((bodypartId) => {
|
||||
// getFieldId(key).then((fieldId) => {
|
||||
// saveHeader_fields(
|
||||
// messageId,
|
||||
// fieldId,
|
||||
// bodypartId,
|
||||
// undefined, // todo ?
|
||||
// undefined
|
||||
// );
|
||||
// });
|
||||
// });
|
||||
saveBodypart(size, hash, parsed[key], "").then((bodypartId) => {
|
||||
getFieldId(key).then((fieldId) => {
|
||||
saveHeader_fields(
|
||||
messageId,
|
||||
fieldId,
|
||||
bodypartId,
|
||||
undefined, // todo ?
|
||||
undefined
|
||||
);
|
||||
});
|
||||
});
|
||||
} else if (key == "attachments") {
|
||||
// todo
|
||||
} else if (["date", "messageId", "headers", "headerLines"].includes(key)) {
|
||||
// messageId and date are already saved
|
||||
// other field are not improted and can be retrieved in source
|
||||
// other field are not important and can be retrieved in source
|
||||
return;
|
||||
} else {
|
||||
DEBUG.log("doesn't know key: " + key);
|
||||
|
||||
@@ -10,6 +10,7 @@ const schema_mailbox = require("../schemas/mailbox_schema.json");
|
||||
const { addMailbox } = require("../controllers/addMailbox.js");
|
||||
const { getMailboxes } = require("../db/api.js");
|
||||
const { rooms } = require("../controllers/rooms.js");
|
||||
const { messages } = require("../controllers/messages.js");
|
||||
|
||||
const validate_mailbox = ajv.compile(schema_mailbox);
|
||||
|
||||
@@ -37,6 +38,13 @@ router.get("/:mailboxId/rooms", async (req, res) => {
|
||||
|
||||
});
|
||||
|
||||
router.get("/:roomId/messages", async (req, res) => {
|
||||
const { roomId } = req.params;
|
||||
console.log("called")
|
||||
// todo check token
|
||||
await messages(req.params, res);
|
||||
});
|
||||
|
||||
/**
|
||||
* Register a new mailbox inside the app
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user