diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index 70a345fd9a..5c26b9a7e0 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -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) diff --git a/src/app/chat/views/channels_list.nim b/src/app/chat/views/channels_list.nim index 43cc6f1d22..524bb39cb1 100644 --- a/src/app/chat/views/channels_list.nim +++ b/src/app/chat/views/channels_list.nim @@ -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] diff --git a/src/models/chat.nim b/src/models/chat.nim index 11e577bea9..752ebdd29b 100644 --- a/src/models/chat.nim +++ b/src/models/chat.nim @@ -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) diff --git a/src/status/chat.nim b/src/status/chat.nim index e99c57e566..2dfaa4cdcf 100644 --- a/src/status/chat.nim +++ b/src/status/chat.nim @@ -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 diff --git a/ui/app/AppLayouts/Chat/ChatColumn.qml b/ui/app/AppLayouts/Chat/ChatColumn.qml index da30b67848..bc2fbf46b0 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn.qml @@ -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() } + } } } }