mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-09-04 23:17:34 +08:00
feat: add user send mail client
This commit is contained in:
@@ -297,17 +297,23 @@ api.get('/api/sendbox', async (c) => {
|
||||
return getSendbox(c, address, limit, offset);
|
||||
})
|
||||
|
||||
api.delete('/api/sendbox/:id', async (c) => {
|
||||
export const deleteSendbox = async (
|
||||
c: Context<HonoCustomType>, address: string, id: string | number
|
||||
): Promise<Response> => {
|
||||
const msgs = i18n.getMessagesbyContext(c);
|
||||
if (!getBooleanValue(c.env.ENABLE_USER_DELETE_EMAIL)) {
|
||||
return c.text(msgs.UserDeleteEmailDisabledMsg, 403)
|
||||
}
|
||||
const { address } = c.get("jwtPayload")
|
||||
const { id } = c.req.param();
|
||||
const { success } = await c.env.DB.prepare(
|
||||
`DELETE FROM sendbox WHERE address = ? and id = ? `
|
||||
).bind(address, id).run();
|
||||
return c.json({
|
||||
success: success
|
||||
})
|
||||
}
|
||||
|
||||
api.delete('/api/sendbox/:id', async (c) => {
|
||||
const { address } = c.get("jwtPayload")
|
||||
const { id } = c.req.param();
|
||||
return deleteSendbox(c, address, id);
|
||||
})
|
||||
|
||||
@@ -6,6 +6,7 @@ import bind_address from './bind_address';
|
||||
import passkey from './passkey';
|
||||
import oauth2 from './oauth2';
|
||||
import user_mail_api from './user_mail_api';
|
||||
import user_send_mail_api from './user_send_mail_api';
|
||||
|
||||
export const api = new Hono<HonoCustomType>();
|
||||
|
||||
@@ -17,6 +18,13 @@ api.get('/user_api/settings', settings.settings);
|
||||
api.get('/user_api/mails', user_mail_api.getMails);
|
||||
api.delete('/user_api/mails/:id', user_mail_api.deleteMail);
|
||||
|
||||
// send mail api
|
||||
api.get('/user_api/address/:address_id/settings', user_send_mail_api.settings);
|
||||
api.post('/user_api/address/:address_id/request_send_mail_access', user_send_mail_api.requestAccess);
|
||||
api.post('/user_api/address/:address_id/send_mail', user_send_mail_api.send);
|
||||
api.get('/user_api/address/:address_id/sendbox', user_send_mail_api.listSendbox);
|
||||
api.delete('/user_api/address/:address_id/sendbox/:mail_id', user_send_mail_api.removeSendboxMail);
|
||||
|
||||
// user api
|
||||
api.post('/user_api/login', user.login);
|
||||
api.post('/user_api/verify_code', user.verifyCode);
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
import { Context } from "hono";
|
||||
|
||||
import { commonGetUserRole } from "../common";
|
||||
import i18n from "../i18n";
|
||||
import {
|
||||
deleteSendbox,
|
||||
getSendbox,
|
||||
sendMail,
|
||||
} from "../mails_api/send_mail_api";
|
||||
import {
|
||||
getSendBalanceState,
|
||||
requestSendMailAccess,
|
||||
} from "../mails_api/send_balance";
|
||||
|
||||
const getBindedAddress = async (
|
||||
c: Context<HonoCustomType>
|
||||
): Promise<string | null> => {
|
||||
const addressId = Number(c.req.param("address_id"));
|
||||
if (!Number.isInteger(addressId) || addressId <= 0) {
|
||||
return null;
|
||||
}
|
||||
const { user_id } = c.get("userPayload");
|
||||
return await c.env.DB.prepare(
|
||||
`SELECT a.name FROM users_address ua`
|
||||
+ ` JOIN address a ON a.id = ua.address_id`
|
||||
+ ` WHERE ua.user_id = ? AND a.id = ?`
|
||||
).bind(user_id, addressId).first<string>("name");
|
||||
}
|
||||
|
||||
const getAddressOrError = async (
|
||||
c: Context<HonoCustomType>
|
||||
): Promise<string | Response> => {
|
||||
const address = await getBindedAddress(c);
|
||||
if (address) {
|
||||
return address;
|
||||
}
|
||||
const msgs = i18n.getMessagesbyContext(c);
|
||||
return c.text(msgs.AddressNotBindedMsg, 400);
|
||||
}
|
||||
|
||||
const setUserRole = async (c: Context<HonoCustomType>): Promise<void> => {
|
||||
const { user_id } = c.get("userPayload");
|
||||
const userRole = await commonGetUserRole(c, user_id);
|
||||
c.set("userRolePayload", userRole?.role);
|
||||
}
|
||||
|
||||
const settings = async (c: Context<HonoCustomType>): Promise<Response> => {
|
||||
const address = await getAddressOrError(c);
|
||||
if (address instanceof Response) {
|
||||
return address;
|
||||
}
|
||||
await setUserRole(c);
|
||||
const { balance } = await getSendBalanceState(c, address);
|
||||
return c.json({
|
||||
address,
|
||||
send_balance: balance || 0,
|
||||
});
|
||||
}
|
||||
|
||||
const requestAccess = async (c: Context<HonoCustomType>): Promise<Response> => {
|
||||
const address = await getAddressOrError(c);
|
||||
if (address instanceof Response) {
|
||||
return address;
|
||||
}
|
||||
const msgs = i18n.getMessagesbyContext(c);
|
||||
const result = await requestSendMailAccess(c, address);
|
||||
if (result.status === "ok") {
|
||||
return c.json({ status: "ok" });
|
||||
}
|
||||
if (result.status === "already_requested") {
|
||||
return c.text(msgs.AlreadyRequestedMsg, 400);
|
||||
}
|
||||
return c.text(msgs.OperationFailedMsg, 500);
|
||||
}
|
||||
|
||||
const send = async (c: Context<HonoCustomType>): Promise<Response> => {
|
||||
const address = await getAddressOrError(c);
|
||||
if (address instanceof Response) {
|
||||
return address;
|
||||
}
|
||||
await setUserRole(c);
|
||||
try {
|
||||
const reqJson = await c.req.json();
|
||||
await sendMail(c, address, reqJson);
|
||||
} catch (error) {
|
||||
console.error("Failed to send user mail", error);
|
||||
return c.text(`Failed to send mail ${(error as Error).message}`, 400);
|
||||
}
|
||||
return c.json({ status: "ok" });
|
||||
}
|
||||
|
||||
const listSendbox = async (c: Context<HonoCustomType>): Promise<Response> => {
|
||||
const address = await getAddressOrError(c);
|
||||
if (address instanceof Response) {
|
||||
return address;
|
||||
}
|
||||
const { limit, offset } = c.req.query();
|
||||
return getSendbox(c, address, limit, offset);
|
||||
}
|
||||
|
||||
const removeSendboxMail = async (c: Context<HonoCustomType>): Promise<Response> => {
|
||||
const address = await getAddressOrError(c);
|
||||
if (address instanceof Response) {
|
||||
return address;
|
||||
}
|
||||
return deleteSendbox(c, address, c.req.param("mail_id"));
|
||||
}
|
||||
|
||||
export default {
|
||||
settings,
|
||||
requestAccess,
|
||||
send,
|
||||
listSendbox,
|
||||
removeSendboxMail,
|
||||
};
|
||||
Reference in New Issue
Block a user