refactor: Do not use eventupdate type for verification requests

This commit is contained in:
Krille 2024-12-31 14:12:48 +01:00
parent f2e2ee8daf
commit e7065afe31
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652
4 changed files with 39 additions and 38 deletions

View File

@ -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());
}

View File

@ -88,29 +88,29 @@ 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
@ -118,22 +118,22 @@ class KeyVerificationManager {
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

View File

@ -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);

View File

@ -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!,
);
}
@ -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': {
@ -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);