Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ArmaEvents API for event display #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

NeutraleNull
Copy link

This PR updates the frontend part of the HP to use the arma events instead of the our old forum.

I didn't touch any old stuff and mocked the output of AE to resemble the same format as before so the old code can work with it "out of the box".

@NeutraleNull NeutraleNull added the enhancement New feature or request label Dec 14, 2024
@NeutraleNull NeutraleNull requested a review from DerZade December 14, 2024 19:56
Copy link
Member

@DerZade DerZade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this.

You adjusted the frontend to access arma.events.
Instead we should keep the frontend implementation and just update the HP API to access arma.events instead of the forum.

See here:

private async fetchEvents (): Promise<ArmaEvent[]> {
const response = await fetch(`${ArmaEventsService.FORUM_URI}/api/events/cid-3`);
if (!response.ok) {
throw new Error(`Error while trying to fetch events. Forum API responded with status code ${response.status}.`);
}
const { topics } = await response.json() as { topics: Array<{ slug: string, titleRaw: string, tid: number, deleted: 1 | 0, timestamp: number }> };
const rawEvents: Array<Omit<ArmaEvent, 'attendance'> & { tid: number }> = [];
for (const topic of topics) {
if (topic.timestamp < ArmaEventsService.ATTENDANCE_PLUGIN_INTRODUCTION) continue;
if (topic.deleted === 1) {
console.warn(`Skipping topic ${topic.titleRaw} (ID: ${topic.tid}), because it was deleted`);
continue;
}
if (!ArmaEventsService.TOPIC_TITLE_REGEX.test(topic.titleRaw)) {
console.warn(`Skipping topic ${topic.titleRaw} (ID: ${topic.tid}), because its title did not match the required pattern.`);
continue;
}
rawEvents.push({
date: new Date(topic.titleRaw.substr(0, 10)),
title: topic.titleRaw.replace(ArmaEventsService.TOPIC_TITLE_REGEX, ''),
url: `${ArmaEventsService.FORUM_URI}/topic/${topic.slug}`,
tid: topic.tid
});
}
// events sorted with event furthest back in past as first item
const sortedEvents = rawEvents.sort((a, b) => a.date.getTime() - b.date.getTime());
// we only want one future event and a max of 10 events;
// if there is no future event we just want the 10 most recent events
const firstFutureEvent = sortedEvents.findIndex(e => ArmaEventsService.isInFuture(e.date)) + 1;
const filteredEvents = sortedEvents.slice(0, firstFutureEvent > 0 ? firstFutureEvent : sortedEvents.length).reverse().slice(0, 10);
const promises = filteredEvents.map(async ({ tid, ...rest }) => await ArmaEventsService.fetchAttendance(tid).then((attendance): ArmaEvent => ({ ...rest, attendance })));
return await Promise.all(promises);
}

This ensures that we don’t send unnecessary requests to arma.events and therefore improves performance for the user :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants