mail/back/mails/saveMessage.js

96 lines
4.0 KiB
JavaScript

const {
createRoom,
registerMessageInRoom,
createThread,
registerMessageInThread,
isRoomGroup,
findRoomsFromMessage,
hasSameMembersAsParent,
} = require("../db/saveMessageApp");
const { findRoomByOwner, getAddresseId } = require("../db/mail");
const { isDmOnEnvelope } = require("./utils/statusUtils");
/**
* take object address and join mailbox and host to return mailbox@host
*/
function createAddress(elt) {
return `${elt.mailbox}@${elt.host}`;
}
async function registerMessageInApp(messageId, attrs) {
const isSeen = attrs.flags.includes("Seen") ? 1 : 0; // todo verify
const envelope = attrs.envelope;
await getAddresseId(createAddress(envelope.sender[0])).then(async (ownerId) => {
if (envelope.inReplyTo) {
await registerReplyMessage(envelope, messageId, isSeen, ownerId);
} else {
await findRoomByOwner(ownerId).then(async (res) => {
if (res.length == 0) {
// first message of this sender
await createRoom(envelope.subject, ownerId, messageId).then(async (roomId) => {
await registerMessageInRoom(messageId, roomId, isSeen, envelope.date);
});
} else {
// not a reply, add to the list of message if this sender
await registerMessageInRoom(messageId, res[0].room_id, isSeen, envelope.date);
}
});
}
});
}
async function registerReplyMessage(envelope, messageId, isSeen, ownerId) {
const messageID = envelope.messageId;
await findRoomsFromMessage(messageId).then(async (rooms) => {
if (rooms.length == 0) {
// no rooms, so is a transfer
// todo test if members of transferred message are included
} else if (rooms.length === 1) {
// only one room so message is only in a room and not in a thread
// as a thread is associated to a room to begin
await isRoomGroup(rooms[0].room_id).then(async (isGroup) => {
if (isGroup) {
const hasSameMembers = await hasSameMembersAsParent(messageID, envelope.inReplyTo);
if (hasSameMembers) {
await registerMessageInRoom(messageId, rooms[0].room_id, isSeen, envelope.date);
} else {
// is a group and has not the same member as the previous message
// some recipient has been removed create a thread
const isDm = isDmOnEnvelope(envelope);
await createThread(envelope.subject, ownerId, messageId, rooms[0].room_id, isDm).then(async (threadId) => {
await registerMessageInThread(messageId, threadId, isSeen);
});
}
} else {
// reply from channel
// todo
// if (sender == owner) { // correction from the original sender
// // leave in the same channel
// }
}
});
} else if (rooms.length > 1) {
// get the lowest thread (order by room_id)
const room = rooms[rooms.length-1];
const hasSameMembers = await hasSameMembersAsParent(messageID, envelope.inReplyTo);
if (hasSameMembers) {
await registerMessageInThread(messageId, room.room_id, isSeen);
} else {
// has not the same members so it is a derivation of this thread
// todo put this in a function and add default message in the reply chain
const isDm = isDmOnEnvelope(envelope);
await createThread(envelope.subject, ownerId, messageId, room.room_id, isDm).then(async (threadId) => {
await registerMessageInThread(messageId, threadId, isSeen);
});
}
}
});
}
module.exports = {
registerMessageInApp
};