-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df3bb43
commit a5b7c75
Showing
11 changed files
with
4,488 additions
and
1,099 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
const AWS = require('aws-sdk'); | ||
const s3 = new AWS.S3(); | ||
const dynamoDb = new AWS.DynamoDB.DocumentClient(); | ||
const ses = new AWS.SES(); | ||
// AWS SDK v3 imports | ||
const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3"); | ||
const { DynamoDBClient, ScanCommand } = require("@aws-sdk/client-dynamodb"); | ||
const { SESClient, SendEmailCommand } = require("@aws-sdk/client-ses"); | ||
const { stringify } = require("querystring"); | ||
|
||
// Initialize AWS SDK v3 clients | ||
const s3 = new S3Client({}); | ||
const dynamoDb = new DynamoDBClient({}); | ||
const ses = new SESClient({}); | ||
|
||
exports.handler = async (event) => { | ||
const bucketName = process.env.BUCKET_NAME; | ||
|
@@ -16,17 +22,17 @@ exports.handler = async (event) => { | |
}; | ||
|
||
try { | ||
const data = await s3.getObject(params).promise(); | ||
const announcementContent = data.Body.toString('utf-8'); | ||
const data = await s3.send(new GetObjectCommand(params)); | ||
const announcementContent = (await streamToString(data.Body)).toString('utf-8'); | ||
|
||
// Extract the title and content without front matter from the markdown file | ||
const { title, description } = extractTitleAndContent(announcementContent); | ||
|
||
const subscribers = await dynamoDb.scan({ TableName: tableName }).promise(); | ||
const subscribers = await dynamoDb.send(new ScanCommand({ TableName: tableName })); | ||
const emailPromises = subscribers.Items.map(item => { | ||
const emailParams = { | ||
Source: '[email protected]', | ||
Destination: { ToAddresses: [item.email] }, | ||
Destination: { ToAddresses: [`${item.email.S}`] }, | ||
Message: { | ||
Subject: { Data: `New Announcement from the Martini Force Field Initiative.` }, | ||
Body: { Html: { Data: | ||
|
@@ -43,7 +49,7 @@ exports.handler = async (event) => { | |
<br><br> | ||
<hr style="border: 0.5px solid #000;"> | ||
<em>If you no longer wish to receive emails from us, you can <a href="https://q8hgi2weih.execute-api.ca-central-1.amazonaws.com/prod/unsubscribe?email=${encodeURIComponent(item.email)}&token=${item.token}">unsubscribe from our mailing list</a>.</em> | ||
<em>If you no longer wish to receive emails from us, you can <a href="https://q8hgi2weih.execute-api.ca-central-1.amazonaws.com/prod/unsubscribe?email=${encodeURIComponent(item.email.S)}&token=${item.token.S}">unsubscribe from our mailing list</a>.</em> | ||
<hr style="border: 0.5px solid #000;"> | ||
|
@@ -52,7 +58,7 @@ exports.handler = async (event) => { | |
} }, | ||
}, | ||
}; | ||
return ses.sendEmail(emailParams).promise(); | ||
return ses.send(new SendEmailCommand(emailParams)); | ||
}); | ||
|
||
await Promise.all(emailPromises); | ||
|
@@ -63,6 +69,16 @@ exports.handler = async (event) => { | |
} | ||
}; | ||
|
||
// Utility function to convert stream to string | ||
const streamToString = async (stream) => { | ||
const chunks = []; | ||
for await (const chunk of stream) { | ||
chunks.push(chunk); | ||
} | ||
return Buffer.concat(chunks); | ||
}; | ||
|
||
// function to extract title and content from markdown content | ||
function extractTitleAndContent(markdownContent) { | ||
const frontMatterMatch = markdownContent.match(/---([\s\S]*?)---/); | ||
if (!frontMatterMatch) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.