mirror of
https://github.com/status-im/status-desktop.git
synced 2025-01-10 22:36:24 +00:00
feat: load stickerpacks async
This commit is contained in:
parent
847ac9bc3c
commit
9d5c71fb4e
@ -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"
|
||||
|
||||
@ -36,22 +34,8 @@ proc init*(self: ChatController) =
|
||||
self.handleChatEvents()
|
||||
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:
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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]()
|
||||
|
@ -91,37 +91,38 @@ proc getPackCount*(): int =
|
||||
|
||||
# Gets sticker pack data
|
||||
proc getPackData*(id: Stuint[256]): StickerPack =
|
||||
let
|
||||
contract = contracts.getContract("stickers")
|
||||
contractMethod = contract.methods["getPackData"]
|
||||
getPackData = GetPackData(packId: id)
|
||||
payload = %* [{
|
||||
"to": $contract.address,
|
||||
"data": contractMethod.encodeAbi(getPackData)
|
||||
}, "latest"]
|
||||
responseStr = status.callPrivateRPC("eth_call", payload)
|
||||
response = Json.decode(responseStr, RpcResponse)
|
||||
if not response.error.isNil:
|
||||
raise newException(RpcException, "Error getting sticker pack data: " & response.error.message)
|
||||
{.gcsafe.}:
|
||||
let
|
||||
contract = contracts.getContract("stickers")
|
||||
contractMethod = contract.methods["getPackData"]
|
||||
getPackData = GetPackData(packId: id)
|
||||
payload = %* [{
|
||||
"to": $contract.address,
|
||||
"data": contractMethod.encodeAbi(getPackData)
|
||||
}, "latest"]
|
||||
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)
|
||||
|
||||
let packData = contracts.decodeContractResponse[PackData](response.result)
|
||||
let packData = contracts.decodeContractResponse[PackData](response.result)
|
||||
|
||||
# contract response includes a contenthash, which needs to be decoded to reveal
|
||||
# an IPFS identifier. Once decoded, download the content from IPFS. This content
|
||||
# is in EDN format, ie https://ipfs.infura.io/ipfs/QmWVVLwVKCwkVNjYJrRzQWREVvEk917PhbHYAUhA1gECTM
|
||||
# and it also needs to be decoded in to a nim type
|
||||
var client = newHttpClient()
|
||||
let contentHash = contracts.toHex(packData.contentHash)
|
||||
let url = "https://ipfs.infura.io/ipfs/" & decodeContentHash(contentHash)
|
||||
var ednMeta = client.getContent(url)
|
||||
# contract response includes a contenthash, which needs to be decoded to reveal
|
||||
# an IPFS identifier. Once decoded, download the content from IPFS. This content
|
||||
# is in EDN format, ie https://ipfs.infura.io/ipfs/QmWVVLwVKCwkVNjYJrRzQWREVvEk917PhbHYAUhA1gECTM
|
||||
# and it also needs to be decoded in to a nim type
|
||||
var client = newHttpClient()
|
||||
let contentHash = contracts.toHex(packData.contentHash)
|
||||
let url = "https://ipfs.infura.io/ipfs/" & decodeContentHash(contentHash)
|
||||
var ednMeta = client.getContent(url)
|
||||
|
||||
# decode the EDN content in to a StickerPack
|
||||
result = edn_helpers.decode[StickerPack](ednMeta)
|
||||
# EDN doesn't include a packId for each sticker, so add it here
|
||||
result.stickers.apply(proc(sticker: var Sticker) =
|
||||
sticker.packId = truncate(id, int))
|
||||
result.id = truncate(id, int)
|
||||
result.price = packData.price
|
||||
# decode the EDN content in to a StickerPack
|
||||
result = edn_helpers.decode[StickerPack](ednMeta)
|
||||
# EDN doesn't include a packId for each sticker, so add it here
|
||||
result.stickers.apply(proc(sticker: var Sticker) =
|
||||
sticker.packId = truncate(id, int))
|
||||
result.id = truncate(id, int)
|
||||
result.price = packData.price
|
||||
|
||||
# Buys a sticker pack for user
|
||||
# See https://notes.status.im/Q-sQmQbpTOOWCQcYiXtf5g#Buy-a-Sticker-Pack for more
|
||||
|
Loading…
x
Reference in New Issue
Block a user