load chats on login

This commit is contained in:
Richard Ramos 2020-05-28 16:14:31 -04:00 committed by Iuri Matias
parent f08372879f
commit 8d2b955bcd
10 changed files with 51 additions and 37 deletions

View File

@ -27,18 +27,19 @@ proc delete*(self: ChatController) =
delete self.variant delete self.variant
proc init*(self: ChatController) = proc init*(self: ChatController) =
self.model.events.on("chatsLoaded") do(e: Args):
var chatArgs = ChatArgs(e)
for c in chatArgs.chats:
self.view.pushChatItem(c.toChatItem)
self.model.events.on("messageSent") do(e: Args): self.model.events.on("messageSent") do(e: Args):
var sentMessage = MsgArgs(e) var sentMessage = MsgArgs(e)
var chatMessage = sentMessage.payload.toChatMessage() var chatMessage = sentMessage.payload.toChatMessage()
chatMessage.message = sentMessage.message chatMessage.message = sentMessage.message
chatMessage.isCurrentUser = true chatMessage.isCurrentUser = true
self.view.pushMessage(sentMessage.chatId, chatMessage) self.view.pushMessage(sentMessage.chatId, chatMessage)
proc load*(self: ChatController, chatId: string) = self.model.load()
# TODO: we need a function to load the channels from the db.
# and... called from init() instead from nim_status_client
discard self.view.joinChat(chatId, ChatType.Public.int)
self.view.setActiveChannelByIndex(0) self.view.setActiveChannelByIndex(0)
proc handleMessage(self: ChatController, data: Signal) = proc handleMessage(self: ChatController, data: Signal) =

View File

@ -50,6 +50,8 @@ QtObject:
proc activeChannelChanged*(self: ChatsView) {.signal.} proc activeChannelChanged*(self: ChatsView) {.signal.}
proc setActiveChannelByIndex*(self: ChatsView, index: int) {.slot.} = proc setActiveChannelByIndex*(self: ChatsView, index: int) {.slot.} =
if(self.chats.chats.len == 0): return
let selectedChannel = self.chats.getChannel(index) let selectedChannel = self.chats.getChannel(index)
if self.activeChannel == selectedChannel.id: return if self.activeChannel == selectedChannel.id: return
self.activeChannel = selectedChannel.id self.activeChannel = selectedChannel.id
@ -80,6 +82,9 @@ QtObject:
read = getMessageList read = getMessageList
notify = activeChannelChanged notify = activeChannelChanged
proc pushChatItem*(self: ChatsView, chatItem: ChatItem) =
discard self.chats.addChatItemToList(chatItem)
proc joinChat*(self: ChatsView, channel: string, chatTypeInt: int): int {.slot.} = proc joinChat*(self: ChatsView, channel: string, chatTypeInt: int): int {.slot.} =
let chatType = ChatType(chatTypeInt) let chatType = ChatType(chatTypeInt)

View File

@ -44,6 +44,7 @@ QtObject:
proc newChannelsList*(model: ChatModel): ChannelsList = proc newChannelsList*(model: ChatModel): ChannelsList =
new(result, delete) new(result, delete)
result.chats = @[]
result.model = model result.model = model
result.setup() result.setup()
@ -90,8 +91,7 @@ QtObject:
result = self.chats.len result = self.chats.len
proc getChannel*(self: ChannelsList, index: int): ChatItem = proc getChannel*(self: ChannelsList, index: int): ChatItem = self.chats[index]
self.chats[index]
proc upsertChannel(self: ChannelsList, channel: ChatItem): int = proc upsertChannel(self: ChannelsList, channel: ChatItem): int =
let idx = self.chats.findById(channel.id) let idx = self.chats.findById(channel.id)

View File

@ -2,7 +2,7 @@ import eventemitter, sets, json, strutils
import ../status/utils import ../status/utils
import ../status/chat as status_chat import ../status/chat as status_chat
import chronicles import chronicles
import ../signals/types
import chat/chat_item import chat/chat_item
import chat/chat_message import chat/chat_message
export chat_item export chat_item
@ -13,6 +13,9 @@ type MsgArgs* = ref object of Args
chatId*: string chatId*: string
payload*: JsonNode payload*: JsonNode
type ChatArgs* = ref object of Args
chats*: seq[Chat]
type type
ChatModel* = ref object ChatModel* = ref object
events*: EventEmitter events*: EventEmitter
@ -29,7 +32,7 @@ proc delete*(self: ChatModel) =
proc hasChannel*(self: ChatModel, chatId: string): bool = proc hasChannel*(self: ChatModel, chatId: string): bool =
result = self.channels.contains(chatId) result = self.channels.contains(chatId)
proc join*(self: ChatModel, chatId: string) = proc join*(self: ChatModel, chatId: string, isNewChat: bool = true) =
if self.hasChannel(chatId): return if self.hasChannel(chatId): return
self.channels.incl chatId self.channels.incl chatId
@ -39,15 +42,16 @@ proc join*(self: ChatModel, chatId: string) =
# TODO get this from the connection or something # TODO get this from the connection or something
let peer = "enode://44160e22e8b42bd32a06c1532165fa9e096eebedd7fa6d6e5f8bbef0440bc4a4591fe3651be68193a7ec029021cdb496cfe1d7f9f1dc69eb99226e6f39a7a5d4@35.225.221.245:443" let peer = "enode://44160e22e8b42bd32a06c1532165fa9e096eebedd7fa6d6e5f8bbef0440bc4a4591fe3651be68193a7ec029021cdb496cfe1d7f9f1dc69eb99226e6f39a7a5d4@35.225.221.245:443"
# TODO: save chat list in the db
let oneToOne = isOneToOneChat(chatId) let oneToOne = isOneToOneChat(chatId)
if isNewChat: status_chat.saveChat(chatId, oneToOne)
let filterResult = status_chat.loadFilters(chatId = chatId, oneToOne = oneToOne) let filterResult = status_chat.loadFilters(chatId = chatId, oneToOne = oneToOne)
status_chat.saveChat(chatId, oneToOne)
status_chat.chatMessages(chatId) status_chat.chatMessages(chatId)
let parsedResult = parseJson(filterResult)["result"] let parsedResult = parseJson(filterResult)["result"]
echo parsedResult
var topics = newSeq[string](0) var topics = newSeq[string](0)
for topicObj in parsedResult: for topicObj in parsedResult:
if (($topicObj["chatId"]).strip(chars = {'"'}) == chatId): if (($topicObj["chatId"]).strip(chars = {'"'}) == chatId):
@ -58,6 +62,12 @@ proc join*(self: ChatModel, chatId: string) =
else: else:
status_chat.requestMessages(topics, generatedSymKey, peer, 20) status_chat.requestMessages(topics, generatedSymKey, peer, 20)
proc load*(self: ChatModel) =
let chatList = status_chat.loadChats()
for chat in chatList:
self.join(chat.id, false)
self.events.emit("chatsLoaded", ChatArgs(chats: chatList))
proc leave*(self: ChatModel, chatId: string) = proc leave*(self: ChatModel, chatId: string) =
let oneToOne = isOneToOneChat(chatId) let oneToOne = isOneToOneChat(chatId)
discard status_chat.removeFilters(chatId = chatId, oneToOne = oneToOne) discard status_chat.removeFilters(chatId = chatId, oneToOne = oneToOne)

View File

