refactor: Rename methods and get rid of all Future getter

This commit is contained in:
Christian Pauly 2022-06-08 08:30:05 +02:00
parent a38d726c6b
commit 58cfd1f688
9 changed files with 71 additions and 57 deletions

View File

@ -91,7 +91,7 @@ class MatrixWidget {
// See https://github.com/matrix-org/matrix-doc/issues/1236 for a
// description, specifically the section
// `What does the other stuff in content mean?`
final userProfile = await room.client.ownProfile;
final userProfile = await room.client.fetchOwnProfile();
var parsedUri = url;
// a key-value map with the strings to be replaced

View File

@ -707,11 +707,14 @@ class Client extends MatrixApi {
return id;
}
@Deprecated('Use fetchOwnProfile() instead')
Future<Profile> get ownProfile => fetchOwnProfile();
/// Returns the user's own displayname and avatar url. In Matrix it is possible that
/// one user can have different displaynames and avatar urls in different rooms. So
/// this endpoint first checks if the profile is the same in all rooms. If not, the
/// profile will be requested from the homserver.
Future<Profile> get ownProfile async {
Future<Profile> fetchOwnProfile() async {
if (rooms.isNotEmpty) {
final profileSet = <Profile>{};
for (final room in rooms) {

View File

@ -37,8 +37,12 @@ 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.
class Event extends MatrixEvent {
Future<User?> get eventSender async =>
await room.requestUser(senderId, ignoreErrors: true);
/// Requests the user object of the sender of this event.
Future<User?> fetchSenderUser() => room.requestUser(
senderId,
ignoreErrors: true,
);
@Deprecated(
'Use eventSender instead or senderFromMemoryOrFallback for a synchronous alternative')
User get sender => senderFromMemoryOrFallback;
@ -632,21 +636,21 @@ class Event extends MatrixEvent {
/// plaintextBody instead of the normal body.
/// [removeMarkdown] allow to remove the markdown formating from the event body.
/// Usefull form message preview or notifications text.
Future<String> getLocalizedBodyAsync(MatrixLocalizations i18n,
Future<String> calcLocalizedBody(MatrixLocalizations i18n,
{bool withSenderNamePrefix = false,
bool hideReply = false,
bool hideEdit = false,
bool plaintextBody = false,
bool removeMarkdown = false}) async {
if (redacted) {
await redactedBecause?.eventSender;
await redactedBecause?.fetchSenderUser();
}
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;
await fetchSenderUser();
}
return _getLocalizedBody(i18n,
@ -657,7 +661,7 @@ class Event extends MatrixEvent {
removeMarkdown: removeMarkdown);
}
@Deprecated('Use getLocalizedBodyAsync')
@Deprecated('Use calcLocalizedBody')
String getLocalizedBody(MatrixLocalizations i18n,
{bool withSenderNamePrefix = false,
bool hideReply = false,

View File

@ -525,9 +525,12 @@ class Room {
/// in muted rooms, use [hasNewMessages].
bool get isUnread => notificationCount > 0 || markedUnread;
@Deprecated('Use waitForRoomInSync() instead')
Future<SyncUpdate> get waitForSync => waitForRoomInSync();
/// Wait for the room to appear in join, leave or invited section of the
/// sync.
Future<SyncUpdate> get waitForSync async {
Future<SyncUpdate> waitForRoomInSync() async {
return await client.waitForRoomInSync(id);
}

View File

@ -159,8 +159,11 @@ class User extends Event {
@Deprecated('Deprecated in favour of currentPresence.')
Presence? get presence => room.client.presences[id]?.toPresence();
@Deprecated('Use fetchCurrentPresence() instead')
Future<CachedPresence> get currentPresence => fetchCurrentPresence();
/// The newest presence of this user if there is any. Fetches it from the server if necessary or returns offline.
Future<CachedPresence> get currentPresence async {
Future<CachedPresence> fetchCurrentPresence() async {
final cachedPresence = room.client.presences[id];
if (cachedPresence != null) {
return cachedPresence;

View File

@ -1240,8 +1240,8 @@ class VoIP {
final newCall = createNewCall(opts);
newCall.remotePartyId = partyId;
newCall.remoteUser =
(await event.eventSender) ?? User(event.senderId, room: event.room);
newCall.remoteUser = (await event.fetchSenderUser()) ??
User(event.senderId, room: event.room);
final offer = RTCSessionDescription(
event.content['offer']['sdp'],
event.content['offer']['type'],
@ -1285,8 +1285,8 @@ class VoIP {
}
call.remotePartyId = partyId;
call.remoteUser =
await event.eventSender ?? User(event.senderId, room: event.room);
call.remoteUser = await event.fetchSenderUser() ??
User(event.senderId, room: event.room);
final answer = RTCSessionDescription(
event.content['answer']['sdp'], event.content['answer']['type']);

File diff suppressed because one or more lines are too long

View File

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

View File

@ -131,7 +131,8 @@ void main() {
]
}
}));
expect((await user1.currentPresence).presence, PresenceType.online);
expect(
(await user1.fetchCurrentPresence()).presence, PresenceType.online);
});
test('canBan', () async {
expect(user1.canBan, false);