refactor: Use Event instead of EventUpdate for pending decryption event queue and for decrypt events in general
This should removes an unnecessary step of json serialization and deserialization and should therefore improve performance. Gets rid of some unnecessary code as well.
This commit is contained in:
parent
992c8e42f2
commit
017a39c792
|
|
@ -314,12 +314,12 @@ class Encryption {
|
|||
bool store = false,
|
||||
EventUpdateType updateType = EventUpdateType.timeline,
|
||||
}) async {
|
||||
try {
|
||||
if (event.type != EventTypes.Encrypted || event.redacted) {
|
||||
return event;
|
||||
}
|
||||
final content = event.parsedRoomEncryptedContent;
|
||||
final sessionId = content.sessionId;
|
||||
try {
|
||||
if (client.database != null &&
|
||||
sessionId != null &&
|
||||
!(keyManager
|
||||
|
|
|
|||
|
|
@ -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 (decrypted.type != EventTypes.Encrypted) {
|
||||
event = decrypted;
|
||||
} else {
|
||||
// if the event failed to decrypt, add it to the queue
|
||||
if (update.content.tryGet<String>('type') == EventTypes.Encrypted) {
|
||||
_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());
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue