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,
|
bool store = false,
|
||||||
EventUpdateType updateType = EventUpdateType.timeline,
|
EventUpdateType updateType = EventUpdateType.timeline,
|
||||||
}) async {
|
}) async {
|
||||||
if (event.type != EventTypes.Encrypted || event.redacted) {
|
|
||||||
return event;
|
|
||||||
}
|
|
||||||
final content = event.parsedRoomEncryptedContent;
|
|
||||||
final sessionId = content.sessionId;
|
|
||||||
try {
|
try {
|
||||||
|
if (event.type != EventTypes.Encrypted || event.redacted) {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
final content = event.parsedRoomEncryptedContent;
|
||||||
|
final sessionId = content.sessionId;
|
||||||
if (client.database != null &&
|
if (client.database != null &&
|
||||||
sessionId != null &&
|
sessionId != null &&
|
||||||
!(keyManager
|
!(keyManager
|
||||||
|
|
|
||||||
|
|
@ -2561,16 +2561,16 @@ class Client extends MatrixApi {
|
||||||
if (room != null) {
|
if (room != null) {
|
||||||
final List<BasicEvent> events = [];
|
final List<BasicEvent> events = [];
|
||||||
for (final event in _eventsPendingDecryption) {
|
for (final event in _eventsPendingDecryption) {
|
||||||
if (event.event.roomID != roomId) continue;
|
if (event.event.room.id != roomId) continue;
|
||||||
if (!sessionIds.contains(
|
if (!sessionIds.contains(
|
||||||
event.event.content['content']?['session_id'],
|
event.event.content.tryGet<String>('session_id'),
|
||||||
)) {
|
)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final decryptedEvent = await event.event.decrypt(room);
|
final decryptedEvent =
|
||||||
if (decryptedEvent.content.tryGet<String>('type') !=
|
await encryption!.decryptRoomEvent(event.event);
|
||||||
EventTypes.Encrypted) {
|
if (decryptedEvent.type != EventTypes.Encrypted) {
|
||||||
events.add(BasicEvent.fromJson(decryptedEvent.content));
|
events.add(BasicEvent.fromJson(decryptedEvent.content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2747,7 +2747,7 @@ class Client extends MatrixApi {
|
||||||
// we collect them first before we handle them.
|
// we collect them first before we handle them.
|
||||||
final callEvents = <Event>[];
|
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
|
// The client must ignore any new m.room.encryption event to prevent
|
||||||
// man-in-the-middle attacks!
|
// man-in-the-middle attacks!
|
||||||
if ((event.type == EventTypes.Encryption &&
|
if ((event.type == EventTypes.Encryption &&
|
||||||
|
|
@ -2760,25 +2760,30 @@ class Client extends MatrixApi {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var update =
|
if (event is MatrixEvent &&
|
||||||
EventUpdate(roomID: room.id, type: type, content: event.toJson());
|
event.type == EventTypes.Encrypted &&
|
||||||
if (event.type == EventTypes.Encrypted && encryptionEnabled) {
|
encryptionEnabled) {
|
||||||
update = await update.decrypt(room);
|
final decrypted = await encryption!.decryptRoomEvent(
|
||||||
|
Event.fromMatrixEvent(event, room),
|
||||||
|
updateType: type,
|
||||||
|
);
|
||||||
|
|
||||||
// if the event failed to decrypt, add it to the queue
|
if (decrypted.type != EventTypes.Encrypted) {
|
||||||
if (update.content.tryGet<String>('type') == EventTypes.Encrypted) {
|
event = decrypted;
|
||||||
|
} else {
|
||||||
|
// if the event failed to decrypt, add it to the queue
|
||||||
_eventsPendingDecryption.add(
|
_eventsPendingDecryption.add(
|
||||||
_EventPendingDecryption(
|
_EventPendingDecryption(Event.fromMatrixEvent(event, room)),
|
||||||
EventUpdate(
|
|
||||||
roomID: update.roomID,
|
|
||||||
type: EventUpdateType.decryptedTimelineQueue,
|
|
||||||
content: update.content,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final update = EventUpdate(
|
||||||
|
roomID: room.id,
|
||||||
|
type: type,
|
||||||
|
content: event.toJson(),
|
||||||
|
);
|
||||||
|
|
||||||
// Any kind of member change? We should invalidate the profile then:
|
// Any kind of member change? We should invalidate the profile then:
|
||||||
if (event is StrippedStateEvent && event.type == EventTypes.RoomMember) {
|
if (event is StrippedStateEvent && event.type == EventTypes.RoomMember) {
|
||||||
final userId = event.stateKey;
|
final userId = event.stateKey;
|
||||||
|
|
@ -3985,7 +3990,7 @@ class ArchivedRoom {
|
||||||
class _EventPendingDecryption {
|
class _EventPendingDecryption {
|
||||||
DateTime addedAt = DateTime.now();
|
DateTime addedAt = DateTime.now();
|
||||||
|
|
||||||
EventUpdate event;
|
Event event;
|
||||||
|
|
||||||
bool get timedOut =>
|
bool get timedOut =>
|
||||||
addedAt.add(Duration(minutes: 5)).isBefore(DateTime.now());
|
addedAt.add(Duration(minutes: 5)).isBefore(DateTime.now());
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,6 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import 'package:matrix/matrix.dart';
|
|
||||||
|
|
||||||
enum EventUpdateType {
|
enum EventUpdateType {
|
||||||
/// Newly received events from /sync
|
/// Newly received events from /sync
|
||||||
timeline,
|
timeline,
|
||||||
|
|
@ -58,28 +56,4 @@ class EventUpdate {
|
||||||
required this.type,
|
required this.type,
|
||||||
required this.content,
|
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