Merge pull request #1975 from famedly/krille/use-event-instead-of-event-update-for-decryption

refactor: Use Event instead of EventUpdate for pending decryption event queue and for decrypt events in general
This commit is contained in:
Krille-chan 2024-12-30 09:38:45 +01:00 committed by GitHub
commit 07e490aa98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 51 deletions

View File

@ -314,12 +314,12 @@ class Encryption {
bool store = false,
EventUpdateType updateType = EventUpdateType.timeline,
}) async {
if (event.type != EventTypes.Encrypted || event.redacted) {
return event;
}
final content = event.parsedRoomEncryptedContent;
final sessionId = content.sessionId;
try {
if (event.type != EventTypes.Encrypted || event.redacted) {
return event;
}
final content = event.parsedRoomEncryptedContent;
final sessionId = content.sessionId;
if (client.database != null &&
sessionId != null &&
!(keyManager

View File

@ -2561,16 +2561,16 @@ class Client extends MatrixApi {
if (room != null) {
final List<BasicEvent> events = [];
for (final event in _eventsPendingDecryption) {
if (event.event.roomID != roomId) continue;
if (event.event.room.id != roomId) continue;
if (!sessionIds.contains(
event.event.content['content']?['session_id'],
event.event.content.tryGet<String>('session_id'),
)) {
continue;
}
final decryptedEvent = await event.event.decrypt(room);
if (decryptedEvent.content.tryGet<String>('type') !=
EventTypes.Encrypted) {
final decryptedEvent =
await encryption!.decryptRoomEvent(event.event);
if (decryptedEvent.type != EventTypes.Encrypted) {
events.add(BasicEvent.fromJson(decryptedEvent.content));
}
}
@ -2747,7 +2747,7 @@ class Client extends MatrixApi {
// we collect them first before we handle them.
final callEvents = <Event>[];
for (final event in events) {
for (var event in events) {
// The client must ignore any new m.room.encryption event to prevent
// man-in-the-middle attacks!
if ((event.type == EventTypes.Encryption &&
@ -2760,25 +2760,30 @@ class Client extends MatrixApi {
continue;
}
var update =
EventUpdate(roomID: room.id, type: type, content: event.toJson());
if (event.type == EventTypes.Encrypted && encryptionEnabled) {
update = await update.decrypt(room);
if (event is MatrixEvent &&
event.type == EventTypes.Encrypted &&
encryptionEnabled) {
final decrypted = await encryption!.decryptRoomEvent(
Event.fromMatrixEvent(event, room),
updateType: type,
);
// if the event failed to decrypt, add it to the queue
if (update.content.tryGet<String>('type') == EventTypes.Encrypted) {
if (decrypted.type != EventTypes.Encrypted) {
event = decrypted;
} else {
// if the event failed to decrypt, add it to the queue
_eventsPendingDecryption.add(
_EventPendingDecryption(
EventUpdate(
roomID: update.roomID,
type: EventUpdateType.decryptedTimelineQueue,
content: update.content,
),
),
_EventPendingDecryption(Event.fromMatrixEvent(event, room)),
);
}
}
final update = EventUpdate(
roomID: room.id,
type: type,
content: event.toJson(),
);
// Any kind of member change? We should invalidate the profile then:
if (event is StrippedStateEvent && event.type == EventTypes.RoomMember) {
final userId = event.stateKey;
@ -3985,7 +3990,7 @@ class ArchivedRoom {
class _EventPendingDecryption {
DateTime addedAt = DateTime.now();
EventUpdate event;
Event event;
bool get timedOut =>
addedAt.add(Duration(minutes: 5)).isBefore(DateTime.now());

View File

@ -16,8 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'package:matrix/matrix.dart';
enum EventUpdateType {
/// Newly received events from /sync
timeline,
@ -58,28 +56,4 @@ class EventUpdate {
required this.type,
required this.content,
});
Future<EventUpdate> decrypt(Room room, {bool store = false}) async {
final encryption = room.client.encryption;
if (content['type'] != EventTypes.Encrypted ||
!room.client.encryptionEnabled ||
encryption == null) {
return this;
}
try {
final decrpytedEvent = await encryption.decryptRoomEvent(
Event.fromJson(content, room),
store: store,
updateType: type,
);
return EventUpdate(
roomID: roomID,
type: type,
content: decrpytedEvent.toJson(),
);
} catch (e, s) {
Logs().e('[LibOlm] Could not decrypt megolm event', e, s);
return this;
}
}
}