feat(communities): use async getCommunityTokens for list creation

This commit is contained in:
Jonathan Rainville 2023-09-25 15:47:30 -04:00
parent 34dfa00c57
commit c6449f67c6
5 changed files with 59 additions and 4 deletions

View File

@ -162,6 +162,11 @@ proc init*(self: Controller) =
let args = CommunitiesArgs(e)
self.delegate.curatedCommunitiesLoaded(args.communities)
# We use once here because we only need it to generate the original list of tokens from communities
self.events.once(SIGNAL_ALL_COMMUNITY_TOKENS_LOADED) do(e: Args):
let args = CommunityTokensArgs(e)
self.delegate.onAllCommunityTokensLoaded(args.communityTokens)
self.events.on(SIGNAL_COMMUNITY_TOKEN_METADATA_ADDED) do(e: Args):
let args = CommunityTokenMetadataArgs(e)
self.delegate.onCommunityTokenMetadataAdded(args.communityId, args.tokenMetadata)
@ -343,6 +348,9 @@ proc requestCancelDiscordCommunityImport*(self: Controller, id: string) =
proc getCommunityTokens*(self: Controller, communityId: string): seq[CommunityTokenDto] =
self.communityTokensService.getCommunityTokens(communityId)
proc getAllCommunityTokensAsync*(self: Controller) =
self.communityTokensService.getAllCommunityTokensAsync()
proc getNetwork*(self:Controller, chainId: int): NetworkDto =
self.networksService.getNetwork(chainId)

View File

@ -1,6 +1,7 @@
import tables
import ../../../../app_service/service/community/service as community_service
import ../../../../app_service/service/chat/service as chat_service
import ../../../../app_service/service/community_tokens/dto/community_token
import ../../shared_models/section_item
type
@ -218,3 +219,6 @@ method onCommunityCheckAllChannelPermissionsFailed*(self: AccessInterface, commu
method onCommunityMemberRevealedAccountsLoaded*(self: AccessInterface, communityId: string, memberPubkey: string,
revealedAccounts: seq[RevealedAccount]) {.base.} =
raise newException(ValueError, "No implementation available")
method onAllCommunityTokensLoaded*(self: AccessInterface, communityTokens: seq[CommunityTokenDto]) {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -51,7 +51,6 @@ type
method setCommunityTags*(self: Module, communityTags: string)
method setAllCommunities*(self: Module, communities: seq[CommunityDto])
method setCuratedCommunities*(self: Module, curatedCommunities: seq[CommunityDto])
proc buildTokensAndCollectiblesFromCommunities(self: Module)
proc newModule*(
delegate: delegate_interface.AccessInterface,
@ -107,7 +106,8 @@ method viewDidLoad*(self: Module) =
method communityDataLoaded*(self: Module) =
self.setCommunityTags(self.controller.getCommunityTags())
self.setAllCommunities(self.controller.getAllCommunities())
self.buildTokensAndCollectiblesFromCommunities()
# Get all community tokens to construct the original list of collectibles and assets from communities
self.controller.getAllCommunityTokensAsync()
method onActivated*(self: Module) =
self.controller.asyncLoadCuratedCommunities()
@ -420,7 +420,7 @@ proc createCommunityTokenItem(self: Module, token: CommunityTokensMetadataDto, c
infiniteSupply,
)
proc buildTokensAndCollectiblesFromCommunities(self: Module) =
proc buildTokensAndCollectiblesFromCommunities(self: Module, communityTokens: seq[CommunityTokenDto]) =
var tokenListItems: seq[TokenListItem]
var collectiblesListItems: seq[TokenListItem]
@ -430,7 +430,6 @@ proc buildTokensAndCollectiblesFromCommunities(self: Module) =
# No need to include those tokens, we do not manage that community
continue
let communityTokens = self.controller.getCommunityTokens(community.id)
for tokenMetadata in community.communityTokensMetadata:
# Set fallback supply to infinite in case we don't have it
var supply = "1"
@ -479,6 +478,9 @@ proc buildTokensAndCollectiblesFromWallet(self: Module) =
method onWalletAccountTokensRebuilt*(self: Module) =
self.buildTokensAndCollectiblesFromWallet()
method onAllCommunityTokensLoaded*(self: Module, communityTokens: seq[CommunityTokenDto]) =
self.buildTokensAndCollectiblesFromCommunities(communityTokens)
method onCommunityTokenMetadataAdded*(self: Module, communityId: string, tokenMetadata: CommunityTokensMetadataDto) =
let communityTokens = self.controller.getCommunityTokens(communityId)
var tokenListItem: TokenListItem

View File

@ -314,3 +314,22 @@ const getCommunityTokensDetailsTaskArg: Task = proc(argEncoded: string) {.gcsafe
"error": e.msg
}
arg.finish(output)
type
GetAllCommunityTokensArg = ref object of QObjectTaskArg
const getAllCommunityTokensTaskArg: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[GetAllCommunityTokensArg](argEncoded)
try:
let response = tokens_backend.getAllCommunityTokens()
let output = %* {
"response": response,
"error": ""
}
arg.finish(output)
except Exception as e:
let output = %* {
"error": e.msg
}
arg.finish(output)

View File

@ -56,6 +56,10 @@ type
transactionHash*: string
deployState*: DeployState
type
CommunityTokensArgs* = ref object of Args
communityTokens*: seq[CommunityTokenDto]
type
CommunityTokenDeploymentArgs* = ref object of Args
communityToken*: CommunityTokenDto
@ -155,6 +159,7 @@ const SIGNAL_COMMUNITY_TOKEN_REMOVED* = "communityTokenRemoved"
const SIGNAL_OWNER_TOKEN_DEPLOY_STATUS* = "ownerTokenDeployStatus"
const SIGNAL_OWNER_TOKEN_DEPLOYMENT_STARTED* = "ownerTokenDeploymentStarted"
const SIGNAL_COMMUNITY_TOKENS_DETAILS_LOADED* = "communityTokenDetailsLoaded"
const SIGNAL_ALL_COMMUNITY_TOKENS_LOADED* = "allCommunityTokensLoaded"
const SIGNAL_DEPLOY_OWNER_TOKEN* = "deployOwnerToken"
@ -505,6 +510,23 @@ QtObject:
except RpcException as e:
error "Error getting community tokens details", message = e.msg
proc getAllCommunityTokensAsync*(self: Service) =
let arg = GetAllCommunityTokensArg(
tptr: cast[ByteAddress](getAllCommunityTokensTaskArg),
vptr: cast[ByteAddress](self.vptr),
slot: "onGotAllCommunityTokens",
)
self.threadpool.start(arg)
proc onGotAllCommunityTokens*(self:Service, response: string) {.slot.} =
try:
let responseJson = parseJson(response)
let communityTokens = map(responseJson["response"]["result"].getElems(),
proc(x: JsonNode): CommunityTokenDto = x.toCommunityTokenDto())
self.events.emit(SIGNAL_ALL_COMMUNITY_TOKENS_LOADED, CommunityTokensArgs(communityTokens: communityTokens))
except RpcException as e:
error "Error getting all community tokens async", message = e.msg
proc removeCommunityToken*(self: Service, communityId: string, chainId: int, address: string) =
try:
let response = tokens_backend.removeCommunityToken(chainId, address)