refactor: centralize mail state feature checks

This commit is contained in:
dreamhunter2333
2026-08-26 00:33:04 +08:00
parent fe73bbc54c
commit 523f9b8af6
7 changed files with 14 additions and 11 deletions
+6 -3
View File
@@ -1,3 +1,5 @@
import { getBooleanValue } from './utils';
export const MAIL_FLAGS = {
UNREAD: 1 << 0,
ANSWERED: 1 << 1,
@@ -94,12 +96,12 @@ export const createCustomMailStateDefinitions = (
export const serializeMailState = <T extends Record<string, unknown>>(
row: T,
enabled: boolean,
env: Bindings,
): T => {
const result = { ...row };
const flags = Number(result.flags ?? 0);
delete result.flags;
if (!enabled) {
if (!getBooleanValue(env.ENABLE_MAIL_FLAGS)) {
return result;
}
result.unread = (flags & MAIL_FLAGS.UNREAD) !== 0;
@@ -196,6 +198,7 @@ type MailScope = {
export const applyMailStateUpdate = async (
db: D1Database,
env: Bindings,
scope: MailScope,
value: unknown,
customStates: MailStateDefinition[] = [],
@@ -224,6 +227,6 @@ export const applyMailStateUpdate = async (
return {
success: true,
changes: result.meta.changes ?? 0,
results: results.map(row => serializeMailState(row, true)),
results: results.map(row => serializeMailState(row, env)),
};
};