feature: update address updated_at in multi api (#675)

This commit is contained in:
Dream Hunter
2025-06-21 01:41:28 +08:00
committed by GitHub
parent da5482e095
commit 483c429feb
2 changed files with 36 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
import { Hono } from 'hono'
import { Context, Hono } from 'hono'
import i18n from '../i18n';
import { getBooleanValue, getJsonSetting, checkCfTurnstile, getStringValue, getSplitStringListValue } from '../utils';
@@ -20,12 +20,34 @@ api.post('/api/attachment/delete', s3_attachment.deleteKey)
api.post('/api/attachment/put_url', s3_attachment.getSignedPutUrl)
api.post('/api/attachment/get_url', s3_attachment.getSignedGetUrl)
export async function updateAddressUpdatedAt(
c: Context<HonoCustomType>,
address: string | undefined | null
): Promise<void> {
if (!address) {
return;
}
// update address updated_at
try {
if (address) {
c.env.DB.prepare(
`UPDATE address SET updated_at = datetime('now') where name = ?`
).bind(address).run();
}
} catch (e) {
console.warn("Failed to update address updated_at")
}
}
api.get('/api/mails', async (c) => {
const { address } = c.get("jwtPayload")
if (!address) {
return c.json({ "error": "No address" }, 400)
}
const { limit, offset } = c.req.query();
if (Number.parseInt(offset) <= 0) await updateAddressUpdatedAt(c, address);
return await handleListQuery(c,
`SELECT * FROM raw_mails where address = ?`,
`SELECT count(*) as count FROM raw_mails where address = ?`,
@@ -89,14 +111,9 @@ api.get('/api/settings', async (c) => {
} catch (error) {
return c.text(msgs.InvalidAddressMsg, 400)
}
// update address updated_at
try {
c.env.DB.prepare(
`UPDATE address SET updated_at = datetime('now') where name = ?`
).bind(address).run();
} catch (e) {
console.warn("Failed to update address")
}
await updateAddressUpdatedAt(c, address);
const no_limit_roles = getSplitStringListValue(c.env.NO_LIMIT_SEND_ROLE);
const is_no_limit_send_balance = user_role && no_limit_roles.includes(user_role);
const balance = is_no_limit_send_balance ? 99999 : await c.env.DB.prepare(

View File

@@ -64,6 +64,16 @@ export default {
exp: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60,
iat: Math.floor(Date.now() / 1000),
}, c.env.JWT_SECRET, "HS256");
// update address updated_at
try {
c.env.DB.prepare(
`UPDATE address SET updated_at = datetime('now') where id IN `
+ `(SELECT address_id FROM users_address WHERE user_id = ?)`
).bind(user.user_id).run();
} catch (e) {
console.warn("Failed to update address updated_at")
}
return c.json({
...user,
is_admin: is_admin,