Skip to content

Commit

Permalink
refactor: Do not use eventupdate type for verification requests
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Jan 7, 2025
1 parent f2e2ee8 commit e7065af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
20 changes: 10 additions & 10 deletions lib/encryption/encryption.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,20 @@ class Encryption {
}
}

Future<void> handleEventUpdate(EventUpdate update) async {
if (update.type == EventUpdateType.history) {
Future<void> handleEventUpdate(Event event, EventUpdateType type) async {
if (type == EventUpdateType.history) {
return;
}
if (update.content['type'].startsWith('m.key.verification.') ||
(update.content['type'] == EventTypes.Message &&
(update.content['content']['msgtype'] is String) &&
update.content['content']['msgtype']
.startsWith('m.key.verification.'))) {
if (event.type.startsWith('m.key.verification.') ||
(event.type == EventTypes.Message &&
event.content
.tryGet<String>('msgtype')
?.startsWith('m.key.verification.') ==
true)) {
// "just" key verification, no need to do this in sync
runInRoot(() => keyVerificationManager.handleEventUpdate(update));
runInRoot(() => keyVerificationManager.handleEventUpdate(event));
}
if (update.content['sender'] == client.userID &&
update.content['unsigned']?['transaction_id'] == null) {
if (event.senderId == client.userID && event.status.isSynced) {
// maybe we need to re-try SSSS secrets
runInRoot(() => ssss.periodicallyRequestMissingCache());
}
Expand Down
34 changes: 17 additions & 17 deletions lib/encryption/key_verification_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,52 +88,52 @@ class KeyVerificationManager {
}
}

Future<void> handleEventUpdate(EventUpdate update) async {
final event = update.content;
final type = event['type'].startsWith('m.key.verification.')
? event['type']
: event['content']['msgtype'];
Future<void> handleEventUpdate(Event update) async {
final type = update.type.startsWith('m.key.verification.')
? update.type
: update.content.tryGet<String>('msgtype');
if (type == null ||
!type.startsWith('m.key.verification.') ||
client.verificationMethods.isEmpty) {
return;
}
if (type == EventTypes.KeyVerificationRequest) {
event['content']['timestamp'] = event['origin_server_ts'];
update.content['timestamp'] =
update.originServerTs.millisecondsSinceEpoch;
}

final transactionId =
KeyVerification.getTransactionId(event['content']) ?? event['event_id'];
KeyVerification.getTransactionId(update.content) ?? update.eventId;

final req = _requests[transactionId];
if (req != null) {
final otherDeviceId = event['content']['from_device'];
if (event['sender'] != client.userID) {
await req.handlePayload(type, event['content'], event['event_id']);
} else if (event['sender'] == client.userID &&
final otherDeviceId = update.content.tryGet<String>('from_device');
if (update.senderId != client.userID) {
await req.handlePayload(type, update.content, update.eventId);
} else if (update.senderId == client.userID &&
otherDeviceId != null &&
otherDeviceId != client.deviceID) {
// okay, another of our devices answered
req.otherDeviceAccepted();
req.dispose();
_requests.remove(transactionId);
}
} else if (event['sender'] != client.userID) {
} else if (update.senderId != client.userID) {
if (!{EventTypes.KeyVerificationRequest, EventTypes.KeyVerificationStart}
.contains(type)) {
return; // we can only start on these
}
final room = client.getRoomById(update.roomID) ??
Room(id: update.roomID, client: client);
final room = client.getRoomById(update.roomId!) ??
Room(id: update.roomId!, client: client);
final newKeyRequest = KeyVerification(
encryption: encryption,
userId: event['sender'],
userId: update.senderId,
room: room,
);
await newKeyRequest.handlePayload(
type,
event['content'],
event['event_id'],
update.content,
update.eventId,
);
if (newKeyRequest.state != KeyVerificationState.askAccept) {
// something went wrong, let's just dispose the request
Expand Down
7 changes: 5 additions & 2 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2801,8 +2801,11 @@ class Client extends MatrixApi {
if (store) {
await database?.storeEventUpdate(update, this);
}
if (encryptionEnabled) {
await encryption?.handleEventUpdate(update);
if (event is MatrixEvent && encryptionEnabled) {
await encryption?.handleEventUpdate(
Event.fromMatrixEvent(event, room),
type,
);
}
onEvent.add(update);

Expand Down
16 changes: 7 additions & 9 deletions test/encryption/key_verification_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,20 @@ import 'package:matrix/matrix.dart';
import '../fake_client.dart';
import '../fake_database.dart';

EventUpdate getLastSentEvent(KeyVerification req) {
Event getLastSentEvent(KeyVerification req) {
final entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/'));
final type = entry.key.split('/')[6];
final content = json.decode(entry.value.first);
return EventUpdate(
content: {
return Event.fromJson(
{
'event_id': req.transactionId,
'type': type,
'content': content,
'origin_server_ts': DateTime.now().millisecondsSinceEpoch,
'sender': req.client.userID,
},
type: EventUpdateType.timeline,
roomID: req.room!.id,
req.room!,
);
}

Expand Down Expand Up @@ -615,8 +614,8 @@ void main() async {
await sub.cancel();

await client2.encryption!.keyVerificationManager.handleEventUpdate(
EventUpdate(
content: {
Event.fromJson(
{
'event_id': req2.transactionId,
'type': EventTypes.KeyVerificationReady,
'content': {
Expand All @@ -630,8 +629,7 @@ void main() async {
'origin_server_ts': DateTime.now().millisecondsSinceEpoch,
'sender': client2.userID,
},
type: EventUpdateType.timeline,
roomID: req2.room!.id,
req2.room!,
),
);
expect(req2.state, KeyVerificationState.error);
Expand Down

0 comments on commit e7065af

Please sign in to comment.