getLocalizedDisplayname method
Returns a localized displayname for this server. If the room is a groupchat without a name, then it will return the localized version of 'Group with Alice' instead of just 'Alice' to make it different to a direct chat. Empty chats will become the localized version of 'Empty Chat'. This method requires a localization class which implements SDNLocalizations
Implementation
String getLocalizedDisplayname([
SDNLocalizations i18n = const SDNDefaultLocalizations(),
]) {
if (name.isNotEmpty) return name;
final canonicalAlias = this.canonicalAlias.localpart;
if (canonicalAlias != null && canonicalAlias.isNotEmpty) {
return canonicalAlias;
}
final directChatSDNID = this.directChatSDNID;
final heroes =
summary.mHeroes ?? (directChatSDNID == null ? [] : [directChatSDNID]);
if (heroes.isNotEmpty) {
final result = heroes
.where((hero) => hero.isNotEmpty)
.map((hero) => unsafeGetUserFromMemoryOrFallback(hero)
.calcDisplayname(i18n: i18n))
.join(', ');
if (isAbandonedDMRoom) {
return i18n.wasDirectChatDisplayName(result);
}
return isDirectChat ? result : i18n.groupWith(result);
}
if (membership == Membership.invite) {
final sender = getState(EventTypes.RoomMember, client.userID!)
?.senderFromMemoryOrFallback
.calcDisplayname(i18n: i18n);
if (sender != null) return sender;
}
if (membership == Membership.leave) {
final invitation = getState(EventTypes.RoomMember, client.userID!);
if (invitation != null &&
invitation.unsigned?.tryGet<String>('prev_sender') != null) {
final name = unsafeGetUserFromMemoryOrFallback(
invitation.unsigned!.tryGet<String>('prev_sender')!)
.calcDisplayname(i18n: i18n);
return i18n.wasDirectChatDisplayName(name);
}
}
return i18n.emptyChat;
}