Skip to content

Commit

Permalink
Merge pull request #221 from poap-xyz/release/v1.12.10
Browse files Browse the repository at this point in the history
Release v1.12.10
  • Loading branch information
jm42 authored May 10, 2024
2 parents cb5d885 + e580995 commit 12d66a1
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 50 deletions.
4 changes: 2 additions & 2 deletions netlify/edge-functions/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ export default async function handler(request, context) {
})
}

totalSupply += eventInfo.owners.length
totalSupply += eventInfo.owners?.owners.length ?? 0
totalReservations += eventInfo.metrics?.emailReservations ?? 0
names = [...names, eventInfo.event.name]
ts = Math.max(ts, eventInfo.ts ?? eventInfo.metrics?.ts)
ts = Math.max(ts, eventInfo.owners?.ts ?? eventInfo.metrics?.ts)
}

return new Response(
Expand Down
6 changes: 3 additions & 3 deletions netlify/edge-functions/frame.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export default async function handler(request, context) {

for (const eventInfo of eventsInfo) {
allCollectors = allCollectors == null
? eventInfo.owners
: intersection(allCollectors, eventInfo.owners)
totalSupply += eventInfo.owners.length
? eventInfo.owners?.owners ?? []
: intersection(allCollectors, eventInfo.owners?.owners ?? [])
totalSupply += eventInfo.owners?.owners.length ?? 0
totalReservations += eventInfo.metrics?.emailReservations ?? 0
totalMoments += eventInfo.metrics?.momentsUploaded ?? 0
}
Expand Down
23 changes: 8 additions & 15 deletions netlify/loaders/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export async function getEventInfo(eventId, env) {
return null
}
const response = await fetch(
`${env.FAMILY_API_URL}/event/${eventId}?description=false&metrics=true&fresh=true&refresh=false`,
`${env.FAMILY_API_URL}/event/${eventId}?description=false&metrics=true`,
{
headers: {
'x-api-key': env.FAMILY_API_KEY,
Expand Down Expand Up @@ -64,7 +64,7 @@ export async function getEvents(eventIds, env) {
return null
}
const response = await fetch(
`${env.FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}?fresh=true`,
`${env.FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}`,
{
headers: {
'x-api-key': env.FAMILY_API_KEY,
Expand Down Expand Up @@ -93,11 +93,7 @@ export async function getEvents(eventIds, env) {
if (typeof body !== 'object') {
return null
}
return Object.fromEntries(
Object.entries(body).map(
([eventId, event]) => [eventId, event]
)
)
return body
}

export async function getEventsOwners(eventIds, env) {
Expand All @@ -106,16 +102,13 @@ export async function getEventsOwners(eventIds, env) {
}
const response = await fetch(
`${env.FAMILY_API_URL}/events` +
`/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}/owners?fresh=false`,
`/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}/owners`,
{
headers: {
'x-api-key': env.FAMILY_API_KEY,
},
}
)
if (response.status === 404) {
return null
}
if (response.status !== 200) {
let message
try {
Expand Down Expand Up @@ -143,7 +136,7 @@ export async function getEventsMetrics(eventIds, env) {
throw new Error(`Events (${eventIds.length}) metrics could not be fetched, configure Family API key`)
}
const response = await fetch(
`${env.FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}/metrics?fresh=true`,
`${env.FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}/metrics`,
{
headers: {
'x-api-key': env.FAMILY_API_KEY,
Expand All @@ -168,9 +161,9 @@ export async function getEventsMetrics(eventIds, env) {
}
throw new Error(`Events (${eventIds.length}) failed to fetch metrics (status ${response.status})`)
}
const metricsMap = await response.json()
if (typeof metricsMap !== 'object') {
const body = await response.json()
if (typeof body !== 'object') {
return null
}
return metricsMap
return body
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poap-family",
"version": "1.12.9",
"version": "1.12.10",
"author": {
"name": "POAP",
"url": "https://poap.xyz"
Expand Down
33 changes: 14 additions & 19 deletions src/loaders/api.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import axios from 'axios'
import { FAMILY_API_KEY, FAMILY_API_URL } from '../models/api'
import { encodeExpiryDates, Event } from '../models/event'
import { encodeExpiryDates, Event, EventOwners } from '../models/event'

async function getEventAndOwners(
eventId,
abortSignal,
includeDescription = false,
includeMetrics = true,
fresh = true,
refresh = false,
) {
if (!FAMILY_API_KEY) {
Expand All @@ -17,7 +16,6 @@ async function getEventAndOwners(
`${FAMILY_API_URL}/event/${eventId}?` +
`description=${encodeURIComponent(includeDescription)}&` +
`metrics=${encodeURIComponent(includeMetrics)}` +
`${fresh ? '&fresh=true' : ''}` +
`${refresh ? '&refresh=true' : ''}`,
{
signal: abortSignal instanceof AbortSignal ? abortSignal : undefined,
Expand Down Expand Up @@ -213,13 +211,12 @@ async function getLastEvents(page = 1, qty = 3) {
}
}

async function getEvents(eventIds, fresh = true) {
async function getEvents(eventIds) {
if (!FAMILY_API_KEY) {
return null
}
const response = await fetch(
`${FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}` +
`${fresh ? '?fresh=true' : ''}`,
`${FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}`,
{
headers: {
'x-api-key': FAMILY_API_KEY,
Expand Down Expand Up @@ -255,25 +252,22 @@ async function getEvents(eventIds, fresh = true) {
)
}

async function getEventsOwners(eventIds, abortSignal, expiryDates, fresh = true) {
async function getEventsOwners(eventIds, abortSignal, expiryDates) {
if (!FAMILY_API_KEY) {
return null
}
const queryString = expiryDates ? encodeExpiryDates(expiryDates) : ''
const response = await fetch(
`${FAMILY_API_URL}/events` +
`/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}` +
`/owners${queryString ? `?${queryString}${fresh ? '&fresh=true' : ''}` : fresh ? '?fresh=true' : ''}`,
`/owners${queryString ? `?${queryString}` : ''}`,
{
signal: abortSignal instanceof AbortSignal ? abortSignal : null,
headers: {
'x-api-key': FAMILY_API_KEY,
},
}
)
if (response.status === 404) {
return null
}
if (response.status !== 200) {
let message
try {
Expand All @@ -293,17 +287,19 @@ async function getEventsOwners(eventIds, abortSignal, expiryDates, fresh = true)
if (typeof body !== 'object') {
return null
}
return body
return Object.fromEntries(
Object.entries(body).map(
([eventId, event]) => [eventId, EventOwners(event)]
)
)
}

async function getEventMetrics(eventId, abortSignal, refresh = false, fresh = true) {
async function getEventMetrics(eventId, abortSignal, refresh = false) {
if (!FAMILY_API_KEY) {
throw new Error(`Event ${eventId} metrics could not be fetched, configure Family API key`)
}
const response = await fetch(
`${FAMILY_API_URL}/event/${eventId}/metrics?` +
`refresh=${encodeURIComponent(refresh)}&` +
`fresh=${encodeURIComponent(fresh)}`,
`${FAMILY_API_URL}/event/${eventId}/metrics${refresh ? '?refresh=true' : ''}`,
{
signal: abortSignal instanceof AbortSignal ? abortSignal : null,
headers: {
Expand Down Expand Up @@ -340,13 +336,12 @@ async function getEventMetrics(eventId, abortSignal, refresh = false, fresh = tr
}
}

async function getEventsMetrics(eventIds, abortSignal, fresh = true) {
async function getEventsMetrics(eventIds, abortSignal) {
if (!FAMILY_API_KEY) {
throw new Error(`Events (${eventIds.length}) metrics could not be fetched, configure Family API key`)
}
const response = await fetch(
`${FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}/metrics?` +
`fresh=${encodeURIComponent(fresh)}`,
`${FAMILY_API_URL}/events/${eventIds.map((eventId) => encodeURIComponent(eventId)).join(',')}/metrics`,
{
signal: abortSignal instanceof AbortSignal ? abortSignal : null,
headers: {
Expand Down
5 changes: 2 additions & 3 deletions src/loaders/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ async function eventLoader({ params, request }) {
/*abortSignal*/undefined,
/*includeDescription*/true,
/*includeMetrics*/true,
/*fresh*/true,
/*refresh*/force === 'true'
)
if (eventAndOwners != null) {
Expand All @@ -177,7 +176,7 @@ async function eventLoader({ params, request }) {
}
const [tokensSettled, metricsSettled] = await Promise.allSettled([
fetchPOAPs(params.eventId),
getEventMetrics(params.eventId, null, /*refresh*/force === 'true', /*fresh*/true),
getEventMetrics(params.eventId, null, /*refresh*/force === 'true'),
])
if (tokensSettled.status === 'rejected') {
throw new Response('', {
Expand Down Expand Up @@ -233,7 +232,7 @@ async function eventsLoader({ params, request }) {
const force = new URL(request.url).searchParams.get('force')
if (!force) {
try {
const events = await getEvents(eventIds, /*fresh*/true)
const events = await getEvents(eventIds)
if (events) {
const notFoundEventIds = []
for (const eventId of eventIds) {
Expand Down
22 changes: 22 additions & 0 deletions src/models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ function Event(event, includeDescription) {
}
}

function EventOwners(eventOwners) {
if (
eventOwners == null ||
typeof eventOwners !== 'object' ||
!('owners' in eventOwners) ||
(
eventOwners.owners != null &&
!Array.isArray(eventOwners.owners)
) ||
!('ts' in eventOwners) ||
eventOwners.ts == null ||
typeof eventOwners.ts !== 'number'
) {
throw new Error('Malformed event owners')
}
return {
owners: eventOwners.owners,
ts: eventOwners.ts,
}
}

function parseEventIds(rawIds) {
let eventIds = (rawIds ?? '').split(',')
.filter((value, index, all) => all.indexOf(value) === index)
Expand Down Expand Up @@ -65,6 +86,7 @@ const SEARCH_LIMIT = 10

export {
Event,
EventOwners,
parseEventIds,
joinEventIds,
parseExpiryDates,
Expand Down
5 changes: 4 additions & 1 deletion src/pages/Addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ function Addresses() {
setLoadingEventsOwners(false)
if (ownersMap) {
const uniqueOwners = Object.values(ownersMap).reduce(
(allOwners, eventOwners) => new Set([...allOwners, ...eventOwners]),
(allOwners, eventOwners) => new Set([
...allOwners,
...(eventOwners?.owners ?? []),
]),
new Set()
)
const addresses = [...uniqueOwners].map((owner) => parseAddress(owner))
Expand Down
27 changes: 21 additions & 6 deletions src/pages/Events.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ function Events() {
}
removeErrors(eventId)
setLoading((alsoLoading) => ({ ...alsoLoading, [eventId]: LOADING_OWNERS }))
return getEventAndOwners(eventId, abortSignal, /*includeDescription*/false, /*includeMetrics*/true, /*fresh*/true).then(
return getEventAndOwners(
eventId,
abortSignal,
/*includeDescription*/false,
/*includeMetrics*/true,
/*refresh*/false
).then(
(eventAndOwners) => {
removeLoading(eventId)
if (eventAndOwners != null) {
Expand Down Expand Up @@ -157,7 +163,7 @@ function Events() {
setLoading((alsoLoading) => ({ ...alsoLoading, [eventId]: LOADING_OWNERS }))
return Promise.allSettled([
fetchPOAPs(eventId, abortSignal),
getEventMetrics(eventId, abortSignal, searchParams.get('force') === 'true', /*fresh*/true),
getEventMetrics(eventId, abortSignal, searchParams.get('force') === 'true'),
]).then(
([eventOwnerTokensResult, eventMetricsResult]) => {
removeLoading(eventId)
Expand Down Expand Up @@ -337,17 +343,26 @@ function Events() {
const controller = new AbortController()
const expiryDates = parseExpiryDates(events)
Promise.all([
getEventsOwners(eventIds, controller.signal, expiryDates, /*fresh*/false),
getEventsMetrics(eventIds, controller.signal, /*fresh*/true),
getEventsOwners(eventIds, controller.signal, expiryDates),
getEventsMetrics(eventIds, controller.signal),
]).then(
([newOwners, eventsMetrics]) => {
([eventsOwners, eventsMetrics]) => {
if (eventsMetrics) {
const foundMetrics = Object.fromEntries(
Object.entries(eventsMetrics).filter(([, metrics]) => metrics != null)
)
setMetrics((oldReservations) => ({ ...oldReservations, ...foundMetrics }))
}
if (newOwners) {
if (eventsOwners) {
const newOwners = Object.fromEntries(
Object.entries(eventsOwners)
.map(
([eventId, eventOwners]) => [
eventId,
eventOwners == null ? [] : eventOwners.owners,
]
)
)
setOwners((oldOwners) => ({ ...oldOwners, ...newOwners }))
if (Object.keys(newOwners).length === eventIds.length) {
resolveEnsNames(
Expand Down

0 comments on commit 12d66a1

Please sign in to comment.