Merge branch 'henri/make-sender-getter-async' into 'main'

refactor: make sender getter async

Closes #296

See merge request famedly/company/frontend/famedlysdk!1042
This commit is contained in:
Nicolas Werner 2022-06-02 08:13:21 +00:00
commit 311233f20a
10 changed files with 187 additions and 104 deletions

View File

@ -1,3 +1,9 @@
## [0.9.8] - 2nd Jun 2022
- feat: Add search for events in timeline (Krille Fear)
- feat: Add waitForSync helper (Henri Carnot)
- feat: Allow setting image size when generating a thumbnail (Henri Carnot)
- refactr: Make event.sender async (Henri Carnot)
## [0.9.7] - 23rd May 2022 ## [0.9.7] - 23rd May 2022
- ci: use flutter images to install less (Nicolas Werner) - ci: use flutter images to install less (Nicolas Werner)
- feat: implement session export (Lanna Michalke) - feat: implement session export (Lanna Michalke)

View File

@ -37,7 +37,13 @@ abstract class RelationshipTypes {
/// All data exchanged over Matrix is expressed as an "event". Typically each client action (e.g. sending a message) correlates with exactly one event. /// All data exchanged over Matrix is expressed as an "event". Typically each client action (e.g. sending a message) correlates with exactly one event.
class Event extends MatrixEvent { class Event extends MatrixEvent {
User get sender => room.getUserByMXIDSync(senderId); Future<User?> get eventSender async =>
await room.requestUser(senderId, ignoreErrors: true);
@Deprecated(
'Use eventSender instead or senderFromMemoryOrFallback for a synchronous alternative')
User get sender => senderFromMemoryOrFallback;
User get senderFromMemoryOrFallback =>
room.unsafeGetUserFromMemoryOrFallback(senderId);
/// The room this event belongs to. May be null. /// The room this event belongs to. May be null.
final Room room; final Room room;
@ -58,7 +64,9 @@ class Event extends MatrixEvent {
bool get redacted => redactedBecause != null; bool get redacted => redactedBecause != null;
User? get stateKeyUser => room.getUserByMXIDSync(stateKey!); User? get stateKeyUser => stateKey != null
? room.unsafeGetUserFromMemoryOrFallback(stateKey!)
: null;
Event({ Event({
this.status = defaultStatus, this.status = defaultStatus,
@ -300,7 +308,8 @@ class Event extends MatrixEvent {
if (receipt == null) return []; if (receipt == null) return [];
return receipt.content.entries return receipt.content.entries
.where((entry) => entry.value['event_id'] == eventId) .where((entry) => entry.value['event_id'] == eventId)
.map((entry) => Receipt(room.getUserByMXIDSync(entry.key), .map((entry) => Receipt(
room.unsafeGetUserFromMemoryOrFallback(entry.key),
DateTime.fromMillisecondsSinceEpoch(entry.value['ts']))) DateTime.fromMillisecondsSinceEpoch(entry.value['ts'])))
.toList(); .toList();
} }
@ -623,15 +632,58 @@ class Event extends MatrixEvent {
/// plaintextBody instead of the normal body. /// plaintextBody instead of the normal body.
/// [removeMarkdown] allow to remove the markdown formating from the event body. /// [removeMarkdown] allow to remove the markdown formating from the event body.
/// Usefull form message preview or notifications text. /// Usefull form message preview or notifications text.
Future<String> getLocalizedBodyAsync(MatrixLocalizations i18n,
{bool withSenderNamePrefix = false,
bool hideReply = false,
bool hideEdit = false,
bool plaintextBody = false,
bool removeMarkdown = false}) async {
if (redacted) {
await redactedBecause?.eventSender;
}
if (withSenderNamePrefix &&
(type == EventTypes.Message || type.contains(EventTypes.Encrypted))) {
// To be sure that if the event need to be localized, the user is in memory.
// used by EventLocalizations._localizedBodyNormalMessage
await eventSender;
}
return _getLocalizedBody(i18n,
withSenderNamePrefix: withSenderNamePrefix,
hideReply: hideReply,
hideEdit: hideEdit,
plaintextBody: plaintextBody,
removeMarkdown: removeMarkdown);
}
@Deprecated("Use getLocalizedBodyAsync")
String getLocalizedBody(MatrixLocalizations i18n, String getLocalizedBody(MatrixLocalizations i18n,
{bool withSenderNamePrefix = false, {bool withSenderNamePrefix = false,
bool hideReply = false, bool hideReply = false,
bool hideEdit = false, bool hideEdit = false,
bool plaintextBody = false, bool plaintextBody = false,
bool removeMarkdown = false}) { bool removeMarkdown = false}) {
return _getLocalizedBody(i18n,
withSenderNamePrefix: withSenderNamePrefix,
hideReply: hideReply,
hideEdit: hideEdit,
plaintextBody: plaintextBody,
removeMarkdown: removeMarkdown);
}
String _getLocalizedBody(MatrixLocalizations i18n,
{bool withSenderNamePrefix = false,
bool hideReply = false,
bool hideEdit = false,
bool plaintextBody = false,
bool removeMarkdown = false}) {
if (redacted) { if (redacted) {
return i18n.removedBy(redactedBecause?.sender.calcDisplayname() ?? ''); return i18n.removedBy(
(redactedBecause?.senderFromMemoryOrFallback)?.calcDisplayname() ??
senderId);
} }
var body = plaintextBody ? this.plaintextBody : this.body; var body = plaintextBody ? this.plaintextBody : this.body;
// we need to know if the message is an html message to be able to determine // we need to know if the message is an html message to be able to determine
@ -684,7 +736,7 @@ class Event extends MatrixEvent {
textOnlyMessageTypes.contains(messageType)) { textOnlyMessageTypes.contains(messageType)) {
final senderNameOrYou = senderId == room.client.userID final senderNameOrYou = senderId == room.client.userID
? i18n.you ? i18n.you
: (sender.calcDisplayname()); : senderFromMemoryOrFallback.calcDisplayname();
localizedBody = '$senderNameOrYou: $localizedBody'; localizedBody = '$senderNameOrYou: $localizedBody';
} }

View File

@ -263,11 +263,13 @@ class Room {
if (isDirectChat) { if (isDirectChat) {
final user = directChatMatrixID; final user = directChatMatrixID;
if (user != null) { if (user != null) {
return getUserByMXIDSync(user).avatarUrl; return unsafeGetUserFromMemoryOrFallback(user).avatarUrl;
} }
} }
if (membership == Membership.invite) { if (membership == Membership.invite) {
return getState(EventTypes.RoomMember, client.userID!)?.sender.avatarUrl; return getState(EventTypes.RoomMember, client.userID!)
?.senderFromMemoryOrFallback
.avatarUrl;
} }
return null; return null;
} }
@ -356,7 +358,10 @@ class Room {
List<User> get typingUsers { List<User> get typingUsers {
final typingMxid = ephemerals['m.typing']?.content['user_ids']; final typingMxid = ephemerals['m.typing']?.content['user_ids'];
return (typingMxid is List) return (typingMxid is List)
? typingMxid.cast<String>().map(getUserByMXIDSync).toList() ? typingMxid
.cast<String>()
.map(unsafeGetUserFromMemoryOrFallback)
.toList()
: []; : [];
} }
@ -401,18 +406,19 @@ class Room {
if (heroes != null && heroes.isNotEmpty) { if (heroes != null && heroes.isNotEmpty) {
return heroes return heroes
.where((hero) => hero.isNotEmpty) .where((hero) => hero.isNotEmpty)
.map((hero) => getUserByMXIDSync(hero).calcDisplayname()) .map((hero) =>
unsafeGetUserFromMemoryOrFallback(hero).calcDisplayname())
.join(', '); .join(', ');
} }
if (isDirectChat) { if (isDirectChat) {
final user = directChatMatrixID; final user = directChatMatrixID;
if (user != null) { if (user != null) {
return getUserByMXIDSync(user).calcDisplayname(); return unsafeGetUserFromMemoryOrFallback(user).calcDisplayname();
} }
} }
if (membership == Membership.invite) { if (membership == Membership.invite) {
final sender = getState(EventTypes.RoomMember, client.userID!) final sender = getState(EventTypes.RoomMember, client.userID!)
?.sender ?.senderFromMemoryOrFallback
.calcDisplayname(); .calcDisplayname();
if (sender != null) return sender; if (sender != null) return sender;
} }
@ -1009,7 +1015,7 @@ class Room {
if (invitation != null && if (invitation != null &&
invitation.content['is_direct'] is bool && invitation.content['is_direct'] is bool &&
invitation.content['is_direct']) { invitation.content['is_direct']) {
await addToDirectChat(invitation.sender.id); await addToDirectChat(invitation.senderId);
} }
} on MatrixException catch (exception) { } on MatrixException catch (exception) {
if (leaveIfNotFound && if (leaveIfNotFound &&
@ -1410,9 +1416,10 @@ class Room {
(summary.mJoinedMemberCount ?? 0) + (summary.mInvitedMemberCount ?? 0); (summary.mJoinedMemberCount ?? 0) + (summary.mInvitedMemberCount ?? 0);
} }
/// Returns the [User] object for the given [mxID] or requests it from /// Returns the [User] object for the given [mxID] or return
/// the homeserver and returns a default [User] object while waiting. /// a fallback [User] and start a request to get the user
User getUserByMXIDSync(String mxID) { /// from the homeserver.
User unsafeGetUserFromMemoryOrFallback(String mxID) {
final user = getState(EventTypes.RoomMember, mxID); final user = getState(EventTypes.RoomMember, mxID);
if (user != null) { if (user != null) {
return user.asUser; return user.asUser;

View File

@ -20,13 +20,11 @@ import '../matrix.dart';
/// Represents a Matrix User which may be a participant in a Matrix Room. /// Represents a Matrix User which may be a participant in a Matrix Room.
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, required Room room}) {
required Room room,
}) {
return User.fromState( return User.fromState(
stateKey: id, stateKey: id,
content: { content: {

View File

@ -31,17 +31,23 @@ abstract class EventLocalizations {
Event event, MatrixLocalizations i18n, String body) { Event event, MatrixLocalizations i18n, String body) {
switch (event.messageType) { switch (event.messageType) {
case MessageTypes.Image: case MessageTypes.Image:
return i18n.sentAPicture(event.sender.calcDisplayname()); return i18n
.sentAPicture(event.senderFromMemoryOrFallback.calcDisplayname());
case MessageTypes.File: case MessageTypes.File:
return i18n.sentAFile(event.sender.calcDisplayname()); return i18n
.sentAFile(event.senderFromMemoryOrFallback.calcDisplayname());
case MessageTypes.Audio: case MessageTypes.Audio:
return i18n.sentAnAudio(event.sender.calcDisplayname()); return i18n
.sentAnAudio(event.senderFromMemoryOrFallback.calcDisplayname());
case MessageTypes.Video: case MessageTypes.Video:
return i18n.sentAVideo(event.sender.calcDisplayname()); return i18n
.sentAVideo(event.senderFromMemoryOrFallback.calcDisplayname());
case MessageTypes.Location: case MessageTypes.Location:
return i18n.sharedTheLocation(event.sender.calcDisplayname()); return i18n.sharedTheLocation(
event.senderFromMemoryOrFallback.calcDisplayname());
case MessageTypes.Sticker: case MessageTypes.Sticker:
return i18n.sentASticker(event.sender.calcDisplayname()); return i18n
.sentASticker(event.senderFromMemoryOrFallback.calcDisplayname());
case MessageTypes.Emote: case MessageTypes.Emote:
return '* $body'; return '* $body';
case MessageTypes.BadEncrypted: case MessageTypes.BadEncrypted:
@ -78,30 +84,33 @@ abstract class EventLocalizations {
String Function(Event event, MatrixLocalizations i18n, String body)?> String Function(Event event, MatrixLocalizations i18n, String body)?>
localizationsMap = { localizationsMap = {
EventTypes.Sticker: (event, i18n, body) => EventTypes.Sticker: (event, i18n, body) =>
i18n.sentASticker(event.sender.calcDisplayname()), i18n.sentASticker(event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.Redaction: (event, i18n, body) => EventTypes.Redaction: (event, i18n, body) => i18n
i18n.redactedAnEvent(event.sender.calcDisplayname()), .redactedAnEvent(event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.RoomAliases: (event, i18n, body) => EventTypes.RoomAliases: (event, i18n, body) => i18n.changedTheRoomAliases(
i18n.changedTheRoomAliases(event.sender.calcDisplayname()), event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.RoomCanonicalAlias: (event, i18n, body) => EventTypes.RoomCanonicalAlias: (event, i18n, body) =>
i18n.changedTheRoomInvitationLink(event.sender.calcDisplayname()), i18n.changedTheRoomInvitationLink(
event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.RoomCreate: (event, i18n, body) => EventTypes.RoomCreate: (event, i18n, body) =>
i18n.createdTheChat(event.sender.calcDisplayname()), i18n.createdTheChat(event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.RoomTombstone: (event, i18n, body) => i18n.roomHasBeenUpgraded, EventTypes.RoomTombstone: (event, i18n, body) => i18n.roomHasBeenUpgraded,
EventTypes.RoomJoinRules: (event, i18n, body) { EventTypes.RoomJoinRules: (event, i18n, body) {
final joinRules = JoinRules.values.firstWhereOrNull((r) => final joinRules = JoinRules.values.firstWhereOrNull((r) =>
r.toString().replaceAll('JoinRules.', '') == r.toString().replaceAll('JoinRules.', '') ==
event.content['join_rule']); event.content['join_rule']);
if (joinRules == null) { if (joinRules == null) {
return i18n.changedTheJoinRules(event.sender.calcDisplayname()); return i18n.changedTheJoinRules(
event.senderFromMemoryOrFallback.calcDisplayname());
} else { } else {
return i18n.changedTheJoinRulesTo( return i18n.changedTheJoinRulesTo(
event.sender.calcDisplayname(), joinRules.getLocalizedString(i18n)); event.senderFromMemoryOrFallback.calcDisplayname(),
joinRules.getLocalizedString(i18n));
} }
}, },
EventTypes.RoomMember: (event, i18n, body) { EventTypes.RoomMember: (event, i18n, body) {
final targetName = event.stateKeyUser?.calcDisplayname() ?? ''; final targetName = event.stateKeyUser?.calcDisplayname() ?? '';
final senderName = event.sender.calcDisplayname(); final senderName = event.senderFromMemoryOrFallback.calcDisplayname();
final userIsTarget = event.stateKey == event.room.client.userID; final userIsTarget = event.stateKey == event.room.client.userID;
final userIsSender = event.senderId == event.room.client.userID; final userIsSender = event.senderId == event.room.client.userID;
@ -190,22 +199,27 @@ abstract class EventLocalizations {
return text; return text;
}, },
EventTypes.RoomPowerLevels: (event, i18n, body) => EventTypes.RoomPowerLevels: (event, i18n, body) =>
i18n.changedTheChatPermissions(event.sender.calcDisplayname()), i18n.changedTheChatPermissions(
event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.RoomName: (event, i18n, body) => i18n.changedTheChatNameTo( EventTypes.RoomName: (event, i18n, body) => i18n.changedTheChatNameTo(
event.sender.calcDisplayname(), event.content['name']), event.senderFromMemoryOrFallback.calcDisplayname(),
event.content['name']),
EventTypes.RoomTopic: (event, i18n, body) => EventTypes.RoomTopic: (event, i18n, body) =>
i18n.changedTheChatDescriptionTo( i18n.changedTheChatDescriptionTo(
event.sender.calcDisplayname(), event.content['topic']), event.senderFromMemoryOrFallback.calcDisplayname(),
EventTypes.RoomAvatar: (event, i18n, body) => event.content['topic']),
i18n.changedTheChatAvatar(event.sender.calcDisplayname()), EventTypes.RoomAvatar: (event, i18n, body) => i18n.changedTheChatAvatar(
event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.GuestAccess: (event, i18n, body) { EventTypes.GuestAccess: (event, i18n, body) {
final guestAccess = GuestAccess.values.firstWhereOrNull((r) => final guestAccess = GuestAccess.values.firstWhereOrNull((r) =>
r.toString().replaceAll('GuestAccess.', '') == r.toString().replaceAll('GuestAccess.', '') ==
event.content['guest_access']); event.content['guest_access']);
if (guestAccess == null) { if (guestAccess == null) {
return i18n.changedTheGuestAccessRules(event.sender.calcDisplayname()); return i18n.changedTheGuestAccessRules(
event.senderFromMemoryOrFallback.calcDisplayname());
} else { } else {
return i18n.changedTheGuestAccessRulesTo(event.sender.calcDisplayname(), return i18n.changedTheGuestAccessRulesTo(
event.senderFromMemoryOrFallback.calcDisplayname(),
guestAccess.getLocalizedString(i18n)); guestAccess.getLocalizedString(i18n));
} }
}, },
@ -214,35 +228,36 @@ abstract class EventLocalizations {
r.toString().replaceAll('HistoryVisibility.', '') == r.toString().replaceAll('HistoryVisibility.', '') ==
event.content['history_visibility']); event.content['history_visibility']);
if (historyVisibility == null) { if (historyVisibility == null) {
return i18n.changedTheHistoryVisibility(event.sender.calcDisplayname()); return i18n.changedTheHistoryVisibility(
event.senderFromMemoryOrFallback.calcDisplayname());
} else { } else {
return i18n.changedTheHistoryVisibilityTo( return i18n.changedTheHistoryVisibilityTo(
event.sender.calcDisplayname(), event.senderFromMemoryOrFallback.calcDisplayname(),
historyVisibility.getLocalizedString(i18n)); historyVisibility.getLocalizedString(i18n));
} }
}, },
EventTypes.Encryption: (event, i18n, body) { EventTypes.Encryption: (event, i18n, body) {
var localizedBody = var localizedBody = i18n.activatedEndToEndEncryption(
i18n.activatedEndToEndEncryption(event.sender.calcDisplayname()); event.senderFromMemoryOrFallback.calcDisplayname());
if (event.room.client.encryptionEnabled == false) { if (event.room.client.encryptionEnabled == false) {
localizedBody += '. ' + i18n.needPantalaimonWarning; localizedBody += '. ' + i18n.needPantalaimonWarning;
} }
return localizedBody; return localizedBody;
}, },
EventTypes.CallAnswer: (event, i18n, body) => EventTypes.CallAnswer: (event, i18n, body) => i18n
i18n.answeredTheCall(event.sender.calcDisplayname()), .answeredTheCall(event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.CallHangup: (event, i18n, body) => EventTypes.CallHangup: (event, i18n, body) =>
i18n.endedTheCall(event.sender.calcDisplayname()), i18n.endedTheCall(event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.CallInvite: (event, i18n, body) => EventTypes.CallInvite: (event, i18n, body) =>
i18n.startedACall(event.sender.calcDisplayname()), i18n.startedACall(event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.CallCandidates: (event, i18n, body) => EventTypes.CallCandidates: (event, i18n, body) => i18n.sentCallInformations(
i18n.sentCallInformations(event.sender.calcDisplayname()), event.senderFromMemoryOrFallback.calcDisplayname()),
EventTypes.Encrypted: (event, i18n, body) => EventTypes.Encrypted: (event, i18n, body) =>
_localizedBodyNormalMessage(event, i18n, body), _localizedBodyNormalMessage(event, i18n, body),
EventTypes.Message: (event, i18n, body) => EventTypes.Message: (event, i18n, body) =>
_localizedBodyNormalMessage(event, i18n, body), _localizedBodyNormalMessage(event, i18n, body),
EventTypes.Reaction: (event, i18n, body) => i18n.sentReaction( EventTypes.Reaction: (event, i18n, body) => i18n.sentReaction(
event.sender.calcDisplayname(), event.senderFromMemoryOrFallback.calcDisplayname(),
event.content event.content
.tryGetMap<String, dynamic>('m.relates_to') .tryGetMap<String, dynamic>('m.relates_to')
?.tryGet<String>('key') ?? ?.tryGet<String>('key') ??

View File

@ -92,7 +92,7 @@ class WrappedMediaStream {
String? get displayName => getUser().displayName; String? get displayName => getUser().displayName;
User getUser() { User getUser() {
return room.getUserByMXIDSync(userId); return room.unsafeGetUserFromMemoryOrFallback(userId);
} }
bool isLocal() { bool isLocal() {
@ -1240,7 +1240,8 @@ class VoIP {
final newCall = createNewCall(opts); final newCall = createNewCall(opts);
newCall.remotePartyId = partyId; newCall.remotePartyId = partyId;
newCall.remoteUser = event.sender; newCall.remoteUser =
(await event.eventSender) ?? User(event.senderId, room: event.room);
final offer = RTCSessionDescription( final offer = RTCSessionDescription(
event.content['offer']['sdp'], event.content['offer']['sdp'],
event.content['offer']['type'], event.content['offer']['type'],
@ -1284,7 +1285,8 @@ class VoIP {
} }
call.remotePartyId = partyId; call.remotePartyId = partyId;
call.remoteUser = event.sender; call.remoteUser =
await event.eventSender ?? User(event.senderId, room: event.room);
final answer = RTCSessionDescription( final answer = RTCSessionDescription(
event.content['answer']['sdp'], event.content['answer']['type']); event.content['answer']['sdp'], event.content['answer']['type']);

View File

@ -1,6 +1,6 @@
name: matrix name: matrix
description: Matrix Dart SDK description: Matrix Dart SDK
version: 0.9.7 version: 0.9.8
homepage: https://famedly.com homepage: https://famedly.com
repository: https://gitlab.com/famedly/company/frontend/famedlysdk.git repository: https://gitlab.com/famedly/company/frontend/famedlysdk.git

View File

@ -964,7 +964,7 @@ void main() {
expect(event?.eventId, '123'); expect(event?.eventId, '123');
expect(event?.body, 'Hello world'); expect(event?.body, 'Hello world');
expect(event?.senderId, '@alicyy:example.com'); expect(event?.senderId, '@alicyy:example.com');
expect(event?.sender.calcDisplayname(), 'AlicE'); expect(event?.senderFromMemoryOrFallback.calcDisplayname(), 'AlicE');
expect(event?.type, 'm.room.message'); expect(event?.type, 'm.room.message');
expect(event?.messageType, 'm.text'); expect(event?.messageType, 'm.text');
expect(event?.room.id, '!localpart2:server.abc'); expect(event?.room.id, '!localpart2:server.abc');

View File

@ -362,7 +362,7 @@ void main() {
} }
} }
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Removed by Example'); 'Removed by Example');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -429,7 +429,7 @@ void main() {
'type': 'm.sticker', 'type': 'm.sticker',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example sent a sticker'); 'Example sent a sticker');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -443,7 +443,7 @@ void main() {
'type': 'm.room.redaction', 'type': 'm.room.redaction',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example redacted an event'); 'Example redacted an event');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -459,7 +459,7 @@ void main() {
'type': 'm.room.aliases', 'type': 'm.room.aliases',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example changed the room aliases'); 'Example changed the room aliases');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -475,7 +475,7 @@ void main() {
'type': 'm.room.aliases', 'type': 'm.room.aliases',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example changed the room aliases'); 'Example changed the room aliases');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -489,7 +489,7 @@ void main() {
'type': 'm.room.canonical_alias', 'type': 'm.room.canonical_alias',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example changed the room invitation link'); 'Example changed the room invitation link');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -511,7 +511,7 @@ void main() {
'type': 'm.room.create', 'type': 'm.room.create',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example created the chat'); 'Example created the chat');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -528,7 +528,7 @@ void main() {
'type': 'm.room.tombstone', 'type': 'm.room.tombstone',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Room has been upgraded'); 'Room has been upgraded');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -542,7 +542,7 @@ void main() {
'type': 'm.room.join_rules', 'type': 'm.room.join_rules',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example changed the join rules to Anyone can join'); 'Example changed the join rules to Anyone can join');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -560,7 +560,7 @@ void main() {
'type': 'm.room.member', 'type': 'm.room.member',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Alice joined the chat'); 'Alice joined the chat');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -573,7 +573,7 @@ void main() {
'state_key': '@alice:example.org', 'state_key': '@alice:example.org',
'type': 'm.room.member' 'type': 'm.room.member'
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example has invited Alice'); 'Example has invited Alice');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -589,7 +589,7 @@ void main() {
'prev_content': {'membership': 'join'}, 'prev_content': {'membership': 'join'},
} }
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example kicked Alice'); 'Example kicked Alice');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -605,7 +605,7 @@ void main() {
'prev_content': {'membership': 'join'}, 'prev_content': {'membership': 'join'},
} }
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example banned Alice'); 'Example banned Alice');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -621,7 +621,7 @@ void main() {
'prev_content': {'membership': 'invite'}, 'prev_content': {'membership': 'invite'},
} }
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Alice accepted the invitation'); 'Alice accepted the invitation');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -637,7 +637,7 @@ void main() {
'prev_content': {'membership': 'join'}, 'prev_content': {'membership': 'join'},
} }
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example has invited Alice'); 'Example has invited Alice');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -653,7 +653,7 @@ void main() {
'prev_content': {'membership': 'invite'}, 'prev_content': {'membership': 'invite'},
} }
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example has withdrawn the invitation for Alice'); 'Example has withdrawn the invitation for Alice');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -669,7 +669,7 @@ void main() {
'prev_content': {'membership': 'invite'}, 'prev_content': {'membership': 'invite'},
} }
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Alice rejected the invitation'); 'Alice rejected the invitation');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -694,7 +694,7 @@ void main() {
'type': 'm.room.power_levels', 'type': 'm.room.power_levels',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example changed the chat permissions'); 'Example changed the chat permissions');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -708,7 +708,7 @@ void main() {
'type': 'm.room.name', 'type': 'm.room.name',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example changed the chat name to The room name'); 'Example changed the chat name to The room name');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -722,7 +722,7 @@ void main() {
'type': 'm.room.topic', 'type': 'm.room.topic',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example changed the chat description to A room topic'); 'Example changed the chat description to A room topic');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -739,7 +739,7 @@ void main() {
'type': 'm.room.avatar', 'type': 'm.room.avatar',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example changed the chat avatar'); 'Example changed the chat avatar');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -753,7 +753,7 @@ void main() {
'type': 'm.room.history_visibility', 'type': 'm.room.history_visibility',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example changed the history visibility to Visible for all participants'); 'Example changed the history visibility to Visible for all participants');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -771,7 +771,7 @@ void main() {
'type': 'm.room.encryption', 'type': 'm.room.encryption',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example activated end to end encryption. Need pantalaimon'); 'Example activated end to end encryption. Need pantalaimon');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -789,7 +789,7 @@ void main() {
'type': 'm.room.message', 'type': 'm.room.message',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'This is an example text message'); 'This is an example text message');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -807,7 +807,7 @@ void main() {
'type': 'm.room.message', 'type': 'm.room.message',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'* thinks this is an example emote'); '* thinks this is an example emote');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -825,7 +825,7 @@ void main() {
'type': 'm.room.message', 'type': 'm.room.message',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'This is an example notice'); 'This is an example notice');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -843,7 +843,7 @@ void main() {
'type': 'm.room.message', 'type': 'm.room.message',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example sent a picture'); 'Example sent a picture');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -862,7 +862,7 @@ void main() {
'type': 'm.room.message', 'type': 'm.room.message',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example sent a file'); 'Example sent a file');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -884,7 +884,7 @@ void main() {
'type': 'm.room.message', 'type': 'm.room.message',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example sent an audio'); 'Example sent an audio');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -910,7 +910,7 @@ void main() {
'type': 'm.room.message', 'type': 'm.room.message',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example shared the location'); 'Example shared the location');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -941,7 +941,7 @@ void main() {
'type': 'm.room.message', 'type': 'm.room.message',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Example sent a video'); 'Example sent a video');
expect(event.isEventTypeKnown, true); expect(event.isEventTypeKnown, true);
@ -954,12 +954,12 @@ void main() {
'type': 'unknown.event.type', 'type': 'unknown.event.type',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect(event.getLocalizedBody(MatrixDefaultLocalizations()), expect(await event.getLocalizedBodyAsync(MatrixDefaultLocalizations()),
'Unknown event unknown.event.type'); 'Unknown event unknown.event.type');
expect(event.isEventTypeKnown, false); expect(event.isEventTypeKnown, false);
}); });
test('getLocalizedBody, parameters', () { test('getLocalizedBody, parameters', () async {
final matrix = Client('testclient', httpClient: FakeMatrixApi()); final matrix = Client('testclient', httpClient: FakeMatrixApi());
final room = Room(id: '!1234:example.com', client: matrix); final room = Room(id: '!1234:example.com', client: matrix);
var event = Event.fromJson({ var event = Event.fromJson({
@ -977,7 +977,7 @@ void main() {
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect( expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), await event.getLocalizedBodyAsync(MatrixDefaultLocalizations(),
plaintextBody: true), plaintextBody: true),
'**This is an example text message**'); '**This is an example text message**');
@ -1006,10 +1006,11 @@ void main() {
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect( expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), hideEdit: true), await event.getLocalizedBodyAsync(MatrixDefaultLocalizations(),
hideEdit: true),
'This is an example text message'); 'This is an example text message');
expect( expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), await event.getLocalizedBodyAsync(MatrixDefaultLocalizations(),
hideEdit: true, plaintextBody: true), hideEdit: true, plaintextBody: true),
'**This is an example text message**'); '**This is an example text message**');
@ -1028,10 +1029,11 @@ void main() {
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect( expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), hideReply: true), await event.getLocalizedBodyAsync(MatrixDefaultLocalizations(),
hideReply: true),
'hmm, fox'); 'hmm, fox');
expect( expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), await event.getLocalizedBodyAsync(MatrixDefaultLocalizations(),
hideReply: true, plaintextBody: true), hideReply: true, plaintextBody: true),
'hmm, *fox*'); 'hmm, *fox*');
@ -1052,19 +1054,19 @@ void main() {
'unsigned': {'age': 1234} 'unsigned': {'age': 1234}
}, room); }, room);
expect( expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), await event.getLocalizedBodyAsync(MatrixDefaultLocalizations(),
removeMarkdown: true), removeMarkdown: true),
'Title\nsome text and link\nokay and this is important'); 'Title\nsome text and link\nokay and this is important');
expect( expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), await event.getLocalizedBodyAsync(MatrixDefaultLocalizations(),
removeMarkdown: true, plaintextBody: true), removeMarkdown: true, plaintextBody: true),
'Title\nsome text and 🔗link\nokay and this is important'); 'Title\nsome text and 🔗link\nokay and this is important');
expect( expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), await event.getLocalizedBodyAsync(MatrixDefaultLocalizations(),
removeMarkdown: true, withSenderNamePrefix: true), removeMarkdown: true, withSenderNamePrefix: true),
'Example: Title\nsome text and link\nokay and this is important'); 'Example: Title\nsome text and link\nokay and this is important');
expect( expect(
event.getLocalizedBody(MatrixDefaultLocalizations(), await event.getLocalizedBodyAsync(MatrixDefaultLocalizations(),
removeMarkdown: true, removeMarkdown: true,
plaintextBody: true, plaintextBody: true,
withSenderNamePrefix: true), withSenderNamePrefix: true),

View File

@ -103,7 +103,8 @@ void main() {
expect(removeList, []); expect(removeList, []);
expect(timeline.events.length, 2); expect(timeline.events.length, 2);
expect(timeline.events[0].eventId, '1'); expect(timeline.events[0].eventId, '1');
expect(timeline.events[0].sender.id, '@alice:example.com'); expect(timeline.events[0].senderFromMemoryOrFallback.id,
'@alice:example.com');
expect(timeline.events[0].originServerTs.millisecondsSinceEpoch, expect(timeline.events[0].originServerTs.millisecondsSinceEpoch,
testTimeStamp); testTimeStamp);
expect(timeline.events[0].body, 'Testcase'); expect(timeline.events[0].body, 'Testcase');