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
This commit is contained in:
Michał Cieślak 2024-10-01 13:20:17 +02:00 committed by Michał
parent ee9091377f
commit f8d33656e6
3 changed files with 91 additions and 38 deletions

View File

@ -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

View File

@ -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,6 +234,7 @@ QtObject:
if roles.len == 0:
return
if preferredDisplayNameChanged:
roles.add(ModelRole.PreferredDisplayName.int)
let index = self.createIndex(ind, 0, nil)
@ -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:

View File

@ -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
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
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*(