add support to leave a chat

add support to leave a chat

add support to leave a chat

fix

add leave chat methods to model
This commit is contained in:
Iuri Matias 2020-05-26 16:37:38 -04:00
parent 0ee8f5c0fe
commit dda6b3c76d
5 changed files with 46 additions and 7 deletions

View File

@ -89,5 +89,14 @@ QtObject:
let randomColorIndex = rand(channelColors.len - 1)
result = self.chats.addChatItemToList(ChatItem(id: channel, name: channel, color: channelColors[randomColorIndex]))
proc leaveActiveChat*(self: ChatsView) {.slot.} =
self.model.leave(self.activeChannel)
let channelCount = self.chats.removeChatItemFromList(self.activeChannel)
if channelCount == 0:
self.setActiveChannel("")
else:
let nextChannel = self.chats.chats[self.chats.chats.len - 1]
self.setActiveChannel(nextChannel.name)
proc updateChat*(self: ChatsView, chat: ChatItem) =
self.chats.updateChat(chat)

View File

@ -76,6 +76,14 @@ QtObject:
result = self.chats.len - 1
proc removeChatItemFromList*(self: ChannelsList, channel: string): int =
let idx = self.chats.findById(channel)
self.beginRemoveRows(newQModelIndex(), idx, idx)
self.chats.delete(idx)
self.endRemoveRows()
result = self.chats.len
proc getChannel*(self: ChannelsList, index: int): ChatItem =
self.chats[index]

View File

@ -48,6 +48,7 @@ proc join*(self: ChatModel, chatId: string) =
status_chat.chatMessages(chatId)
let parsedResult = parseJson(filterResult)["result"]
echo parsedResult
var topics = newSeq[string](0)
for topicObj in parsedResult:
if (($topicObj["chatId"]).strip(chars = {'"'}) == chatId):
@ -58,9 +59,8 @@ proc join*(self: ChatModel, chatId: string) =
else:
status_chat.requestMessages(topics, generatedSymKey, peer, 20)
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
proc leave*(self: ChatModel, chatId: string) =
let oneToOne = isOneToOneChat(chatId)
discard status_chat.removeFilters(chatId = chatId, oneToOne = oneToOne)
# TODO: other calls (if any)
self.channels.excl(chatId)

View File

@ -7,6 +7,22 @@ import chronicles
proc loadFilters*(chatId: string, filterId: string = "", symKeyId: string = "", oneToOne: bool = false, identity: string = "", topic: string = "", discovery: bool = false, negotiated: bool = false, listen: bool = true): string =
result = callPrivateRPC("loadFilters".prefix, %* [
[{
"ChatID": chatId, # identifier of the chat
"FilterID": filterId, # whisper filter id generated
"SymKeyID": symKeyId, # symmetric key id used for symmetric filters
"OneToOne": oneToOne, # if asymmetric encryption is used for this chat
"Identity": identity, # public key of the other recipient for non-public filters.
# FIXME: passing empty string to the topic makes it error
# "Topic": topic, # whisper topic
"Discovery": discovery,
"Negotiated": negotiated,
"Listen": listen # whether we are actually listening for messages on this chat, or the filter is only created in order to be able to post on the topic
}]
])
proc removeFilters*(chatId: string, filterId: string = "", symKeyId: string = "", oneToOne: bool = false, identity: string = "", topic: string = "", discovery: bool = false, negotiated: bool = false, listen: bool = true): string =
result = callPrivateRPC("removeFilters".prefix, %* [
[{
"ChatID": chatId, # identifier of the chat
"FilterID": filterId, # whisper filter id generated

View File

@ -109,8 +109,14 @@ StackLayout {
anchors.rightMargin: -15
anchors.leftMargin: -15
anchors.fill: parent
onClicked: console.log("Options click. Will do something later...")
onClicked: contextMenu.open()
cursorShape: Qt.PointingHandCursor
acceptedButtons: Qt.LeftButton | Qt.RightButton
Menu {
id: contextMenu
MenuItem { text: "Leave Chat"; onTriggered: chatsModel.leaveActiveChat() }
}
}
}
}