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
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):
var sentMessage = MsgArgs(e)
var chatMessage = sentMessage.payload.toChatMessage()
chatMessage.message = sentMessage.message
chatMessage.isCurrentUser = true
self.view.pushMessage(sentMessage.chatId, chatMessage)
proc load*(self: ChatController, chatId: string) =
# 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.model.load()
self.view.setActiveChannelByIndex(0)
proc handleMessage(self: ChatController, data: Signal) =

View File

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

View File

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

View File

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

View File

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

View File

@ -45,7 +45,6 @@ proc mainProc() =
engine.setRootContextProperty("assetsModel", wallet.variant)
var chat = chat.newController(appEvents)
chat.init()
engine.setRootContextProperty("chatsModel", chat.variant)
var node = node.newController(appEvents)
@ -58,6 +57,7 @@ proc mainProc() =
appEvents.once("login") do(a: Args):
var args = AccountArgs(a)
status_core.startMessenger()
chat.init()
wallet.init()
profile.init(args.account)
@ -84,17 +84,6 @@ proc mainProc() =
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")
# Please note that this must use the `cdecl` calling convention because

View File

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

View File

@ -4,6 +4,8 @@ import utils
import times
import strutils
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 =
result = callPrivateRPC("loadFilters".prefix, %* [
@ -40,19 +42,26 @@ proc removeFilters*(chatId: string, filterId: string = "", symKeyId: string = ""
proc saveChat*(chatId: string, oneToOne = false) =
discard callPrivateRPC("saveChat".prefix, %* [
{
"lastClockValue": 0,
"color": "#51d0f0",
"lastClockValue": 0, # TODO:
"color": "#51d0f0", # TODO:
"name": chatId,
"lastMessage": nil,
"active": true,
"lastMessage": nil, # TODO:
"active": true, # TODO:
"id": chatId,
"unviewedMessagesCount": 0,
"unviewedMessagesCount": 0, # TODO:
# TODO use constants for those too or use the Date
"chatType": if oneToOne: 1 else: 2,
"timestamp": 1588940692659
"chatType": if oneToOne: 1 else: 2, # TODO: use constants
"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) =
discard callPrivateRPC("chatMessages".prefix, %* [chatId, nil, 20])

View File

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

View File

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