diff --git a/src/app/chat/core.nim b/src/app/chat/core.nim index 3a4153f390..0a0217566c 100644 --- a/src/app/chat/core.nim +++ b/src/app/chat/core.nim @@ -6,21 +6,6 @@ import messages import ../signals/types import ../../models/chat -var sendMessage = proc (view: ChatsView, chatId: string, msg: string): string = - echo "sending public message!" - var sentMessage = status_chat.sendChatMessage(chatId, msg) - var parsedMessage = parseJson(sentMessage)["result"]["chats"][0]["lastMessage"] - - let chatMessage = newChatMessage() - chatMessage.userName = parsedMessage["alias"].str - chatMessage.message = msg - chatMessage.timestamp = $parsedMessage["timestamp"] - chatMessage.identicon = parsedMessage["identicon"].str - chatMessage.isCurrentUser = true - - view.pushMessage(chatId, chatMessage) - sentMessage - type ChatController* = ref object of SignalSubscriber view*: ChatsView model*: ChatModel @@ -29,7 +14,7 @@ type ChatController* = ref object of SignalSubscriber proc newController*(events: EventEmitter): ChatController = result = ChatController() result.model = newChatModel(events) - result.view = newChatsView(result.model, sendMessage) + result.view = newChatsView(result.model) result.variant = newQVariant(result.view) proc delete*(self: ChatController) = @@ -37,7 +22,17 @@ proc delete*(self: ChatController) = delete self.variant proc init*(self: ChatController) = - discard + self.model.events.on("messageSent") do(e: Args): + var sentMessage = MsgArgs(e) + + let chatMessage = newChatMessage() + chatMessage.userName = sentMessage.payload["alias"].str + chatMessage.message = sentMessage.message + chatMessage.timestamp = $sentMessage.payload["timestamp"] + chatMessage.identicon = sentMessage.payload["identicon"].str + 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. diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index bd6d4262d1..0e1266ee40 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -17,16 +17,14 @@ QtObject: callResult: string messageList: Table[string, ChatMessageList] activeChannel: string - sendMessage: proc (view: ChatsView, channel: string, msg: string): string proc setup(self: ChatsView) = self.QAbstractListModel.setup proc delete(self: ChatsView) = self.QAbstractListModel.delete - proc newChatsView*(model: ChatModel, sendMessage: proc): ChatsView = + proc newChatsView*(model: ChatModel): ChatsView = new(result, delete) result.model = model - result.sendMessage = sendMessage result.names = @[] result.activeChannel = "" result.messageList = initTable[string, ChatMessageList]() @@ -49,7 +47,7 @@ QtObject: { RoleNames.Name.int:"name"}.toTable proc onSend*(self: ChatsView, inputJSON: string) {.slot.} = - discard self.sendMessage(self, self.activeChannel, inputJSON) + discard self.model.sendMessage(self.activeChannel, inputJSON) proc pushMessage*(self:ChatsView, channel: string, message: ChatMessage) = self.upsertChannel(channel) @@ -98,4 +96,3 @@ QtObject: else: self.model.join(channel) result = self.addToList(channel) - diff --git a/src/models/chat.nim b/src/models/chat.nim index 1e3ef8a235..1579cdfb9f 100644 --- a/src/models/chat.nim +++ b/src/models/chat.nim @@ -1,7 +1,13 @@ import eventemitter, sets +import json, sets, eventemitter import ../status/utils import ../status/chat as status_chat +type MsgArgs* = ref object of Args + message*: string + chatId*: string + payload*: JsonNode + type ChatModel* = ref object events*: EventEmitter @@ -26,8 +32,13 @@ proc join*(self: ChatModel, chatId: string) = # 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 + +proc sendMessage*(self: ChatModel, chatId: string, msg: string): string = + var sentMessage = status_chat.sendChatMessage(chatId, msg) + var parsedMessage = parseJson(sentMessage)["result"]["chats"][0]["lastMessage"] + self.events.emit("messageSent", MsgArgs(message: msg, chatId: chatId, payload: parsedMessage)) + sentMessage