Skip to content

Commit

Permalink
feat: removed cc and bcc and updated sql statments
Browse files Browse the repository at this point in the history
  • Loading branch information
vwh committed Nov 22, 2024
1 parent 692ebc2 commit d695c87
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 87 deletions.
32 changes: 15 additions & 17 deletions src/app/api/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,17 @@ export default function Component() {
<ul className="list-inside list-disc text-sm text-foreground/80">
<li>email (string): The email address to fetch messages for</li>
</ul>
<h2 className="text-md font-semibold text-primary">Response</h2>
<h2 className="text-md font-semibold text-primary">Example response</h2>
<div className="overflow-x-auto rounded bg-primary/5 p-2">
<pre className="text-sm text-foreground/80">
{`[
{
"id": "{inboxId}",
"subject": "Test",
"createdAt": 1732026087000,
"expiresAt": 1732285287596,
"fromAddress": "example@gmail.com",
"toAddress": "{email}",
"ccAddress": null,
"bccAddress": null
"id": "cm3sqher40005276o336z4fvw",
"subject": "Test Email",
"createdAt": 1732279492000,
"expiresAt": 1732538692335,
"fromAddress": "sender@example.com",
"toAddress": "recipient@vwh.sh"
}
]`}
</pre>
Expand Down Expand Up @@ -103,14 +101,14 @@ export default function Component() {
<div className="overflow-x-auto rounded bg-primary/5 p-2">
<pre className="text-sm text-foreground/80">
{`{
"id": "{inboxId}",
"textContent": "Hello\\n",
"htmlContent": "<div dir=\\"rtl\\"><div dir=\\"ltr\\">Hello</div></div>\\n",
"subject": "Test",
"expiresAt": 1732285287596,
"createdAt": 1732026087000,
"fromAddress": "example@gmail.com",
"toAddress": "example@vwh.sh"
"id": "cm3sqher40005276o336z4fvw",
"textContent": "This is a test email body.\n",
"htmlContent": null,
"subject": "Test Email",
"expiresAt": 1732538692335,
"createdAt": 1732279492000,
"fromAddress": "sender@example.com",
"toAddress": "recipient@vwh.sh"
}`}
</pre>
</div>
Expand Down
15 changes: 2 additions & 13 deletions src/database/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export function insertEmail(
const recipientGroups = [
{ type: "from", values: emailData.from?.value || [] },
{ type: "to", values: emailData.to?.value || [] },
{ type: "cc", values: emailData.cc?.value || [] },
{ type: "bcc", values: emailData.bcc?.value || [] }
];

for (const { type, values } of recipientGroups) {
Expand All @@ -56,24 +54,15 @@ export function insertEmail(
// Insert inbox entries
const allRecipients = [
...(emailData.to?.value || []),
...(emailData.cc?.value || []),
...(emailData.bcc?.value || [])
];

for (const recipient of allRecipients) {
const type = emailData.to?.value?.includes(recipient)
? "to"
: emailData.cc?.value?.includes(recipient)
? "cc"
: "bcc";

insertInbox.run(
cuid(),
emailId,
recipient.address,
type,
emailData.text ?? null,
emailData.html ?? null
emailData.text,
emailData.html
);
}

Expand Down
71 changes: 18 additions & 53 deletions src/database/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ export const SQL_STATEMENTS = {
id INTEGER PRIMARY KEY AUTOINCREMENT,
subject TEXT,
createdAt INTEGER DEFAULT (strftime('%s', 'now') * 1000),
expiresAt DATETIME
expiresAt DATETIME
);
CREATE TABLE IF NOT EXISTS Inbox (
id TEXT PRIMARY KEY,
address TEXT,
type TEXT, -- Can be 'to', 'cc', 'bcc'
textContent TEXT,
htmlContent TEXT,
createdAt INTEGER DEFAULT (strftime('%s', 'now') * 1000),
Expand All @@ -24,81 +23,53 @@ export const SQL_STATEMENTS = {
CREATE TABLE IF NOT EXISTS EmailAddress (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT, -- Can be 'from', 'to', 'cc', 'bcc'
type TEXT, -- Can be 'from', 'to'
address TEXT,
emailId INTEGER,
FOREIGN KEY (emailId) REFERENCES Email(id) ON DELETE CASCADE
);
`,

// Create indexes
CREATE_INDEX: `
CREATE_INDEX: `
CREATE INDEX IF NOT EXISTS idx_email_id ON EmailAddress(emailId);
CREATE INDEX IF NOT EXISTS idx_inbox_address ON Inbox(address);
`,

// PRAGMA settings for performance optimization
PRAGMA: `
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
`,
`,

// Insert statement for Email
INSERT_EMAIL: `
INSERT INTO Email
(subject, expiresAt)
INSERT INTO Email (subject, expiresAt)
VALUES (?, ?)
`,
`,

// Insert statement for Inbox
INSERT_INBOX: `
INSERT INTO Inbox
(id, emailId, address, type, textContent, htmlContent)
VALUES (?, ?, ?, ?, ?, ?)
`,
INSERT INTO Inbox (id, emailId, address, textContent, htmlContent)
VALUES (?, ?, ?, ?, ?)
`,

// Insert statement for EmailAddress
INSERT_EMAIL_ADDRESS: `
INSERT INTO EmailAddress
(emailId, type, address)
INSERT INTO EmailAddress (emailId, type, address)
VALUES (?, ?, ?)
`,
`,

// For fetching emails for a specific address
GET_EMAILS_FOR_ADDRESS: `
SELECT
Inbox.id,
Email.subject,
Email.createdAt,
Email.expiresAt,
(SELECT EmailAddress.address
FROM EmailAddress
WHERE EmailAddress.emailId = Email.id AND EmailAddress.type = 'from') as fromAddress,
GROUP_CONCAT(
CASE
WHEN EmailAddress.type = 'to' THEN EmailAddress.address
ELSE NULL
END, ', ') as toAddress,
GROUP_CONCAT(
CASE
WHEN EmailAddress.type = 'cc' THEN EmailAddress.address
ELSE NULL
END, ', ') as ccAddress,
GROUP_CONCAT(
CASE
WHEN EmailAddress.type = 'bcc' THEN EmailAddress.address
ELSE NULL
END, ', ') as bccAddress
(SELECT address FROM EmailAddress WHERE emailId = Email.id AND type = 'from') as fromAddress,
(SELECT GROUP_CONCAT(address, ', ') FROM EmailAddress WHERE emailId = Email.id AND type = 'to') as toAddress
FROM Email
JOIN Inbox ON Email.id = Inbox.emailId
LEFT JOIN EmailAddress ON Email.id = EmailAddress.emailId
WHERE Inbox.address = ?
GROUP BY Email.id;
`,

// For fetching email by inbox ID (to get the full body)
GET_INBOX_BY_ID: `
SELECT
Inbox.id,
Expand All @@ -107,24 +78,18 @@ export const SQL_STATEMENTS = {
Email.subject,
Email.expiresAt,
Email.createdAt,
(SELECT EmailAddress.address FROM EmailAddress WHERE EmailAddress.emailId = Email.id AND EmailAddress.type = 'from') as fromAddress,
GROUP_CONCAT(EmailAddress.address, ', ') as toAddress
(SELECT address FROM EmailAddress WHERE emailId = Email.id AND type = 'from') as fromAddress,
(SELECT GROUP_CONCAT(address, ', ') FROM EmailAddress WHERE emailId = Email.id AND type = 'to') as toAddress
FROM Inbox
JOIN Email ON Inbox.emailId = Email.id
LEFT JOIN EmailAddress ON Email.id = EmailAddress.emailId AND EmailAddress.type = 'to'
WHERE Inbox.id = ?
GROUP BY Email.id
`,

// Delete email by inbox ID
DELETE_BY_INBOX_ID: `
DELETE FROM Inbox
WHERE id = ?
DELETE FROM Inbox WHERE id = ?
`,

// To delete expired entries
DELETE_EXPIRED_ENTRIES: `
DELETE FROM Email
WHERE expiresAt < strftime('%s', 'now') * 1000;
`
DELETE FROM Email WHERE expiresAt < strftime('%s', 'now') * 1000
`
};
2 changes: 0 additions & 2 deletions src/utils/emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export function simplifyEmail(email: ParsedMail): SimplifiedEmail {
id: email.messageId || "",
from: convertAddress(email.from),
to: convertAddress(email.to),
cc: convertAddress(email.cc),
bcc: convertAddress(email.bcc),
subject: email.subject || null,
text: email.text || null,
html: email.html || null,
Expand Down
2 changes: 0 additions & 2 deletions src/utils/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export const emailSchema = z.object({
id: z.string(),
from: emailAddressSchema.nullable(),
to: emailAddressSchema.nullable(),
cc: emailAddressSchema.nullable(),
bcc: emailAddressSchema.nullable(),
subject: z.string().nullable(),
text: z.string().nullable(),
html: z.string().nullable(),
Expand Down

0 comments on commit d695c87

Please sign in to comment.