move sendMessage to the chats model
This commit is contained in:
parent
f29191bc59
commit
68b48a7082
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -31,3 +37,8 @@ proc join*(self: ChatModel, chatId: string) =
|
|||
status_chat.saveChat(chatId, oneToOne)
|
||||
status_chat.chatMessages(chatId)
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue