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 { Future<void> handleEventUpdate(Event event, EventUpdateType type) async {
if (update.type == EventUpdateType.history) { if (type == EventUpdateType.history) {
return; return;
} }
if (update.content['type'].startsWith('m.key.verification.') || if (event.type.startsWith('m.key.verification.') ||
(update.content['type'] == EventTypes.Message && (event.type == EventTypes.Message &&
(update.content['content']['msgtype'] is String) && event.content
update.content['content']['msgtype'] .tryGet<String>('msgtype')
.startsWith('m.key.verification.'))) { ?.startsWith('m.key.verification.') ==
true)) {
// "just" key verification, no need to do this in sync // "just" key verification, no need to do this in sync
runInRoot(() => keyVerificationManager.handleEventUpdate(update)); runInRoot(() => keyVerificationManager.handleEventUpdate(event));
} }
if (update.content['sender'] == client.userID && if (event.senderId == client.userID && event.status.isSynced) {
update.content['unsigned']?['transaction_id'] == null) {
// maybe we need to re-try SSSS secrets // maybe we need to re-try SSSS secrets
runInRoot(() => ssss.periodicallyRequestMissingCache()); runInRoot(() => ssss.periodicallyRequestMissingCache());
} }

View File

@ -88,29 +88,29 @@ class KeyVerificationManager {
} }
} }
Future<void> handleEventUpdate(EventUpdate update) async { Future<void> handleEventUpdate(Event update) async {
final event = update.content; final type = update.type.startsWith('m.key.verification.')
final type = event['type'].startsWith('m.key.verification.') ? update.type
? event['type'] : update.content.tryGet<String>('msgtype');
: event['content']['msgtype'];
if (type == null || if (type == null ||
!type.startsWith('m.key.verification.') || !type.startsWith('m.key.verification.') ||
client.verificationMethods.isEmpty) { client.verificationMethods.isEmpty) {
return; return;
} }
if (type == EventTypes.KeyVerificationRequest) { if (type == EventTypes.KeyVerificationRequest) {
event['content']['timestamp'] = event['origin_server_ts']; update.content['timestamp'] =
update.originServerTs.millisecondsSinceEpoch;
} }
final transactionId = final transactionId =
KeyVerification.getTransactionId(event['content']) ?? event['event_id']; KeyVerification.getTransactionId(update.content) ?? update.eventId;
final req = _requests[transactionId]; final req = _requests[transactionId];
if (req != null) { if (req != null) {
final otherDeviceId = event['content']['from_device']; final otherDeviceId = update.content.tryGet<String>('from_device');
if (event['sender'] != client.userID) { if (update.senderId != client.userID) {
await req.handlePayload(type, event['content'], event['event_id']); await req.handlePayload(type, update.content, update.eventId);
} else if (event['sender'] == client.userID && } else if (update.senderId == client.userID &&
otherDeviceId != null && otherDeviceId != null &&
otherDeviceId != client.deviceID) { otherDeviceId != client.deviceID) {
// okay, another of our devices answered // okay, another of our devices answered
@ -118,22 +118,22 @@ class KeyVerificationManager {
req.dispose(); req.dispose();
_requests.remove(transactionId); _requests.remove(transactionId);
} }
} else if (event['sender'] != client.userID) { } else if (update.senderId != client.userID) {
if (!{EventTypes.KeyVerificationRequest, EventTypes.KeyVerificationStart} if (!{EventTypes.KeyVerificationRequest, EventTypes.KeyVerificationStart}
.contains(type)) { .contains(type)) {
return; // we can only start on these return; // we can only start on these
} }
final room = client.getRoomById(update.roomID) ?? final room = client.getRoomById(update.roomId!) ??
Room(id: update.roomID, client: client); Room(id: update.roomId!, client: client);
final newKeyRequest = KeyVerification( final newKeyRequest = KeyVerification(
encryption: encryption, encryption: encryption,
userId: event['sender'], userId: update.senderId,
room: room, room: room,
); );
await newKeyRequest.handlePayload( await newKeyRequest.handlePayload(
type, type,
event['content'], update.content,
event['event_id'], update.eventId,
); );
if (newKeyRequest.state != KeyVerificationState.askAccept) { if (newKeyRequest.state != KeyVerificationState.askAccept) {
// something went wrong, let's just dispose the request // something went wrong, let's just dispose the request

View File

@ -2801,8 +2801,11 @@ class Client extends MatrixApi {
if (store) { if (store) {
await database?.storeEventUpdate(update, this); await database?.storeEventUpdate(update, this);
} }
if (encryptionEnabled) { if (event is MatrixEvent && encryptionEnabled) {
await encryption?.handleEventUpdate(update); await encryption?.handleEventUpdate(
Event.fromMatrixEvent(event, room),
type,
);
} }
onEvent.add(update); onEvent.add(update);

View File

@ -28,21 +28,20 @@ import 'package:matrix/matrix.dart';
import '../fake_client.dart'; import '../fake_client.dart';
import '../fake_database.dart'; import '../fake_database.dart';
EventUpdate getLastSentEvent(KeyVerification req) { Event getLastSentEvent(KeyVerification req) {
final entry = FakeMatrixApi.calledEndpoints.entries final entry = FakeMatrixApi.calledEndpoints.entries
.firstWhere((p) => p.key.contains('/send/')); .firstWhere((p) => p.key.contains('/send/'));
final type = entry.key.split('/')[6]; final type = entry.key.split('/')[6];
final content = json.decode(entry.value.first); final content = json.decode(entry.value.first);
return EventUpdate( return Event.fromJson(
content: { {
'event_id': req.transactionId, 'event_id': req.transactionId,
'type': type, 'type': type,
'content': content, 'content': content,
'origin_server_ts': DateTime.now().millisecondsSinceEpoch, 'origin_server_ts': DateTime.now().millisecondsSinceEpoch,
'sender': req.client.userID, 'sender': req.client.userID,
}, },
type: EventUpdateType.timeline, req.room!,
roomID: req.room!.id,
); );
} }
@ -615,8 +614,8 @@ void main() async {
await sub.cancel(); await sub.cancel();
await client2.encryption!.keyVerificationManager.handleEventUpdate( await client2.encryption!.keyVerificationManager.handleEventUpdate(
EventUpdate( Event.fromJson(
content: { {
'event_id': req2.transactionId, 'event_id': req2.transactionId,
'type': EventTypes.KeyVerificationReady, 'type': EventTypes.KeyVerificationReady,
'content': { 'content': {
@ -630,8 +629,7 @@ void main() async {
'origin_server_ts': DateTime.now().millisecondsSinceEpoch, 'origin_server_ts': DateTime.now().millisecondsSinceEpoch,
'sender': client2.userID, 'sender': client2.userID,
}, },
type: EventUpdateType.timeline, req2.room!,
roomID: req2.room!.id,
), ),
); );
expect(req2.state, KeyVerificationState.error); expect(req2.state, KeyVerificationState.error);