Skip to content

Commit

Permalink
Merge pull request #476 from reservoirprotocol/ted/grwth-4125-search-…
Browse files Browse the repository at this point in the history
…only-working-on-ethereum

Fix search filter spam bug
  • Loading branch information
ted-palmer authored Feb 21, 2024
2 parents e6a3ecc + 0723103 commit a58d9ab
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions pages/api/globalSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ const locallyFilterSpam = (results: any[]) => {
spamCollections[result.data.chainId]
) {
return !spamCollections[result.data.chainId].includes(
result.data.collectionId,
result.data.collectionId
)
? true
: false
} else {
return true
}
})
}
Expand All @@ -59,7 +61,7 @@ export default async function handler(req: Request) {

if (searchChain) {
const chain = supportedChains.find(
(chain) => chain.routePrefix === searchChain,
(chain) => chain.routePrefix === searchChain
)

if (chain) {
Expand All @@ -82,7 +84,7 @@ export default async function handler(req: Request) {
'content-type': 'application/json',
'Cache-Control': 'maxage=0, s-maxage=3600 stale-while-revalidate',
},
},
}
)
}

Expand All @@ -109,7 +111,7 @@ async function searchSingleChain(chain: ReservoirChain, query: string) {
const promise = fetcher(
`${reservoirBaseUrl}/search/collections/v1`,
queryData,
headers,
headers
)
promise.catch((e: any) => console.warn('Failed to search', e))

Expand All @@ -120,7 +122,7 @@ async function searchSingleChain(chain: ReservoirChain, query: string) {
const { data } = await fetcher(
`${reservoirBaseUrl}/collections/v7?contract=${query}&limit=6`,
{},
headers,
headers
)
if (data.collections.length > 0) {
const processedCollections = data.collections.map(
Expand All @@ -146,14 +148,14 @@ async function searchSingleChain(chain: ReservoirChain, query: string) {
type: 'collection',
data: processedCollection,
}
},
}
)
searchResults = processedCollections
}
// if ethereum chain
else if (chain.id === 1) {
let ensData = await fetch(
`https://api.ensideas.com/ens/resolve/${query}`,
`https://api.ensideas.com/ens/resolve/${query}`
).then((res) => res.json())
searchResults = [
{
Expand All @@ -170,11 +172,11 @@ async function searchSingleChain(chain: ReservoirChain, query: string) {
else if (
chain.id === 1 &&
/[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi.test(
query as string,
query as string
)
) {
let ensData = await fetch(
`https://api.ensideas.com/ens/resolve/${query}`,
`https://api.ensideas.com/ens/resolve/${query}`
).then((res) => res.json())

if (ensData.address) {
Expand Down Expand Up @@ -206,7 +208,7 @@ async function searchSingleChain(chain: ReservoirChain, query: string) {
tokenCount: collection.tokenCount,
allTimeUsdVolume: collection.allTimeVolume,
},
}),
})
)
searchResults = processedSearchResults
}
Expand Down Expand Up @@ -245,7 +247,7 @@ async function searchAllChains(query: string) {
const promise = fetcher(
`${reservoirBaseUrl}/search/collections/v1`,
query,
headers,
headers
)
promise.catch((e: any) => console.warn('Failed to search', e))
promises.push(promise)
Expand All @@ -264,7 +266,7 @@ async function searchAllChains(query: string) {
const { data } = await fetcher(
`${reservoirBaseUrl}/collections/v7?contract=${query}&limit=6`,
{},
headers,
headers
)
return data.collections.map((collection: Collection) => {
const processedCollection: SearchCollection = {
Expand Down Expand Up @@ -293,7 +295,7 @@ async function searchAllChains(query: string) {
let results = await Promise.allSettled(promises).then((results) => {
return results
.filter(
(result) => result.status === 'fulfilled' && result.value.length > 0,
(result) => result.status === 'fulfilled' && result.value.length > 0
)
.flatMap((result) => (result as PromiseFulfilledResult<any>).value)
})
Expand All @@ -302,7 +304,7 @@ async function searchAllChains(query: string) {
searchResults = results
} else {
let ensData = await fetch(
`https://api.ensideas.com/ens/resolve/${query}`,
`https://api.ensideas.com/ens/resolve/${query}`
).then((res) => res.json())
searchResults = [
{
Expand All @@ -316,11 +318,11 @@ async function searchAllChains(query: string) {
}
} else if (
/[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi.test(
query as string,
query as string
)
) {
let ensData = await fetch(
`https://api.ensideas.com/ens/resolve/${query}`,
`https://api.ensideas.com/ens/resolve/${query}`
).then((res) => res.json())

if (ensData.address) {
Expand All @@ -336,7 +338,7 @@ async function searchAllChains(query: string) {
} else {
// Get current usd prices for each chain
const usdCoinPrices = await fetch(`${HOST_URL}/api/usdCoinConversion`).then(
(res) => res.json(),
(res) => res.json()
)

const responses = await Promise.allSettled(promises)
Expand Down Expand Up @@ -364,15 +366,15 @@ async function searchAllChains(query: string) {
usdCoinPrices?.prices?.[index]?.current_price) ||
0,
},
}),
})
)
searchResults = [...searchResults, ...chainSearchResults]
})

// Sort results by all time usd volume only if usdCoinPrices is not null
if (usdCoinPrices) {
searchResults = searchResults.sort(
(a, b) => b.data.allTimeUsdVolume - a.data.allTimeUsdVolume,
(a, b) => b.data.allTimeUsdVolume - a.data.allTimeUsdVolume
)
}

Expand Down

0 comments on commit a58d9ab

Please sign in to comment.