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 ../signals/types
|
||||||
import ../../models/chat
|
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
|
type ChatController* = ref object of SignalSubscriber
|
||||||
view*: ChatsView
|
view*: ChatsView
|
||||||
model*: ChatModel
|
model*: ChatModel
|
||||||
|
@ -29,7 +14,7 @@ type ChatController* = ref object of SignalSubscriber
|
||||||
proc newController*(events: EventEmitter): ChatController =
|
proc newController*(events: EventEmitter): ChatController =
|
||||||
result = ChatController()
|
result = ChatController()
|
||||||
result.model = newChatModel(events)
|
result.model = newChatModel(events)
|
||||||
result.view = newChatsView(result.model, sendMessage)
|
result.view = newChatsView(result.model)
|
||||||
result.variant = newQVariant(result.view)
|
result.variant = newQVariant(result.view)
|
||||||
|
|
||||||
proc delete*(self: ChatController) =
|
proc delete*(self: ChatController) =
|
||||||
|
@ -37,7 +22,17 @@ proc delete*(self: ChatController) =
|
||||||
delete self.variant
|
delete self.variant
|
||||||
|
|
||||||
proc init*(self: ChatController) =
|
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) =
|
proc load*(self: ChatController, chatId: string) =
|
||||||
# TODO: we need a function to load the channels from the db.
|
# TODO: we need a function to load the channels from the db.
|
||||||
|
|
|
@ -17,16 +17,14 @@ QtObject:
|
||||||
callResult: string
|
callResult: string
|
||||||
messageList: Table[string, ChatMessageList]
|
messageList: Table[string, ChatMessageList]
|
||||||
activeChannel: string
|
activeChannel: string
|
||||||
sendMessage: proc (view: ChatsView, channel: string, msg: string): string
|
|
||||||
|
|
||||||
proc setup(self: ChatsView) = self.QAbstractListModel.setup
|
proc setup(self: ChatsView) = self.QAbstractListModel.setup
|
||||||
|
|
||||||
proc delete(self: ChatsView) = self.QAbstractListModel.delete
|
proc delete(self: ChatsView) = self.QAbstractListModel.delete
|
||||||
|
|
||||||
proc newChatsView*(model: ChatModel, sendMessage: proc): ChatsView =
|
proc newChatsView*(model: ChatModel): ChatsView =
|
||||||
new(result, delete)
|
new(result, delete)
|
||||||
result.model = model
|
result.model = model
|
||||||
result.sendMessage = sendMessage
|
|
||||||
result.names = @[]
|
result.names = @[]
|
||||||
result.activeChannel = ""
|
result.activeChannel = ""
|
||||||
result.messageList = initTable[string, ChatMessageList]()
|
result.messageList = initTable[string, ChatMessageList]()
|
||||||
|
@ -49,7 +47,7 @@ QtObject:
|
||||||
{ RoleNames.Name.int:"name"}.toTable
|
{ RoleNames.Name.int:"name"}.toTable
|
||||||
|
|
||||||
proc onSend*(self: ChatsView, inputJSON: string) {.slot.} =
|
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) =
|
proc pushMessage*(self:ChatsView, channel: string, message: ChatMessage) =
|
||||||
self.upsertChannel(channel)
|
self.upsertChannel(channel)
|
||||||
|
@ -98,4 +96,3 @@ QtObject:
|
||||||
else:
|
else:
|
||||||
self.model.join(channel)
|
self.model.join(channel)
|
||||||
result = self.addToList(channel)
|
result = self.addToList(channel)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,13 @@
|
||||||
import eventemitter, sets
|
import eventemitter, sets
|
||||||
|
import json, sets, eventemitter
|
||||||
import ../status/utils
|
import ../status/utils
|
||||||
import ../status/chat as status_chat
|
import ../status/chat as status_chat
|
||||||
|
|
||||||
|
type MsgArgs* = ref object of Args
|
||||||
|
message*: string
|
||||||
|
chatId*: string
|
||||||
|
payload*: JsonNode
|
||||||
|
|
||||||
type
|
type
|
||||||
ChatModel* = ref object
|
ChatModel* = ref object
|
||||||
events*: EventEmitter
|
events*: EventEmitter
|
||||||
|
@ -26,8 +32,13 @@ proc join*(self: ChatModel, chatId: string) =
|
||||||
# TODO: save chat list in the db
|
# TODO: save chat list in the db
|
||||||
|
|
||||||
let oneToOne = isOneToOneChat(chatId)
|
let oneToOne = isOneToOneChat(chatId)
|
||||||
|
|
||||||
status_chat.loadFilters(chatId, oneToOne)
|
status_chat.loadFilters(chatId, oneToOne)
|
||||||
status_chat.saveChat(chatId, oneToOne)
|
status_chat.saveChat(chatId, oneToOne)
|
||||||
status_chat.chatMessages(chatId)
|
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