Skip to content

Commit

Permalink
fix(stickers): fix crash in async task + clean up + set bought status
Browse files Browse the repository at this point in the history
Fixes #12664
  • Loading branch information
jrainville committed Nov 13, 2023
1 parent 3b06ae5 commit cf74fc2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/app/modules/main/stickers/models/sticker_pack_list.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ QtObject:
self.packs.apply(proc(it: var StickerPackView) =
if it.pack.id == packId:
it.installed = installed
if installed:
it.bought = true
it.pending = pending)
self.dataChanged(index, index, @[StickerPackRoles.Installed.int, StickerPackRoles.Pending.int])
self.dataChanged(index, index, @[StickerPackRoles.Installed.int, StickerPackRoles.Bought.int,
StickerPackRoles.Pending.int])

proc getStickers*(self: StickerPackList): QVariant {.slot.} =
let packInfo = self.packs[self.packIdToRetrieve]
Expand Down
7 changes: 4 additions & 3 deletions src/app/modules/main/stickers/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ method onUserAuthenticated*(self: Module, password: string) =
self.tmpBuyStickersTransactionDetails.eip1559Enabled
)
if response.error.isEmptyOrWhitespace():
self.view.stickerPacks.updateStickerPackInList(self.tmpBuyStickersTransactionDetails.packId, false, true)
self.view.stickerPacks.updateStickerPackInList(self.tmpBuyStickersTransactionDetails.packId, installed = false,
pending = true)
self.view.transactionWasSent(chainId = response.chainId, txHash = response.txHash, error = response.error)

method obtainMarketStickerPacks*(self: Module) =
Expand Down Expand Up @@ -250,10 +251,10 @@ method getChainIdForStickers*(self: Module): int =
return self.controller.getChainIdForStickers()

method stickerTransactionConfirmed*(self: Module, trxType: string, packID: string, transactionHash: string) =
self.view.stickerPacks.updateStickerPackInList(packID, true, false)
self.view.stickerPacks.updateStickerPackInList(packID, installed = true, pending = false)
self.controller.installStickerPack(packID)
self.view.emitTransactionCompletedSignal(true, transactionHash, packID, trxType)

method stickerTransactionReverted*(self: Module, trxType: string, packID: string, transactionHash: string) =
self.view.stickerPacks.updateStickerPackInList(packID, false, false)
self.view.stickerPacks.updateStickerPackInList(packID, installed = false, pending = false)
self.view.emitTransactionCompletedSignal(false, transactionHash, packID, trxType)
7 changes: 3 additions & 4 deletions src/app_service/service/stickers/async_tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ type
packId*: string
fromAddress*: string
uuid*: string

ObtainMarketStickerPacksTaskArg = ref object of QObjectTaskArg
chainId*: int
InstallStickerPackTaskArg = ref object of QObjectTaskArg
packId*: string
chainId*: int
hasKey*: bool

AsyncGetRecentStickersTaskArg* = ref object of QObjectTaskArg
AsyncGetInstalledStickerPacksTaskArg* = ref object of QObjectTaskArg

Expand Down Expand Up @@ -68,9 +69,7 @@ const obtainMarketStickerPacksTask: Task = proc(argEncoded: string) {.gcsafe, ni

const installStickerPackTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[InstallStickerPackTaskArg](argEncoded)
if not arg.hasKey:
arg.finish(false)
return

var installed = false
try:
let installResponse = status_stickers.install(arg.chainId, arg.packId)
Expand Down
18 changes: 8 additions & 10 deletions src/app_service/service/stickers/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ QtObject:
error "Error reverting sticker transaction", message = getCurrentExceptionMsg()

proc init*(self: Service) =
# TODO redo the connect check when the network is refactored
# if self.status.network.isConnected:
self.obtainMarketStickerPacks() # TODO: rename this to obtain sticker market items

self.events.on(PendingTransactionTypeDto.BuyStickerPack.event) do(e: Args):
var receivedData = TransactionMinedArgs(e)
if receivedData.success:
Expand Down Expand Up @@ -258,16 +254,16 @@ QtObject:

for stickerPack in availableStickers:
if self.marketStickerPacks.contains(stickerPack.id): continue
let isBought = stickerPack.status == StickerPackStatus.Purchased

self.marketStickerPacks[stickerPack.id] = stickerPack
self.events.emit(SIGNAL_STICKER_PACK_LOADED, StickerPackLoadedArgs(
stickerPack: stickerPack,
isInstalled: false,
isBought: isBought,
isBought: stickerPack.status == StickerPackStatus.Purchased,
isPending: false
))

let chainId = self.networkService.getNetworkForStickers().chainId
# TODO move this to be async
let pendingStickerPacksResponse = status_stickers.pending()
for (packID, stickerPackJson) in pendingStickerPacksResponse.result.pairs():
if self.marketStickerPacks.contains(packID): continue
Expand Down Expand Up @@ -379,7 +375,6 @@ QtObject:
error "error loading installed sticker packs", msg = error.message
return

var stickerPacks: Table[string, StickerPackDto] = initTable[string, StickerPackDto]()
for (packID, stickerPackJson) in rpcResponseObj{"result"}.pairs():
self.installedStickerPacks[packID] = stickerPackJson.toStickerPackDto()
self.events.emit(SIGNAL_LOAD_INSTALLED_STICKER_PACKS_DONE, StickerPacksArgs(packs: self.installedStickerPacks))
Expand All @@ -403,20 +398,23 @@ QtObject:
slot: "onStickerPackInstalled",
chainId: self.networkService.getNetworkForStickers().chainId,
packId: packId,
hasKey: self.marketStickerPacks.hasKey(packId)
)
self.threadpool.start(arg)

proc onStickerPackInstalled*(self: Service, installedPackJson: string) {.slot.} =
let installedPack = Json.decode(installedPackJson, tuple[packId: string, installed: bool])
if installedPack.installed:
if self.marketStickerPacks.hasKey(installedPack.packId):
self.marketStickerPacks[installedPack.packId].status = StickerPackStatus.Installed
self.events.emit(SIGNAL_STICKER_PACK_INSTALLED, StickerPackInstalledArgs(
packId: installedPack.packId
))
else:
error "Sticker pack did not get installed", packId = installedPack.packId

proc uninstallStickerPack*(self: Service, packId: string) =
try:
let installedResponse = status_stickers.uninstall(packId)
discard status_stickers.uninstall(packId)
except RpcException:
error "Error removing installed sticker", message = getCurrentExceptionMsg()

Expand Down

0 comments on commit cf74fc2

Please sign in to comment.