@ -47,6 +47,7 @@ proc toChatItem*(chat: Chat): ChatItem =
result = ChatItem( result = ChatItem(
id: chat.id, id: chat.id,
name: chatName(chat), name: chatName(chat),
color: chat.color,
chatType: chat.chatType, chatType: chat.chatType,
lastMessage: chat.lastMessage.text, lastMessage: chat.lastMessage.text,
timestamp: chat.timestamp, timestamp: chat.timestamp,

View File

@ -45,7 +45,6 @@ proc mainProc() =
engine.setRootContextProperty("assetsModel", wallet.variant) engine.setRootContextProperty("assetsModel", wallet.variant)
var chat = chat.newController(appEvents) var chat = chat.newController(appEvents)
chat.init()
engine.setRootContextProperty("chatsModel", chat.variant) engine.setRootContextProperty("chatsModel", chat.variant)
var node = node.newController(appEvents) var node = node.newController(appEvents)
@ -58,6 +57,7 @@ proc mainProc() =
appEvents.once("login") do(a: Args): appEvents.once("login") do(a: Args):
var args = AccountArgs(a) var args = AccountArgs(a)
status_core.startMessenger() status_core.startMessenger()
chat.init()
wallet.init() wallet.init()
profile.init(args.account) profile.init(args.account)
@ -84,17 +84,6 @@ proc mainProc() =
engine.setRootContextProperty("signals", signalController.variant) engine.setRootContextProperty("signals", signalController.variant)
appState.subscribe(proc () =
for channel in appState.channels:
chat.load(channel.name)
)
# accountsModel.appEvents.once("login") do(a: Args):
# appEvents.once("login") do(a: Args):
# appState.addChannel("test")
# appState.addChannel("test2")
# appState.addChannel("status")
engine.load("../ui/main.qml") engine.load("../ui/main.qml")
# Please note that this must use the `cdecl` calling convention because # Please note that this must use the `cdecl` calling convention because

View File

@ -1,8 +1,8 @@
import json import json
import types import types
proc toMessage(jsonMsg: JsonNode): Message proc toMessage*(jsonMsg: JsonNode): Message
proc toChat(jsonChat: JsonNode): Chat proc toChat*(jsonChat: JsonNode): Chat
proc fromEvent*(event: JsonNode): Signal = proc fromEvent*(event: JsonNode): Signal =
var signal:MessageSignal = MessageSignal() var signal:MessageSignal = MessageSignal()
@ -18,7 +18,7 @@ proc fromEvent*(event: JsonNode): Signal =
result = signal result = signal
proc toChat(jsonChat: JsonNode): Chat = proc toChat*(jsonChat: JsonNode): Chat =
result = Chat( result = Chat(
id: jsonChat{"id"}.getStr, id: jsonChat{"id"}.getStr,
name: jsonChat{"name"}.getStr, name: jsonChat{"name"}.getStr,
@ -32,7 +32,7 @@ proc toChat(jsonChat: JsonNode): Chat =
lastMessage: jsonChat{"lastMessage"}.toMessage lastMessage: jsonChat{"lastMessage"}.toMessage
) )
proc toMessage(jsonMsg: JsonNode): Message = proc toMessage*(jsonMsg: JsonNode): Message =
result = Message( result = Message(
alias: jsonMsg{"alias"}.getStr, alias: jsonMsg{"alias"}.getStr,
chatId: jsonMsg{"chatId"}.getStr, chatId: jsonMsg{"chatId"}.getStr,

View File

@ -4,6 +4,8 @@ import utils
import times import times
import strutils import strutils
import chronicles import chronicles
import ../signals/types
import ../signals/messages
proc loadFilters*(chatId: string, filterId: string = "", symKeyId: string = "", oneToOne: bool = false, identity: string = "", topic: string = "", discovery: bool = false, negotiated: bool = false, listen: bool = true): string = proc loadFilters*(chatId: string, filterId: string = "", symKeyId: string = "", oneToOne: bool = false, identity: string = "", topic: string = "", discovery: bool = false, negotiated: bool = false, listen: bool = true): string =
result = callPrivateRPC("loadFilters".prefix, %* [ result = callPrivateRPC("loadFilters".prefix, %* [
@ -40,19 +42,26 @@ proc removeFilters*(chatId: string, filterId: string = "", symKeyId: string = ""
proc saveChat*(chatId: string, oneToOne = false) = proc saveChat*(chatId: string, oneToOne = false) =
discard callPrivateRPC("saveChat".prefix, %* [ discard callPrivateRPC("saveChat".prefix, %* [
{ {
"lastClockValue": 0, "lastClockValue": 0, # TODO:
"color": "#51d0f0", "color": "#51d0f0", # TODO:
"name": chatId, "name": chatId,
"lastMessage": nil, "lastMessage": nil, # TODO:
"active": true, "active": true, # TODO:
"id": chatId, "id": chatId,
"unviewedMessagesCount": 0, "unviewedMessagesCount": 0, # TODO:
# TODO use constants for those too or use the Date # TODO use constants for those too or use the Date
"chatType": if oneToOne: 1 else: 2, "chatType": if oneToOne: 1 else: 2, # TODO: use constants
"timestamp": 1588940692659 "timestamp": 1588940692659 # TODO:
} }
]) ])
proc loadChats*(): seq[Chat] =
result = @[]
let jsonResponse = parseJson($callPrivateRPC("chats".prefix))
if jsonResponse["result"].kind != JNull:
for jsonChat in jsonResponse{"result"}:
result.add(jsonChat.toChat)
proc chatMessages*(chatId: string) = proc chatMessages*(chatId: string) =
discard callPrivateRPC("chatMessages".prefix, %* [chatId, nil, 20]) discard callPrivateRPC("chatMessages".prefix, %* [chatId, nil, 20])

View File

@ -16,7 +16,6 @@ proc callPrivateRPC*(methodName: string, payload = %* []): string =
"method": methodName, "method": methodName,
"params": %payload "params": %payload
} }
echo inputJSON
result = $libstatus.callPrivateRPC($inputJSON) result = $libstatus.callPrivateRPC($inputJSON)
except: except:
echo "error doing rpc request" echo "error doing rpc request"

View File

@ -19,7 +19,7 @@ Item {
delegate: Channel {} delegate: Channel {}
onCountChanged: { onCountChanged: {
if (count > 0) { if (count > 0) {
currentIndex = count - 1; currentIndex = 0;
} }
} }
} }