move logic related methods out of libstatus chat (#23)

* refactor: move eth logic out of libstatus wrapper

* move signing phrases out of status go wrapper (libstatus)

* move logic related methods out of libstatus

move logic related methods out of libstatus

move logic related methods out of libstatus

fix sorting
This commit is contained in:
Iuri Matias 2021-09-11 08:10:51 -04:00 committed by GitHub
parent fd0dfb4a7b
commit b3968bfc67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 30 deletions

View File

@ -1,8 +1,9 @@
import json, strutils, sequtils, tables, chronicles, times, sugar import json, strutils, sequtils, tables, chronicles, times, sugar, algorithm
import libstatus/chat as status_chat import libstatus/chat as status_chat
import libstatus/chatCommands as status_chat_commands import libstatus/chatCommands as status_chat_commands
import types/[message, status_update, activity_center_notification, import types/[message, status_update, activity_center_notification,
sticker, removed_message] sticker, removed_message]
import types/chat as chat_type
import utils as status_utils import utils as status_utils
import stickers import stickers
import ../eventemitter import ../eventemitter
@ -20,6 +21,11 @@ logScope:
const backToFirstChat* = "__goBackToFirstChat" const backToFirstChat* = "__goBackToFirstChat"
const ZERO_ADDRESS* = "0x0000000000000000000000000000000000000000" const ZERO_ADDRESS* = "0x0000000000000000000000000000000000000000"
proc buildFilter*(chat: Chat):JsonNode =
if chat.chatType == ChatType.PrivateGroupChat:
return newJNull()
result = %* { "ChatID": chat.id, "OneToOne": chat.chatType == ChatType.OneToOne }
type type
ChatUpdateArgs* = ref object of Args ChatUpdateArgs* = ref object of Args
chats*: seq[Chat] chats*: seq[Chat]
@ -188,7 +194,7 @@ proc getActiveChannel*(self: ChatModel): string =
if (self.channels.len == 0): "" else: toSeq(self.channels.values)[self.channels.len - 1].id if (self.channels.len == 0): "" else: toSeq(self.channels.values)[self.channels.len - 1].id
proc emitTopicAndJoin(self: ChatModel, chat: Chat) = proc emitTopicAndJoin(self: ChatModel, chat: Chat) =
let filterResult = status_chat.loadFilters(@[status_chat.buildFilter(chat)]) let filterResult = status_chat.loadFilters(@[buildFilter(chat)])
self.events.emit("channelJoined", ChannelArgs(chat: chat)) self.events.emit("channelJoined", ChannelArgs(chat: chat))
proc join*(self: ChatModel, chatId: string, chatType: ChatType, ensName: string = "", pubKey: string = "") = proc join*(self: ChatModel, chatId: string, chatType: ChatType, ensName: string = "", pubKey: string = "") =
@ -230,11 +236,25 @@ proc requestMissingCommunityInfos*(self: ChatModel) =
for communityId in self.communitiesToFetch: for communityId in self.communitiesToFetch:
status_chat.requestCommunityInfo(communityId) status_chat.requestCommunityInfo(communityId)
proc sortChats(x, y: chat_type.Chat): int =
var t1 = x.lastMessage.whisperTimestamp
var t2 = y.lastMessage.whisperTimestamp
if t1 <= $x.joined:
t1 = $x.joined
if t2 <= $y.joined:
t2 = $y.joined
if t1 > t2: 1
elif t1 == t2: 0
else: -1
proc init*(self: ChatModel, pubKey: string) = proc init*(self: ChatModel, pubKey: string) =
self.publicKey = pubKey self.publicKey = pubKey
var contacts = getAddedContacts() var contacts = getAddedContacts()
var chatList = status_chat.loadChats() var chatList = status_chat.loadChats()
chatList.sort(sortChats)
let profileUpdatesChatIds = chatList.filter(c => c.chatType == ChatType.Profile).map(c => c.id) let profileUpdatesChatIds = chatList.filter(c => c.chatType == ChatType.Profile).map(c => c.id)
@ -265,7 +285,7 @@ proc init*(self: ChatModel, pubKey: string) =
for chat in chatList: for chat in chatList:
if self.hasChannel(chat.id): if self.hasChannel(chat.id):
continue continue
filters.add status_chat.buildFilter(chat) filters.add buildFilter(chat)
self.channels[chat.id] = chat self.channels[chat.id] = chat
self.events.emit("channelLoaded", ChannelArgs(chat: chat)) self.events.emit("channelLoaded", ChannelArgs(chat: chat))

View File

@ -4,11 +4,6 @@ import ../types/[chat, message, community, activity_center_notification,
status_update, rpc_response, setting, sticker] status_update, rpc_response, setting, sticker]
import ./settings as status_settings import ./settings as status_settings
proc buildFilter*(chat: Chat):JsonNode =
if chat.chatType == ChatType.PrivateGroupChat:
return newJNull()
result = %* { "ChatID": chat.id, "OneToOne": chat.chatType == ChatType.OneToOne }
proc loadFilters*(filters: seq[JsonNode]): string = proc loadFilters*(filters: seq[JsonNode]): string =
result = callPrivateRPC("loadFilters".prefix, %* [filter(filters, proc(x:JsonNode):bool = x.kind != JNull)]) result = callPrivateRPC("loadFilters".prefix, %* [filter(filters, proc(x:JsonNode):bool = x.kind != JNull)])
@ -49,19 +44,6 @@ proc deactivateChat*(chat: Chat):string =
proc createProfileChat*(pubKey: string):string = proc createProfileChat*(pubKey: string):string =
callPrivateRPC("createProfileChat".prefix, %* [{ "ID": pubKey }]) callPrivateRPC("createProfileChat".prefix, %* [{ "ID": pubKey }])
proc sortChats(x, y: Chat): int =
var t1 = x.lastMessage.whisperTimestamp
var t2 = y.lastMessage.whisperTimestamp
if t1 <= $x.joined:
t1 = $x.joined
if t2 <= $y.joined:
t2 = $y.joined
if t1 > t2: 1
elif t1 == t2: 0
else: -1
proc loadChats*(): seq[Chat] = proc loadChats*(): seq[Chat] =
result = @[] result = @[]
let jsonResponse = parseJson($callPrivateRPC("chats".prefix)) let jsonResponse = parseJson($callPrivateRPC("chats".prefix))
@ -70,15 +52,6 @@ proc loadChats*(): seq[Chat] =
let chat = jsonChat.toChat let chat = jsonChat.toChat
if chat.isActive and chat.chatType != ChatType.Unknown: if chat.isActive and chat.chatType != ChatType.Unknown:
result.add(chat) result.add(chat)
result.sort(sortChats)
proc parseActivityCenterNotifications*(rpcResult: JsonNode): (string, seq[ActivityCenterNotification]) =
var notifs: seq[ActivityCenterNotification] = @[]
var msg: Message
if rpcResult{"notifications"}.kind != JNull:
for jsonMsg in rpcResult["notifications"]:
notifs.add(jsonMsg.toActivityCenterNotification())
return (rpcResult{"cursor"}.getStr, notifs)
proc statusUpdates*(): seq[StatusUpdate] = proc statusUpdates*(): seq[StatusUpdate] =
let rpcResult = callPrivateRPC("statusUpdates".prefix, %* []).parseJson()["result"] let rpcResult = callPrivateRPC("statusUpdates".prefix, %* []).parseJson()["result"]
@ -544,6 +517,14 @@ proc rpcActivityCenterNotifications*(cursorVal: JsonNode, limit: int, success: v
success = false success = false
result = e.msg result = e.msg
proc parseActivityCenterNotifications*(rpcResult: JsonNode): (string, seq[ActivityCenterNotification]) =
var notifs: seq[ActivityCenterNotification] = @[]
var msg: Message
if rpcResult{"notifications"}.kind != JNull:
for jsonMsg in rpcResult["notifications"]:
notifs.add(jsonMsg.toActivityCenterNotification())
return (rpcResult{"cursor"}.getStr, notifs)
proc activityCenterNotification*(cursor: string = ""): (string, seq[ActivityCenterNotification]) = proc activityCenterNotification*(cursor: string = ""): (string, seq[ActivityCenterNotification]) =
var cursorVal: JsonNode var cursorVal: JsonNode