load mailservers messages on future logins
This commit is contained in:
parent
31b0207dcc
commit
15bd1c3c2c
|
@ -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)
|
||||
|
||||
|
|
|
@ -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) =
|
||||
|
|
|
@ -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
|
||||
}
|
||||
])
|
||||
|
||||
|
|
|
@ -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])
|
||||
|
|
|
@ -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,18 +67,25 @@ 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:
|
||||
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))
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue