diff --git a/src/routes/creatorStaking/index.ts b/src/routes/creatorStaking/index.ts index 26f260a..ef35d25 100644 --- a/src/routes/creatorStaking/index.ts +++ b/src/routes/creatorStaking/index.ts @@ -30,7 +30,7 @@ const createCreatorStakingRouter = (apis: Connections) => { router.get('/spaces/info', async function (req, res) { const { ids } = req.query - const info = await getCreatorsSpacesInfo({ spaceIds: ids as string[] }) + const info = await getCreatorsSpacesInfo({ apis: apis.mixedApis, spaceIds: ids as string[] }) res.send(info) }) @@ -59,7 +59,7 @@ const createCreatorStakingRouter = (apis: Connections) => { router.get('/backer/count', async function (_req, res) { const info = await getBackerCount({ - apis: apis.mixedApis, + apis: apis.mixedApis }) res.send(info) diff --git a/src/services/creatorStaking/creatorsSpaces.ts b/src/services/creatorStaking/creatorsSpaces.ts index 77ae859..e75891b 100644 --- a/src/services/creatorStaking/creatorsSpaces.ts +++ b/src/services/creatorStaking/creatorsSpaces.ts @@ -2,11 +2,15 @@ import { gql } from 'graphql-request' import Cache from '../../cache' import { FIVE_MINUTES } from '../../constant' import { subsocialGraphQlClient } from '../../constant/graphQlClients' +import { isEmptyArray } from '@subsocial/utils' +import { Apis } from '../../connections/networks/types' +import { runQueryOrUndefined } from '../utils' const creatorsSpacesCache = new Cache('creators-spaces', FIVE_MINUTES) type CreatorsSpacesInfo = { spaceIds: string[] + apis: Apis } export const GET_CREATORS_SPACES = gql` @@ -27,26 +31,44 @@ export const GET_CREATORS_SPACES = gql` } ` -export const getCreatorsSpacesInfo = async ({ spaceIds }: CreatorsSpacesInfo) => { - const result = await subsocialGraphQlClient.request(GET_CREATORS_SPACES, { ids: spaceIds }) +export const getCreatorsSpacesInfo = async ({ apis, spaceIds }: CreatorsSpacesInfo) => { + const cacheData = (await creatorsSpacesCache.getAllValues(spaceIds)) as any[] + const needUpdate = creatorsSpacesCache.needUpdate + const forceUpdate = needUpdate && (await needUpdate()) - if (!result) return [] + const subsocial = apis.subsocial - const needUpdate = creatorsSpacesCache.needUpdate + const cacheDataKeys = cacheData ? Object.keys(cacheData) : [] - const forceUpdate = needUpdate && (await needUpdate()) - const cacheData = await creatorsSpacesCache.get() + const needUpdateSpaceIds = spaceIds.filter((spaceId) => !cacheDataKeys.includes(spaceId)) || [] + + if (!cacheData || forceUpdate || !isEmptyArray(needUpdateSpaceIds)) { + const result = await subsocialGraphQlClient.request(GET_CREATORS_SPACES, { + ids: spaceIds + }) - if (!cacheData || forceUpdate) { - const spaces = result.spaces.map((space) => { - return { + if (!result) return cacheData || [] + + const spacesPromise = result.spaces.map(async (space) => { + const { ownedByAccount, id: spaceId } = space + + const account = ownedByAccount?.id + + const domain = await runQueryOrUndefined(subsocial, (api) => + api.query.domains.domainByInnerValue(account, { Space: spaceId }) + ) + + creatorsSpacesCache.set(space.id, { ...space, - links: space.linksOriginal?.split(',') || [] - } + links: space.linksOriginal?.split(',') || [], + domain: domain?.toHuman() + }) }) - creatorsSpacesCache.set(undefined, spaces) + await Promise.all(spacesPromise) } - return creatorsSpacesCache.get() + const values = await creatorsSpacesCache.getAllValues(spaceIds) + + return Object.values(values || {}) }