diff options
Diffstat (limited to 'src/lib/Data/AniList/lastActivity.ts')
| -rw-r--r-- | src/lib/Data/AniList/lastActivity.ts | 71 |
1 files changed, 71 insertions, 0 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 }; +}; |