Skip to content

Commit

Permalink
refactor: Remove unnecessary roomId parameter from decryptRoomEvent m…
Browse files Browse the repository at this point in the history
…ethod
  • Loading branch information
krille-chan committed Dec 17, 2024
1 parent 04a4622 commit 2e5466d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/versions.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
flutter_version=3.24.3
dart_version=3.5.3
flutter_version=3.27.1
dart_version=3.6.0
25 changes: 13 additions & 12 deletions lib/encryption/encryption.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class Encryption {
}
}

Event decryptRoomEventSync(String roomId, Event event) {
Event decryptRoomEventSync(Event event) {
if (event.type != EventTypes.Encrypted || event.redacted) {
return event;
}
Expand All @@ -214,7 +214,7 @@ class Encryption {
}

final inboundGroupSession =
keyManager.getInboundGroupSession(roomId, sessionId);
keyManager.getInboundGroupSession(event.room.id, sessionId);
if (!(inboundGroupSession?.isValid ?? false)) {
canRequestSession = true;
throw DecryptException(DecryptException.unknownSession);
Expand Down Expand Up @@ -249,7 +249,7 @@ class Encryption {
// ignore: discarded_futures
?.updateInboundGroupSessionIndexes(
json.encode(inboundGroupSession.indexes),
roomId,
event.room.id,
sessionId,
)
// ignore: discarded_futures
Expand All @@ -260,14 +260,16 @@ class Encryption {
// alright, if this was actually by our own outbound group session, we might as well clear it
if (exception.toString() != DecryptException.unknownSession &&
(keyManager
.getOutboundGroupSession(roomId)
.getOutboundGroupSession(event.room.id)
?.outboundGroupSession
?.session_id() ??
'') ==
content.sessionId) {
runInRoot(
() async =>
keyManager.clearOrUseOutboundGroupSession(roomId, wipe: true),
() async => keyManager.clearOrUseOutboundGroupSession(
event.room.id,
wipe: true,
),
);
}
if (canRequestSession) {
Expand Down Expand Up @@ -308,7 +310,6 @@ class Encryption {
}

Future<Event> decryptRoomEvent(
String roomId,
Event event, {
bool store = false,
EventUpdateType updateType = EventUpdateType.timeline,
Expand All @@ -323,22 +324,22 @@ class Encryption {
sessionId != null &&
!(keyManager
.getInboundGroupSession(
roomId,
event.room.id,
sessionId,
)
?.isValid ??
false)) {
await keyManager.loadInboundGroupSession(
roomId,
event.room.id,
sessionId,
);
}
event = decryptRoomEventSync(roomId, event);
event = decryptRoomEventSync(event);
if (event.type == EventTypes.Encrypted &&
event.content['can_request_session'] == true &&
sessionId != null) {
keyManager.maybeAutoRequest(
roomId,
event.room.id,
sessionId,
content.senderKey,
);
Expand All @@ -350,7 +351,7 @@ class Encryption {
await client.database?.storeEventUpdate(
EventUpdate(
content: event.toJson(),
roomID: roomId,
roomID: event.room.id,
type: updateType,
),
client,
Expand Down
2 changes: 1 addition & 1 deletion lib/encryption/key_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class KeyManager {
if (event != null &&
event.type == EventTypes.Encrypted &&
event.content['session_id'] == sessionId) {
final decrypted = encryption.decryptRoomEventSync(roomId, event);
final decrypted = encryption.decryptRoomEventSync(event);
if (decrypted.type != EventTypes.Encrypted) {
// Update the last event in memory first
room.lastEvent = decrypted;
Expand Down
9 changes: 3 additions & 6 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1243,10 +1243,7 @@ class Client extends MatrixApi {
if (archivedRoom.encrypted && archivedRoom.client.encryptionEnabled) {
if (timeline.events[i].type == EventTypes.Encrypted) {
await archivedRoom.client.encryption!
.decryptRoomEvent(
archivedRoom.id,
timeline.events[i],
)
.decryptRoomEvent(timeline.events[i])
.then(
(decrypted) => timeline.events[i] = decrypted,
);
Expand Down Expand Up @@ -1880,11 +1877,11 @@ class Client extends MatrixApi {

final encryption = this.encryption;
if (event.type == EventTypes.Encrypted && encryption != null) {
var decrypted = await encryption.decryptRoomEvent(roomId, event);
var decrypted = await encryption.decryptRoomEvent(event);
if (decrypted.messageType == MessageTypes.BadEncrypted &&
prevBatch != null) {
await oneShotSync();
decrypted = await encryption.decryptRoomEvent(roomId, event);
decrypted = await encryption.decryptRoomEvent(event);
}
event = decrypted;
}
Expand Down
17 changes: 3 additions & 14 deletions lib/src/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1460,10 +1460,7 @@ class Room {
for (var i = 0; i < events.length; i++) {
if (events[i].type == EventTypes.Encrypted &&
events[i].content['can_request_session'] == true) {
events[i] = await client.encryption!.decryptRoomEvent(
id,
events[i],
);
events[i] = await client.encryption!.decryptRoomEvent(events[i]);
}
}
}
Expand Down Expand Up @@ -1529,10 +1526,7 @@ class Room {
// Try to decrypt encrypted events but don't update the database.
if (encrypted && client.encryptionEnabled) {
if (events[i].type == EventTypes.Encrypted) {
events[i] = await client.encryption!.decryptRoomEvent(
id,
events[i],
);
events[i] = await client.encryption!.decryptRoomEvent(events[i]);
}
}
}
Expand Down Expand Up @@ -1576,7 +1570,6 @@ class Room {
// for the fragmented timeline, we don't cache the decrypted
//message in the database
chunk.events[i] = await client.encryption!.decryptRoomEvent(
id,
chunk.events[i],
);
} else if (client.database != null) {
Expand All @@ -1585,7 +1578,6 @@ class Room {
for (var i = 0; i < chunk.events.length; i++) {
if (chunk.events[i].content['can_request_session'] == true) {
chunk.events[i] = await client.encryption!.decryptRoomEvent(
id,
chunk.events[i],
store: !isArchived,
updateType: EventUpdateType.history,
Expand Down Expand Up @@ -1913,10 +1905,7 @@ class Room {
final event = Event.fromMatrixEvent(matrixEvent, this);
if (event.type == EventTypes.Encrypted && client.encryptionEnabled) {
// attempt decryption
return await client.encryption?.decryptRoomEvent(
id,
event,
);
return await client.encryption?.decryptRoomEvent(event);
}
return event;
} on MatrixException catch (err) {
Expand Down
4 changes: 1 addition & 3 deletions lib/src/timeline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ class Timeline {
for (var i = 0; i < newEvents.length; i++) {
if (newEvents[i].type == EventTypes.Encrypted) {
newEvents[i] = await room.client.encryption!.decryptRoomEvent(
room.id,
newEvents[i],
);
}
Expand Down Expand Up @@ -390,7 +389,6 @@ class Timeline {
events[i].messageType == MessageTypes.BadEncrypted &&
events[i].content['session_id'] == sessionId) {
events[i] = await encryption.decryptRoomEvent(
room.id,
events[i],
store: true,
updateType: EventUpdateType.history,
Expand Down Expand Up @@ -707,7 +705,7 @@ class Timeline {
for (final matrixEvent in resp.chunk) {
var event = Event.fromMatrixEvent(matrixEvent, room);
if (event.type == EventTypes.Encrypted && encryption != null) {
event = await encryption.decryptRoomEvent(room.id, event);
event = await encryption.decryptRoomEvent(event);
if (event.type == EventTypes.Encrypted &&
event.messageType == MessageTypes.BadEncrypted &&
event.content['can_request_session'] == true) {
Expand Down
1 change: 0 additions & 1 deletion lib/src/utils/event_update.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class EventUpdate {
}
try {
final decrpytedEvent = await encryption.decryptRoomEvent(
room.id,
Event.fromJson(content, room),
store: store,
updateType: type,
Expand Down
9 changes: 4 additions & 5 deletions test/encryption/encrypt_decrypt_room_message_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void main() {
senderId: client.userID!,
);
final decryptedEvent =
await client.encryption!.decryptRoomEvent(roomId, encryptedEvent);
await client.encryption!.decryptRoomEvent(encryptedEvent);
expect(decryptedEvent.type, 'm.room.message');
expect(decryptedEvent.content['msgtype'], 'm.text');
expect(decryptedEvent.content['text'], 'Hello foxies!');
Expand All @@ -80,7 +80,7 @@ void main() {
senderId: client.userID!,
);
final decryptedEvent =
await client.encryption!.decryptRoomEvent(roomId, encryptedEvent);
await client.encryption!.decryptRoomEvent(encryptedEvent);
expect(decryptedEvent.type, 'm.room.message');
expect(decryptedEvent.content['msgtype'], 'm.text');
expect(decryptedEvent.content['text'], 'Hello foxies!');
Expand All @@ -98,12 +98,11 @@ void main() {
senderId: '@alice:example.com',
);
final decryptedEvent =
await client.encryption!.decryptRoomEvent(roomId, encryptedEvent);
await client.encryption!.decryptRoomEvent(encryptedEvent);
expect(decryptedEvent.type, 'm.room.message');
expect(decryptedEvent.content['msgtype'], 'm.text');
expect(decryptedEvent.content['text'], 'Hello foxies!');
await client.encryption!
.decryptRoomEvent(roomId, encryptedEvent, store: true);
await client.encryption!.decryptRoomEvent(encryptedEvent, store: true);
});

test('dispose client', () async {
Expand Down

0 comments on commit 2e5466d

Please sign in to comment.