refactor: join public chats
This commit is contained in:
parent
0e2131d499
commit
ec46dd13f1
|
@ -1,10 +1,10 @@
|
||||||
import NimQml
|
import NimQml
|
||||||
import json, sets
|
import json, sets, eventemitter
|
||||||
import ../../status/chat as status_chat
|
import ../../status/chat as status_chat
|
||||||
import view
|
import view
|
||||||
import messages
|
import messages
|
||||||
import ../signals/types
|
import ../signals/types
|
||||||
import ../../status/utils
|
import ../../models/chat
|
||||||
|
|
||||||
var sendMessage = proc (view: ChatsView, chatId: string, msg: string): string =
|
var sendMessage = proc (view: ChatsView, chatId: string, msg: string): string =
|
||||||
echo "sending public message!"
|
echo "sending public message!"
|
||||||
|
@ -23,13 +23,13 @@ var sendMessage = proc (view: ChatsView, chatId: string, msg: string): string =
|
||||||
|
|
||||||
type ChatController* = ref object of SignalSubscriber
|
type ChatController* = ref object of SignalSubscriber
|
||||||
view*: ChatsView
|
view*: ChatsView
|
||||||
|
model*: ChatModel
|
||||||
variant*: QVariant
|
variant*: QVariant
|
||||||
channels: HashSet[string]
|
|
||||||
|
|
||||||
proc newController*(): ChatController =
|
proc newController*(events: EventEmitter): ChatController =
|
||||||
result = ChatController()
|
result = ChatController()
|
||||||
result.channels = initHashSet[string]()
|
result.model = newChatModel(events)
|
||||||
result.view = newChatsView(sendMessage)
|
result.view = newChatsView(result.model, sendMessage)
|
||||||
result.variant = newQVariant(result.view)
|
result.variant = newQVariant(result.view)
|
||||||
|
|
||||||
proc delete*(self: ChatController) =
|
proc delete*(self: ChatController) =
|
||||||
|
@ -39,28 +39,11 @@ proc delete*(self: ChatController) =
|
||||||
proc init*(self: ChatController) =
|
proc init*(self: ChatController) =
|
||||||
discard
|
discard
|
||||||
|
|
||||||
proc join*(self: ChatController, chatId: string) =
|
proc load*(self: ChatController, chatId: string) =
|
||||||
if self.channels.contains(chatId):
|
# TODO: we need a function to load the channels from the db.
|
||||||
# TODO:
|
# and... called from init() instead from nim_status_client
|
||||||
return
|
discard self.view.joinChat(chatId)
|
||||||
|
self.view.setActiveChannelByIndex(0)
|
||||||
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"]
|
|
||||||
|
|
||||||
method onSignal(self: ChatController, data: Signal) =
|
method onSignal(self: ChatController, data: Signal) =
|
||||||
var chatSignal = cast[ChatSignal](data)
|
var chatSignal = cast[ChatSignal](data)
|
||||||
|
|
|
@ -3,7 +3,7 @@ import Tables
|
||||||
import messages
|
import messages
|
||||||
import messageList
|
import messageList
|
||||||
import ../signals/types
|
import ../signals/types
|
||||||
# import core as chat
|
import ../../models/chat
|
||||||
|
|
||||||
type
|
type
|
||||||
RoleNames {.pure.} = enum
|
RoleNames {.pure.} = enum
|
||||||
|
@ -12,39 +12,31 @@ type
|
||||||
QtObject:
|
QtObject:
|
||||||
type
|
type
|
||||||
ChatsView* = ref object of QAbstractListModel
|
ChatsView* = ref object of QAbstractListModel
|
||||||
|
model: ChatModel
|
||||||
names*: seq[string]
|
names*: seq[string]
|
||||||
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
|
sendMessage: proc (view: ChatsView, channel: string, msg: string): string
|
||||||
|
|
||||||
proc delete(self: ChatsView) = self.QAbstractListModel.delete
|
|
||||||
|
|
||||||
proc setup(self: ChatsView) = self.QAbstractListModel.setup
|
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)
|
new(result, delete)
|
||||||
|
result.model = model
|
||||||
result.sendMessage = sendMessage
|
result.sendMessage = sendMessage
|
||||||
result.names = @[]
|
result.names = @[]
|
||||||
result.activeChannel = ""
|
result.activeChannel = ""
|
||||||
result.messageList = initTable[string, ChatMessageList]() # newChatMessageList()
|
result.messageList = initTable[string, ChatMessageList]()
|
||||||
result.setup
|
result.setup()
|
||||||
|
|
||||||
proc upsertChannel(self: ChatsView, channel: string) =
|
proc upsertChannel(self: ChatsView, channel: string) =
|
||||||
if not self.messageList.hasKey(channel):
|
if not self.messageList.hasKey(channel):
|
||||||
self.messageList[channel] = newChatMessageList()
|
self.messageList[channel] = newChatMessageList()
|
||||||
|
|
||||||
proc addNameTolist*(self: ChatsView, channel: string) {.slot.} =
|
method rowCount(self: ChatsView, index: QModelIndex = nil): int = self.names.len
|
||||||
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 data(self: ChatsView, index: QModelIndex, role: int): QVariant =
|
method data(self: ChatsView, index: QModelIndex, role: int): QVariant =
|
||||||
if not index.isValid:
|
if not index.isValid:
|
||||||
|
@ -56,45 +48,54 @@ QtObject:
|
||||||
method roleNames(self: ChatsView): Table[int, string] =
|
method roleNames(self: ChatsView): Table[int, string] =
|
||||||
{ RoleNames.Name.int:"name"}.toTable
|
{ 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.} =
|
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) =
|
proc pushMessage*(self:ChatsView, channel: string, message: ChatMessage) =
|
||||||
self.upsertChannel(channel)
|
self.upsertChannel(channel)
|
||||||
self.messageList[channel].add(message)
|
self.messageList[channel].add(message)
|
||||||
|
|
||||||
|
proc activeChannel*(self: ChatsView): string {.slot.} = self.activeChannel
|
||||||
|
|
||||||
proc activeChannelChanged*(self: ChatsView) {.signal.}
|
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
|
if self.activeChannel == self.names[index]: return
|
||||||
self.activeChannel = self.names[index]
|
self.activeChannel = self.names[index]
|
||||||
self.activeChannelChanged()
|
self.activeChannelChanged()
|
||||||
|
|
||||||
QtProperty[string] activeChannel:
|
QtProperty[string] activeChannel:
|
||||||
|
read = activeChannel
|
||||||
write = setActiveChannel
|
write = setActiveChannel
|
||||||
notify = activeChannelChanged
|
notify = activeChannelChanged
|
||||||
|
|
||||||
proc getMessageList(self: ChatsView): QVariant {.slot.} =
|
proc getMessageList(self: ChatsView): QVariant {.slot.} =
|
||||||
|
self.upsertChannel(self.activeChannel)
|
||||||
return newQVariant(self.messageList[self.activeChannel])
|
return newQVariant(self.messageList[self.activeChannel])
|
||||||
|
|
||||||
QtProperty[QVariant] messageList:
|
QtProperty[QVariant] messageList:
|
||||||
read = getMessageList
|
read = getMessageList
|
||||||
notify = activeChannelChanged
|
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)
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -46,7 +46,7 @@ proc mainProc() =
|
||||||
var wallet = wallet.newController()
|
var wallet = wallet.newController()
|
||||||
engine.setRootContextProperty("assetsModel", wallet.variant)
|
engine.setRootContextProperty("assetsModel", wallet.variant)
|
||||||
|
|
||||||
var chat = chat.newController()
|
var chat = chat.newController(events)
|
||||||
chat.init()
|
chat.init()
|
||||||
engine.setRootContextProperty("chatsModel", chat.variant)
|
engine.setRootContextProperty("chatsModel", chat.variant)
|
||||||
|
|
||||||
|
@ -75,9 +75,9 @@ proc mainProc() =
|
||||||
engine.setRootContextProperty("signals", signalController.variant)
|
engine.setRootContextProperty("signals", signalController.variant)
|
||||||
|
|
||||||
appState.subscribe(proc () =
|
appState.subscribe(proc () =
|
||||||
# chatsModel.names = @[]
|
|
||||||
for channel in appState.channels:
|
for channel in appState.channels:
|
||||||
chat.join(channel.name)
|
echo channel.name
|
||||||
|
chat.load(channel.name)
|
||||||
)
|
)
|
||||||
|
|
||||||
accountsModel.events.on("accountsReady") do(a: Args):
|
accountsModel.events.on("accountsReady") do(a: Args):
|
||||||
|
|
|
@ -115,7 +115,8 @@ SplitView {
|
||||||
MouseArea {
|
MouseArea {
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
onClicked : {
|
onClicked : {
|
||||||
chatsModel.addNameTolist(searchText.text)
|
chatGroupsListView.currentIndex = chatsModel.joinChat(searchText.text);
|
||||||
|
searchText.text = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue