Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

730 email notify server config #733

Merged
merged 7 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/backend/src/config/getDeliveryServiceProperties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ describe('ReadDeliveryServiceProperties', () => {
messageTTL: 12345,
sizeLimit: 456,
notificationChannel: [],
smtpHost: 'smtp.host',
smtpPort: 587,
smtpEmail: '[email protected]',
smtpUsername: '[email protected]',
smtpPassword: 'dm312345',
});

expect(config).toStrictEqual({
messageTTL: 12345,
sizeLimit: 456,
notificationChannel: [],
smtpHost: 'smtp.host',
smtpPort: 587,
smtpEmail: '[email protected]',
smtpUsername: '[email protected]',
smtpPassword: 'dm312345',
});
});

Expand All @@ -51,6 +61,11 @@ describe('ReadDeliveryServiceProperties', () => {
},
},
],
smtpHost: 'smtp.host',
smtpPort: 587,
smtpEmail: '[email protected]',
smtpUsername: '[email protected]',
smtpPassword: 'dm312345',
}),
{ encoding: 'utf-8' },
);
Expand All @@ -74,13 +89,23 @@ describe('ReadDeliveryServiceProperties', () => {
},
},
],
smtpHost: 'smtp.host',
smtpPort: 587,
smtpEmail: '[email protected]',
smtpUsername: '[email protected]',
smtpPassword: 'dm312345',
});
});
it('Adds default properties if config.yml is not fully specified', () => {
writeFileSync(
path,
stringify({
messageTTL: 12345,
smtpHost: 'smtp.host',
smtpPort: 587,
smtpEmail: '[email protected]',
smtpUsername: '[email protected]',
smtpPassword: 'dm312345',
}),
{ encoding: 'utf-8' },
);
Expand All @@ -90,6 +115,11 @@ describe('ReadDeliveryServiceProperties', () => {
messageTTL: 12345,
sizeLimit: 100000,
notificationChannel: [],
smtpHost: 'smtp.host',
smtpPort: 587,
smtpEmail: '[email protected]',
smtpUsername: '[email protected]',
smtpPassword: 'dm312345',
});
});
});
5 changes: 5 additions & 0 deletions packages/backend/src/config/getDeliveryServiceProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const DEFAULT_DELIVERY_SERVICE_PROPERTIES: DeliveryServiceProperties = {
//100Kb
sizeLimit: 100000,
notificationChannel: [],
smtpHost: process.env.SMTP_HOST as string,
smtpPort: Number(process.env.SMTP_PORT),
smtpEmail: process.env.SMTP_EMAIL as string,
smtpUsername: process.env.SMTP_USERNAME as string,
smtpPassword: process.env.SMTP_PASSWORD as string,
};

export function getDeliveryServiceProperties(
Expand Down
6 changes: 6 additions & 0 deletions packages/lib/delivery/src/Delivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ export interface DeliveryServiceProperties {
//Number of bytes an envelop object should not exceed
sizeLimit: number;
notificationChannel: NotificationChannel[];
// properties for email service
smtpHost: string;
smtpPort: number;
smtpEmail: string;
smtpUsername: string;
smtpPassword: string;
}
32 changes: 20 additions & 12 deletions packages/lib/delivery/src/Messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import { getConversationId, getMessages, incomingMessage } from './Messages';
import { Session } from './Session';
import { SpamFilterRules } from './spam-filter/SpamFilterRules';

import { MAIL_HTML, MAIL_SUBJECT } from './notifications/channels/Email';
import { NotificationChannelType } from './notifications/types';
import {
NEW_MSG_EMAIL_TEMPLATE,
NEW_MSG_EMAIL_SUBJECT,
} from './notifications/templates/newMessage';
import {
NotificationChannelType,
NotificationType,
} from './notifications/types';

const SENDER_NAME = 'alice.eth';
const RECEIVER_NAME = 'bob.eth';
Expand Down Expand Up @@ -451,7 +457,10 @@ describe('Messages', () => {
return Promise.resolve([
{
type: NotificationChannelType.EMAIL,
config: { recipientAddress: '[email protected]' },
config: {
recipientAddress: '[email protected]',
notificationType: NotificationType.NEW_MESSAGE,
},
},
]);
};
Expand Down Expand Up @@ -479,14 +488,11 @@ describe('Messages', () => {
{
type: NotificationChannelType.EMAIL,
config: {
host: 'exmaple.host',
host: '[email protected]',
port: 1234,
secure: true,
auth: {
user: 'foo',
pass: 'bar',
},
senderAddress: '[email protected]',
username: '[email protected]',
password: 'bar',
emailID: '[email protected]',
},
},
],
Expand All @@ -503,8 +509,10 @@ describe('Messages', () => {

expect(sendMailMock).toHaveBeenCalledWith({
from: '[email protected]',
html: MAIL_HTML(testData.delvieryInformationBUnecrypted),
subject: MAIL_SUBJECT,
html: NEW_MSG_EMAIL_TEMPLATE(
testData.delvieryInformationBUnecrypted,
),
subject: NEW_MSG_EMAIL_SUBJECT,
to: '[email protected]',
});
//Check if the message was submitted to the socket
Expand Down
6 changes: 5 additions & 1 deletion packages/lib/delivery/src/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { NotificationBroker } from './notifications';
import {
GetNotificationChannels,
NotificationChannel,
NotificationType,
} from './notifications/types';
import { checkToken, Session } from './Session';
import { isSpam } from './spam-filter';
Expand Down Expand Up @@ -209,7 +210,10 @@ export async function incomingMessage(
});
//If not we're notifing the user that there is a new message waiting for them
} else {
const { sendNotification } = NotificationBroker(dsNotificationChannels);
const { sendNotification } = NotificationBroker(
dsNotificationChannels,
NotificationType.NEW_MESSAGE,
);
await sendNotification(
deliveryInformation,
getUsersNotificationChannels,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
NotificationChannel,
NotificationChannelType,
INotificationChannel,
NotificationType,
} from '../types';

/**
Expand Down Expand Up @@ -55,10 +56,12 @@ export const _setupNotficationBroker = (
*/
export const NotificationBroker = (
notificationChannel: NotificationChannel[],
notificationType: NotificationType,
): INotificationBroker => {
const channels = notificationChannel.map((channel) => {
switch (channel.type) {
case NotificationChannelType.EMAIL:
channel.config.notificationType = notificationType;
return {
type: NotificationChannelType.EMAIL,
send: Email(channel.config).send,
Expand Down
65 changes: 40 additions & 25 deletions packages/lib/delivery/src/notifications/channels/Email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,61 @@ import { DeliveryInformation } from '@dm3-org/dm3-lib-messaging';
import { logError } from '@dm3-org/dm3-lib-shared';
import nodemailer from 'nodemailer';
import SMTPTransport from 'nodemailer/lib/smtp-transport';
import { NotificationType } from '../types';
import { fetchEmailSubjectAndTemplate } from '../utils';

// Define types for email server and user configuration
export type EmailNotificationServerConfig = SMTPTransport.Options & {
senderAddress: string;
// email server configuration
export type EmailNotificationServerConfig = {
host: string;
port: number;
username: string;
password: string;
emailID: string;
};

// email notification configuration
export type EmailNotificationUserConfig = {
recipientAddress: string;
};

// Constants for email subject and HTML template
export const MAIL_SUBJECT = 'New DM3 Message';
export const MAIL_HTML = (
deliveryInformation: DeliveryInformation,
) => `<html lang="en">
<body>
<p>You have received a new DM3 message from ${deliveryInformation.from}.
<br/>
Open <a href="app.dm3.network">DM3</a> to read it</p>
<script src="index.js"></script>
</body>
</html>`;

// Define the Email function
type UserEmailConfig = {
recipientAddress: string;
notificationType: NotificationType;
};

// method to send email
export function Email(config: EmailNotificationServerConfig) {
const send = async (
mailConfig: EmailNotificationUserConfig,
mailConfig: UserEmailConfig,
deliveryInformation: DeliveryInformation,
) => {
nodemailer.createTestAccount();
const transport = nodemailer.createTransport(new SMTPTransport(config));

try {
// Send the email using nodemailer
// create transport with email credentials
const transport: nodemailer.Transporter<SMTPTransport.SentMessageInfo> =
nodemailer.createTransport({
host: config.host,
port: config.port,
auth: {
user: config.username,
pass: config.password,
},
});

// fetch the specific subject & template of email
const { subject, template } = fetchEmailSubjectAndTemplate(
mailConfig.notificationType,
deliveryInformation,
);

// send the email using nodemailer
await transport.sendMail({
from: config.senderAddress,
from: config.emailID,
to: mailConfig.recipientAddress,
subject: MAIL_SUBJECT,
html: MAIL_HTML(deliveryInformation),
subject: subject,
html: template,
});

// close the connection
transport.close();
} catch (err) {
logError('Send mail failed ' + err);
Expand Down
12 changes: 12 additions & 0 deletions packages/lib/delivery/src/notifications/templates/newMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DeliveryInformation } from '@dm3-org/dm3-lib-messaging';

export const NEW_MSG_EMAIL_TEMPLATE = (
deliveryInformation: DeliveryInformation,
) =>
`<html lang="en">
<body>
<p>You received a new message from ${deliveryInformation.from}.
</body>
</html>`;

export const NEW_MSG_EMAIL_SUBJECT = 'DM3 New Message';
8 changes: 8 additions & 0 deletions packages/lib/delivery/src/notifications/templates/otp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const OTP_EMAIL_TEMPLATE = (otp: string) =>
`<html lang="en">
<body>
<p>Your OTP to verify email ID is : ${otp}. The OTP will expire in 10 minutes.
</body>
</html>`;

export const OTP_EMAIL_SUBJECT = 'Email Verification';
5 changes: 5 additions & 0 deletions packages/lib/delivery/src/notifications/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export enum NotificationChannelType {
EMAIL = 'EMAIL',
}

export enum NotificationType {
NEW_MESSAGE = 'NEW_MESSAGE',
OTP = 'OTP',
}

//The properties of a notification channel.
// Those properties are stored in the DB to let the user specify their notificatin channels
export interface NotificationChannel {
Expand Down
38 changes: 38 additions & 0 deletions packages/lib/delivery/src/notifications/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { DeliveryInformation } from '@dm3-org/dm3-lib-messaging';
import { NotificationType } from './types';
import {
NEW_MSG_EMAIL_SUBJECT,
NEW_MSG_EMAIL_TEMPLATE,
} from './templates/newMessage';
import { OTP_EMAIL_SUBJECT, OTP_EMAIL_TEMPLATE } from './templates/otp';

// TODO: generates 5 digit OTP
export const generateOtp = () => {
return '12345';
};

// to fetch subject & template of email based on notification type
export const fetchEmailSubjectAndTemplate = (
notificationType: NotificationType,
deliveryInformation: DeliveryInformation,
): {
subject: string;
template: string;
} => {
switch (notificationType) {
case NotificationType.NEW_MESSAGE:
return {
subject: NEW_MSG_EMAIL_SUBJECT,
template: NEW_MSG_EMAIL_TEMPLATE(deliveryInformation),
};
case NotificationType.OTP:
return {
subject: OTP_EMAIL_SUBJECT,
template: OTP_EMAIL_TEMPLATE(generateOtp()),
};
default:
throw new Error(
`Notification type ${notificationType} is not supported`,
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { GlobalContext } from '../../utils/context-utils';
import DeleteDM3Name from '../DeleteDM3Name/DeleteDM3Name';
import './ConfigureProfile.css';
import {
ACTION_TYPE,
BUTTON_CLASS,
NAME_TYPE,
PROFILE_INPUT_FIELD_CLASS,
closeConfigurationModal,
fetchChainIdFromServiceName,
fetchComponent,
Expand All @@ -30,6 +26,12 @@ import { AuthContext } from '../../context/AuthContext';
import { useNetwork } from 'wagmi';
import { useMainnetProvider } from '../../hooks/mainnetprovider/useMainnetProvider';
import { switchNetwork } from '@wagmi/core';
import {
ACTION_TYPE,
BUTTON_CLASS,
NAME_TYPE,
PROFILE_INPUT_FIELD_CLASS,
} from './chain/common';

export function ConfigureDM3Profile() {
// global context state
Expand Down
Loading
Loading