Add review window and manual content generation

This commit is contained in:
Loma Bot Ops
2026-11-27 13:34:41 +07:00
parent 9d5bfe7d9d
commit 8086197f58
16 changed files with 357 additions and 62 deletions
+31 -15
View File
@@ -6,6 +6,10 @@ type Schedule = {
seed?: string;
};
const BANGKOK_OFFSET = 7 * 60 * 60_000;
const DRAFT_HOUR = 3;
const BOARD_HOUR = 8;
function seededMinutes(seed: string, count: number): number[] {
let state = 2166136261;
for (const char of seed) state = Math.imul(state ^ char.charCodeAt(0), 16777619) >>> 0;
@@ -19,24 +23,36 @@ function seededMinutes(seed: string, count: number): number[] {
}
/** Weekly slots in Asia/Bangkok (UTC+7, no daylight saving), strictly after now. */
export function contentSlotsForBangkokDay(schedule: Schedule, localDay: Date): Date[] {
const { times, mode = "FIXED", randomPostsPerDay = 1, seed = "content-auto" } = schedule;
const dateKey = localDay.toISOString().slice(0, 10);
const minutesOfDay = mode === "RANDOM"
? seededMinutes(`${seed}:${dateKey}`, Math.min(3, Math.max(1, randomPostsPerDay)))
: [...times].sort().map((time) => {
const [hours, minutes] = time.split(":").map(Number);
return hours * 60 + minutes;
});
const midnight = Date.UTC(localDay.getUTCFullYear(), localDay.getUTCMonth(), localDay.getUTCDate());
return minutesOfDay.map((minutes) => new Date(midnight + minutes * 60_000 - BANGKOK_OFFSET));
}
/** Next 03:00 draft run in Bangkok, one day before a selected publication day. */
export function nextContentRun(schedule: Schedule, now = new Date()): Date {
const { weekdays, times, mode = "FIXED", randomPostsPerDay = 1, seed = "content-auto" } = schedule;
const offset = 7 * 60 * 60_000;
const local = new Date(now.getTime() + offset);
const { weekdays } = schedule;
const local = new Date(now.getTime() + BANGKOK_OFFSET);
for (let day = 0; day <= 7; day++) {
const midnight = Date.UTC(local.getUTCFullYear(), local.getUTCMonth(), local.getUTCDate() + day);
if (!weekdays.includes(new Date(midnight).getUTCDay())) continue;
const dateKey = new Date(midnight).toISOString().slice(0, 10);
const minutesOfDay = mode === "RANDOM"
? seededMinutes(`${seed}:${dateKey}`, Math.min(3, Math.max(1, randomPostsPerDay)))
: [...times].sort().map((time) => {
const [hours, minutes] = time.split(":").map(Number);
return hours * 60 + minutes;
});
for (const minutes of minutesOfDay) {
const candidate = new Date(midnight + minutes * 60_000 - offset);
if (candidate > now) return candidate;
}
const publicationDay = new Date(midnight + 24 * 60 * 60_000).getUTCDay();
if (!weekdays.includes(publicationDay)) continue;
const candidate = new Date(midnight + DRAFT_HOUR * 60 * 60_000 - BANGKOK_OFFSET);
if (candidate > now) return candidate;
}
throw new Error("No valid weekly content slot");
}
export function automaticRunWindow(draftRunAt: Date, schedule: Schedule) {
const localDraftDay = new Date(draftRunAt.getTime() + BANGKOK_OFFSET);
const publicationDay = new Date(Date.UTC(localDraftDay.getUTCFullYear(), localDraftDay.getUTCMonth(), localDraftDay.getUTCDate() + 1));
const placeOnBoardAt = new Date(Date.UTC(localDraftDay.getUTCFullYear(), localDraftDay.getUTCMonth(), localDraftDay.getUTCDate(), BOARD_HOUR) - BANGKOK_OFFSET);
return { publicationSlots: contentSlotsForBangkokDay(schedule, publicationDay), placeOnBoardAt };
}