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/common/types
import ../../../app_service/service/contacts/dto/contacts import ../../../app_service/service/contacts/dto/contacts
import member_item import member_item
import contacts_utils
type type
ModelRole {.pure.} = enum ModelRole {.pure.} = enum
@ -120,13 +121,8 @@ QtObject:
of ModelRole.DisplayName: of ModelRole.DisplayName:
result = newQVariant(item.displayName) result = newQVariant(item.displayName)
of ModelRole.PreferredDisplayName: of ModelRole.PreferredDisplayName:
if item.localNickname != "": return newQVariant(resolvePreferredDisplayName(
return newQVariant(item.localNickname) item.localNickname, item.ensName, item.displayName, item.alias))
if item.ensName != "":
return newQVariant(item.ensName)
if item.displayName != "":
return newQVariant(item.displayName)
return newQVariant(item.alias)
of ModelRole.EnsName: of ModelRole.EnsName:
result = newQVariant(item.ensName) result = newQVariant(item.ensName)
of ModelRole.IsEnsVerified: of ModelRole.IsEnsVerified:
@ -219,6 +215,10 @@ QtObject:
var roles: seq[int] = @[] 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: if self.items[ind].displayName != displayName:
self.items[ind].displayName = displayName self.items[ind].displayName = displayName
roles.add(ModelRole.DisplayName.int) roles.add(ModelRole.DisplayName.int)
@ -234,6 +234,7 @@ QtObject:
if roles.len == 0: if roles.len == 0:
return return
if preferredDisplayNameChanged:
roles.add(ModelRole.PreferredDisplayName.int) roles.add(ModelRole.PreferredDisplayName.int)
let index = self.createIndex(ind, 0, nil) let index = self.createIndex(ind, 0, nil)
@ -275,21 +276,20 @@ QtObject:
var roles: seq[int] = @[] 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: if self.items[ind].displayName != displayName:
self.items[ind].displayName = displayName self.items[ind].displayName = displayName
preferredNameMightHaveChanged = true
roles.add(ModelRole.DisplayName.int) roles.add(ModelRole.DisplayName.int)
if self.items[ind].ensName != ensName: if self.items[ind].ensName != ensName:
self.items[ind].ensName = ensName self.items[ind].ensName = ensName
preferredNameMightHaveChanged = true
roles.add(ModelRole.EnsName.int) roles.add(ModelRole.EnsName.int)
if self.items[ind].localNickname != localNickname: if self.items[ind].localNickname != localNickname:
self.items[ind].localNickname = localNickname self.items[ind].localNickname = localNickname
preferredNameMightHaveChanged = true
roles.add(ModelRole.LocalNickname.int) roles.add(ModelRole.LocalNickname.int)
if self.items[ind].isEnsVerified != isEnsVerified: if self.items[ind].isEnsVerified != isEnsVerified:
@ -298,7 +298,6 @@ QtObject:
if self.items[ind].alias != alias: if self.items[ind].alias != alias:
self.items[ind].alias = alias self.items[ind].alias = alias
preferredNameMightHaveChanged = true
roles.add(ModelRole.Alias.int) roles.add(ModelRole.Alias.int)
if self.items[ind].icon != icon: if self.items[ind].icon != icon:
@ -325,7 +324,7 @@ QtObject:
self.items[ind].isUntrustworthy = isUntrustworthy self.items[ind].isUntrustworthy = isUntrustworthy
roles.add(ModelRole.IsUntrustworthy.int) roles.add(ModelRole.IsUntrustworthy.int)
if preferredNameMightHaveChanged: if preferredDisplayNameChanged:
roles.add(ModelRole.PreferredDisplayName.int) roles.add(ModelRole.PreferredDisplayName.int)
if roles.len == 0: if roles.len == 0:

View File

@ -2,11 +2,13 @@ import NimQml, Tables, stew/shims/strformat, sequtils, sugar
import user_item import user_item
import ../../../app_service/common/types import ../../../app_service/common/types
import contacts_utils
type type
ModelRole {.pure.} = enum ModelRole {.pure.} = enum
PubKey = UserRole + 1 PubKey = UserRole + 1
DisplayName DisplayName
PreferredDisplayName
EnsName EnsName
IsEnsVerified IsEnsVerified
LocalNickname LocalNickname
@ -83,6 +85,7 @@ QtObject:
{ {
ModelRole.PubKey.int: "pubKey", ModelRole.PubKey.int: "pubKey",
ModelRole.DisplayName.int: "displayName", ModelRole.DisplayName.int: "displayName",
ModelRole.PreferredDisplayName.int: "preferredDisplayName",
ModelRole.EnsName.int: "ensName", ModelRole.EnsName.int: "ensName",
ModelRole.IsEnsVerified.int: "isEnsVerified", ModelRole.IsEnsVerified.int: "isEnsVerified",
ModelRole.LocalNickname.int: "localNickname", ModelRole.LocalNickname.int: "localNickname",
@ -128,6 +131,9 @@ QtObject:
result = newQVariant(item.pubKey) result = newQVariant(item.pubKey)
of ModelRole.DisplayName: of ModelRole.DisplayName:
result = newQVariant(item.displayName) result = newQVariant(item.displayName)
of ModelRole.PreferredDisplayName:
return newQVariant(resolvePreferredDisplayName(
item.localNickname, item.ensName, item.displayName, item.alias))
of ModelRole.EnsName: of ModelRole.EnsName:
result = newQVariant(item.ensName) result = newQVariant(item.ensName)
of ModelRole.IsEnsVerified: of ModelRole.IsEnsVerified:
@ -260,17 +266,33 @@ QtObject:
if(ind == -1): if(ind == -1):
return 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 self.items[ind].displayName = displayName
roles.add(ModelRole.DisplayName.int)
if self.items[ind].ensName != ensName:
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 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) let index = self.createIndex(ind, 0, nil)
defer: index.delete defer: index.delete
self.dataChanged(index, index, @[ self.dataChanged(index, index, roles)
ModelRole.DisplayName.int,
ModelRole.EnsName.int,
ModelRole.LocalNickname.int,
])
self.itemChanged(pubKey) self.itemChanged(pubKey)
proc setIcon*(self: Model, pubKey: string, icon: string) = proc setIcon*(self: Model, pubKey: string, icon: string) =
@ -300,25 +322,49 @@ QtObject:
if(ind == -1): if(ind == -1):
return 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 self.items[ind].displayName = displayName
roles.add(ModelRole.DisplayName.int)
if self.items[ind].ensName != ensName:
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 self.items[ind].isEnsVerified = isEnsVerified
roles.add(ModelRole.IsEnsVerified.int)
if self.items[ind].localNickname != localNickname:
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 self.items[ind].alias = alias
roles.add(ModelRole.Alias.int)
if self.items[ind].icon != icon:
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 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) let index = self.createIndex(ind, 0, nil)
defer: index.delete defer: index.delete
self.dataChanged(index, index, @[ self.dataChanged(index, index, roles)
ModelRole.DisplayName.int,
ModelRole.EnsName.int,
ModelRole.IsEnsVerified.int,
ModelRole.LocalNickname.int,
ModelRole.Alias.int,
ModelRole.Icon.int,
ModelRole.IsUntrustworthy.int,
])
self.itemChanged(pubKey) self.itemChanged(pubKey)
proc updateIncomingRequestStatus*( proc updateIncomingRequestStatus*(