Skip to content

Commit ea1fe67

Browse files
committed
MXCrypto: Use the last olm session that got a message
With the add MXOlmSession.lastReceivedMessageTs element-hq/element-ios#2128
1 parent c7d9e6e commit ea1fe67

File tree

6 files changed

+67
-18
lines changed

6 files changed

+67
-18
lines changed

CHANGES.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
Changes in Matrix iOS SDK in 0.12.1 (2012-12-xx)
22
===============================================
33

4+
Improvements:
5+
* MXCrypto: Use the last olm session that got a message (vector-im/riot-ios/issues/2128).
6+
* MXScanManager: Support the encrypted body (the request body is now encrypted by default using the server public key).
7+
* MXMediaManager: Support the encrypted body.
8+
49
Bug Fix:
510
* MXCryptoStore: Stop duplicating devices in the store (vector-im/riot-ios/issues/2132).
611

7-
Improvements:
8-
* MXScanManager: Support the encrypted body (the request body is now encrypted by default using the server public key).
9-
* MXMediaManager: Support the encrypted body.
10-
1112
Changes in Matrix iOS SDK in 0.12.0 (2018-12-06)
1213
===============================================
1314

MatrixSDK/Crypto/Data/MXOlmSession.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ NS_ASSUME_NONNULL_BEGIN
3737
*/
3838
@property (nonatomic, readonly) OLMSession *session;
3939

40+
/**
41+
Timestamp at which the session last received a message.
42+
*/
43+
@property (nonatomic) NSTimeInterval lastReceivedMessageTs;
44+
45+
46+
/**
47+
Notify this model that a message has been received on this olm session
48+
so that it updates `lastReceivedMessageTs`
49+
*/
50+
- (void)didReceiveMessage;
51+
4052
@end
4153

4254
NS_ASSUME_NONNULL_END

MatrixSDK/Crypto/Data/MXOlmSession.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ - (instancetype)initWithOlmSession:(OLMSession *)session
2424
if (self)
2525
{
2626
_session = session;
27+
_lastReceivedMessageTs = 0;
2728
}
2829
return self;
2930
}
3031

32+
- (void)didReceiveMessage
33+
{
34+
_lastReceivedMessageTs = [[NSDate date] timeIntervalSince1970];
35+
}
36+
3137
@end

MatrixSDK/Crypto/Data/Store/MXCryptoStore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202

