diff options
| author | Fuwn <[email protected]> | 2023-09-12 16:47:11 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-09-12 16:47:11 -0700 |
| commit | d6e737f207d0d5df3c7aa2726cde889536dbb566 (patch) | |
| tree | 2c12f7c1602f4075ddb86ae0ef1e55c552691776 | |
| parent | feat(layout): navigation (diff) | |
| download | due.moe-d6e737f207d0d5df3c7aa2726cde889536dbb566.tar.xz due.moe-d6e737f207d0d5df3c7aa2726cde889536dbb566.zip | |
feat: activity history risk
| -rw-r--r-- | src/lib/AniList/activity.ts | 20 | ||||
| -rw-r--r-- | src/routes/activity-history/+page.svelte | 92 |
2 files changed, 107 insertions, 5 deletions
diff --git a/src/lib/AniList/activity.ts b/src/lib/AniList/activity.ts index 0d79bf72..bd4bafc0 100644 --- a/src/lib/AniList/activity.ts +++ b/src/lib/AniList/activity.ts @@ -1,7 +1,14 @@ import type { UserIdentity } from './identity'; -export const lastActivityDate = async (userIdentity: UserIdentity): Promise<Date> => { - const activityHistory = ( +export interface ActivityHistoryEntry { + date: number; + amount: number; +} + +export const activityHistory = async ( + userIdentity: UserIdentity +): Promise<ActivityHistoryEntry[]> => { + return ( await ( await fetch('https://graphql.anilist.co', { method: 'POST', @@ -11,15 +18,18 @@ export const lastActivityDate = async (userIdentity: UserIdentity): Promise<Date }, body: JSON.stringify({ query: `{ User(id: ${userIdentity.id}) { - stats { activityHistory { date } } + stats { activityHistory { date amount } } } }` }) }) ).json() )['data']['User']['stats']['activityHistory']; +}; + +export const lastActivityDate = async (userIdentity: UserIdentity): Promise<Date> => { + const history = await activityHistory(userIdentity); const date = new Date( - Number(activityHistory[activityHistory.length - 1]['date']) * 1000 + - new Date().getTimezoneOffset() + Number(history[history.length - 1]['date']) * 1000 + new Date().getTimezoneOffset() ); date.setDate(date.getDate() + 1); diff --git a/src/routes/activity-history/+page.svelte b/src/routes/activity-history/+page.svelte new file mode 100644 index 00000000..a1938ee9 --- /dev/null +++ b/src/routes/activity-history/+page.svelte @@ -0,0 +1,92 @@ +<script lang="ts"> + import { activityHistory, type ActivityHistoryEntry } from '$lib/AniList/activity.js'; + import { onMount } from 'svelte'; + import userIdentity from '../../stores/userIdentity.js'; + import { userIdentity as getUserIdentity } from '$lib/AniList/identity'; + + export let data; + + let activityHistoryData: Promise<ActivityHistoryEntry[]>; + let currentUserIdentity = { name: '', id: -1 }; + const timezoneOffset = new Date().getTimezoneOffset() * 60 * 1000; + + onMount(async () => { + if (data.user !== undefined) { + if ($userIdentity === '') { + userIdentity.set(JSON.stringify(await getUserIdentity(data.user))); + } + + currentUserIdentity = JSON.parse($userIdentity); + currentUserIdentity.name = currentUserIdentity.name; + activityHistoryData = activityHistory(currentUserIdentity); + console.log(fillMissingDays(await activityHistory(currentUserIdentity))); + } + }); + + const fillMissingDays = (inputActivities: ActivityHistoryEntry[]): ActivityHistoryEntry[] => { + let activities = inputActivities; + const firstDate = new Date(activities[0].date * 1000 + timezoneOffset); + const lastDate = new Date(activities[activities.length - 1].date * 1000 + timezoneOffset); + const currentDate = firstDate; + + while (currentDate <= lastDate) { + const current_unix_timestamp = currentDate.getTime(); + let found = false; + + for (let i = 0; i < activities.length; i++) { + if (activities[i].date * 1000 + timezoneOffset === current_unix_timestamp) { + found = true; + + break; + } + } + + if (!found) { + activities.push({ + date: current_unix_timestamp / 1000, + amount: 0 + }); + } + + currentDate.setDate(currentDate.getDate() + 1); + } + + // activities.sort((a: { date: number }, b: { date: number }) => a.date - b.date); + + return activities; + }; + + // const incrementDate = (date: Date): Date => { + // date.setDate(date.getDate() + 1); + + // return date; + // }; +</script> + +<h1>Activity History Hole Risk</h1> + +Days in risk of developing an activity history hole. (days with one activity) + +<p /> + +{#if data.user === undefined} + Please log in to view this page. +{:else} + {#await activityHistoryData} + Loading ... + {:then activities} + {#if activities === undefined} + Loading ... + {:else} + <ul> + {#each fillMissingDays(activities) as activity} + {#if activity.amount === 0} + <li> + {new Date(activity.date * 1000 + timezoneOffset).toDateString()} + </li> + {/if} + {/each} + </ul> + {/if} + {/await} +{/if} |