refactor: join public chats
This commit is contained in:
parent
0e2131d499
commit
ec46dd13f1
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
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()
|
||||
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):
|
||||
|
|
|
@ -115,7 +115,8 @@ SplitView {
|
|||
MouseArea {
|
||||
anchors.fill: parent
|
||||
onClicked : {
|
||||
chatsModel.addNameTolist(searchText.text)
|
||||
chatGroupsListView.currentIndex = chatsModel.joinChat(searchText.text);
|
||||
searchText.text = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue