diff --git a/.gitignore b/.gitignore index 10c6986..f02cbb7 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,6 @@ pnpm-debug.log* log* config.json .direnv -.envrc \ No newline at end of file +.envrc +*.txt +*.json \ No newline at end of file diff --git a/back/imap/index.js b/back/imap/index.js index 5c537fa..4719bb6 100644 --- a/back/imap/index.js +++ b/back/imap/index.js @@ -22,7 +22,7 @@ imap.once("ready", function () { console.log(results[results.length - 1]); }); - const f = imap.fetch(969, { size: true, struct: true, envelope: true }); + const f = imap.fetch(970, { bodies: ['TEXT'], size: true, struct: true, envelope: true }); // var f = imap.seq.fetch('1:3', { // bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)', // struct: true @@ -30,22 +30,23 @@ imap.once("ready", function () { f.on("message", function (msg, seqno) { // console.log("Message #%d", seqno); // var prefix = "(#" + seqno + ") "; - // msg.on("body", function (stream, info) { - // simpleParser(stream, async (err, parsed) => { - // // find box id; - // console.log(parsed) - // const boxId = 1; - // // saveMessage(parsed, boxId); - // console.log(parsed.subject); - // fs.writeFileSync("./test.json", JSON.stringify(parsed)); - // }); - // // console.log(prefix + 'Body'); - // // stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt')); - // }); + msg.on("body", function (stream, info) { + simpleParser(stream, async (err, parsed) => { + // find box id; + console.log(parsed) + const boxId = 1; + // saveMessage(parsed, boxId); + console.log(parsed.subject); + fs.writeFileSync("./test.txt", JSON.stringify(parsed)); + }); + // console.log(prefix + 'Body'); + // stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt')); + }); msg.once('attributes', attrs => { // todo find boxId const boxId = 1; - saveMessage(attrs, boxId, imap); + console.log(attrs) + // saveMessage(attrs, boxId, imap); }); }); diff --git a/back/imap/storeMessage.js b/back/imap/storeMessage.js index 6de75e1..555aba3 100644 --- a/back/imap/storeMessage.js +++ b/back/imap/storeMessage.js @@ -95,15 +95,30 @@ function saveMessage(attrs, mailboxId, imap) { } }); - // save more header - // message.headerLines.forEach(elt => { - // const newKey = keyNormalizer(elt.key); - // getFieldId(newKey).then((fieldId) => { - // saveHeader_fields(messageId, part, 2, fieldId, elt[line]); - // }); - // }); + // todo check for different provider + if (envelope?.inReplyTo) { + `SELECT app_room_messages.room, app_room_messages.thread FROM app_room_messages INNER JOIN messages WHERE messages.messageID = '${envelope.inReplyTo}' AND app_room_messages.message = messages.id`; + // parent message is in a thread, so register this message only in a thread + if (thread) { + `INSERT IGNORE INTO app_room_messages (thread, message) VALUES('${thread}', '${messageId}')`; + // increment not read counter and lastUpdate is not read (both) + } else if (room) { + + if (!isGroup) { + // check if create thread + } else { + // check if create new group and delete thread + } + } else { + + } + } + /** + * if reply to then add to same group as previous and update type if necessary + * else add to sender group + * + */ - // // todo add date field } ); } diff --git a/back/routers/mail.js b/back/routers/mail.js index b0c944d..000f834 100644 --- a/back/routers/mail.js +++ b/back/routers/mail.js @@ -1,13 +1,30 @@ const statusCodes = require("../utils/statusCodes.js").statusCodes; +const express = require('express'); +const router = require('router'); /** * Return all mailboxes and folders for an user */ -function getMailboxes(req, res) { +router.get("/mailboxes", (req, res) => { const {token} = req.params; const query = ``; -} -module.exports = { - getFolders, -} +}); + +/** + * @param {number} mailboxId the id of the mailbox from which to fetch the messages, 0 if from all + * @param {number} offset the offset of the query + * @param {number} limit the number of message to return + * @param {string} token the token of the user + * @return {object} a list of room and their preview + */ +router.get("/{mailboxId}/messages", (req, res) => { + const {token, mailboxId, offset, limit} = req.params; + // todo check token + // todo use offset + const query = ``; + SELECT header_fields.value FROM header_fields INNER JOIN field_names WHERE header_fields.field = field_names.id AND (field_names.name = "subject" OR field_names.name = "date"); ORDER BY messages.idate + // number of message missed in the room +}); + +module.exports = router; diff --git a/back/server.js b/back/server.js index 4dcdfd3..d54b625 100644 --- a/back/server.js +++ b/back/server.js @@ -1,4 +1,4 @@ -const mails = require("./api/mails.js"); +const mails = require("./routers/mail.js"); const express = require('express'); const cors = require('cors') diff --git a/back/sql/database.dart b/back/sql/database.dart index 335c16d..ffd2f83 100644 --- a/back/sql/database.dart +++ b/back/sql/database.dart @@ -25,6 +25,7 @@ Table "mailboxes" { Table "messages" { "id" int [pk, not null, increment] + "messageID" text [pk, not null] "idate" timestamp [not null] "rfc822size" int } @@ -103,3 +104,37 @@ Ref: address_fields.message > messages.id Ref: address_fields.part > part_numbers.part Ref: address_fields.field > field_names.id Ref: address_fields.address > addresses.id + +// app table +Table "front_threads" { + "id" int [pk, not null, increment] + "room" int [not null] + "name" text + "notSeen" int [not null, default: true] + "lastUpdate" timestamp [not null] + "isDm" bool [not null, default: true] +} + +Ref: front_threads.room > front_rooms.id + +Table "front_rooms" { + "id" int [pk, not null, increment] + "name" text + "isGroup" bool [not null, default: false] + "notSeen" int [not null] + "lastUpdate" timestamp [not null] +} + +Table "front_room_messages" { + "room" int [not null] + "thread" int [not null] + "message" int [not null] +} + +Ref: front_room_messages.room > front_rooms.id +Ref: front_room_messages.message - messages.id +Ref: front_room_messages.thread > front_threads.id + + + + diff --git a/back/sql/database.png b/back/sql/database.png index 97d79fd..2b16605 100644 Binary files a/back/sql/database.png and b/back/sql/database.png differ diff --git a/front/babel.config.js b/front/babel.config.js index 2b7083e..e69de29 100644 --- a/front/babel.config.js +++ b/front/babel.config.js @@ -1,3 +0,0 @@ - - -