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

Merged
merged 1 commit into from
Jan 20, 2025
Merged

Use ArmaEvents API for event display #45

merged 1 commit into from
Jan 20, 2025

Conversation

NeutraleNull
Copy link
Contributor

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 :)

@NeutraleNull
Copy link
Contributor Author

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 :)

I have reverted the changes to the frontend code i made and moved the query logic into the api backend part as recommended.
@DerZade could you test it out for me? I cannot build the backend code locally as i get this:
image

@DerZade DerZade self-requested a review January 20, 2025 11:57
@DerZade
Copy link
Member

DerZade commented Jan 20, 2025

@NeutraleNull I took the liberty and force-pushed to your branch. Are you happy with the changes?

@NeutraleNull
Copy link
Contributor Author

@DerZade i had a look and build it locally and it looks fine, however i still was unable to do a local serve for testing. From my side it's fine.

@DerZade DerZade merged commit 1fb51bb into master Jan 20, 2025
1 check passed
@DerZade DerZade deleted the impl-arma-events branch January 20, 2025 21:42
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