From f8d33656e6e195cb41ab121100f7327a45f4d107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Tue, 1 Oct 2024 13:20:17 +0200 Subject: [PATCH] fix: add PreferredDisplayName to user_model - excludes logic of resolving preferred display name to shared contacts_utils - expose PreferredDisplayName role from user_model - improves PreferredDisplayName handling in member_model Closes: #16429 --- .../modules/shared_models/contacts_utils.nim | 8 ++ .../modules/shared_models/member_model.nim | 27 +++--- src/app/modules/shared_models/user_model.nim | 94 ++++++++++++++----- 3 files changed, 91 insertions(+), 38 deletions(-) create mode 100644 src/app/modules/shared_models/contacts_utils.nim diff --git a/src/app/modules/shared_models/contacts_utils.nim b/src/app/modules/shared_models/contacts_utils.nim new file mode 100644 index 0000000000..b7716a00cf --- /dev/null +++ b/src/app/modules/shared_models/contacts_utils.nim @@ -0,0 +1,8 @@ +proc resolvePreferredDisplayName*(localNickName: string, ensName: string, displayName: string, alias: string): string = + if localNickname != "": + return localNickname + if ensName != "": + return ensName + if displayName != "": + return displayName + return alias diff --git a/src/app/modules/shared_models/member_model.nim b/src/app/modules/shared_models/member_model.nim index d14aa712d2..61d9dcaaa6 100644 --- a/src/app/modules/shared_models/member_model.nim +++ b/src/app/modules/shared_models/member_model.nim @@ -5,6 +5,7 @@ import NimQml, Tables, stew/shims/strformat, sequtils, sugar import ../../../app_service/common/types import ../../../app_service/service/contacts/dto/contacts import member_item +import contacts_utils type ModelRole {.pure.} = enum @@ -120,13 +121,8 @@ QtObject: of ModelRole.DisplayName: result = newQVariant(item.displayName) of ModelRole.PreferredDisplayName: - if item.localNickname != "": - return newQVariant(item.localNickname) - if item.ensName != "": - return newQVariant(item.ensName) - if item.displayName != "": - return newQVariant(item.displayName) - return newQVariant(item.alias) + return newQVariant(resolvePreferredDisplayName( + item.localNickname, item.ensName, item.displayName, item.alias)) of ModelRole.EnsName: result = newQVariant(item.ensName) of ModelRole.IsEnsVerified: @@ -219,6 +215,10 @@ QtObject: var roles: seq[int] = @[] + let preferredDisplayNameChanged = + resolvePreferredDisplayName(self.items[ind].localNickname, self.items[ind].ensName, self.items[ind].displayName, self.items[ind].alias) != + resolvePreferredDisplayName(localNickname, ensName, displayName, self.items[ind].alias) + if self.items[ind].displayName != displayName: self.items[ind].displayName = displayName roles.add(ModelRole.DisplayName.int) @@ -234,7 +234,8 @@ QtObject: if roles.len == 0: return - roles.add(ModelRole.PreferredDisplayName.int) + if preferredDisplayNameChanged: + roles.add(ModelRole.PreferredDisplayName.int) let index = self.createIndex(ind, 0, nil) defer: index.delete @@ -275,21 +276,20 @@ QtObject: var roles: seq[int] = @[] - var preferredNameMightHaveChanged = false + let preferredDisplayNameChanged = + resolvePreferredDisplayName(self.items[ind].localNickname, self.items[ind].ensName, self.items[ind].displayName, self.items[ind].alias) != + resolvePreferredDisplayName(localNickname, ensName, displayName, alias) if self.items[ind].displayName != displayName: self.items[ind].displayName = displayName - preferredNameMightHaveChanged = true roles.add(ModelRole.DisplayName.int) if self.items[ind].ensName != ensName: self.items[ind].ensName = ensName - preferredNameMightHaveChanged = true roles.add(ModelRole.EnsName.int) if self.items[ind].localNickname != localNickname: self.items[ind].localNickname = localNickname - preferredNameMightHaveChanged = true roles.add(ModelRole.LocalNickname.int) if self.items[ind].isEnsVerified != isEnsVerified: @@ -298,7 +298,6 @@ QtObject: if self.items[ind].alias != alias: self.items[ind].alias = alias - preferredNameMightHaveChanged = true roles.add(ModelRole.Alias.int) if self.items[ind].icon != icon: @@ -325,7 +324,7 @@ QtObject: self.items[ind].isUntrustworthy = isUntrustworthy roles.add(ModelRole.IsUntrustworthy.int) - if preferredNameMightHaveChanged: + if preferredDisplayNameChanged: roles.add(ModelRole.PreferredDisplayName.int) if roles.len == 0: diff --git a/src/app/modules/shared_models/user_model.nim b/src/app/modules/shared_models/user_model.nim index abe1e3ac0f..a671e70f1a 100644 --- a/src/app/modules/shared_models/user_model.nim +++ b/src/app/modules/shared_models/user_model.nim @@ -2,11 +2,13 @@ import NimQml, Tables, stew/shims/strformat, sequtils, sugar import user_item import ../../../app_service/common/types +import contacts_utils type ModelRole {.pure.} = enum PubKey = UserRole + 1 DisplayName + PreferredDisplayName EnsName IsEnsVerified LocalNickname @@ -83,6 +85,7 @@ QtObject: { ModelRole.PubKey.int: "pubKey", ModelRole.DisplayName.int: "displayName", + ModelRole.PreferredDisplayName.int: "preferredDisplayName", ModelRole.EnsName.int: "ensName", ModelRole.IsEnsVerified.int: "isEnsVerified", ModelRole.LocalNickname.int: "localNickname", @@ -128,6 +131,9 @@ QtObject: result = newQVariant(item.pubKey) of ModelRole.DisplayName: result = newQVariant(item.displayName) + of ModelRole.PreferredDisplayName: + return newQVariant(resolvePreferredDisplayName( + item.localNickname, item.ensName, item.displayName, item.alias)) of ModelRole.EnsName: result = newQVariant(item.ensName) of ModelRole.IsEnsVerified: @@ -260,17 +266,33 @@ QtObject: if(ind == -1): return - self.items[ind].displayName = displayName - self.items[ind].ensName = ensName - self.items[ind].localNickname = localNickname + var roles: seq[int] = @[] + + let preferredDisplayNameChanged = + resolvePreferredDisplayName(self.items[ind].localNickname, self.items[ind].ensName, self.items[ind].displayName, self.items[ind].alias) != + resolvePreferredDisplayName(localNickname, ensName, displayName, self.items[ind].alias) + + if self.items[ind].displayName != displayName: + self.items[ind].displayName = displayName + roles.add(ModelRole.DisplayName.int) + + if self.items[ind].ensName != ensName: + self.items[ind].ensName = ensName + roles.add(ModelRole.EnsName.int) + + if self.items[ind].localNickname != localNickname: + self.items[ind].localNickname = localNickname + roles.add(ModelRole.LocalNickname.int) + + if preferredDisplayNameChanged: + roles.add(ModelRole.PreferredDisplayName.int) + + if roles.len == 0: + return let index = self.createIndex(ind, 0, nil) defer: index.delete - self.dataChanged(index, index, @[ - ModelRole.DisplayName.int, - ModelRole.EnsName.int, - ModelRole.LocalNickname.int, - ]) + self.dataChanged(index, index, roles) self.itemChanged(pubKey) proc setIcon*(self: Model, pubKey: string, icon: string) = @@ -300,25 +322,49 @@ QtObject: if(ind == -1): return - self.items[ind].displayName = displayName - self.items[ind].ensName = ensName - self.items[ind].isEnsVerified = isEnsVerified - self.items[ind].localNickname = localNickname - self.items[ind].alias = alias - self.items[ind].icon = icon - self.items[ind].isUntrustworthy = isUntrustworthy + var roles: seq[int] = @[] + + let preferredDisplayNameChanged = + resolvePreferredDisplayName(self.items[ind].localNickname, self.items[ind].ensName, self.items[ind].displayName, self.items[ind].alias) != + resolvePreferredDisplayName(localNickname, ensName, displayName, alias) + + if self.items[ind].displayName != displayName: + self.items[ind].displayName = displayName + roles.add(ModelRole.DisplayName.int) + + if self.items[ind].ensName != ensName: + self.items[ind].ensName = ensName + roles.add(ModelRole.EnsName.int) + + if self.items[ind].isEnsVerified != isEnsVerified: + self.items[ind].isEnsVerified = isEnsVerified + roles.add(ModelRole.IsEnsVerified.int) + + if self.items[ind].localNickname != localNickname: + self.items[ind].localNickname = localNickname + roles.add(ModelRole.LocalNickname.int) + + if self.items[ind].alias != alias: + self.items[ind].alias = alias + roles.add(ModelRole.Alias.int) + + if self.items[ind].icon != icon: + self.items[ind].icon = icon + roles.add(ModelRole.Icon.int) + + if self.items[ind].isUntrustworthy != isUntrustworthy: + self.items[ind].isUntrustworthy = isUntrustworthy + roles.add(ModelRole.IsUntrustworthy.int) + + if preferredDisplayNameChanged: + roles.add(ModelRole.PreferredDisplayName.int) + + if roles.len == 0: + return let index = self.createIndex(ind, 0, nil) defer: index.delete - self.dataChanged(index, index, @[ - ModelRole.DisplayName.int, - ModelRole.EnsName.int, - ModelRole.IsEnsVerified.int, - ModelRole.LocalNickname.int, - ModelRole.Alias.int, - ModelRole.Icon.int, - ModelRole.IsUntrustworthy.int, - ]) + self.dataChanged(index, index, roles) self.itemChanged(pubKey) proc updateIncomingRequestStatus*(