From 15bd1c3c2c81949c3961ed0a2b21a1c308dd6d7b Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 2 Jun 2020 15:57:48 -0400 Subject: [PATCH] load mailservers messages on future logins --- src/app/chat/core.nim | 11 +++++++++- src/status/chat.nim | 16 +++++++++------ src/status/libstatus/chat.nim | 2 +- src/status/libstatus/core.nim | 4 ++-- src/status/mailservers.nim | 38 +++++++++++++++++++++++++++++++---- 5 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/app/chat/core.nim b/src/app/chat/core.nim index 3f3143dc31..31e734a483 100644 --- a/src/app/chat/core.nim +++ b/src/app/chat/core.nim @@ -47,11 +47,20 @@ proc handleChatEvents(self: ChatController) = self.status.events.on("activeChannelChanged") do(e: Args): self.view.setActiveChannel(ChannelArgs(e).channel) +proc handleMailserverEvents(self: ChatController) = + self.status.events.on("mailserverTopics") do(e: Args): + self.status.mailservers.addTopics(TopicArgs(e).topics) + + self.status.events.on("mailserverAvailable") do(e:Args): + self.status.mailservers.requestMessages() + + proc init*(self: ChatController) = + self.handleMailserverEvents() self.handleChatEvents() - self.status.chat.init() self.status.mailservers.init() + self.status.chat.init() self.view.setActiveChannelByIndex(0) diff --git a/src/status/chat.nim b/src/status/chat.nim index e2dcf45a68..fc287ae568 100644 --- a/src/status/chat.nim +++ b/src/status/chat.nim @@ -24,17 +24,22 @@ type ChannelArgs* = ref object of Args type ChatArgs* = ref object of Args chats*: seq[Chat] +type TopicArgs* = ref object of Args + topics*: seq[string] + type ChatModel* = ref object events*: EventEmitter channels*: HashSet[string] filters*: Table[string, string] + topics*: seq[string] proc newChatModel*(events: EventEmitter): ChatModel = result = ChatModel() result.events = events result.channels = initHashSet[string]() result.filters = initTable[string, string]() + result.topics = newSeq[string](0) proc delete*(self: ChatModel) = discard @@ -70,7 +75,7 @@ proc join*(self: ChatModel, chatId: string, chatType: ChatType) = if (topics.len == 0): warn "No topic found for the chat. Cannot load past messages" else: - status_chat.requestMessages(topics, generatedSymKey, peer, 20) + self.events.emit("mailserverTopics", TopicArgs(topics: topics)); self.events.emit("channelJoined", ChannelArgs(channel: chatId, chatTypeInt: chatType)) self.events.emit("activeChannelChanged", ChannelArgs(channel: self.getActiveChannel())) @@ -99,15 +104,14 @@ proc init*(self: ChatModel) = self.events.emit("chatsLoaded", ChatArgs(chats: chatList)) let parsedResult = parseJson(filterResult)["result"] - var topics = newSeq[string](0) for topicObj in parsedResult: - topics.add($topicObj["topic"].getStr) + self.topics.add($topicObj["topic"].getStr) self.filters[$topicObj["chatId"].getStr] = topicObj["filterId"].getStr - if (topics.len == 0): - warn "No topic found for the chat. Cannot load past messages" + if (self.topics.len == 0): + warn "No topics found for chats. Cannot load past messages" else: - status_chat.requestMessages(topics, generatedSymKey, peer, 20) + self.events.emit("mailserverTopics", TopicArgs(topics: self.topics)); proc leave*(self: ChatModel, chatId: string) = diff --git a/src/status/libstatus/chat.nim b/src/status/libstatus/chat.nim index c2b63b11fd..2abc517b72 100644 --- a/src/status/libstatus/chat.nim +++ b/src/status/libstatus/chat.nim @@ -90,7 +90,7 @@ proc requestMessages*(topics: seq[string], symKeyID: string, peer: string, numbe "timeout": 30, "limit": numberOfMessages, "cursor": nil, - "from": times.toUnix(times.getTime()) - 30000 # Unhardcode this. Need to keep the last fetch in a DB + "from": (times.toUnix(times.getTime()) - 86400) # Unhardcode this. Need to keep the last fetch in a DB } ]) diff --git a/src/status/libstatus/core.nim b/src/status/libstatus/core.nim index dc97962d70..f569460eaf 100644 --- a/src/status/libstatus/core.nim +++ b/src/status/libstatus/core.nim @@ -29,7 +29,7 @@ proc startMessenger*() = discard callPrivateRPC("startMessenger".prefix) proc addPeer*(peer: string) = - echo libstatus.addPeer(peer) + discard libstatus.addPeer(peer) proc markTrustedPeer*(peer: string) = - echo callPrivateRPC("markTrustedPeer".prefix(false), %* [peer]) + discard callPrivateRPC("markTrustedPeer".prefix(false), %* [peer]) diff --git a/src/status/mailservers.nim b/src/status/mailservers.nim index aa63a726b3..722e687c7b 100644 --- a/src/status/mailservers.nim +++ b/src/status/mailservers.nim @@ -3,6 +3,7 @@ import libstatus/core as status_core import libstatus/chat as status_chat import libstatus/mailservers as status_mailservers import tables +import sets import chronicles import eventemitter @@ -15,6 +16,7 @@ type peer*: string MailserverStatus* = enum + Unknown = -1, Disconnected = 0, Connecting = 1 Connected = 2, @@ -23,6 +25,8 @@ type MailserverModel* = ref object events*: EventEmitter nodes*: Table[string, MailserverStatus] + selectedMailserver*: string + topics*: seq[string] proc cmpMailserverReply(x, y: (string, int)): int = @@ -36,11 +40,26 @@ proc newMailserverModel*(events: EventEmitter): MailserverModel = result = MailserverModel() result.events = events result.nodes = initTable[string, MailserverStatus]() + result.selectedMailserver = "" + result.topics = @[] + +proc addTopics*(self: MailserverModel, topics: seq[string]) = + self.topics = self.topics & topics proc trustPeer*(self: MailserverModel, enode:string) = markTrustedPeer(enode) + self.nodes[enode] = MailserverStatus.Trusted + if self.selectedMailserver == enode: + debug "Mailserver available", enode + self.events.emit("mailserverAvailable", Args()) + +proc selectedServerStatus*(self: MailserverModel): MailserverStatus = + if self.selectedMailserver == "": MailserverStatus.Unknown + else: self.nodes[self.selectedMailserver] proc connect*(self: MailserverModel, enode: string) = + debug "Connecting to mailserver", enode + self.selectedMailserver = enode if self.nodes.hasKey(enode): if self.nodes[enode] == MailserverStatus.Connected: self.trustPeer(enode) @@ -48,20 +67,27 @@ proc connect*(self: MailserverModel, enode: string) = self.nodes[enode] = MailserverStatus.Connecting addPeer(enode) # TODO: check if connection is made after a connection timeout? - echo status_mailservers.update(enode) proc peerSummaryChange*(self: MailserverModel, peers: seq[string]) = + # TODO: check if peer received is a mailserver from the list before doing any operation + for peer in self.nodes.keys: if not peers.contains(peer): self.nodes[peer] = MailserverStatus.Disconnected self.events.emit("peerDisconnected", MailserverArg(peer: peer)) # TODO: reconnect peer up to N times on 'peerDisconnected' + # TODO: this should come from settings + var knownMailservers = initHashSet[string]() + for m in getMailservers(): + knownMailservers.incl m[1] + for peer in peers: - if not self.nodes.hasKey(peer) or self.nodes[peer] == MailserverStatus.Disconnected: - self.nodes[peer] = MailserverStatus.Connected - self.events.emit("peerConnected", MailserverArg(peer: peer)) + if not knownMailservers.contains(peer): continue + if self.nodes.hasKey(peer) and self.nodes[peer] == MailserverStatus.Trusted: continue + self.nodes[peer] = MailserverStatus.Connected + self.events.emit("peerConnected", MailserverArg(peer: peer)) proc init*(self: MailserverModel) = @@ -88,4 +114,8 @@ proc init*(self: MailserverModel) = let mailServer = availableMailservers[rand(poolSize(availableMailservers.len))][0] self.connect(mailserver) +proc requestMessages*(self: MailserverModel) = + debug "Requesting messages from", mailserver=self.selectedMailserver + let generatedSymKey = status_chat.generateSymKeyFromPassword() + status_chat.requestMessages(self.topics, generatedSymKey, self.selectedMailserver, 1000)