Skip to content

Commit

Permalink
feat: allow user delete mail && notify when send access changed (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamhunter2333 authored Apr 15, 2024
1 parent 50f04b2 commit 00231e7
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 5 deletions.
27 changes: 25 additions & 2 deletions frontend/src/views/MailBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ const { t } = useI18n({
refresh: 'Refresh',
attachments: 'Show Attachments',
downloadMail: 'Download Mail',
pleaseSelectMail: "Please select a mail to view."
pleaseSelectMail: "Please select a mail to view.",
delete: 'Delete',
deleteMailTip: 'Are you sure you want to delete this mail?'
},
zh: {
autoRefresh: '自动刷新',
refresh: '刷新',
downloadMail: '下载邮件',
attachments: '查看附件',
pleaseSelectMail: "请选择一封邮件查看。"
pleaseSelectMail: "请选择一封邮件查看。",
delete: '删除',
deleteMailTip: '确定要删除这封邮件吗?'
}
}
});
Expand Down Expand Up @@ -100,6 +104,19 @@ const mailItemClass = (row) => {
return curMail.value && row.id == curMail.value.id ? (themeSwitch.value ? 'overlay overlay-dark-backgroud' : 'overlay overlay-light-backgroud') : '';
};
const deleteMail = async () => {
try {
await api.fetch(`/api/mails/${curMail.value.id}`, {
method: 'DELETE'
});
message.success(t("success"));
curMail.value = null;
await refresh();
} catch (error) {
message.error(error.message || "error");
}
};
onMounted(async () => {
await refresh();
});
Expand Down Expand Up @@ -158,6 +175,12 @@ onMounted(async () => {
<n-tag type="info">
FROM: {{ curMail.source }}
</n-tag>
<n-popconfirm @positive-click="deleteMail">
<template #trigger>
<n-button tertiary type="error" size="small">{{ t('delete') }}</n-button>
</template>
{{ t('deleteMailTip') }}
</n-popconfirm>
<n-button v-if="curMail.attachments && curMail.attachments.length > 0" size="small" tertiary type="info"
@click="getAttachments(curMail.attachments)">
{{ t('attachments') }}
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/views/admin/MailsUnknow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ onMounted(async () => {
<n-tag type="info">
ID: {{ row.id }}
</n-tag>
<n-tag type="info">
TO: {{ row.address }}
</n-tag>
</n-space>
</template>
<div v-html="row.message"></div>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/views/admin/SenderAccess.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const updateData = async () => {
await api.fetch(`/admin/address_sender`, {
method: 'POST',
body: JSON.stringify({
address: curRow.value.address,
address_id: curRow.value.id,
balance: senderBalance.value,
enabled: senderEnabled.value ? 1 : 0
Expand Down
2 changes: 1 addition & 1 deletion vitepress-docs/docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ features:
- title: Use rust wasm to parse emails
details: Use rust wasm to parse emails, support various RFC standards for emails, support attachments, extremely fast
- title: Support sending emails
details: Support sending txt or html emails through domain name mailboxes
details: Support sending txt or html emails through domain name mailboxes,Support DKIM signature
---
2 changes: 1 addition & 1 deletion vitepress-docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ features:
- title: 使用 rust wasm 解析邮件
details: 使用 rust wasm 解析邮件,支持邮件各种RFC标准,支持附件, 速度极快
- title: 支持发送邮件
details: 支持通过域名邮箱发送 txt 或者 html 邮件
details: 支持通过域名邮箱发送 txt 或者 html 邮件,支持 DKIM 签名
---
7 changes: 6 additions & 1 deletion worker/src/admin_api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono } from 'hono'
import { Jwt } from 'hono/utils/jwt'
import { getSendbox } from './send_mail_api'
import { sendAdminInternalMail } from './utils'

const api = new Hono()

Expand Down Expand Up @@ -164,7 +165,7 @@ api.get('/admin/address_sender', async (c) => {
})

api.post('/admin/address_sender', async (c) => {
let { address_id, balance, enabled } = await c.req.json();
let { address, address_id, balance, enabled } = await c.req.json();
if (!address_id) {
return c.text("Invalid address_id", 400)
}
Expand All @@ -175,6 +176,10 @@ api.post('/admin/address_sender', async (c) => {
if (!success) {
return c.text("Failed to update address sender", 500)
}
await sendAdminInternalMail(
c, address, "Account Send Access Updated",
`You send access has been ${enabled ? "enabled" : "disabled"}, balance: ${balance}`
);
return c.json({
success: success
})
Expand Down
11 changes: 11 additions & 0 deletions worker/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ api.get('/api/mails', async (c) => {
})
})

api.delete('/api/mails/:id', async (c) => {
const { address } = c.get("jwtPayload")
const { id } = c.req.param();
const { success } = await c.env.DB.prepare(
`DELETE FROM raw_mails WHERE address = ? and id = ?`
).bind(address, id).run();
return c.json({
success: success
})
})

api.get('/api/settings', async (c) => {
const { address, address_id } = c.get("jwtPayload")
if (address_id && address_id > 0) {
Expand Down
32 changes: 32 additions & 0 deletions worker/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createMimeMessage } from "mimetext";

export const getDomains = (c) => {
if (!c.env.DOMAINS) {
return [];
Expand Down Expand Up @@ -45,3 +47,33 @@ export const getAdminPasswords = (c) => {
}
return c.env.ADMIN_PASSWORDS;
}

export const sendAdminInternalMail = async (c, toMail, subject, text) => {
try {

const msg = createMimeMessage();
msg.setSender({
name: "Admin",
addr: "admin@internal"
});
msg.setRecipient(toMail);
msg.setSubject(subject);
msg.addMessage({
contentType: 'text/plain',
data: text
});
const message_id = Math.random().toString(36).substring(2, 15);
const { success } = await c.env.DB.prepare(
`INSERT INTO raw_mails (source, address, raw, message_id) VALUES (?, ?, ?, ?)`
).bind(
"admin@internal", toMail, msg.asRaw(), message_id
).run();
if (!success) {
console.log(`Failed save message from admin@internal to ${toMail}`);
}
return success;
} catch (error) {
console.log("sendAdminInternalMail error", error);
return false;
}
};

0 comments on commit 00231e7

Please sign in to comment.