feat: load stickerpacks async

This commit is contained in:
Richard Ramos 2020-07-30 15:28:31 -04:00 committed by Iuri Matias
parent 847ac9bc3c
commit 9d5c71fb4e
4 changed files with 60 additions and 55 deletions

View File

@ -8,8 +8,6 @@ import ../../status/libstatus/wallet as status_wallet
import ../../status/[chat, contacts, status]
import view, views/channels_list, views/message_list
from eth/common/utils import parseAddress
logScope:
topics = "chat-controller"
@ -37,21 +35,7 @@ proc init*(self: ChatController) =
self.status.mailservers.init()
self.status.chat.init()
let currAcct = status_wallet.getWalletAccounts()[0] # TODO: make generic
let currAddr = parseAddress(currAcct.address)
let installedStickerPacks = self.status.chat.getInstalledStickerPacks()
let purchasedStickerPacks = self.status.chat.getPurchasedStickerPacks(currAddr)
# TODO: getting available stickers should be done in a separate thread as there
# a long wait for contract response, decoded, downloading from IPFS, EDN decoding,
# etc
let availableStickerPacks = self.status.chat.getAvailableStickerPacks()
for packId, stickerPack in availableStickerPacks.pairs:
let isInstalled = installedStickerPacks.hasKey(packId)
let isBought = purchasedStickerPacks.contains(packId)
self.view.addStickerPackToList(stickerPack, isInstalled, isBought)
self.view.obtainAvailableStickerPacks()
let recentStickers = self.status.chat.getRecentStickers()
for sticker in recentStickers:

View File

@ -1,19 +1,20 @@
import NimQml, Tables, json, sequtils, chronicles, times, re, sugar, strutils, os
import ../../status/status
import ../../status/libstatus/accounts/constants
import ../../status/accounts as status_accounts
import ../../status/chat as status_chat
import ../../status/messages as status_messages
import ../../status/libstatus/wallet as status_wallet
import ../../status/contacts as status_contacts
import ../../status/ens as status_ens
import ../../status/chat/[chat, message]
import ../../status/libstatus/types
import ../../status/profile/profile
import eth/common/eth_types
import ../../status/threads
import views/channels_list, views/message_list, views/chat_item, views/sticker_pack_list, views/sticker_list, views/suggestions_list
import json_serialization
from eth/common/utils import parseAddress
logScope:
topics = "chats-view"
@ -68,6 +69,27 @@ QtObject:
QtProperty[QVariant] stickerPacks:
read = getStickerPackList
proc obtainAvailableStickerPacks*(self: ChatsView) =
spawnAndSend(self, "setAvailableStickerPacks") do:
let availableStickerPacks = status_chat.getAvailableStickerPacks()
var packs: seq[StickerPack] = @[]
for packId, stickerPack in availableStickerPacks.pairs:
packs.add(stickerPack)
$(%*(packs))
proc setAvailableStickerPacks*(self: ChatsView, availableStickersJSON: string) {.slot.} =
let currAcct = status_wallet.getWalletAccounts()[0] # TODO: make generic
let currAddr = parseAddress(currAcct.address)
let installedStickerPacks = self.status.chat.getInstalledStickerPacks()
let purchasedStickerPacks = self.status.chat.getPurchasedStickerPacks(currAddr)
let availableStickers = JSON.decode($availableStickersJSON, seq[StickerPack])
for stickerPack in availableStickers:
let isInstalled = installedStickerPacks.hasKey(stickerPack.id)
let isBought = purchasedStickerPacks.contains(stickerPack.id)
self.status.chat.availableStickerPacks[stickerPack.id] = stickerPack
self.addStickerPackToList(stickerPack, isInstalled, isBought)
proc getChatsList(self: ChatsView): QVariant {.slot.} =
newQVariant(self.chats)

View File

@ -126,19 +126,17 @@ proc getInstalledStickerPacks*(self: ChatModel): Table[int, StickerPack] =
self.installedStickerPacks = status_stickers.getInstalledStickerPacks()
result = self.installedStickerPacks
proc getAvailableStickerPacks*(self: ChatModel): Table[int, StickerPack] =
if self.availableStickerPacks != initTable[int, StickerPack]():
return self.availableStickerPacks
proc getAvailableStickerPacks*(): Table[int, StickerPack] =
var availableStickerPacks = initTable[int, StickerPack]()
try:
let numPacks = status_stickers.getPackCount()
for i in 0..<numPacks:
try:
let stickerPack = status_stickers.getPackData(i.u256)
self.availableStickerPacks[stickerPack.id] = stickerPack
availableStickerPacks[stickerPack.id] = stickerPack
except:
continue
result = self.availableStickerPacks
result = availableStickerPacks
except RpcException:
error "Error in getAvailableStickerPacks", message = getCurrentExceptionMsg()
result = initTable[int, StickerPack]()

View File

@ -91,6 +91,7 @@ proc getPackCount*(): int =
# Gets sticker pack data
proc getPackData*(id: Stuint[256]): StickerPack =
{.gcsafe.}:
let
contract = contracts.getContract("stickers")
contractMethod = contract.methods["getPackData"]
@ -99,8 +100,8 @@ proc getPackData*(id: Stuint[256]): StickerPack =
"to": $contract.address,
"data": contractMethod.encodeAbi(getPackData)
}, "latest"]
responseStr = status.callPrivateRPC("eth_call", payload)
response = Json.decode(responseStr, RpcResponse)
let responseStr = status.callPrivateRPC("eth_call", payload)
let response = Json.decode(responseStr, RpcResponse)
if not response.error.isNil:
raise newException(RpcException, "Error getting sticker pack data: " & response.error.message)