diff options
| author | Fuwn <[email protected]> | 2026-03-22 04:05:52 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-03-22 04:05:52 +0000 |
| commit | 192aa0d61b58288499e7c0fc179d25dcc01dba98 (patch) | |
| tree | 576568f9802093a3aed912342df2ca101b05cbd3 /src/lib | |
| parent | perf: lazy-load landing demo sections (diff) | |
| download | due.moe-192aa0d61b58288499e7c0fc179d25dcc01dba98.tar.xz due.moe-192aa0d61b58288499e7c0fc179d25dcc01dba98.zip | |
refactor: isolate last activity fetch path
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/Data/AniList/lastActivity.ts | 71 | ||||
| -rw-r--r-- | src/lib/Home/LastActivity.svelte | 2 |
2 files changed, 72 insertions, 1 deletions
diff --git a/src/lib/Data/AniList/lastActivity.ts b/src/lib/Data/AniList/lastActivity.ts new file mode 100644 index 00000000..49daa8fd --- /dev/null +++ b/src/lib/Data/AniList/lastActivity.ts @@ -0,0 +1,71 @@ +import type { AniListAuthorisation, UserIdentity } from "./identity"; + +interface ActivityHistoryOptions { + stats: { + activityHistory: { + date: number; + }[]; + }; + options: { + timezone: string; + }; +} + +interface LastActivity { + date: Date; + timezone: string; +} + +const convertToTimezoneOffset = (timeStr: string) => { + const [hours, minutes] = timeStr.split(":"); + let totalMinutes = parseInt(hours, 10) * 60 + parseInt(minutes, 10); + + totalMinutes = -totalMinutes; + + return totalMinutes; +}; + +const activityHistoryOptions = async ( + userIdentity: UserIdentity, + authorisation: AniListAuthorisation, +): Promise<ActivityHistoryOptions> => + ( + await ( + await fetch("https://graphql.anilist.co", { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `${authorisation.tokenType} ${authorisation.accessToken}`, + }, + body: JSON.stringify({ + query: `{ User(id: ${userIdentity.id}) { + stats { activityHistory { date } } + options { timezone } + } }`, + }), + }) + ).json() + ).data.User; + +export const lastActivityDate = async ( + userIdentity: UserIdentity, + authorisation: AniListAuthorisation, +): Promise<LastActivity> => { + if (userIdentity.id === -1 || userIdentity.id === -2) + return { date: new Date(8640000000000000), timezone: "" }; + + const history = await activityHistoryOptions(userIdentity, authorisation); + const date = new Date( + Number( + history.stats.activityHistory[history.stats.activityHistory.length - 1] + .date, + ) * + 1000 + + convertToTimezoneOffset(history.options.timezone), + ); + + date.setDate(date.getDate() + 1); + + return { date, timezone: history.options.timezone }; +}; diff --git a/src/lib/Home/LastActivity.svelte b/src/lib/Home/LastActivity.svelte index 39b35198..4ade371a 100644 --- a/src/lib/Home/LastActivity.svelte +++ b/src/lib/Home/LastActivity.svelte @@ -2,7 +2,7 @@ import userIdentity from "$stores/identity"; import { onMount } from "svelte"; import type { AniListAuthorisation } from "$lib/Data/AniList/identity"; -import { lastActivityDate } from "../Data/AniList/activity"; +import { lastActivityDate } from "../Data/AniList/lastActivity"; import settings from "$stores/settings"; let { user }: { user: AniListAuthorisation } = $props(); |