71 lines
3.4 KiB
TypeScript
71 lines
3.4 KiB
TypeScript
type Schedule = {
|
||
weekdays: number[];
|
||
times: string[];
|
||
mode?: "FIXED" | "RANDOM";
|
||
randomPostsPerDay?: number;
|
||
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;
|
||
const slots = new Set<number>();
|
||
while (slots.size < count) {
|
||
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
|
||
// 08:00–20:00, snapped to 5-minute increments for readable schedules.
|
||
slots.add(8 * 60 + (state % 145) * 5);
|
||
}
|
||
return [...slots].sort((a, b) => a - b);
|
||
}
|
||
|
||
/** 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 } = 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);
|
||
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 };
|
||
}
|
||
|
||
/** Future configured publication slots, used to place legacy auto drafts that predate plannedAt. */
|
||
export function upcomingContentSlots(schedule: Schedule, now = new Date(), limit = 20): Date[] {
|
||
const localNow = new Date(now.getTime() + BANGKOK_OFFSET);
|
||
const slots: Date[] = [];
|
||
for (let day = 0; day <= 28 && slots.length < limit; day++) {
|
||
const localDay = new Date(Date.UTC(localNow.getUTCFullYear(), localNow.getUTCMonth(), localNow.getUTCDate() + day));
|
||
if (!schedule.weekdays.includes(localDay.getUTCDay())) continue;
|
||
slots.push(...contentSlotsForBangkokDay(schedule, localDay).filter((slot) => slot.getTime() - now.getTime() >= 10 * 60_000));
|
||
}
|
||
return slots.sort((a, b) => a.getTime() - b.getTime()).slice(0, limit);
|
||
}
|