Implement remaining protocol changes

This commit is contained in:
nielsandriesse 2020-06-16 16:33:14 +10:00
parent ce47719c07
commit e023bb50c2
6 changed files with 50 additions and 32 deletions

View file

@ -270,7 +270,7 @@ NSString *const kSyncManagerLastContactSyncKey = @"kTSStorageManagerOWSSyncManag
- (AnyPromise *)syncContact:(NSString *)hexEncodedPubKey transaction:(YapDatabaseReadTransaction *)transaction
{
return [LKSyncMessagesProtocol syncContactWithHexEncodedPublicKey:hexEncodedPubKey in:transaction];
return [LKSyncMessagesProtocol syncContactWithPublicKey:hexEncodedPubKey in:transaction];
}
- (AnyPromise *)syncAllContacts

View file

@ -135,7 +135,7 @@ public final class FriendRequestProtocol : NSObject {
}
}
@objc(sendFriendRequestAcceptedMessageToHexEncodedPublicKey:using:)
@objc(sendFriendRequestAcceptedMessageToPublicKey:using:)
public static func sendFriendRequestAcceptedMessage(to publicKey: String, using transaction: YapDatabaseReadWriteTransaction) {
guard ECKeyPair.isValidHexEncodedPublicKey(candidate: publicKey) else {
print("[Loki] Invalid Session ID: \(publicKey).")

View file

@ -178,8 +178,8 @@ public final class SessionManagementProtocol : NSObject {
}
}
@objc(isSessionRestoreMessage:)
public static func isSessionRestoreMessage(_ dataMessage: SSKProtoDataMessage) -> Bool {
@objc(isSessionRestorationRequest:)
public static func isSessionRestorationRequest(_ dataMessage: SSKProtoDataMessage) -> Bool {
let sessionRestoreFlag = SSKProtoDataMessage.SSKProtoDataMessageFlags.sessionRestore
return dataMessage.flags & UInt32(sessionRestoreFlag.rawValue) != 0
}
@ -217,6 +217,14 @@ public final class SessionManagementProtocol : NSObject {
storage.setPreKeyBundle(preKeyBundle, forContact: sender, transaction: transaction)
}
@objc(sendSessionEstablishedMessageToPublicKey:in:)
public static func sendSessionEstablishedMessage(to publicKey: String, in transaction: YapDatabaseReadWriteTransaction) {
let thread = TSContactThread.getOrCreateThread(withContactId: publicKey, transaction: transaction)
let ephemeralMessage = EphemeralMessage(thread: thread)
let messageSenderJobQueue = SSKEnvironment.shared.messageSenderJobQueue
messageSenderJobQueue.add(message: ephemeralMessage, transaction: transaction)
}
@objc(handleEndSessionMessageReceivedInThread:using:)
public static func handleEndSessionMessageReceived(in thread: TSContactThread, using transaction: YapDatabaseReadWriteTransaction) {
let publicKey = thread.contactIdentifier()

View file

@ -40,10 +40,10 @@ public final class SyncMessagesProtocol : NSObject {
}
}
@objc(syncContactWithHexEncodedPublicKey:in:)
public static func syncContact(_ hexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> AnyPromise {
@objc(syncContactWithPublicKey:in:)
public static func syncContact(_ publicKey: String, in transaction: YapDatabaseReadTransaction) -> AnyPromise {
let syncManager = SSKEnvironment.shared.syncManager
return syncManager.syncContacts(for: [ SignalAccount(recipientId: hexEncodedPublicKey) ])
return syncManager.syncContacts(for: [ SignalAccount(recipientId: publicKey) ])
}
@objc(syncAllContacts)

View file

@ -423,12 +423,6 @@ NS_ASSUME_NONNULL_BEGIN
envelope.timestamp);
return;
}
// Loki: Ignore any friend requests from before restoration
if ([LKFriendRequestProtocol isFriendRequestFromBeforeRestoration:envelope]) {
[LKLogger print:@"[Loki] Ignoring friend request from before restoration."];
return;
}
if (envelope.content != nil) {
NSError *error;
@ -438,7 +432,13 @@ NS_ASSUME_NONNULL_BEGIN
return;
}
OWSLogInfo(@"Handling content: <Content: %@>.", [self descriptionForContent:contentProto]);
// Loki: Ignore any friend requests from before restoration
if ([LKFriendRequestProtocol isFriendRequestFromBeforeRestoration:envelope]) {
[LKLogger print:@"[Loki] Ignoring friend request from before restoration."];
return;
}
// Loki: Ignore any duplicate sync transcripts
if ([LKSyncMessagesProtocol isDuplicateSyncMessage:contentProto fromHexEncodedPublicKey:envelope.source]) { return; }
@ -452,7 +452,7 @@ NS_ASSUME_NONNULL_BEGIN
}
// Loki: Handle session restoration request if needed
if ([LKSessionManagementProtocol isSessionRestoreMessage:contentProto.dataMessage]) { return; } // Don't process the message any further
if ([LKSessionManagementProtocol isSessionRestorationRequest:contentProto.dataMessage]) { return; } // Don't process the message any further
// Loki: Handle friend request acceptance if needed
[LKFriendRequestProtocol handleFriendRequestAcceptanceIfNeeded:envelope in:transaction];
@ -1033,8 +1033,8 @@ NS_ASSUME_NONNULL_BEGIN
}
// Loki: Handle session reset
NSString *hexEncodedPublicKey = envelope.source;
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:hexEncodedPublicKey transaction:transaction];
NSString *sender = envelope.source;
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:sender transaction:transaction];
[LKSessionManagementProtocol handleEndSessionMessageReceivedInThread:thread using:transaction];
}
@ -1434,11 +1434,10 @@ NS_ASSUME_NONNULL_BEGIN
serverTimestamp:serverTimestamp
wasReceivedByUD:wasReceivedByUD];
// Loki: Parse Loki specific properties if needed
/*
if (envelope.isPtpMessage) { incomingMessage.isP2P = YES; }
*/
if (dataMessage.publicChatInfo != nil && dataMessage.publicChatInfo.hasServerID) { incomingMessage.openGroupServerMessageID = dataMessage.publicChatInfo.serverID; }
// Loki: Set open group server ID if needed
if (dataMessage.publicChatInfo != nil && dataMessage.publicChatInfo.hasServerID) {
incomingMessage.openGroupServerMessageID = dataMessage.publicChatInfo.serverID;
}
NSArray<TSAttachmentPointer *> *attachmentPointers =
[TSAttachmentPointer attachmentPointersFromProtos:dataMessage.attachments
@ -1529,15 +1528,9 @@ NS_ASSUME_NONNULL_BEGIN
serverTimestamp:serverTimestamp
wasReceivedByUD:wasReceivedByUD];
// TODO: Are we sure this works correctly with multi device?
[LKSessionMetaProtocol updateDisplayNameIfNeededForHexEncodedPublicKey:incomingMessage.authorId using:dataMessage appendingShortID:YES in:transaction];
[LKSessionMetaProtocol updateProfileKeyIfNeededForHexEncodedPublicKey:thread.contactIdentifier using:dataMessage];
// Loki: Parse Loki specific properties if needed
/*
if (envelope.isPtpMessage) { incomingMessage.isP2P = YES; }
*/
NSArray<TSAttachmentPointer *> *attachmentPointers =
[TSAttachmentPointer attachmentPointersFromProtos:dataMessage.attachments albumMessage:incomingMessage];
for (TSAttachmentPointer *pointer in attachmentPointers) {
@ -1545,15 +1538,28 @@ NS_ASSUME_NONNULL_BEGIN
[incomingMessage.attachmentIds addObject:pointer.uniqueId];
}
// Loki: Do this before the check below
// Loki: Handle friend request if needed
[LKFriendRequestProtocol handleFriendRequestMessageIfNeededFromEnvelope:envelope using:transaction];
if (body.length == 0 && attachmentPointers.count < 1 && !contact) {
OWSLogWarn(@"Ignoring empty incoming message from: %@ with timestamp: %lu.",
hexEncodedPublicKey,
(unsigned long)timestamp);
if (envelope.type == SSKProtoEnvelopeTypeFriendRequest) {
// Loki: This is needed for compatibility with refactored desktop clients
[LKSessionManagementProtocol sendSessionEstablishedMessageToPublicKey:hexEncodedPublicKey in:transaction];
} else {
OWSLogWarn(@"Ignoring empty incoming message from: %@ with timestamp: %lu.", hexEncodedPublicKey, (unsigned long)timestamp);
}
return nil;
}
// Loki: This is needed for compatibility with refactored desktop clients
LKFriendRequestStatus friendRequestStatus = [self.primaryStorage getFriendRequestStatusForContact:hexEncodedPublicKey transaction:transaction];
if (friendRequestStatus == LKFriendRequestStatusNone || friendRequestStatus == LKFriendRequestStatusRequestExpired) {
[self.primaryStorage setFriendRequestStatus:LKFriendRequestStatusRequestReceived forContact:hexEncodedPublicKey transaction:transaction];
} else if (friendRequestStatus == LKFriendRequestStatusRequestSent) {
[self.primaryStorage setFriendRequestStatus:LKFriendRequestStatusFriends forContact:hexEncodedPublicKey transaction:transaction];
[LKFriendRequestProtocol sendFriendRequestAcceptedMessageToPublicKey:hexEncodedPublicKey using:transaction];
[LKSyncMessagesProtocol syncContactWithPublicKey:masterHexEncodedPublicKey in:transaction];
}
// Loki: If we received a message from a contact in the last 2 minutes that wasn't P2P, then we need to ping them.
// We assume this occurred because they don't have our P2P details.

View file

@ -13,6 +13,7 @@
#import "OWSDevice.h"
#import "OWSDisappearingMessagesJob.h"
#import "OWSDispatch.h"
#import "OWSEndSessionMessage.h"
#import "OWSError.h"
#import "OWSIdentityManager.h"
#import "OWSMessageServiceParams.h"
@ -1191,6 +1192,9 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
}
NSDictionary *signalMessageInfo = deviceMessages.firstObject;
SSKProtoEnvelopeType type = ((NSNumber *)signalMessageInfo[@"type"]).integerValue;
if ([message isKindOfClass:OWSEndSessionMessage.class]) {
type = SSKProtoEnvelopeTypeFriendRequest;
}
uint64_t timestamp = message.timestamp;
NSString *senderID = type == SSKProtoEnvelopeTypeUnidentifiedSender ? @"" : userHexEncodedPublicKey;
uint32_t senderDeviceID = type == SSKProtoEnvelopeTypeUnidentifiedSender ? 0 : OWSDevicePrimaryDeviceId;