aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-12-15 23:54:53 -0800
committerFuwn <[email protected]>2023-12-15 23:54:53 -0800
commitf056a1e19470967198b1dab61cf6133047f07afd (patch)
treeea3ac4647037c257d1866aff7ff23cf64d9f9f3b
parentfeat(media): multiple dueincludes (diff)
downloaddue.moe-f056a1e19470967198b1dab61cf6133047f07afd.tar.xz
due.moe-f056a1e19470967198b1dab61cf6133047f07afd.zip
feat(notifications): refresh token
-rw-r--r--src/lib/AniList/notifications.ts33
-rw-r--r--src/routes/api/oauth/refresh/+server.ts43
-rw-r--r--src/routes/feeds/activity-notifications/+server.ts14
-rw-r--r--src/routes/settings/+page.svelte6
4 files changed, 52 insertions, 44 deletions
diff --git a/src/lib/AniList/notifications.ts b/src/lib/AniList/notifications.ts
index 4fdb9cb5..bebf1b42 100644
--- a/src/lib/AniList/notifications.ts
+++ b/src/lib/AniList/notifications.ts
@@ -30,7 +30,7 @@ export interface Notification {
| 'THREAD_LIKE';
}
-export const notifications = async (accessToken: string): Promise<Notification[]> => {
+export const notifications = async (accessToken: string): Promise<Notification[] | null> => {
const activityNotification = (type: string, extend = '') => `... on ${type} {
id user { name avatar { large } } context createdAt type ${extend}
}`;
@@ -45,18 +45,16 @@ export const notifications = async (accessToken: string): Promise<Notification[]
)}`;
const threadNotification = (type: string) =>
`${activityNotification(type, `thread { title id }`)}`;
-
- return (
- await (
- await fetch('https://graphql.anilist.co', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${accessToken}`,
- Accept: 'application/json'
- },
- body: JSON.stringify({
- query: `{ Page(page: 1, perPage: 50) { notifications {
+ const data = await (
+ await fetch('https://graphql.anilist.co', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${accessToken}`,
+ Accept: 'application/json'
+ },
+ body: JSON.stringify({
+ query: `{ Page(page: 1, perPage: 50) { notifications {
${activityNotification('FollowingNotification')}
${activityNotification('ActivityMessageNotification')}
${richActivityNotification('ActivityMentionNotification')}
@@ -70,8 +68,11 @@ export const notifications = async (accessToken: string): Promise<Notification[]
${threadNotification('ThreadCommentLikeNotification')}
${threadNotification('ThreadLikeNotification')}
} } }`
- })
})
- ).json()
- )['data']['Page']['notifications'];
+ })
+ ).json();
+
+ if (data['errors']) return null;
+
+ return data['data']['Page']['notifications'];
};
diff --git a/src/routes/api/oauth/refresh/+server.ts b/src/routes/api/oauth/refresh/+server.ts
index 0e6eef8c..ceb2566d 100644
--- a/src/routes/api/oauth/refresh/+server.ts
+++ b/src/routes/api/oauth/refresh/+server.ts
@@ -1,36 +1,29 @@
import { dev } from '$app/environment';
import { env } from '$env/dynamic/private';
import { env as env2 } from '$env/dynamic/public';
-import { redirect } from '@sveltejs/kit';
-export const GET = async ({ cookies }) => {
+export const GET = async ({ url, cookies }) => {
const formData = new FormData();
formData.append('grant_type', 'refresh_token');
formData.append('client_id', env2.PUBLIC_ANILIST_CLIENT_ID as string);
formData.append('client_secret', env.ANILIST_CLIENT_SECRET as string);
- formData.append(
- 'refresh_token',
- JSON.parse(cookies.get('user') || '{ refresh_token: null }')['refresh_token']
- );
- cookies.set(
- 'user',
- JSON.stringify(
- await (
- await fetch('https://anilist.co/api/v2/oauth/token', {
- method: 'POST',
- body: formData
- })
- ).json()
- ),
- {
- path: '/',
- maxAge: 60 * 60 * 24 * 7,
- httpOnly: true,
- sameSite: 'lax',
- secure: !dev
- }
- );
+ formData.append('refresh_token', url.searchParams.get('token') || '');
- throw redirect(303, '/');
+ const newUser = await (
+ await fetch('https://anilist.co/api/v2/oauth/token', {
+ method: 'POST',
+ body: formData
+ })
+ ).json();
+
+ cookies.set('user', JSON.stringify(newUser), {
+ path: '/',
+ maxAge: 60 * 60 * 24 * 7,
+ httpOnly: true,
+ sameSite: 'lax',
+ secure: !dev
+ });
+
+ return Response.json(newUser);
};
diff --git a/src/routes/feeds/activity-notifications/+server.ts b/src/routes/feeds/activity-notifications/+server.ts
index fde2027d..f0482194 100644
--- a/src/routes/feeds/activity-notifications/+server.ts
+++ b/src/routes/feeds/activity-notifications/+server.ts
@@ -59,9 +59,19 @@ const render = (posts: Notification[] = []) => `<?xml version="1.0" encoding="UT
`;
export const GET = async ({ url }) => {
- const token = url.searchParams.get('token');
+ let token = url.searchParams.get('token');
+ const refresh = url.searchParams.get('refresh');
+ let notification = await notifications(token || '');
- return new Response(token ? render(await notifications(token)) : render(), {
+ if (notification === null) {
+ token = (
+ await (await fetch(`https://192.168.1.60:5173/api/oauth/refresh?token=${refresh}`)).json()
+ )['access_token'];
+
+ notification = await notifications(token as string);
+ }
+
+ return new Response(token ? render(notification || []) : render(), {
headers: {
'Cache-Control': `max-age=0, s-max-age=0`,
'Content-Type': 'application/xml'
diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte
index a7db1390..2cb077e8 100644
--- a/src/routes/settings/+page.svelte
+++ b/src/routes/settings/+page.svelte
@@ -67,7 +67,11 @@
href={'#'}
on:click={() =>
navigator.clipboard.writeText(
- `https://due.moe/feeds/activity-notifications?token=${data.user.accessToken}`
+ `https://${
+ env.PUBLIC_ANILIST_REDIRECT_URI?.includes('192.168') ? '192.168.1.60:5173' : 'due.moe'
+ }/feeds/activity-notifications?token=${data.user.accessToken}&refresh=${
+ data.user.refreshToken
+ }`
)}
>
Click to copy AniList notifications RSS feed URL and token