getLocalizedDisplayname method

String getLocalizedDisplayname([
  1. MatrixLocalizations i18n = const MatrixDefaultLocalizations()
])

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'. Please note, that necessary room members are lazy loaded. To be sure that you have the room members, call and await Room.loadHeroUsers() before. This method requires a localization class which implements MatrixLocalizations

Implementation

String getLocalizedDisplayname([
  MatrixLocalizations i18n = const MatrixDefaultLocalizations(),
]) {
  if (name.isNotEmpty) return name;

  final canonicalAlias = this.canonicalAlias.localpart;
  if (canonicalAlias != null && canonicalAlias.isNotEmpty) {
    return canonicalAlias;
  }

  final directChatMatrixID = this.directChatMatrixID;
  final heroes = summary.mHeroes ?? [];
  if (directChatMatrixID != null && heroes.isEmpty) {
    heroes.add(directChatMatrixID);
  }
  if (heroes.isNotEmpty) {
    final result = heroes
        .where(
          // removing oneself from the hero list
          (hero) => hero.isNotEmpty && hero != client.userID,
        )
        .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 ownMember = unsafeGetUserFromMemoryOrFallback(client.userID!);

    if (ownMember.senderId != ownMember.stateKey) {
      return i18n.invitedBy(
        unsafeGetUserFromMemoryOrFallback(ownMember.senderId)
            .calcDisplayname(i18n: i18n),
      );
    }
  }
  if (membership == Membership.leave) {
    if (directChatMatrixID != null) {
      return i18n.wasDirectChatDisplayName(
        unsafeGetUserFromMemoryOrFallback(directChatMatrixID)
            .calcDisplayname(i18n: i18n),
      );
    }
  }
  return i18n.emptyChat;
}