From 7b03da296792460843d8cdcfa22d73abb1ec0de7 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Mon, 4 Jan 2021 12:34:47 -0500 Subject: [PATCH] feat: enable removing member and fix invites --- src/app/chat/view.nim | 26 +++++++++++++++---- src/app/chat/views/community_item.nim | 7 +++++ src/app/chat/views/community_members_list.nim | 16 ++++++++++++ src/status/chat.nim | 13 +++++++--- src/status/libstatus/chat.nim | 12 +++------ .../MessageComponents/InvitationBubble.qml | 3 --- .../CommunityMembersPopup.qml | 2 +- 7 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index bc9774d333..3e34fbd807 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -313,7 +313,9 @@ QtObject: self.messageList[msg.chatId].add(msg) self.messagePushed() if self.channelOpenTime.getOrDefault(msg.chatId, high(int64)) < msg.timestamp.parseFloat.fromUnixFloat.toUnix: - let channel = self.status.chat.channels[msg.chatId] + let channel = self.chats.getChannelById(msg.chatId) + if (channel == nil): + continue let isAddedContact = channel.chatType.isOneToOne and self.status.contacts.isAdded(channel.id) if not channel.muted: self.messageNotificationPushed( @@ -608,11 +610,18 @@ QtObject: notify = joinedCommunitiesChanged proc addCommunityToList*(self: ChatsView, community: Community) = - let communityCheck = self.joinedCommunityList.getCommunityById(community.id) + let communityCheck = self.communityList.getCommunityById(community.id) if (communityCheck.id == ""): - self.joinedCommunityList.addCommunityItemToList(community) + self.communityList.addCommunityItemToList(community) else: - self.joinedCommunityList.replaceCommunity(community) + self.communityList.replaceCommunity(community) + + if (community.joined == true): + let joinedCommunityCheck = self.joinedCommunityList.getCommunityById(community.id) + if (joinedCommunityCheck.id == ""): + self.joinedCommunityList.addCommunityItemToList(community) + else: + self.joinedCommunityList.replaceCommunity(community) proc createCommunity*(self: ChatsView, name: string, description: string, color: string, imagePath: string): string {.slot.} = result = "" @@ -728,4 +737,11 @@ QtObject: try: self.status.chat.importCommunity(communityKey) except Exception as e: - error "Error importing the community", msg = e.msg \ No newline at end of file + error "Error importing the community", msg = e.msg + + proc removeUserFromCommunity*(self: ChatsView, pubKey: string) {.slot.} = + try: + self.status.chat.removeUserFromCommunity(self.activeCommunity.id(), pubKey) + self.activeCommunity.removeMember(pubKey) + except Exception as e: + error "Error removing user from the community", msg = e.msg \ No newline at end of file diff --git a/src/app/chat/views/community_item.nim b/src/app/chat/views/community_item.nim index ce53e41cdd..335795d62c 100644 --- a/src/app/chat/views/community_item.nim +++ b/src/app/chat/views/community_item.nim @@ -40,6 +40,12 @@ QtObject: self.status.events.emit("communityActiveChanged", Args()) self.activeChanged() + proc nbMembersChanged*(self: CommunityItemView) {.signal.} + + proc removeMember*(self: CommunityItemView, pubKey: string) = + self.members.removeMember(pubKey) + self.nbMembersChanged() + proc active*(self: CommunityItemView): bool {.slot.} = result = ?.self.active QtProperty[bool] active: @@ -86,6 +92,7 @@ QtObject: QtProperty[int] nbMembers: read = nbMembers + notify = nbMembersChanged proc getChats*(self: CommunityItemView): QVariant {.slot.} = result = newQVariant(self.chats) diff --git a/src/app/chat/views/community_members_list.nim b/src/app/chat/views/community_members_list.nim index afcbcc12c9..f62b14a68b 100644 --- a/src/app/chat/views/community_members_list.nim +++ b/src/app/chat/views/community_members_list.nim @@ -30,6 +30,22 @@ QtObject: self.members = members self.endResetModel() + proc getIndexFromPubKey*(self: CommunityMembersView, pubKey: string): int = + var i = 0 + for memberPubKey in self.members: + if (memberPubKey == pubKey): + return i + i = i + 1 + return -1 + + proc removeMember*(self: CommunityMembersView, pubKey: string) = + let memberIndex = self.getIndexFromPubKey(pubKey) + if (memberIndex == -1): + return + self.beginRemoveRows(newQModelIndex(), memberIndex, memberIndex) + self.members.delete(memberIndex) + self.endRemoveRows() + method rowCount(self: CommunityMembersView, index: QModelIndex = nil): int = self.members.len proc userName(self: CommunityMembersView, pk: string, alias: string): string = diff --git a/src/status/chat.nim b/src/status/chat.nim index 02733a1e3a..a86da08dc2 100644 --- a/src/status/chat.nim +++ b/src/status/chat.nim @@ -224,19 +224,21 @@ proc getLinkPreviewData*(self: ChatModel, link: string): JsonNode = proc setActiveChannel*(self: ChatModel, chatId: string) = self.events.emit("activeChannelChanged", ChatIdArg(chatId: chatId)) -proc processMessageUpdateAfterSend(self: ChatModel, response: string): (seq[Chat], seq[Message]) = +proc processMessageUpdateAfterSend(self: ChatModel, response: string, forceActiveChat: bool = false): (seq[Chat], seq[Message]) = result = self.processChatUpdate(parseJson(response)) var (chats, messages) = result if chats.len == 0 or messages.len == 0: self.events.emit("sendingMessageFailed", MessageArgs()) else: + if (forceActiveChat): + chats[0].isActive = true self.events.emit("chatUpdate", ChatUpdateArgs(messages: messages, chats: chats, contacts: @[])) for msg in messages: self.events.emit("sendingMessage", MessageArgs(id: msg.id, channel: msg.chatId)) -proc sendMessage*(self: ChatModel, chatId: string, msg: string, replyTo: string = "", contentType: int = ContentType.Message.int, communityId: string = "") = +proc sendMessage*(self: ChatModel, chatId: string, msg: string, replyTo: string = "", contentType: int = ContentType.Message.int, communityId: string = "", forceActiveChat: bool = false) = var response = status_chat.sendChatMessage(chatId, msg, replyTo, contentType, communityId) - discard self.processMessageUpdateAfterSend(response) + discard self.processMessageUpdateAfterSend(response, forceActiveChat) proc sendImage*(self: ChatModel, chatId: string, image: string) = var response = status_chat.sendImageMessage(chatId, image) @@ -396,7 +398,10 @@ proc leaveCommunity*(self: ChatModel, communityId: string) = proc inviteUserToCommunity*(self: ChatModel, communityId: string, pubKey: string) = status_chat.inviteUserToCommunity(communityId, pubKey) # After sending the invite, we send a message with the community ID so they can join - self.sendMessage(pubKey, "Upgrade here to see an invitation to community", "", ContentType.Community.int, communityId) + self.sendMessage(pubKey, "Upgrade here to see an invitation to community", "", ContentType.Community.int, communityId, true) + +proc removeUserFromCommunity*(self: ChatModel, communityId: string, pubKey: string) = + status_chat.removeUserFromCommunity(communityId, pubKey) proc exportCommunity*(self: ChatModel, communityId: string): string = result = status_chat.exportCommunity(communityId) diff --git a/src/status/libstatus/chat.nim b/src/status/libstatus/chat.nim index 360d7b84d9..8fb28ce077 100644 --- a/src/status/libstatus/chat.nim +++ b/src/status/libstatus/chat.nim @@ -211,7 +211,6 @@ proc getLinkPreviewData*(link: string): JsonNode = response.result - proc getAllComunities*(): seq[Community] = var communities: seq[Community] = @[] let rpcResult = callPrivateRPC("communities".prefix).parseJSON() @@ -280,12 +279,6 @@ proc createCommunityChannel*(communityId: string, name: string, description: str if rpcResult{"result"}.kind != JNull: result = rpcResult["result"]["chats"][0].toChat() - -#{\"jsonrpc\":\"2.0\",\"id\":0,\"result\":{\"chats\":[{\"id\":\"0x03537a54bd6f697f282ae848452f25ce656026cd8d0b3d3489178c76d3bf9ddf20cbf03afb-89ab-444e-96e9-e23b6c1266c0\",\"name\":\"general\",\"color\":\"#887af9\",\"active\":true,\"chatType\":6,\"timestamp\":1606424570375,\"lastClockValue\":0,\"deletedAtClockValue\":0,\"unviewedMessagesCount\":0,\"lastMessage\":null,\"members\":null,\"membershipUpdateEvents\":null,\"identicon\":\"\",\"communityId\":\"0x03537a54bd6f697f282ae848452f25ce656026cd8d0b3d3489178c76d3bf9ddf20\"}],\"communities\":[{\"id\":\"0x03537a54bd6f697f282ae848452f25ce656026cd8d0b3d3489178c76d3bf9ddf20\",\"description\":{\"clock\":2,\"permissions\":{\"access\":1},\"identity\":{\"display_name\":\"Jo2\",\"description\":\"Jo again\"},\"chats\":{\"cbf03afb-89ab-444e-96e9-e23b6c1266c0\":{\"permissions\":{\"access\":1},\"identity\":{\"display_name\":\"general\",\"description\":\"general channel\"}}}},\"admin\":true,\"verified\":false,\"joined\":true}],\"communitiesChanges\":[{\"MembersAdded\":{},\"MembersRemoved\":{},\"ChatsRemoved\":{},\"ChatsAdded\":{\"cbf03afb-89ab-444e-96e9-e23b6c1266c0\":{\"permissions\":{\"access\":1},\"identity\":{\"display_name\":\"general\",\"description\":\"general channel\"}}},\"ChatsModified\":{}}],\"filters\":[{\"chatId\":\"0x03537a54bd6f697f282ae848452f25ce656026cd8d0b3d3489178c76d3bf9ddf20cbf03afb-89ab-444e-96e9-e23b6c1266c0\",\"filterId\":\"eb220a77bde14d967462529d0ec7c1fa09a0ece4efebefb388ea6e0ecf9a1950\",\"symKeyId\":\"0f08b999ab6571429f79c4762bd922f2b7db38c80ba99cd4a5ac15ef75357d0e\",\"oneToOne\":false,\"identity\":\"\",\"topic\":\"0x46a1c2be\",\"discovery\":false,\"negotiated\":false,\"listen\":true}]}} - - # if rpcResult{"result"}.kind != JNull: - # result = rpcResult["result"]["communities"][0].toCommunity() - proc joinCommunity*(communityId: string) = discard callPrivateRPC("joinCommunity".prefix, %*[communityId]) @@ -299,4 +292,7 @@ proc exportCommunity*(communityId: string):string = result = callPrivateRPC("exportCommunity".prefix, %*[communityId]).parseJson()["result"].getStr proc importCommunity*(communityKey: string) = - discard callPrivateRPC("importCommunity".prefix, %*[communityKey]) \ No newline at end of file + discard callPrivateRPC("importCommunity".prefix, %*[communityKey]) + +proc removeUserFromCommunity*(communityId: string, pubKey: string) = + discard callPrivateRPC("removeUserFromCommunity".prefix, %*[communityId, pubKey]) \ No newline at end of file diff --git a/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/InvitationBubble.qml b/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/InvitationBubble.qml index 327a82739a..91a3d39def 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/InvitationBubble.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/InvitationBubble.qml @@ -12,9 +12,6 @@ Item { id: root anchors.left: parent.left -// anchors.leftMargin: isCurrentUser ? 0 : -// appSettings.compactMode ? Style.current.padding : 48; - width: childrenRect.width height: childrenRect.height Component.onCompleted: { diff --git a/ui/app/AppLayouts/Chat/CommunityComponents/CommunityMembersPopup.qml b/ui/app/AppLayouts/Chat/CommunityComponents/CommunityMembersPopup.qml index 755d665dd9..7f85e61745 100644 --- a/ui/app/AppLayouts/Chat/CommunityComponents/CommunityMembersPopup.qml +++ b/ui/app/AppLayouts/Chat/CommunityComponents/CommunityMembersPopup.qml @@ -136,7 +136,7 @@ ModalPopup { icon.height: 16 icon.color: Style.current.red text: qsTr("Kick") - onTriggered: console.log("TODO") + onTriggered: chatsModel.removeUserFromCommunity(model.pubKey) } Action { icon.source: "../../../img/communities/menu/ban.svg"