reply message from thread and room
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Response } from "express";
|
||||
import { getAccounts, registerAccount } from "../db/api-db";
|
||||
import { getAddresseId } from "../db/utils/mail";
|
||||
import { getAddressId } from "../db/utils/mail";
|
||||
import statusCodes from "../utils/statusCodes";
|
||||
|
||||
export default class Account {
|
||||
@@ -12,7 +12,7 @@ export default class Account {
|
||||
|
||||
static async register(body, res: Response) {
|
||||
const { email, pwd, xoauth, xoauth2, host, port, tls } = body;
|
||||
getAddresseId(email).then((addressId) => {
|
||||
getAddressId(email).then((addressId) => {
|
||||
registerAccount(addressId, pwd, xoauth, xoauth2, host, port, tls)
|
||||
.then((mailboxId) => {
|
||||
res.status(statusCodes.OK).json({ id: mailboxId });
|
||||
|
||||
@@ -2,24 +2,58 @@ import statusCode from "../utils/statusCodes";
|
||||
import { Response } from "express";
|
||||
import { RoomType } from "../mails/message/saveMessage";
|
||||
import { getRoomType } from "../db/message/saveMessage-db";
|
||||
import { getRoomOwner } from "../db/Room-db";
|
||||
import { getLastMsgData, getRoomOwner } from "../db/Room-db";
|
||||
import emailManager from "../mails/EmailManager";
|
||||
import MailBuilder from "../mails/utils/mailBuilder";
|
||||
import { getAddresses } from "../db/utils/mail";
|
||||
|
||||
function rmUserFromAddrs(addresses: { email: string }[], user: string) {
|
||||
let index = addresses.findIndex((a) => a.email == user);
|
||||
if (index != -1) {
|
||||
addresses.splice(index, 1);
|
||||
}
|
||||
}
|
||||
export default class Room {
|
||||
// todo change name
|
||||
// todo change name of reponse
|
||||
static async response(body, res: Response) {
|
||||
const { user, roomId, text, html } = body;
|
||||
console.log(body)
|
||||
const roomType = (await getRoomType(roomId))[0].room_type;
|
||||
|
||||
if (roomType === RoomType.DM) {
|
||||
const ownerEmail = (await getRoomOwner(roomId))[0].email;
|
||||
|
||||
const mailBuilder = new MailBuilder();
|
||||
mailBuilder.from(user).to(ownerEmail).text(text).html(html);
|
||||
|
||||
emailManager.getSmtp(user).sendMail(mailBuilder.message);
|
||||
// send new msg to recipient of dm
|
||||
res.status(statusCode.OK).send();
|
||||
} else if (roomType === RoomType.GROUP || roomType === RoomType.THREAD) {
|
||||
// get all cc and to from of previous message and add them
|
||||
const lastMsgData = (await getLastMsgData(roomId))[0];
|
||||
console.log(lastMsgData);
|
||||
|
||||
const mailBuilder = new MailBuilder();
|
||||
mailBuilder.inReplySubject(lastMsgData.subject).inReplyTo(lastMsgData.messageID).text(text).html(html);
|
||||
|
||||
const from = await getAddresses(lastMsgData.fromA);
|
||||
let to = lastMsgData.toA ? await getAddresses(lastMsgData.toA) : [];
|
||||
let cc = lastMsgData.ccA ? await getAddresses(lastMsgData.ccA) : [];
|
||||
|
||||
// remove us from recipients
|
||||
rmUserFromAddrs(to, user);
|
||||
rmUserFromAddrs(from, user);
|
||||
|
||||
// add sender of previous as recipient if it is not us
|
||||
if (from.findIndex((a) => a.email == user) == -1) {
|
||||
to = to.concat(from);
|
||||
}
|
||||
|
||||
mailBuilder
|
||||
.from(user)
|
||||
.to(to.map((a) => a.email))
|
||||
.cc(cc.map((a) => a.email));
|
||||
|
||||
emailManager.getSmtp(user).sendMail(mailBuilder.message);
|
||||
res.status(statusCode.OK).send();
|
||||
} else {
|
||||
res.status(statusCode.FORBIDDEN).send({ error: "Cannot add a new message in a room or a channel." });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user