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

Add domains to creator spaces #64

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 || {})
}
Loading