diff --git a/back/mails/saveMessage.js b/back/mails/saveMessage.js index 41fb2c5..03fa076 100644 --- a/back/mails/saveMessage.js +++ b/back/mails/saveMessage.js @@ -22,13 +22,6 @@ function createAddress(elt) { return `${elt.mailbox}@${elt.host}`; } -async function initiateRoom(envelope, ownerId, messageId, isSeen) { - await createRoom(envelope.subject, ownerId, messageId).then(async (roomId) => { - // todo register members - await registerMessageInRoom(messageId, roomId, isSeen, envelope.date); - }); -} - const roomType = { ROOM: 0, CHANNEL: 1, @@ -104,7 +97,6 @@ class registerMessageInApp { await registerMessageInRoom(this.messageId, roomId, this.isSeen, this.envelope.date); await this.registerMembers(roomId); }); - } async createOrRegisterOnMembers(roomId) { @@ -134,10 +126,10 @@ class registerMessageInApp { } else { // it is not a reply and not a dm // so it is a channel, which can be possibly a group - initiateRoom(this.envelope, this.ownerId, this.messageId, this.isSeen); + this.initiateRoom(this.ownerId, roomType.ROOM); } } else { - await this.createOrRegisterOnExistence(this.ownerId, roomType.CHANNEL); + await this.createOrRegisterOnExistence(this.ownerId, roomType.ROOM); } } } diff --git a/back/test/mail/saveMessage-test.js b/back/test/mail/saveMessage-test.js index 1898ccb..bbe4087 100644 --- a/back/test/mail/saveMessage-test.js +++ b/back/test/mail/saveMessage-test.js @@ -5,34 +5,83 @@ const { registerMessageInApp, roomType } = require("../../mails/saveMessage"); jest.mock("../../db/mail"); // todo mock db +// new message from us +// to multiple people -> room +// if response has same member => group +// if response is dm => channel +// to one person => dm + +// new message from other +// to only me -> room +// if no reply to multiple message => channel +// else => dm +// to multiple people -> room +// // make it better +// if multiple members reply -> group +// if only me reply -> channel describe("saveMessage", () => { - describe("is not a reply", () => { - it("DM from user should be assigned to other user", async () => { - const users = generateUsers(2); - const attrs = generateAttrs({ from: [users[0].user], to: [users[1].user] }); + describe("new first message", () => { - getUserIdOfMailbox.mockReturnValue([{ user_id: users[0].id }]); - getAddresseId.mockImplementation((email) => { - const match = users.find((user) => user.user.mailbox + "@" + user.user.host == email); - return match.id; + const users = generateUsers(5); + const ownUser = users[0]; + const messageId = 1; + const boxId = 1; + getUserIdOfMailbox.mockReturnValue([{ user_id: ownUser.id }]); + getAddresseId.mockImplementation((email) => { + const match = users.find((user) => user.user.mailbox + "@" + user.user.host == email); + return match.id; + }); + + describe("from us", () => { + it("new first message from us to one recipient should create a DM", async () => { + const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user] }); + + const register = new registerMessageInApp(messageId, attrs, boxId); + + const createOrRegisterOnExistence = jest + .spyOn(register, "createOrRegisterOnExistence") + .mockImplementation(() => undefined); + + await register.save(); + + expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM); }); + it("new first message from us to multiple recipients should create a room", async () => { + const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user, users[2].user] }); + + const register = new registerMessageInApp(messageId, attrs, boxId); + + const initiateRoom = jest + .spyOn(register, "initiateRoom") + .mockImplementation(() => undefined); + + await register.save(); + + expect(initiateRoom).toHaveBeenCalledWith(ownUser.id, roomType.ROOM); + }); + it("response to new first message to multiple recipients with same members should change room type to GROUP", () => { - const register = new registerMessageInApp(1, attrs, 1); + }); + it("response to new first message to multiple recipients with different members should change room type to CHANNEL", () => { - const createOrRegisterOnExistence = jest - .spyOn(register, "createOrRegisterOnExistence") - .mockImplementation(() => undefined); - - await register.save(); - - expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM); + }); }); - - it("DM message from user should be added to DM room", async () => { - // todo multiple messages + describe("from other", () => { + it("new first message from other to me only should create a room", async () => { + const attrs = generateAttrs({ from: [users[1].user], to: [ownUser.user] }); + + const register = new registerMessageInApp(messageId, attrs, boxId); + + const createOrRegisterOnExistence = jest + .spyOn(register, "createOrRegisterOnExistence") + .mockImplementation(() => undefined); + + await register.save(); + + expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.ROOM); + }); }); - // it("first GROUP message should create a group", () => {}); }); describe("replies", () => { it("", () => {});