From ec46dd13f1ed51b2e6cb5992663bc3c8e9608841 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 21 May 2020 14:32:24 -0400 Subject: [PATCH] refactor: join public chats --- src/app/chat/core.nim | 39 ++++---------- src/app/chat/view.nim | 77 ++++++++++++++------------- src/models/chat.nim | 33 ++++++++++++ src/nim_status_client.nim | 6 +-- ui/app/AppLayouts/Chat/ChatLayout.qml | 3 +- 5 files changed, 88 insertions(+), 70 deletions(-) create mode 100644 src/models/chat.nim diff --git a/src/app/chat/core.nim b/src/app/chat/core.nim index 2050b0ce34..3a4153f390 100644 --- a/src/app/chat/core.nim +++ b/src/app/chat/core.nim @@ -1,10 +1,10 @@ import NimQml -import json, sets +import json, sets, eventemitter import ../../status/chat as status_chat import view import messages import ../signals/types -import ../../status/utils +import ../../models/chat var sendMessage = proc (view: ChatsView, chatId: string, msg: string): string = echo "sending public message!" @@ -23,13 +23,13 @@ var sendMessage = proc (view: ChatsView, chatId: string, msg: string): string = type ChatController* = ref object of SignalSubscriber view*: ChatsView + model*: ChatModel variant*: QVariant - channels: HashSet[string] -proc newController*(): ChatController = +proc newController*(events: EventEmitter): ChatController = result = ChatController() - result.channels = initHashSet[string]() - result.view = newChatsView(sendMessage) + result.model = newChatModel(events) + result.view = newChatsView(result.model, sendMessage) result.variant = newQVariant(result.view) proc delete*(self: ChatController) = @@ -39,28 +39,11 @@ proc delete*(self: ChatController) = proc init*(self: ChatController) = discard -proc join*(self: ChatController, chatId: string) = - if self.channels.contains(chatId): - # TODO: - return - - self.channels.incl chatId - - - # TODO: save chat list in the db - echo "Joining chat: ", chatId - let oneToOne = isOneToOneChat(chatId) - echo "Is one to one? ", oneToOne - status_chat.loadFilters(chatId, oneToOne) - status_chat.saveChat(chatId, oneToOne) - status_chat.chatMessages(chatId) - # self.chatsModel.addNameTolist(channel.name) - self.view.addNameTolist(chatId) - -proc load*(self: ChatController): seq[string] = - # TODO: retrieve chats from DB - self.join("test") - result = @["test"] +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) + self.view.setActiveChannelByIndex(0) method onSignal(self: ChatController, data: Signal) = var chatSignal = cast[ChatSignal](data) diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index 9fa0500426..bd6d4262d1 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -3,7 +3,7 @@ import Tables import messages import messageList import ../signals/types -# import core as chat +import ../../models/chat type RoleNames {.pure.} = enum @@ -12,39 +12,31 @@ type QtObject: type ChatsView* = ref object of QAbstractListModel + model: ChatModel names*: seq[string] callResult: string messageList: Table[string, ChatMessageList] activeChannel: string sendMessage: proc (view: ChatsView, channel: string, msg: string): string - proc delete(self: ChatsView) = self.QAbstractListModel.delete - proc setup(self: ChatsView) = self.QAbstractListModel.setup - proc newChatsView*(sendMessage: proc): ChatsView = + proc delete(self: ChatsView) = self.QAbstractListModel.delete + + proc newChatsView*(model: ChatModel, sendMessage: proc): ChatsView = new(result, delete) + result.model = model result.sendMessage = sendMessage result.names = @[] result.activeChannel = "" - result.messageList = initTable[string, ChatMessageList]() # newChatMessageList() - result.setup + result.messageList = initTable[string, ChatMessageList]() + result.setup() proc upsertChannel(self: ChatsView, channel: string) = if not self.messageList.hasKey(channel): self.messageList[channel] = newChatMessageList() - proc addNameTolist*(self: ChatsView, channel: string) {.slot.} = - if(self.activeChannel == ""): - self.activeChannel = channel - - self.beginInsertRows(newQModelIndex(), self.names.len, self.names.len) - self.names.add(channel) - self.upsertChannel(channel) - self.endInsertRows() - - method rowCount(self: ChatsView, index: QModelIndex = nil): int = - return self.names.len + method rowCount(self: ChatsView, index: QModelIndex = nil): int = self.names.len method data(self: ChatsView, index: QModelIndex, role: int): QVariant = if not index.isValid: @@ -56,45 +48,54 @@ QtObject: method roleNames(self: ChatsView): Table[int, string] = { RoleNames.Name.int:"name"}.toTable - # Accesors - proc callResult*(self: ChatsView): string {.slot.} = - result = self.callResult - - proc callResultChanged*(self: ChatsView, callResult: string) {.signal.} - - proc setCallResult(self: ChatsView, callResult: string) {.slot.} = - if self.callResult == callResult: return - self.callResult = callResult - self.callResultChanged(callResult) - - proc `callResult=`*(self: ChatsView, callResult: string) = self.setCallResult(callResult) - - QtProperty[string] callResult: - read = callResult - write = setCallResult - notify = callResultChanged - proc onSend*(self: ChatsView, inputJSON: string) {.slot.} = - self.setCallResult(self.sendMessage(self, self.activeChannel, inputJSON)) + discard self.sendMessage(self, self.activeChannel, inputJSON) proc pushMessage*(self:ChatsView, channel: string, message: ChatMessage) = self.upsertChannel(channel) self.messageList[channel].add(message) + proc activeChannel*(self: ChatsView): string {.slot.} = self.activeChannel + proc activeChannelChanged*(self: ChatsView) {.signal.} - proc setActiveChannelByIndex(self: ChatsView, index: int) {.slot.} = + proc setActiveChannelByIndex*(self: ChatsView, index: int) {.slot.} = if self.activeChannel == self.names[index]: return self.activeChannel = self.names[index] self.activeChannelChanged() QtProperty[string] activeChannel: + read = activeChannel write = setActiveChannel notify = activeChannelChanged proc getMessageList(self: ChatsView): QVariant {.slot.} = + self.upsertChannel(self.activeChannel) return newQVariant(self.messageList[self.activeChannel]) QtProperty[QVariant] messageList: read = getMessageList - notify = activeChannelChanged \ No newline at end of file + notify = activeChannelChanged + + proc setActiveChannel*(self: ChatsView, channel: string) = + self.activeChannel = channel + self.activeChannelChanged() + + proc addToList(self: ChatsView, channel: string): int = + if(self.activeChannel == ""): self.setActiveChannel(channel) + + self.beginInsertRows(newQModelIndex(), self.names.len, self.names.len) + self.names.add(channel) + self.upsertChannel(channel) + self.endInsertRows() + + result = self.names.len - 1 + + proc joinChat*(self: ChatsView, channel: string): int {.slot.} = + self.setActiveChannel(channel) + if self.model.hasChannel(channel): + result = self.names.find(channel) + else: + self.model.join(channel) + result = self.addToList(channel) + diff --git a/src/models/chat.nim b/src/models/chat.nim new file mode 100644 index 0000000000..1e3ef8a235 --- /dev/null +++ b/src/models/chat.nim @@ -0,0 +1,33 @@ +import eventemitter, sets +import ../status/utils +import ../status/chat as status_chat + +type + ChatModel* = ref object + events*: EventEmitter + channels*: HashSet[string] + +proc newChatModel*(events: EventEmitter): ChatModel = + result = ChatModel() + result.channels = initHashSet[string]() + result.events = events + +proc delete*(self: ChatModel) = + discard + +proc hasChannel*(self: ChatModel, chatId: string): bool = + result = self.channels.contains(chatId) + +proc join*(self: ChatModel, chatId: string) = + if self.hasChannel(chatId): return + + self.channels.incl chatId + + # TODO: save chat list in the db + + let oneToOne = isOneToOneChat(chatId) + + status_chat.loadFilters(chatId, oneToOne) + status_chat.saveChat(chatId, oneToOne) + status_chat.chatMessages(chatId) + \ No newline at end of file diff --git a/src/nim_status_client.nim b/src/nim_status_client.nim index 6ac7d34449..311c22ec9d 100644 --- a/src/nim_status_client.nim +++ b/src/nim_status_client.nim @@ -46,7 +46,7 @@ proc mainProc() = var wallet = wallet.newController() engine.setRootContextProperty("assetsModel", wallet.variant) - var chat = chat.newController() + var chat = chat.newController(events) chat.init() engine.setRootContextProperty("chatsModel", chat.variant) @@ -75,9 +75,9 @@ proc mainProc() = engine.setRootContextProperty("signals", signalController.variant) appState.subscribe(proc () = - # chatsModel.names = @[] for channel in appState.channels: - chat.join(channel.name) + echo channel.name + chat.load(channel.name) ) accountsModel.events.on("accountsReady") do(a: Args): diff --git a/ui/app/AppLayouts/Chat/ChatLayout.qml b/ui/app/AppLayouts/Chat/ChatLayout.qml index 99525e7751..7ac7af5b33 100644 --- a/ui/app/AppLayouts/Chat/ChatLayout.qml +++ b/ui/app/AppLayouts/Chat/ChatLayout.qml @@ -115,7 +115,8 @@ SplitView { MouseArea { anchors.fill: parent onClicked : { - chatsModel.addNameTolist(searchText.text) + chatGroupsListView.currentIndex = chatsModel.joinChat(searchText.text); + searchText.text = ""; } } }