start on repond to message (basic input and builder for dm)
This commit is contained in:
27
back/abl/Room-abl.ts
Normal file
27
back/abl/Room-abl.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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 emailManager from "../mails/EmailManager";
|
||||
import MailBuilder from "../mails/utils/mailBuilder";
|
||||
|
||||
export default class Room {
|
||||
// todo change name
|
||||
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
|
||||
} else if (roomType === RoomType.GROUP || roomType === RoomType.THREAD) {
|
||||
// get all cc and to from of previous message and add them
|
||||
} else {
|
||||
res.status(statusCode.FORBIDDEN).send({ error: "Cannot add a new message in a room or a channel." });
|
||||
}
|
||||
}
|
||||
}
|
||||
12
back/db/Room-db.ts
Normal file
12
back/db/Room-db.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { execQueryAsync } from "./db";
|
||||
|
||||
export async function getRoomOwner(roomId: number) {
|
||||
const query = `
|
||||
SELECT address.email
|
||||
FROM app_room
|
||||
INNER JOIN address ON address.address_id = app_room.owner_id
|
||||
WHERE app_room.room_id = ?
|
||||
`;
|
||||
const values = [roomId];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
@@ -5,30 +5,32 @@ import { getAllAccounts } from "../db/imap/imap-db";
|
||||
|
||||
export interface Account {
|
||||
id: number;
|
||||
user: string
|
||||
password?: string
|
||||
user: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
class EmailManager {
|
||||
imapInstances: ImapInstance[]
|
||||
smtpInstances: SmtpInstance[]
|
||||
|
||||
imapInstances: ImapInstance[];
|
||||
smtpInstances: SmtpInstance[];
|
||||
|
||||
constructor() {
|
||||
this.imapInstances = [];
|
||||
this.smtpInstances = [];
|
||||
}
|
||||
|
||||
init() {
|
||||
getAllAccounts().then((accounts: Account[]) => {
|
||||
for (let i = 0; i < accounts.length; i++) {
|
||||
accounts[i].password = accounts[i]?.password?.toString().replace(/[\u{0080}-\u{FFFF}]/gu,"");
|
||||
if (accounts[i].id == 2) continue; //debug_todo
|
||||
this.addImapInstance(accounts[i]);
|
||||
this.addSmtpInstance(accounts[i]);
|
||||
}
|
||||
}).catch((err) => {
|
||||
logger.err(err);
|
||||
});
|
||||
getAllAccounts()
|
||||
.then((accounts: Account[]) => {
|
||||
for (let i = 0; i < accounts.length; i++) {
|
||||
accounts[i].password = accounts[i]?.password?.toString().replace(/[\u{0080}-\u{FFFF}]/gu, "");
|
||||
if (accounts[i].id == 2) continue; //debug_todo
|
||||
this.addImapInstance(accounts[i]);
|
||||
this.addSmtpInstance(accounts[i]);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.err(err);
|
||||
});
|
||||
}
|
||||
|
||||
addImapInstance(config) {
|
||||
@@ -38,7 +40,11 @@ class EmailManager {
|
||||
addSmtpInstance(config) {
|
||||
this.smtpInstances.push(new SmtpInstance(config));
|
||||
}
|
||||
|
||||
getSmtp(email: string): SmtpInstance | undefined {
|
||||
return this.smtpInstances.find((instance) => instance.user == email);
|
||||
}
|
||||
}
|
||||
|
||||
const emailManager = new EmailManager();
|
||||
export default emailManager;
|
||||
export default emailManager;
|
||||
|
||||
@@ -3,9 +3,11 @@ import nodemailer, { Transporter } from "nodemailer";
|
||||
|
||||
export class SmtpInstance {
|
||||
transporter: Transporter;
|
||||
user: string;
|
||||
|
||||
constructor(account: {user: string, password: string}) {
|
||||
constructor(account: { user: string; password: string }) {
|
||||
// todo store other data
|
||||
this.user = account.user;
|
||||
this.transporter = nodemailer.createTransport({
|
||||
host: "smtp.gmail.com",
|
||||
port: 465,
|
||||
@@ -17,20 +19,22 @@ export class SmtpInstance {
|
||||
});
|
||||
}
|
||||
|
||||
sendMail() {
|
||||
const msg = {
|
||||
from: "",
|
||||
to: "",
|
||||
subject: "Hello ✔",
|
||||
text: "Hello world?",
|
||||
html: "<b>Hello world?</b>",
|
||||
};
|
||||
this.transporter.sendMail(msg, (err, message) => {
|
||||
if (err) {
|
||||
logger.err(err);
|
||||
throw err;
|
||||
}
|
||||
logger.log(message);
|
||||
});
|
||||
sendMail(message: any) {
|
||||
console.log(this.user)
|
||||
console.log(message)
|
||||
// const msg = {
|
||||
// from: "",
|
||||
// to: "",
|
||||
// subject: "Hello ✔",
|
||||
// text: "Hello world?",
|
||||
// html: "<b>Hello world?</b>",
|
||||
// };
|
||||
// this.transporter.sendMail(msg, (err, message) => {
|
||||
// if (err) {
|
||||
// logger.err(err);
|
||||
// throw err;
|
||||
// }
|
||||
// logger.log(message);
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
46
back/mails/utils/mailBuilder.ts
Normal file
46
back/mails/utils/mailBuilder.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
export default class MailBuilder {
|
||||
message: any;
|
||||
constructor(message = {}) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
from(addresses: string[] | string): MailBuilder {
|
||||
this.message.from = addresses;
|
||||
return this;
|
||||
}
|
||||
|
||||
to(addresses: string[] | string): MailBuilder {
|
||||
this.message.to = addresses;
|
||||
return this;
|
||||
}
|
||||
|
||||
cc(addresses: string[] | string): MailBuilder {
|
||||
this.message.cc = addresses;
|
||||
return this;
|
||||
}
|
||||
|
||||
bcc(addresses: string[] | string): MailBuilder {
|
||||
this.message.bcc = addresses;
|
||||
return this;
|
||||
}
|
||||
|
||||
subject(subject: string): MailBuilder {
|
||||
this.message.subject = subject;
|
||||
return this;
|
||||
}
|
||||
|
||||
text(textContent: string): MailBuilder {
|
||||
this.message.text = textContent;
|
||||
return this;
|
||||
}
|
||||
|
||||
html(htmlContent: string): MailBuilder {
|
||||
this.message.html = htmlContent;
|
||||
return this;
|
||||
}
|
||||
|
||||
inReplyTo(messageID: string): MailBuilder {
|
||||
this.message.inReplyTo = messageID;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,12 @@ import express from "express";
|
||||
const router = express.Router();
|
||||
|
||||
import { rooms } from "../abl/rooms";
|
||||
import Message from "../abl/Messages-abl";
|
||||
import Message from "../abl/Message-abl";
|
||||
import { messages } from "../abl/messages";
|
||||
import { members } from "../abl/members";
|
||||
import Account from "../abl/Account-abl";
|
||||
import validator from "../validator/validator";
|
||||
import Room from "../abl/Room-abl";
|
||||
|
||||
/**
|
||||
* Return all mailboxes and folders for an user
|
||||
@@ -52,4 +53,8 @@ router.post("/removeFlag", async (req, res) => {
|
||||
await validator.validate("removeFlag", req.body, res, Message.removeFlag);
|
||||
});
|
||||
|
||||
router.post("/response", async (req, res) => {
|
||||
await validator.validate("response", req.body, res, Room.response);
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
21
back/validator/schemas/response-schema.json
Normal file
21
back/validator/schemas/response-schema.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user": {
|
||||
"type": "string"
|
||||
},
|
||||
"roomId": {
|
||||
"type": "number"
|
||||
},
|
||||
"text": {
|
||||
"type": "string"
|
||||
},
|
||||
"html": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"user", "roomId", "text", "html"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import getRoomSchema from "./schemas/getRooms-schema.json";
|
||||
import getMessagesSchema from "./schemas/getMessages-schema.json";
|
||||
import getMembersSchema from "./schemas/getMembers-schema.json";
|
||||
import setFlagSchema from "./schemas/setFlag-schema.json";
|
||||
import responseSchema from "./schemas/response-schema.json";
|
||||
import { Request, Response } from "express";
|
||||
import statusCodes from "../utils/statusCodes";
|
||||
import logger from "../system/Logger";
|
||||
@@ -20,6 +21,7 @@ class Validator {
|
||||
validateGetMessages: any;
|
||||
validateGetMembers: any;
|
||||
validateSetFlag: any;
|
||||
validateResponse: any;
|
||||
|
||||
constructor() {
|
||||
this.validateCreateAccount = ajv.compile(createAccountSchema);
|
||||
@@ -28,6 +30,7 @@ class Validator {
|
||||
this.validateGetMessages = ajv.compile(getMessagesSchema);
|
||||
this.validateGetMembers = ajv.compile(getMembersSchema);
|
||||
this.validateSetFlag = ajv.compile(setFlagSchema);
|
||||
this.validateResponse = ajv.compile(responseSchema);
|
||||
}
|
||||
|
||||
_getSchema(name: string): any {
|
||||
@@ -45,6 +48,8 @@ class Validator {
|
||||
case "addFlag":
|
||||
case "removeFlag":
|
||||
return this.validateSetFlag;
|
||||
case "response":
|
||||
return this.validateResponse;
|
||||
default:
|
||||
logger.err(`Schema ${name} not found`);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user