refactor: make user nullsafe

This commit is contained in:
Nicolas Werner 2021-10-13 17:41:00 +02:00
parent 978c3bb994
commit 8146aa05b8
1 changed files with 22 additions and 22 deletions

View File

@ -1,4 +1,3 @@
// @dart=2.9
/* /*
* Famedly Matrix SDK * Famedly Matrix SDK
* Copyright (C) 2019, 2020, 2021 Famedly GmbH * Copyright (C) 2019, 2020, 2021 Famedly GmbH
@ -26,10 +25,10 @@ import 'room.dart';
class User extends Event { class User extends Event {
factory User( factory User(
String id, { String id, {
String membership, String? membership,
String displayName, String? displayName,
String avatarUrl, String? avatarUrl,
Room room, Room? room,
}) { }) {
return User.fromState( return User.fromState(
stateKey: id, stateKey: id,
@ -47,15 +46,15 @@ class User extends Event {
User.fromState( User.fromState(
{dynamic prevContent, {dynamic prevContent,
String stateKey, required String stateKey,
dynamic content, dynamic content,
String typeKey, required String typeKey,
String eventId, String? eventId,
String roomId, String? roomId,
String senderId, String? senderId,
DateTime originServerTs, required DateTime originServerTs,
dynamic unsigned, dynamic unsigned,
Room room}) Room? room})
: super( : super(
stateKey: stateKey, stateKey: stateKey,
prevContent: prevContent, prevContent: prevContent,
@ -72,12 +71,12 @@ class User extends Event {
String get id => stateKey; String get id => stateKey;
/// The displayname of the user if the user has set one. /// The displayname of the user if the user has set one.
String get displayName => String? get displayName =>
content?.tryGet<String>('displayname') ?? content?.tryGet<String>('displayname') ??
prevContent?.tryGet<String>('displayname'); prevContent?.tryGet<String>('displayname');
/// Returns the power level of this user. /// Returns the power level of this user.
int get powerLevel => room?.getPowerLevelByUserId(id); int get powerLevel => room?.getPowerLevelByUserId(id) ?? 0;
/// The membership status of the user. One of: /// The membership status of the user. One of:
/// join /// join
@ -92,7 +91,7 @@ class User extends Event {
}, orElse: () => Membership.join); }, orElse: () => Membership.join);
/// The avatar if the user has one. /// The avatar if the user has one.
Uri get avatarUrl => content != null && content.containsKey('avatar_url') Uri? get avatarUrl => content != null && content.containsKey('avatar_url')
? (content['avatar_url'] is String ? (content['avatar_url'] is String
? Uri.tryParse(content['avatar_url']) ? Uri.tryParse(content['avatar_url'])
: null) : null)
@ -107,8 +106,8 @@ class User extends Event {
/// If [mxidLocalPartFallback] is true, then the local part of the mxid will be shown /// If [mxidLocalPartFallback] is true, then the local part of the mxid will be shown
/// if there is no other displayname available. If not then this will return "Unknown user". /// if there is no other displayname available. If not then this will return "Unknown user".
String calcDisplayname({ String calcDisplayname({
bool formatLocalpart, bool? formatLocalpart,
bool mxidLocalPartFallback, bool? mxidLocalPartFallback,
}) { }) {
formatLocalpart ??= room?.client?.formatLocalpart ?? true; formatLocalpart ??= room?.client?.formatLocalpart ?? true;
mxidLocalPartFallback ??= room?.client?.mxidLocalPartFallback ?? true; mxidLocalPartFallback ??= room?.client?.mxidLocalPartFallback ?? true;
@ -119,9 +118,9 @@ class User extends Event {
final stateKey = this.stateKey; final stateKey = this.stateKey;
if (stateKey != null && mxidLocalPartFallback) { if (stateKey != null && mxidLocalPartFallback) {
if (!formatLocalpart) { if (!formatLocalpart) {
return stateKey.localpart; return stateKey.localpart ?? '';
} }
final words = stateKey.localpart.replaceAll('_', ' ').split(' '); final words = stateKey.localpart?.replaceAll('_', ' ').split(' ') ?? [];
for (var i = 0; i < words.length; i++) { for (var i = 0; i < words.length; i++) {
if (words[i].isNotEmpty) { if (words[i].isNotEmpty) {
words[i] = words[i][0].toUpperCase() + words[i].substring(1); words[i] = words[i][0].toUpperCase() + words[i].substring(1);
@ -146,10 +145,10 @@ class User extends Event {
/// Returns an existing direct chat ID with this user or creates a new one. /// Returns an existing direct chat ID with this user or creates a new one.
/// Returns null on error. /// Returns null on error.
Future<String> startDirectChat() => room.client.startDirectChat(id); Future<String?> startDirectChat() => room.client.startDirectChat(id);
/// The newest presence of this user if there is any and null if not. /// The newest presence of this user if there is any and null if not.
Presence get presence => room.client.presences[id]; Presence? get presence => room.client.presences[id];
/// Whether the client is able to ban/unban this user. /// Whether the client is able to ban/unban this user.
bool get canBan => room.canBan && powerLevel < room.ownPowerLevel; bool get canBan => room.canBan && powerLevel < room.ownPowerLevel;
@ -228,5 +227,6 @@ class User extends Event {
} }
} }
const _maximumHashLength = 10000;
String _hash(String s) => String _hash(String s) =>
(s.codeUnits.fold<int>(0, (a, b) => a + b) % 10000).toString(); (s.codeUnits.fold<int>(0, (a, b) => a + b) % _maximumHashLength).toString();