From a5b9511a55fd909b881d5bbaee8a69ff0a16f2f3 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 17 Nov 2020 10:37:00 -0400 Subject: [PATCH] fix: show ens usernames when creating a 1:1 chat --- src/app/chat/view.nim | 14 +++++++++++--- src/app/chat/views/channels_list.nim | 14 ++++++++------ src/app/chat/views/chat_item.nim | 9 ++++++++- src/status/chat.nim | 16 +++++++++++++--- src/status/chat/chat.nim | 3 ++- src/status/ens.nim | 2 ++ src/status/libstatus/chat.nim | 4 ++-- src/status/signals/messages.nim | 8 ++++++-- .../Chat/components/PrivateChatPopup.qml | 17 +++++++++++++---- 9 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index 1ce6573ec9..37923f923f 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -256,8 +256,8 @@ QtObject: let selectedChannel = self.chats.getChannel(index) if self.activeChannel.id == selectedChannel.id: return - if selectedChannel.chatType.isOneToOne: - selectedChannel.name = self.userNameOrAlias(selectedChannel.id) + if selectedChannel.chatType.isOneToOne and selectedChannel.id == selectedChannel.name: + selectedChannel.name = self.userNameOrAlias(selectedChannel.id) self.activeChannel.setChatItem(selectedChannel) self.status.chat.setActiveChannel(selectedChannel.id) @@ -418,7 +418,11 @@ QtObject: let channel = self.chats.getChannelById(contact.id) if not channel.isNil: if contact.localNickname == "": - channel.name = contact.username + if channel.name == "" or channel.name == channel.id: + if channel.ensName != "": + channel.name = channel.ensName + else: + channel.name = contact.username else: channel.name = contact.localNickname self.chats.updateChat(channel, false) @@ -461,6 +465,10 @@ QtObject: self.status.chat.join(channel, ChatType(chatTypeInt)) self.setActiveChannel(channel) + proc joinChatWithENS*(self: ChatsView, channel: string, ensName: string): int {.slot.} = + self.status.chat.join(channel, ChatType.OneToOne, ensName=status_ens.addDomain(ensName)) + self.setActiveChannel(channel) + proc chatGroupJoined(self: ChatsView, channel: string) {.signal.} proc joinGroup*(self: ChatsView) {.slot.} = diff --git a/src/app/chat/views/channels_list.nim b/src/app/chat/views/channels_list.nim index 9c7ac79716..4bcd3cb31c 100644 --- a/src/app/chat/views/channels_list.nim +++ b/src/app/chat/views/channels_list.nim @@ -37,11 +37,17 @@ QtObject: result.status = status result.setup() - proc userNameOrAlias(self: ChannelsList, pubKey: string): string {.slot.} = + proc userNameOrAlias(self: ChannelsList, pubKey: string): string = if self.status.chat.contacts.hasKey(pubKey): return ens.userNameOrAlias(self.status.chat.contacts[pubKey]) generateAlias(pubKey) + proc chatName(self: ChannelsList, chatItem: Chat): string = + if not chatItem.chatType.isOneToOne: return chatItem.name + if chatItem.ensName != "": + return "@" & userName(chatItem.ensName).userName(true) + return self.userNameOrAlias(chatItem.id) + method rowCount*(self: ChannelsList, index: QModelIndex = nil): int = self.chats.len proc renderBlock(self: ChannelsList, message: Message): string @@ -54,13 +60,9 @@ QtObject: let chatItem = self.chats[index.row] - var name = chatItem.name - if chatItem.chatType.isOneToOne: - name = self.userNameOrAlias(chatItem.id) - let chatItemRole = role.ChannelsRoles case chatItemRole: - of ChannelsRoles.Name: result = newQVariant(name) + of ChannelsRoles.Name: result = newQVariant(self.chatName(chatItem)) of ChannelsRoles.Timestamp: result = newQVariant($chatItem.timestamp) of ChannelsRoles.LastMessage: result = newQVariant(self.renderBlock(chatItem.lastMessage)) of ChannelsRoles.ContentType: result = newQVariant(chatItem.lastMessage.contentType.int) diff --git a/src/app/chat/views/chat_item.nim b/src/app/chat/views/chat_item.nim index f04b1d3d95..46c50fcb97 100644 --- a/src/app/chat/views/chat_item.nim +++ b/src/app/chat/views/chat_item.nim @@ -44,9 +44,16 @@ QtObject: proc name*(self: ChatItemView): string {.slot.} = if self.chatItem != nil and self.chatItem.chatType.isOneToOne: - result = self.userNameOrAlias(self.chatItem.id) + if self.chatItem.name == self.chatItem.id: + result = self.userNameOrAlias(self.chatItem.id) + else: + if self.chatItem.ensName != "": + result = "@" & userName(self.chatItem.ensName).userName(true) + else: + result = self.chatItem.name else: result = ?.self.chatItem.name + QtProperty[string] name: read = name diff --git a/src/status/chat.nim b/src/status/chat.nim index 3ff04c9766..0182641aa1 100644 --- a/src/status/chat.nim +++ b/src/status/chat.nim @@ -89,12 +89,16 @@ proc hasChannel*(self: ChatModel, chatId: string): bool = proc getActiveChannel*(self: ChatModel): string = if (self.channels.len == 0): "" else: toSeq(self.channels.values)[self.channels.len - 1].id -proc join*(self: ChatModel, chatId: string, chatType: ChatType) = +proc join*(self: ChatModel, chatId: string, chatType: ChatType, ensName: string = "") = if self.hasChannel(chatId): return var chat = newChat(chatId, ChatType(chatType)) self.channels[chat.id] = chat - status_chat.saveChat(chatId, chatType.isOneToOne, true, chat.color) + status_chat.saveChat(chatId, chatType.isOneToOne, true, chat.color, ensName) + if ensName != "": + chat.name = ensName + chat.ensName = ensName + let filterResult = status_chat.loadFilters(@[status_chat.buildFilter(chat)]) var topics:seq[MailserverTopic] = @[] @@ -124,9 +128,15 @@ proc updateContacts*(self: ChatModel, contacts: seq[Profile]) = proc init*(self: ChatModel) = let chatList = status_chat.loadChats() + echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + echo $chatList + var filters:seq[JsonNode] = @[] for chat in chatList: - if self.hasChannel(chat.id): continue + echo ",,,,,,,,,", chat.name + if self.hasChannel(chat.id): + echo "Has channel" + continue filters.add status_chat.buildFilter(chat) self.channels[chat.id] = chat self.events.emit("channelLoaded", ChannelArgs(chat: chat)) diff --git a/src/status/chat/chat.nim b/src/status/chat/chat.nim index feeba41eb2..ebc85961d4 100644 --- a/src/status/chat/chat.nim +++ b/src/status/chat/chat.nim @@ -68,6 +68,7 @@ type Chat* = ref object membershipUpdateEvents*: seq[ChatMembershipEvent] hasMentions*: bool muted*: bool + ensName*: string proc `$`*(self: Chat): string = result = fmt"Chat(id:{self.id}, name:{self.name}, active:{self.isActive}, type:{self.chatType})" @@ -83,7 +84,7 @@ proc toJsonNode*(self: Chat): JsonNode = "lastMessage": nil, "members": self.members.toJsonNode, "membershipUpdateEvents": self.membershipUpdateEvents.toJsonNode, - "name": self.name, + "name": (if self.ensName != "": self.ensName else: self.name), "timestamp": self.timestamp, "unviewedMessagesCount": self.unviewedMessagesCount } diff --git a/src/status/ens.nim b/src/status/ens.nim index 42f7970ee7..a1432832d1 100644 --- a/src/status/ens.nim +++ b/src/status/ens.nim @@ -26,6 +26,8 @@ proc userName*(ensName: string, removeSuffix: bool = false): string = else: result = ensName else: + if ensName.endsWith(".eth") and removeSuffix: + return ensName.split(".")[0] result = ensName proc addDomain*(username: string): string = diff --git a/src/status/libstatus/chat.nim b/src/status/libstatus/chat.nim index b96552a750..b6ffccd946 100644 --- a/src/status/libstatus/chat.nim +++ b/src/status/libstatus/chat.nim @@ -18,14 +18,14 @@ proc removeFilters*(chatId: string, filterId: string) = [{ "ChatID": chatId, "FilterID": filterId }] ]) -proc saveChat*(chatId: string, oneToOne: bool = false, active: bool = true, color: string) = +proc saveChat*(chatId: string, oneToOne: bool = false, active: bool = true, color: string, ensName: string = "") = # TODO: ideally status-go/stimbus should handle some of these fields instead of having the client # send them: lastMessage, unviewedMEssagesCount, timestamp, lastClockValue, name? discard callPrivateRPC("saveChat".prefix, %* [ { "lastClockValue": 0, # TODO: "color": color, - "name": chatId, + "name": (if ensName != "": ensName else: chatId), "lastMessage": nil, # TODO: "active": active, "id": chatId, diff --git a/src/status/signals/messages.nim b/src/status/signals/messages.nim index 0746d2c5fd..e70f8a5a23 100644 --- a/src/status/signals/messages.nim +++ b/src/status/signals/messages.nim @@ -121,7 +121,8 @@ proc toChat*(jsonChat: JsonNode): Chat = deletedAtClockValue: jsonChat{"deletedAtClockValue"}.getBiggestInt, unviewedMessagesCount: jsonChat{"unviewedMessagesCount"}.getInt, hasMentions: false, - muted: false + muted: false, + ensName: "" ) if jsonChat.hasKey("muted") and jsonChat["muted"].kind != JNull: @@ -132,7 +133,10 @@ proc toChat*(jsonChat: JsonNode): Chat = if result.chatType == ChatType.OneToOne: result.identicon = generateIdenticon(result.id) - result.name = generateAlias(result.id) + if result.name.endsWith(".eth"): + result.ensName = result.name + if result.name == "": + result.name = generateAlias(result.id) if jsonChat["members"].kind != JNull: result.members = @[] diff --git a/ui/app/AppLayouts/Chat/components/PrivateChatPopup.qml b/ui/app/AppLayouts/Chat/components/PrivateChatPopup.qml index ebdc6498dc..accd465aaa 100644 --- a/ui/app/AppLayouts/Chat/components/PrivateChatPopup.qml +++ b/ui/app/AppLayouts/Chat/components/PrivateChatPopup.qml @@ -48,8 +48,13 @@ ModalPopup { } function doJoin() { - if (!validate() || pubKey.trim() === "") return; - chatsModel.joinChat(pubKey, Constants.chatTypeOneToOne); + if (!validate() || pubKey.trim() === "" || validationError !== "") return; + if(Utils.isChatKey(chatKey.text)){ + chatsModel.joinChat(pubKey, Constants.chatTypeOneToOne); + } else { + chatsModel.joinChatWithENS(pubKey, chatKey.text); + } + popup.close(); } @@ -87,8 +92,12 @@ ModalPopup { ensUsername.text = qsTrId("user-not-found"); pubKey = ""; } else { - ensUsername.text = chatsModel.formatENSUsername(chatKey.text) + " • " + Utils.compactAddress(resolvedPubKey, 4) - pubKey = resolvedPubKey; + if (profileModel.profile.pubKey === resolvedPubKey) { + validationError = qsTr("Can't chat with yourself"); + } else { + ensUsername.text = chatsModel.formatENSUsername(chatKey.text) + " • " + Utils.compactAddress(resolvedPubKey, 4) + pubKey = resolvedPubKey; + } } loading = false; }