feat: schedule next airing episode
happens when new title is saved, or when new episode internal route is run successfully
This commit is contained in:
32
src/libs/maybeScheduleNextAiringEpisode.ts
Normal file
32
src/libs/maybeScheduleNextAiringEpisode.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Client } from "@upstash/qstash";
|
||||
import type { HonoRequest } from "hono";
|
||||
|
||||
import { setTitleMessage } from "~/models/titleMessages";
|
||||
import type { Env } from "~/types/env";
|
||||
|
||||
import { getNextEpisodeTimeUntilAiring } from "./anilist/getNextEpisodeAiringAt";
|
||||
import { getCurrentDomain } from "./getCurrentDomain";
|
||||
|
||||
export async function maybeScheduleNextAiringEpisode(
|
||||
env: Env,
|
||||
req: HonoRequest,
|
||||
aniListId: number,
|
||||
) {
|
||||
const nextAiring = await getNextEpisodeTimeUntilAiring(aniListId);
|
||||
if (!nextAiring) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { timeUntilAiring, episode: nextEpisode } = nextAiring;
|
||||
const client = new Client({ token: env.QSTASH_TOKEN });
|
||||
|
||||
const domain = getCurrentDomain(req);
|
||||
const { messageId } = await client.publishJSON({
|
||||
url: `${domain}/internal/new-episode`,
|
||||
body: { aniListId, episode: nextEpisode },
|
||||
retries: 0,
|
||||
delay: timeUntilAiring,
|
||||
contentBasedDeduplication: true,
|
||||
});
|
||||
await setTitleMessage(env, aniListId, messageId);
|
||||
}
|
||||
Reference in New Issue
Block a user