refactor: prepare null safefy for user.dart
With this commit, the migration tool needs to do 8 fewer changes.
This commit is contained in:
parent
f811e45102
commit
3130139bde
|
|
@ -31,13 +31,13 @@ class User extends Event {
|
||||||
String avatarUrl,
|
String avatarUrl,
|
||||||
Room room,
|
Room room,
|
||||||
}) {
|
}) {
|
||||||
final content = <String, String>{};
|
|
||||||
if (membership != null) content['membership'] = membership;
|
|
||||||
if (displayName != null) content['displayname'] = displayName;
|
|
||||||
if (avatarUrl != null) content['avatar_url'] = avatarUrl;
|
|
||||||
return User.fromState(
|
return User.fromState(
|
||||||
stateKey: id,
|
stateKey: id,
|
||||||
content: content,
|
content: {
|
||||||
|
if (membership != null) 'membership': membership,
|
||||||
|
if (displayName != null) 'displayname': displayName,
|
||||||
|
if (avatarUrl != null) 'avatar_url': avatarUrl,
|
||||||
|
},
|
||||||
typeKey: EventTypes.RoomMember,
|
typeKey: EventTypes.RoomMember,
|
||||||
roomId: room?.id,
|
roomId: room?.id,
|
||||||
room: room,
|
room: room,
|
||||||
|
|
@ -112,9 +112,11 @@ class User extends Event {
|
||||||
}) {
|
}) {
|
||||||
formatLocalpart ??= room?.client?.formatLocalpart ?? true;
|
formatLocalpart ??= room?.client?.formatLocalpart ?? true;
|
||||||
mxidLocalPartFallback ??= room?.client?.mxidLocalPartFallback ?? true;
|
mxidLocalPartFallback ??= room?.client?.mxidLocalPartFallback ?? true;
|
||||||
if (displayName?.isNotEmpty ?? false) {
|
final displayName = this.displayName;
|
||||||
|
if (displayName != null && displayName.isNotEmpty) {
|
||||||
return displayName;
|
return displayName;
|
||||||
}
|
}
|
||||||
|
final stateKey = this.stateKey;
|
||||||
if (stateKey != null && mxidLocalPartFallback) {
|
if (stateKey != null && mxidLocalPartFallback) {
|
||||||
if (!formatLocalpart) {
|
if (!formatLocalpart) {
|
||||||
return stateKey.localpart;
|
return stateKey.localpart;
|
||||||
|
|
@ -175,18 +177,18 @@ class User extends Event {
|
||||||
// if the displayname has [ or ] or : we can't build our more fancy stuff, so fall back to the id
|
// if the displayname has [ or ] or : we can't build our more fancy stuff, so fall back to the id
|
||||||
// [] is used for the delimitors
|
// [] is used for the delimitors
|
||||||
// If we allowed : we could get collissions with the mxid fallbacks
|
// If we allowed : we could get collissions with the mxid fallbacks
|
||||||
if ((displayName?.isEmpty ?? true) ||
|
final displayName = this.displayName;
|
||||||
{'[', ']', ':'}.any((c) => displayName.contains(c))) {
|
if (displayName == null ||
|
||||||
|
displayName.isEmpty ||
|
||||||
|
{'[', ']', ':'}.any(displayName.contains)) {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
var identifier = '@';
|
final identifier = '@' +
|
||||||
// if we have non-word characters we need to surround with []
|
// if we have non-word characters we need to surround with []
|
||||||
if (!RegExp(r'^\w+$').hasMatch(displayName)) {
|
(RegExp(r'^\w+$').hasMatch(displayName)
|
||||||
identifier += '[$displayName]';
|
? displayName
|
||||||
} else {
|
: '[$displayName]');
|
||||||
identifier += displayName;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get all the users with the same display name
|
// get all the users with the same display name
|
||||||
final allUsersWithSameDisplayname = room.getParticipants();
|
final allUsersWithSameDisplayname = room.getParticipants();
|
||||||
|
|
@ -209,26 +211,22 @@ class User extends Event {
|
||||||
|
|
||||||
/// Get the mention fragments for this user.
|
/// Get the mention fragments for this user.
|
||||||
Set<String> get mentionFragments {
|
Set<String> get mentionFragments {
|
||||||
if ((displayName?.isEmpty ?? true) ||
|
final displayName = this.displayName;
|
||||||
{'[', ']', ':'}.any((c) => displayName.contains(c))) {
|
if (displayName == null ||
|
||||||
|
displayName.isEmpty ||
|
||||||
|
{'[', ']', ':'}.any(displayName.contains)) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
var identifier = '@';
|
final identifier = '@' +
|
||||||
// if we have non-word characters we need to surround with []
|
// if we have non-word characters we need to surround with []
|
||||||
if (!RegExp(r'^\w+$').hasMatch(displayName)) {
|
(RegExp(r'^\w+$').hasMatch(displayName)
|
||||||
identifier += '[$displayName]';
|
? displayName
|
||||||
} else {
|
: '[$displayName]');
|
||||||
identifier += displayName;
|
|
||||||
}
|
|
||||||
final hash = _hash(id);
|
final hash = _hash(id);
|
||||||
return {identifier, '$identifier#$hash'};
|
return {identifier, '$identifier#$hash'};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String _hash(String s) {
|
String _hash(String s) =>
|
||||||
var number = 0;
|
(s.codeUnits.fold<int>(0, (a, b) => a + b) % 10000).toString();
|
||||||
for (var i = 0; i < s.length; i++) {
|
|
||||||
number += s.codeUnitAt(i);
|
|
||||||
}
|
|
||||||
return (number % 10000).toString();
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue