From 4f0cdad8c7026179d602d443854ce3ac89a85e6e Mon Sep 17 00:00:00 2001 From: emizzle Date: Wed, 9 Sep 2020 13:25:08 +1000 Subject: [PATCH] refactor: move stickers to own model Remove from ChatModel and add StickerModel --- src/app/chat/core.nim | 5 +- src/app/chat/view.nim | 22 +++--- src/status/chat.nim | 104 +------------------------ src/status/libstatus/stickers.nim | 15 ++++ src/status/status.nim | 5 ++ src/status/stickers.nim | 118 +++++++++++++++++++++++++++++ src/status/wallet/collectibles.nim | 2 +- 7 files changed, 156 insertions(+), 115 deletions(-) create mode 100644 src/status/stickers.nim diff --git a/src/app/chat/core.nim b/src/app/chat/core.nim index d32863d3b9..b0bf9c09fb 100644 --- a/src/app/chat/core.nim +++ b/src/app/chat/core.nim @@ -36,11 +36,12 @@ proc init*(self: ChatController) = self.status.mailservers.init() self.status.chat.init() + self.status.stickers.init() self.view.obtainAvailableStickerPacks() let pubKey = status_settings.getSetting[string](Setting.PublicKey, "0x0") self.view.pubKey = pubKey - let recentStickers = self.status.chat.getRecentStickers() + let recentStickers = self.status.stickers.getRecentStickers() for sticker in recentStickers: self.view.addRecentStickerToList(sticker) - self.status.chat.addStickerToRecent(sticker) + self.status.stickers.addStickerToRecent(sticker) diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index 280980268e..b4286b9937 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -1,12 +1,14 @@ import NimQml, Tables, json, sequtils, chronicles, times, re, sugar, strutils, os, strformat import ../../status/status import ../../status/mailservers +import ../../status/stickers import ../../status/libstatus/accounts/constants import ../../status/libstatus/mailservers as status_mailservers 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/libstatus/stickers as status_stickers import ../../status/contacts as status_contacts import ../../status/ens as status_ens import ../../status/chat/[chat, message] @@ -98,26 +100,26 @@ QtObject: read = getStickerPackList proc getStickerMarketAddress(self: ChatsView): QVariant {.slot.} = - newQVariant($self.status.chat.getStickerMarketAddress) + newQVariant($self.status.stickers.getStickerMarketAddress) QtProperty[QVariant] stickerMarketAddress: read = getStickerMarketAddress proc getStickerBuyPackGasEstimate*(self: ChatsView, packId: int, address: string, price: string): string {.slot.} = try: - result = self.status.chat.buyPackGasEstimate(packId, address, price) + result = self.status.stickers.buyPackGasEstimate(packId, address, price) except: result = "400000" proc buyStickerPack*(self: ChatsView, packId: int, address: string, price: string, gas: string, gasPrice: string, password: string): string {.slot.} = try: - result = $(%self.status.chat.buyStickerPack(packId, address, price, gas, gasPrice, password)) + result = $(%self.status.stickers.buyStickerPack(packId, address, price, gas, gasPrice, password)) except RpcException as e: result = fmt"""{{ "error": {{ "message": "{e.msg}" }} }}""" proc obtainAvailableStickerPacks*(self: ChatsView) = spawnAndSend(self, "setAvailableStickerPacks") do: - let availableStickerPacks = status_chat.getAvailableStickerPacks() + let availableStickerPacks = status_stickers.getAvailableStickerPacks() var packs: seq[StickerPack] = @[] for packId, stickerPack in availableStickerPacks.pairs: packs.add(stickerPack) @@ -126,14 +128,14 @@ QtObject: 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 installedStickerPacks = self.status.stickers.getInstalledStickerPacks() + let purchasedStickerPacks = self.status.stickers.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.status.stickers.availableStickerPacks[stickerPack.id] = stickerPack self.addStickerPackToList(stickerPack, isInstalled, isBought) proc getChatsList(self: ChatsView): QVariant {.slot.} = @@ -244,12 +246,12 @@ QtObject: notify = activeChannelChanged proc installStickerPack*(self: ChatsView, packId: int) {.slot.} = - self.status.chat.installStickerPack(packId) + self.status.stickers.installStickerPack(packId) self.stickerPacks.updateStickerPackInList(packId, true) proc uninstallStickerPack*(self: ChatsView, packId: int) {.slot.} = - self.status.chat.uninstallStickerPack(packId) - self.status.chat.removeRecentStickers(packId) + self.status.stickers.uninstallStickerPack(packId) + self.status.stickers.removeRecentStickers(packId) self.stickerPacks.updateStickerPackInList(packId, false) self.recentStickers.removeStickersFromList(packId) diff --git a/src/status/chat.nim b/src/status/chat.nim index 3f7a513868..a64f65f0e1 100644 --- a/src/status/chat.nim +++ b/src/status/chat.nim @@ -3,17 +3,13 @@ import libstatus/contracts as status_contracts import libstatus/chat as status_chat import libstatus/mailservers as status_mailservers import libstatus/chatCommands as status_chat_commands -import libstatus/stickers as status_stickers import libstatus/accounts/constants as constants import libstatus/types -import mailservers +import mailservers, stickers import profile/profile import chat/[chat, message] import signals/messages import ens -import eth/common/eth_types -from eth/common/utils import parseAddress -from libstatus/utils as libstatus_utils import eth2Wei, gwei2Wei, toUInt64 logScope: topics = "chat-model" @@ -49,10 +45,6 @@ type channels*: Table[string, Chat] msgCursor*: Table[string, string] emojiCursor*: Table[string, string] - recentStickers*: seq[Sticker] - availableStickerPacks*: Table[int, StickerPack] - installedStickerPacks*: Table[int, StickerPack] - purchasedStickerPacks*: seq[int] lastMessageTimestamps*: Table[string, int64] MessageArgs* = ref object of Args @@ -68,13 +60,8 @@ proc newChatModel*(events: EventEmitter): ChatModel = result.channels = initTable[string, Chat]() result.msgCursor = initTable[string, string]() result.emojiCursor = initTable[string, string]() - result.recentStickers = @[] - result.availableStickerPacks = initTable[int, StickerPack]() - result.installedStickerPacks = initTable[int, StickerPack]() - result.purchasedStickerPacks = @[] result.lastMessageTimestamps = initTable[string, int64]() - proc delete*(self: ChatModel) = discard @@ -127,84 +114,6 @@ proc join*(self: ChatModel, chatId: string, chatType: ChatType) = self.events.emit("channelJoined", ChannelArgs(chat: chat)) -# TODO: Replace this with a more generalised way of estimating gas so can be used for token transfers -proc buyPackGasEstimate*(self: ChatModel, packId: int, address: string, price: string): string = - let - priceTyped = eth2Wei(parseFloat(price), 18) # SNT - hexGas = status_stickers.buyPackGasEstimate(packId.u256, parseAddress(address), priceTyped) - result = $fromHex[int](hexGas) - -proc getStickerMarketAddress*(self: ChatModel): EthAddress = - result = status_contracts.getContract("sticker-market").address - -proc buyStickerPack*(self: ChatModel, packId: int, address, price, gas, gasPrice, password: string): RpcResponse = - try: - let - addressTyped = parseAddress(address) - priceTyped = eth2Wei(parseFloat(price), 18) # SNT - gasTyped = cast[uint64](parseFloat(gas).toUInt64) - gasPriceTyped = gwei2Wei(parseFloat(gasPrice)).truncate(int) - result = status_stickers.buyPack(packId.u256, addressTyped, priceTyped, gasTyped, gasPriceTyped, password) - except RpcException as e: - raise - -proc getPurchasedStickerPacks*(self: ChatModel, address: EthAddress): seq[int] = - if self.purchasedStickerPacks != @[]: - return self.purchasedStickerPacks - - try: - var - balance = status_stickers.getBalance(address) - tokenIds = toSeq[0.. status_stickers.tokenOfOwnerByIndex(address, idx.u256)) - - self.purchasedStickerPacks = tokenIds.map(tokenId => status_stickers.getPackIdFromTokenId(tokenId.u256)) - result = self.purchasedStickerPacks - except RpcException: - error "Error getting purchased sticker packs", message = getCurrentExceptionMsg() - result = @[] - -proc getInstalledStickerPacks*(self: ChatModel): Table[int, StickerPack] = - if self.installedStickerPacks != initTable[int, StickerPack](): - return self.installedStickerPacks - - self.installedStickerPacks = status_stickers.getInstalledStickerPacks() - result = self.installedStickerPacks - -proc getAvailableStickerPacks*(): Table[int, StickerPack] = - var availableStickerPacks = initTable[int, StickerPack]() - try: - let numPacks = status_stickers.getPackCount() - for i in 0.. 24: - self.recentStickers = self.recentStickers[0..23] # take top 24 most recent - if save: - status_stickers.saveRecentStickers(self.recentStickers) - proc sendSticker*(self: ChatModel, chatId: string, sticker: Sticker) = var response = status_chat.sendStickerMessage(chatId, sticker) - self.addStickerToRecent(sticker, save = true) + self.events.emit("stickerSent", StickerArgs(sticker: sticker, save: true)) var (chats, messages) = self.processChatUpdate(parseJson(response)) self.events.emit("chatUpdate", ChatUpdateArgs(messages: messages, chats: chats, contacts: @[])) self.events.emit("sendingMessage", MessageArgs(id: messages[0].id, channel: messages[0].chatId)) diff --git a/src/status/libstatus/stickers.nim b/src/status/libstatus/stickers.nim index 81dd163505..d697364c34 100644 --- a/src/status/libstatus/stickers.nim +++ b/src/status/libstatus/stickers.nim @@ -231,3 +231,18 @@ proc getRecentStickers*(): seq[Sticker] = # stickers are re-reversed when added to the view due to the nature of # inserting recent stickers at the front of the list result.insert(Sticker(hash: $hash, packId: packId), 0) + +proc getAvailableStickerPacks*(): Table[int, StickerPack] = + var availableStickerPacks = initTable[int, StickerPack]() + try: + let numPacks = getPackCount() + for i in 0.. status_stickers.tokenOfOwnerByIndex(address, idx.u256)) + + self.purchasedStickerPacks = tokenIds.map(tokenId => status_stickers.getPackIdFromTokenId(tokenId.u256)) + result = self.purchasedStickerPacks + except RpcException: + error "Error getting purchased sticker packs", message = getCurrentExceptionMsg() + result = @[] + +proc getInstalledStickerPacks*(self: StickersModel): Table[int, StickerPack] = + if self.installedStickerPacks != initTable[int, StickerPack](): + return self.installedStickerPacks + + self.installedStickerPacks = status_stickers.getInstalledStickerPacks() + result = self.installedStickerPacks + +proc getAvailableStickerPacks*(self: StickersModel): Table[int, StickerPack] = status_stickers.getAvailableStickerPacks() + +proc getRecentStickers*(self: StickersModel): seq[Sticker] = + result = status_stickers.getRecentStickers() + +proc installStickerPack*(self: StickersModel, packId: int) = + if not self.availableStickerPacks.hasKey(packId): + return + let pack = self.availableStickerPacks[packId] + self.installedStickerPacks[packId] = pack + status_stickers.saveInstalledStickerPacks(self.installedStickerPacks) + +proc removeRecentStickers*(self: StickersModel, packId: int) = + self.recentStickers.keepItIf(it.packId != packId) + status_stickers.saveRecentStickers(self.recentStickers) + +proc uninstallStickerPack*(self: StickersModel, packId: int) = + if not self.installedStickerPacks.hasKey(packId): + return + let pack = self.availableStickerPacks[packId] + self.installedStickerPacks.del(packId) + status_stickers.saveInstalledStickerPacks(self.installedStickerPacks) + +proc addStickerToRecent*(self: StickersModel, sticker: Sticker, save: bool = false) = + self.recentStickers.insert(sticker, 0) + self.recentStickers = self.recentStickers.deduplicate() + if self.recentStickers.len > 24: + self.recentStickers = self.recentStickers[0..23] # take top 24 most recent + if save: + status_stickers.saveRecentStickers(self.recentStickers) \ No newline at end of file diff --git a/src/status/wallet/collectibles.nim b/src/status/wallet/collectibles.nim index 35adbf1162..d4609002a7 100644 --- a/src/status/wallet/collectibles.nim +++ b/src/status/wallet/collectibles.nim @@ -198,7 +198,7 @@ proc getStickers*(address: EthAddress): string = if (purchasedStickerPacks.len == 0): return $(%*stickers) # TODO find a way to keep those in memory so as not to reload it each time - let availableStickerPacks = status_chat.getAvailableStickerPacks() + let availableStickerPacks = status_stickers.getAvailableStickerPacks() var index = 0 for stickerId in purchasedStickerPacks: