Skip to content

Commit

Permalink
api/calendar: use aggregate endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Sep 11, 2024
1 parent 5c35946 commit e2bb991
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 40 deletions.
20 changes: 11 additions & 9 deletions components/Projection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import uniqolor from "uniqolor";
const props = defineProps<{ projection: Projection }>();
const eventSources = props.projection.participants.map(
(p) =>
({
id: p.discord_username,
url: `/api/calendars/${p.discord_id_str}`,
format: "ics",
color: uniqolor(p.id).color,
} satisfies EventSourceInput)
);
const eventSources = props.projection.participants
.toSorted((a, b) => a.discord_username.localeCompare(b.discord_username))
.map(
(p) =>
({
id: p.discord_username,
url: `/api/calendars/${p.discord_id_str}`,
format: "ics",
color: uniqolor(p.id).color,
} satisfies EventSourceInput)
);
</script>

<template>
Expand Down
32 changes: 2 additions & 30 deletions server/api/calendars/[user].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,11 @@ export default defineEventHandler(async (event) => {

const config = useRuntimeConfig();

// fetch user calendar
let userCalendar: string = "";
const { data } = await authRetry(() =>
nanapi.GET("/calendar/user_calendars/{discord_id}", {
params: {
path: {
discord_id: discord_id as any,
},
},
})
);
if (data !== undefined) {
const icsUrl = data.ics.replace(/^webcal:/, "https:");
userCalendar = await $fetch<string>(icsUrl);
}

// fetch guild calendar
const url = new URL(`${config.nanapiUrl}/calendar/ics`);
url.searchParams.append("client", config.nanapiCustomClientUsername);
url.searchParams.append("user", discord_id);
const guildCalendar = await $fetch<string>(url.toString());

// dirty merge
let ics = "";
if (userCalendar) {
let userLines = userCalendar.trim().split("\n");
let guildLines = guildCalendar.trim().split("\n");
ics = userLines.slice(0, -1).join("\n") + "\n";
ics += "\n";
ics += guildLines.slice(1).join("\n");
} else {
ics = guildCalendar;
}
url.searchParams.append("aggregate", "true");
const ics = await $fetch<string>(url.toString());

setHeader(event, "Content-Type", "text/calendar");
return ics;
Expand Down
5 changes: 4 additions & 1 deletion server/utils/nanapi-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ export async function authRetry<T extends FetchResponse<any, any, any>>(
makeReq: () => Promise<T>
) {
let res;
while (true) {
for (let i = 0; i < 3; i++) {
res = await makeReq();
if (res.response.status === 401) {
access_token = "";
} else {
return res;
}
}
throw new Error(
`Failed to authenticate after 3 attempts: ${res?.response.status}`
);
}

export const nanapi = createClient<paths>({ baseUrl: config.nanapiUrl });
Expand Down

0 comments on commit e2bb991

Please sign in to comment.