@ -439,21 +439,6 @@ NS_ASSUME_NONNULL_BEGIN
if ( isDuplicate ) { return ; }
if ( isDuplicate ) { return ; }
}
}
/ / Loki : Handle device linking message if needed
if ( contentProto . lokiDeviceLinkMessage != nil ) {
NSString * masterHexEncodedPublicKey = contentProto . lokiDeviceLinkMessage . masterHexEncodedPublicKey ;
NSString * slaveHexEncodedPublicKey = contentProto . lokiDeviceLinkMessage . slaveHexEncodedPublicKey ;
NSData * masterSignature = contentProto . lokiDeviceLinkMessage . masterSignature ;
NSData * slaveSignature = contentProto . lokiDeviceLinkMessage . slaveSignature ;
if ( masterSignature != nil ) { / / Authorization
OWSLogInfo ( @ "[ Loki ] Received a device linking authorization from : %@", envelope.source); // Not masterHexEncodedPublicKey
[ LKDeviceLinkingSession . current processLinkingAuthorizationFrom : masterHexEncodedPublicKey for : slaveHexEncodedPublicKey masterSignature : masterSignature slaveSignature : slaveSignature ] ;
} else if ( slaveSignature != nil ) { / / Request
OWSLogInfo ( @ "[ Loki ] Received a device linking request from : %@", envelope.source); // Not slaveHexEncodedPublicKey
[ LKDeviceLinkingSession . current processLinkingRequestFrom : slaveHexEncodedPublicKey to : masterHexEncodedPublicKey with : slaveSignature ] ;
}
}
/ / Loki : Handle pre key bundle message if needed
/ / Loki : Handle pre key bundle message if needed
if ( contentProto . prekeyBundleMessage != nil ) {
if ( contentProto . prekeyBundleMessage != nil ) {
OWSLogInfo ( @ "[ Loki ] Received a pre key bundle message from : %@.", envelope.source);
OWSLogInfo ( @ "[ Loki ] Received a pre key bundle message from : %@.", envelope.source);
@ -471,7 +456,25 @@ NS_ASSUME_NONNULL_BEGIN
[ LKP2PAPI didReceiveLokiAddressMessageForContact : envelope . source address : address port : port receivedThroughP2P : envelope . isPtpMessage ] ;
[ LKP2PAPI didReceiveLokiAddressMessageForContact : envelope . source address : address port : port receivedThroughP2P : envelope . isPtpMessage ] ;
}
}
if ( contentProto . syncMessage ) {
/ / Loki : Handle device linking message if needed
if ( contentProto . lokiDeviceLinkMessage != nil ) {
NSString * masterHexEncodedPublicKey = contentProto . lokiDeviceLinkMessage . masterHexEncodedPublicKey ;
NSString * slaveHexEncodedPublicKey = contentProto . lokiDeviceLinkMessage . slaveHexEncodedPublicKey ;
NSData * masterSignature = contentProto . lokiDeviceLinkMessage . masterSignature ;
NSData * slaveSignature = contentProto . lokiDeviceLinkMessage . slaveSignature ;
if ( masterSignature != nil ) { / / Authorization
OWSLogInfo ( @ "[ Loki ] Received a device linking authorization from : %@", envelope.source); // Not masterHexEncodedPublicKey
[ LKDeviceLinkingSession . current processLinkingAuthorizationFrom : masterHexEncodedPublicKey for : slaveHexEncodedPublicKey masterSignature : masterSignature slaveSignature : slaveSignature ] ;
/ / Set any profile information
if ( contentProto . dataMessage ) {
[ self handleLokiProfileForUserIfNeeded : contentProto . dataMessage transaction : transaction ] ;
}
} else if ( slaveSignature != nil ) { / / Request
OWSLogInfo ( @ "[ Loki ] Received a device linking request from : %@", envelope.source); // Not slaveHexEncodedPublicKey
[ LKDeviceLinkingSession . current processLinkingRequestFrom : slaveHexEncodedPublicKey to : masterHexEncodedPublicKey with : slaveSignature ] ;
}
} else if ( contentProto . syncMessage ) {
[ self throws_handleIncomingEnvelope : envelope
[ self throws_handleIncomingEnvelope : envelope
withSyncMessage : contentProto . syncMessage
withSyncMessage : contentProto . syncMessage
transaction : transaction
transaction : transaction
@ -916,57 +919,15 @@ NS_ASSUME_NONNULL_BEGIN
return ;
return ;
}
}
if ( syncMessage . sent ) {
NSString * userHexEncodedPublicKey = OWSIdentityManager . sharedManager . identityKeyPair . hexEncodedPublicKey ;
NSString * masterHexEncodedPublicKey = [ LKDatabaseUtilities getMasterHexEncodedPublicKeyFor : userHexEncodedPublicKey in : transaction ] ;
NSString * masterHexEncodedPublicKey = [ LKDatabaseUtilities getMasterHexEncodedPublicKeyFor : userHexEncodedPublicKey in : transaction ] ;
BOOL wasSentByMasterDevice = [ masterHexEncodedPublicKey isEqual : envelope . source ] ;
BOOL wasSentByMasterDevice = [ masterHexEncodedPublicKey isEqual : envelope . source ] ;
if ( syncMessage . sent ) {
/ / Loki : Try to update using the provided profile
/ / Loki : Try to update using the provided profile
if ( wasSentByMasterDevice ) {
if ( wasSentByMasterDevice ) {
dispatch_async ( dispatch_get_main_queue ( ) , ^{
[ self handleLokiProfileForUserIfNeeded : syncMessage . sent . message transaction : transaction ] ;
SSKProtoDataMessage * dataMessage = syncMessage . sent . message ;
SSKProtoDataMessageLokiProfile * profile = dataMessage . profile ;
NSString * displayName = profile . displayName ;
NSString * profilePictureURL = profile . profilePicture ;
[ self . profileManager updateUserProfileWithDisplayName : displayName transaction : transaction ] ;
if ( [ dataMessage hasProfileKey ] ) {
[ self . profileManager updateUserProfileKeyData : dataMessage . profileKey avatarURL : profilePictureURL transaction : transaction ] ;
}
} ) ;
}
}
/ / Loki : Handle contact sync if needed
if ( syncMessage . contacts != nil ) {
if ( wasSentByMasterDevice ) {
NSLog ( @ "[ Loki ] Received contact sync message . ") ;
NSData * data = syncMessage . contacts . data ;
ContactParser * parser = [ [ ContactParser alloc ] initWithData : data ] ;
NSArray < NSString * > * hexEncodedPublicKeys = [ parser parseHexEncodedPublicKeys ] ;
/ / Try to establish sessions
for ( NSString * hexEncodedPublicKey in hexEncodedPublicKeys ) {
TSContactThread * thread = [ TSContactThread getOrCreateThreadWithContactId : hexEncodedPublicKey transaction : transaction ] ;
LKThreadFriendRequestStatus friendRequestStatus = thread . friendRequestStatus ;
switch ( friendRequestStatus ) {
case LKThreadFriendRequestStatusNone : {
OWSMessageSender * messageSender = SSKEnvironment . shared . messageSender ;
OWSMessageSend * automatedFriendRequestMessage = [ messageSender getMultiDeviceFriendRequestMessageForHexEncodedPublicKey : hexEncodedPublicKey ] ;
dispatch_async ( OWSDispatch . sendingQueue , ^{
[ messageSender sendMessage : automatedFriendRequestMessage ] ;
} ) ;
break ;
}
case LKThreadFriendRequestStatusRequestReceived : {
[ thread saveFriendRequestStatus : LKThreadFriendRequestStatusFriends withTransaction : transaction ] ;
/ / The two lines below are equivalent to calling [ ThreadUtil enqueueFriendRequestAcceptanceMessageInThread : thread ]
LKEphemeralMessage * backgroundMessage = [ [ LKEphemeralMessage alloc ] initInThread : thread ] ;
[ self . messageSenderJobQueue addMessage : backgroundMessage transaction : transaction ] ;
break ;
}
default : break ; / / Do nothing
}
}
}
} else {
OWSIncomingSentMessageTranscript * transcript =
OWSIncomingSentMessageTranscript * transcript =
[ [ OWSIncomingSentMessageTranscript alloc ] initWithProto : syncMessage . sent transaction : transaction ] ;
[ [ OWSIncomingSentMessageTranscript alloc ] initWithProto : syncMessage . sent transaction : transaction ] ;
@ -1018,7 +979,6 @@ NS_ASSUME_NONNULL_BEGIN
}
}
transaction : transaction ] ;
transaction : transaction ] ;
}
}
}
} else if ( syncMessage . request ) {
} else if ( syncMessage . request ) {
if ( syncMessage . request . type == SSKProtoSyncMessageRequestTypeContacts ) {
if ( syncMessage . request . type == SSKProtoSyncMessageRequestTypeContacts ) {
/ / We respond asynchronously because populating the sync message will
/ / We respond asynchronously because populating the sync message will
@ -1066,11 +1026,53 @@ NS_ASSUME_NONNULL_BEGIN
} else if ( syncMessage . verified ) {
} else if ( syncMessage . verified ) {
OWSLogInfo ( @ "Received verification state for %@", syncMessage.verified.destination);
OWSLogInfo ( @ "Received verification state for %@", syncMessage.verified.destination);
[ self . identityManager throws_processIncomingSyncMessage : syncMessage . verified transaction : transaction ] ;
[ self . identityManager throws_processIncomingSyncMessage : syncMessage . verified transaction : transaction ] ;
} else if ( syncMessage . contacts != nil ) {
if ( false && wasSentByMasterDevice && syncMessage . contacts . data . length > 0 ) {
NSLog ( @ "[ Loki ] Received contact sync message . ") ;
NSData * data = syncMessage . contacts . data ;
ContactParser * parser = [ [ ContactParser alloc ] initWithData : data ] ;
NSArray < NSString * > * hexEncodedPublicKeys = [ parser parseHexEncodedPublicKeys ] ;
/ / Try to establish sessions
for ( NSString * hexEncodedPublicKey in hexEncodedPublicKeys ) {
TSContactThread * thread = [ TSContactThread getOrCreateThreadWithContactId : hexEncodedPublicKey transaction : transaction ] ;
LKThreadFriendRequestStatus friendRequestStatus = thread . friendRequestStatus ;
switch ( friendRequestStatus ) {
case LKThreadFriendRequestStatusNone : {
OWSMessageSender * messageSender = SSKEnvironment . shared . messageSender ;
OWSMessageSend * automatedFriendRequestMessage = [ messageSender getMultiDeviceFriendRequestMessageForHexEncodedPublicKey : hexEncodedPublicKey ] ;
dispatch_async ( OWSDispatch . sendingQueue , ^{
[ messageSender sendMessage : automatedFriendRequestMessage ] ;
} ) ;
break ;
}
case LKThreadFriendRequestStatusRequestReceived : {
[ thread saveFriendRequestStatus : LKThreadFriendRequestStatusFriends withTransaction : transaction ] ;
/ / The two lines below are equivalent to calling [ ThreadUtil enqueueFriendRequestAcceptanceMessageInThread : thread ]
LKEphemeralMessage * backgroundMessage = [ [ LKEphemeralMessage alloc ] initInThread : thread ] ;
[ self . messageSenderJobQueue addMessage : backgroundMessage transaction : transaction ] ;
break ;
}
default : break ; / / Do nothing
}
}
}
} else {
} else {
OWSLogWarn ( @ "Ignoring unsupported sync message . ") ;
OWSLogWarn ( @ "Ignoring unsupported sync message . ") ;
}
}
}
}
- ( void ) handleLokiProfileForUserIfNeeded : ( SSKProtoDataMessage * ) dataMessage transaction : ( YapDatabaseReadWriteTransaction * ) transaction {
if ( dataMessage . profile != nil ) {
SSKProtoDataMessageLokiProfile * profile = dataMessage . profile ;
NSString * displayName = profile . displayName ;
NSString * profilePictureURL = profile . profilePicture ;
[ self . profileManager updateUserProfileWithDisplayName : displayName transaction : transaction ] ;
if ( [ dataMessage hasProfileKey ] ) {
[ self . profileManager updateUserProfileKeyData : dataMessage . profileKey avatarURL : profilePictureURL transaction : transaction ] ;
}
}
}
- ( void ) handleEndSessionMessageWithEnvelope : ( SSKProtoEnvelope * ) envelope
- ( void ) handleEndSessionMessageWithEnvelope : ( SSKProtoEnvelope * ) envelope
dataMessage : ( SSKProtoDataMessage * ) dataMessage
dataMessage : ( SSKProtoDataMessage * ) dataMessage
transaction : ( YapDatabaseReadWriteTransaction * ) transaction
transaction : ( YapDatabaseReadWriteTransaction * ) transaction