203203
/**
204204
Retrieve all end-to-end sessions between the logged-in user and another
205-
device.
205+
device sorted by `lastReceivedMessageTs`, the most recent(higest value) first.
206206
207207
@param deviceKey the public key of the other device.
208208
@return a array of end-to-end sessions.

MatrixSDK/Crypto/Data/Store/MXRealmCryptoStore/MXRealmCryptoStore.m

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#import "MXSession.h"
2424
#import "MXTools.h"
2525

26-
NSUInteger const kMXRealmCryptoStoreVersion = 8;
26+
NSUInteger const kMXRealmCryptoStoreVersion = 9;
2727

2828
static NSString *const kMXRealmCryptoStoreFolder = @"MXRealmCryptoStore";
2929

@@ -84,6 +84,7 @@ + (NSString *)primaryKey
8484
@interface MXRealmOlmSession : RLMObject
8585
@property NSString *sessionId;
8686
@property NSString *deviceKey;
87+
@property NSTimeInterval lastReceivedMessageTs;
8788
@property NSData *olmSessionData;
8889
@end
8990

@@ -621,6 +622,8 @@ - (void)storeSession:(MXOlmSession*)session forDevice:(NSString*)deviceKey
621622
@"deviceKey": deviceKey,
622623
@"olmSessionData": [NSKeyedArchiver archivedDataWithRootObject:session.session]
623624
}];
625+
realmOlmSession.lastReceivedMessageTs = session.lastReceivedMessageTs;
626+
624627
[realm addObject:realmOlmSession];
625628
}
626629
}];
@@ -634,7 +637,9 @@ - (MXOlmSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)
634637
where:@"sessionId = %@ AND deviceKey = %@", sessionId, deviceKey].firstObject;
635638

636639
OLMSession *olmSession = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];
640+
637641
MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
642+
mxOlmSession.lastReceivedMessageTs = realmOlmSession.lastReceivedMessageTs;
638643

639644
return mxOlmSession;
640645
}
@@ -643,7 +648,9 @@ - (MXOlmSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)
643648
{
644649
NSMutableArray<MXOlmSession*> *sessionsWithDevice;
645650

646-
RLMResults<MXRealmOlmSession *> *realmOlmSessions = [MXRealmOlmSession objectsInRealm:self.realm where:@"deviceKey = %@", deviceKey];
651+
RLMResults<MXRealmOlmSession *> *realmOlmSessions = [[MXRealmOlmSession objectsInRealm:self.realm
652+
where:@"deviceKey = %@", deviceKey]
653+
sortedResultsUsingKeyPath:@"lastReceivedMessageTs" ascending:NO];
647654
for (MXRealmOlmSession *realmOlmSession in realmOlmSessions)
648655
{
649656
if (!sessionsWithDevice)
@@ -652,7 +659,9 @@ - (MXOlmSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)
652659
}
653660

654661
OLMSession *olmSession = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];
662+
655663
MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
664+
mxOlmSession.lastReceivedMessageTs = realmOlmSession.lastReceivedMessageTs;
656665

657666
[sessionsWithDevice addObject:mxOlmSession];
658667
}
@@ -1203,6 +1212,23 @@ + (RLMRealm*)realmForUser:(NSString*)userId
12031212
// make queries. So, the cleaning will be done afterwards.
12041213
cleanDuplicatedDevices = YES;
12051214
}
1215+
1216+
case 8:
1217+
{
1218+
// MXRealmOlmSession.lastReceivedMessageTs has been added to implement:
1219+
// Use the last olm session that got a message
1220+
// https://github.com/vector-im/riot-ios/issues/2128
1221+
1222+
NSLog(@"[MXRealmCryptoStore] Migration from schema #8 -> #9");
1223+
1224+
NSLog(@" Add lastReceivedMessageTs = 0 to all MXRealmOlmSession objects");
1225+
[migration enumerateObjects:MXRealmOlmSession.className block:^(RLMObject *oldObject, RLMObject *newObject) {
1226+
1227+
newObject[@"lastReceivedMessageTs"] = 0;
1228+
}];
1229+
1230+
NSLog(@"[MXRealmCryptoStore] Migration from schema #6 -> #7 completed");
1231+
}
12061232
}
12071233
}
12081234
};

MatrixSDK/Crypto/MXOlmDevice.m

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ - (NSString *)createOutboundSession:(NSString *)theirIdentityKey theirOneTimeKey
149149
if (olmSession)
150150
{
151151
MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
152+
153+
// Pretend we've received a message at this point, otherwise
154+
// if we try to send a message to the device, it won't use
155+
// this session
156+
[mxOlmSession didReceiveMessage];
157+
152158
[store storeSession:mxOlmSession forDevice:theirIdentityKey];
153159
return olmSession.sessionIdentifier;
154160
}
@@ -186,6 +192,11 @@ - (NSString*)createInboundSession:(NSString*)theirDeviceIdentityKey messageType:
186192
}
187193

188194
MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
195+
196+
// This counts as a received message: set last received message time
197+
// to now
198+
[mxOlmSession didReceiveMessage];
199+
189200
[store storeSession:mxOlmSession forDevice:theirDeviceIdentityKey];
190201

191202
return olmSession.sessionIdentifier;
@@ -213,17 +224,9 @@ - (NSString*)createInboundSession:(NSString*)theirDeviceIdentityKey messageType:
213224

214225
- (NSString *)sessionIdForDevice:(NSString *)theirDeviceIdentityKey
215226
{
216-
NSString *sessionId;
217-
218-
NSArray<NSString *> *sessionIds = [self sessionIdsForDevice:theirDeviceIdentityKey];
219-
if (sessionIds.count)
220-
{
221-
// Use the session with the lowest ID.
222-
NSArray *sortedSessionIds = [sessionIds sortedArrayUsingSelector:@selector(compare:)];
223-
sessionId = sortedSessionIds[0];
224-
}
225-
226-
return sessionId;
227+
// Use the session that has most recently received a message
228+
// This is the first item in the sorted array returned by the store
229+
return [store sessionsWithDevice:theirDeviceIdentityKey].firstObject.session.sessionIdentifier;
227230
}
228231

229232
- (NSDictionary *)encryptMessage:(NSString *)theirDeviceIdentityKey sessionId:(NSString *)sessionId payloadString:(NSString *)payloadString
@@ -272,6 +275,7 @@ - (NSString*)decryptMessage:(NSString*)ciphertext withType:(NSUInteger)messageTy
272275
NSLog(@"[MXOlmDevice] decryptMessage failed: %@", error);
273276
}
274277

278+
[mxOlmSession didReceiveMessage];
275279
[store storeSession:mxOlmSession forDevice:theirDeviceIdentityKey];
276280
}
277281

0 commit comments

Comments
 (0)