feat: add mail id and url in webhook (#443)

This commit is contained in:
Dream Hunter
2024-09-09 22:29:18 +08:00
committed by GitHub
parent 5ece49a576
commit 393c5902c3
11 changed files with 87 additions and 12 deletions

View File

@@ -22,12 +22,14 @@ async function saveWebhookSettings(c: Context<HonoCustomType>): Promise<Response
async function testWebhookSettings(c: Context<HonoCustomType>): Promise<Response> {
const settings = await c.req.json<WebhookSettings>();
// random raw email
const raw = await c.env.DB.prepare(
`SELECT raw FROM raw_mails ORDER BY RANDOM() LIMIT 1`
).first<string>("raw");
const { id: mailId, raw } = await c.env.DB.prepare(
`SELECT id, raw FROM raw_mails ORDER BY RANDOM() LIMIT 1`
).first<{ id: string, raw: string }>() || {};
const parsedEmail = await commonParseMail(raw);
const res = await sendWebhook(settings, {
id: mailId || "0",
url: c.env.FRONTEND_URL ? `${c.env.FRONTEND_URL}?mail_id=${mailId}` : "",
from: parsedEmail?.sender || "test@test.com",
to: "admin@test.com",
subject: parsedEmail?.subject || "test subject",

View File

@@ -339,6 +339,7 @@ export async function sendWebhook(settings: WebhookSettings, formatMap: WebhookM
);
/* eslint-enable no-useless-escape */
}
console.log("send webhook", settings.url, settings.method, settings.headers, body);
const response = await fetch(settings.url, {
method: settings.method,
headers: JSON.parse(settings.headers),
@@ -354,7 +355,8 @@ export async function sendWebhook(settings: WebhookSettings, formatMap: WebhookM
export async function triggerWebhook(
c: Context<HonoCustomType>,
address: string,
raw_mail: string
raw_mail: string,
message_id: string | null
): Promise<void> {
if (!c.env.KV || !getBooleanValue(c.env.ENABLE_WEBHOOK)) {
return
@@ -382,8 +384,14 @@ export async function triggerWebhook(
if (webhookList.length === 0) {
return
}
const mailId = await c.env.DB.prepare(
`SELECT id FROM raw_mails where address = ? and message_id = ?`
).bind(address, message_id).first<string>("id");
const parsedEmail = await commonParseMail(raw_mail);
const webhookMail = {
id: mailId || "",
url: c.env.FRONTEND_URL ? `${c.env.FRONTEND_URL}?mail_id=${mailId}` : "",
from: parsedEmail?.sender || "",
to: address,
subject: parsedEmail?.subject || "",

View File

@@ -50,7 +50,7 @@ async function email(message: ForwardableEmailMessage, env: Bindings, ctx: Execu
try {
await triggerWebhook(
{ env: env } as Context<HonoCustomType>,
message.to, rawEmail
message.to, rawEmail, message_id
);
} catch (error) {
console.log("send webhook error", error);

View File

@@ -32,6 +32,15 @@ api.get('/api/mails', async (c) => {
);
})
api.get('/api/mail/:mail_id', async (c) => {
const { address } = c.get("jwtPayload")
const { mail_id } = c.req.param();
const result = await c.env.DB.prepare(
`SELECT * FROM raw_mails where id = ? and address = ?`
).bind(mail_id, address).first();
return c.json(result);
})
api.delete('/api/mails/:id', async (c) => {
if (!getBooleanValue(c.env.ENABLE_USER_DELETE_EMAIL)) {
return c.text("User delete email is disabled", 403)

View File

@@ -36,12 +36,14 @@ async function testWebhookSettings(c: Context<HonoCustomType>): Promise<Response
const settings = await c.req.json<WebhookSettings>();
const { address } = c.get("jwtPayload");
// random raw email
const raw = await c.env.DB.prepare(
`SELECT raw FROM raw_mails WHERE address = ? ORDER BY RANDOM() LIMIT 1`
).bind(address).first<string>("raw");
const { id: mailId, raw } = await c.env.DB.prepare(
`SELECT id, raw FROM raw_mails WHERE address = ? ORDER BY RANDOM() LIMIT 1`
).bind(address).first<{ id: string, raw: string }>() || {};
const parsedEmail = await commonParseMail(raw);
const res = await sendWebhook(settings, {
id: mailId || "0",
url: c.env.FRONTEND_URL ? `${c.env.FRONTEND_URL}?mail_id=${mailId}` : "",
from: parsedEmail?.sender || "test@test.com",
to: address,
subject: parsedEmail?.subject || "test subject",

View File

@@ -22,6 +22,8 @@ export class AdminWebhookSettings {
}
export type WebhookMail = {
id: string;
url?: string;
from: string;
to: string;
subject: string;
@@ -128,6 +130,8 @@ export class WebhookSettings {
"Content-Type": "application/json"
}, null, 2)
body: string = JSON.stringify({
"id": "${id}",
"url": "${url}",
"from": "${from}",
"to": "${to}",
"subject": "${subject}",

View File

@@ -61,6 +61,9 @@ export type Bindings = {
TELEGRAM_BOT_TOKEN: string
TG_MAX_ADDRESS: number | undefined
TG_BOT_INFO: string | object | undefined
// webhook config
FRONTEND_URL: string | undefined
}
type JwtPayload = {