feat: store unreleased titles where first episode time is unknown
This commit is contained in:
@@ -4,6 +4,7 @@ import { GraphQLClient } from "graphql-request";
|
||||
const GetNextEpisodeAiringAtQuery = graphql(`
|
||||
query GetNextEpisodeAiringAt($id: Int!) {
|
||||
Media(id: $id) {
|
||||
status
|
||||
nextAiringEpisode {
|
||||
episode
|
||||
timeUntilAiring
|
||||
@@ -18,11 +19,9 @@ export function getNextEpisodeTimeUntilAiring(aniListId: number) {
|
||||
return client
|
||||
.request(GetNextEpisodeAiringAtQuery, { id: aniListId })
|
||||
.then((data) => {
|
||||
const status = data!.Media!.status;
|
||||
const nextAiring = data!.Media!.nextAiringEpisode;
|
||||
if (!nextAiring) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return nextAiring;
|
||||
return { status, nextAiring };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,10 @@ import { Client } from "@upstash/qstash";
|
||||
import type { HonoRequest } from "hono";
|
||||
|
||||
import { setTitleMessage } from "~/models/titleMessages";
|
||||
import {
|
||||
addUnreleasedTitle,
|
||||
removeUnreleasedTitle,
|
||||
} from "~/models/unreleasedTitles";
|
||||
import type { Env } from "~/types/env";
|
||||
|
||||
import { getNextEpisodeTimeUntilAiring } from "./anilist/getNextEpisodeAiringAt";
|
||||
@@ -13,9 +17,14 @@ export async function maybeScheduleNextAiringEpisode(
|
||||
req: HonoRequest,
|
||||
aniListId: number,
|
||||
) {
|
||||
const nextAiring = await getNextEpisodeTimeUntilAiring(aniListId);
|
||||
const { nextAiring, status } = await getNextEpisodeTimeUntilAiring(aniListId);
|
||||
if (!nextAiring) {
|
||||
await deleteMessageIdForTitle(env, aniListId);
|
||||
if (status === "NOT_YET_RELEASED") {
|
||||
await addUnreleasedTitle(env, aniListId);
|
||||
} else {
|
||||
await deleteMessageIdForTitle(env, aniListId);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -30,5 +39,8 @@ export async function maybeScheduleNextAiringEpisode(
|
||||
delay: timeUntilAiring,
|
||||
contentBasedDeduplication: true,
|
||||
});
|
||||
await setTitleMessage(env, aniListId, messageId);
|
||||
await Promise.allSettled([
|
||||
setTitleMessage(env, aniListId, messageId),
|
||||
removeUnreleasedTitle(env, aniListId),
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user