Skip to content

Commit

Permalink
Merge pull request #64 from dappforce/creator-spaces-with-domains
Browse files Browse the repository at this point in the history
Add domains to creator spaces
  • Loading branch information
olehmell authored Dec 19, 2023
2 parents 5798c74 + 848b178 commit 5e16d27
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/routes/creatorStaking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
Expand Down
48 changes: 35 additions & 13 deletions src/services/creatorStaking/creatorsSpaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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 || {})
}

0 comments on commit 5e16d27

Please sign in to comment.