Reduce code duplication

pull/63/head
Niels Andriesse 6 years ago
parent d36bd5d143
commit 4bdb689526

@ -543,16 +543,11 @@ NS_ASSUME_NONNULL_BEGIN
if (message.isFriendRequest) {
// Only show an incoming friend request if the user isn't yet friends with any of the other user's devices
NSString *senderID = ((TSIncomingMessage *)message).authorId;
NSMutableSet<TSContactThread *> *threads = [NSMutableSet new];
__block NSSet<TSContactThread *> *linkedDeviceThreads;
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
NSString *masterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:senderID in:transaction] ?: senderID;
NSSet<LKDeviceLink *> *deviceLinks = [LKDatabaseUtilities getDeviceLinksFor:masterHexEncodedPublicKey in:transaction];
for (LKDeviceLink *deviceLink in deviceLinks) {
[threads addObject:[TSContactThread getThreadWithContactId:deviceLink.master.hexEncodedPublicKey transaction:transaction]];
[threads addObject:[TSContactThread getThreadWithContactId:deviceLink.slave.hexEncodedPublicKey transaction:transaction]];
}
linkedDeviceThreads = [LKDatabaseUtilities getLinkedDeviceThreadsFor:senderID in:transaction];
}];
BOOL isFriend = [threads contains:^BOOL(NSObject *object) {
BOOL isFriend = [linkedDeviceThreads contains:^BOOL(NSObject *object) {
TSContactThread *thread = (TSContactThread *)object;
return thread.isContactFriend;
}];

@ -1647,16 +1647,10 @@ typedef enum : NSUInteger {
BOOL isEnabled;
BOOL isAttachmentButtonHidden;
if ([self.thread isKindOfClass:TSContactThread.class]) {
TSContactThread *thread = (TSContactThread *)self.thread;
NSMutableSet<TSContactThread *> *linkedDeviceThreads = [NSMutableSet new];
NSString *senderID = thread.contactIdentifier;
NSString *senderID = ((TSContactThread *)self.thread).contactIdentifier;
__block NSSet<TSContactThread *> *linkedDeviceThreads;
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
NSString *masterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:senderID in:transaction] ?: senderID;
NSSet<LKDeviceLink *> *deviceLinks = [LKDatabaseUtilities getDeviceLinksFor:masterHexEncodedPublicKey in:transaction];
for (LKDeviceLink *deviceLink in deviceLinks) {
[linkedDeviceThreads addObject:[TSContactThread getThreadWithContactId:deviceLink.master.hexEncodedPublicKey transaction:transaction]];
[linkedDeviceThreads addObject:[TSContactThread getThreadWithContactId:deviceLink.slave.hexEncodedPublicKey transaction:transaction]];
}
linkedDeviceThreads = [LKDatabaseUtilities getLinkedDeviceThreadsFor:senderID in:transaction];
}];
if ([linkedDeviceThreads contains:^BOOL(NSObject *object) {
return ((TSContactThread *)object).isContactFriend;
@ -4463,16 +4457,11 @@ typedef enum : NSUInteger {
// Accept all outstanding friend requests associated with this user and try to establish sessions with the
// subset of their devices that haven't sent a friend request.
NSString *senderID = friendRequest.authorId;
NSMutableSet<TSContactThread *> *threads = [NSMutableSet new];
__block NSSet<TSContactThread *> *linkedDeviceThreads;
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
NSString *masterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:senderID in:transaction] ?: senderID;
NSSet<LKDeviceLink *> *deviceLinks = [LKDatabaseUtilities getDeviceLinksFor:masterHexEncodedPublicKey in:transaction];
for (LKDeviceLink *deviceLink in deviceLinks) {
[threads addObject:[TSContactThread getThreadWithContactId:deviceLink.master.hexEncodedPublicKey transaction:transaction]];
[threads addObject:[TSContactThread getThreadWithContactId:deviceLink.slave.hexEncodedPublicKey transaction:transaction]];
}
linkedDeviceThreads = [LKDatabaseUtilities getLinkedDeviceThreadsFor:senderID in:transaction];
}];
for (TSContactThread *thread in threads) {
for (TSContactThread *thread in linkedDeviceThreads) {
if (thread.hasPendingFriendRequest) {
[ThreadUtil enqueueFriendRequestAcceptanceMessageInThread:self.thread];
} else {

@ -22,6 +22,21 @@ public final class LokiDatabaseUtilities : NSObject {
}
// MARK: Device Links
@objc(getLinkedDeviceHexEncodedPublicKeysFor:in:)
public static func getLinkedDeviceHexEncodedPublicKeys(for hexEncodedPublicKey: String, in transaction: YapDatabaseReadWriteTransaction) -> Set<String> {
var result = Set<TSContactThread>()
let storage = OWSPrimaryStorage.shared()
let masterHexEncodedPublicKey = storage.getMasterHexEncodedPublicKey(for: hexEncodedPublicKey, in: transaction) ?? hexEncodedPublicKey
return Set(storage.getDeviceLinks(for: masterHexEncodedPublicKey, in: transaction).flatMap { deviceLink in
return [ deviceLink.master.hexEncodedPublicKey, deviceLink.slave.hexEncodedPublicKey ]
})
}
@objc(getLinkedDeviceThreadsFor:in:)
public static func getLinkedDeviceThreads(for hexEncodedPublicKey: String, in transaction: YapDatabaseReadWriteTransaction) -> Set<TSContactThread> {
return Set(getLinkedDeviceHexEncodedPublicKeys(for: hexEncodedPublicKey, in: transaction).compactMap { TSContactThread.getWithContactId($0, transaction: transaction) })
}
@objc(getMasterHexEncodedPublicKeyFor:in:)
public static func objc_getMasterHexEncodedPublicKey(for slaveHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> String? {
return OWSPrimaryStorage.shared().getMasterHexEncodedPublicKey(for: slaveHexEncodedPublicKey, in: transaction)

@ -1536,6 +1536,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)canFriendRequestBeAutoAcceptedForThread:(TSContactThread *)thread transaction:(YapDatabaseReadWriteTransaction *)transaction
{
NSString *senderHexEncodedPublicKey = thread.contactIdentifier;
if (thread.hasCurrentUserSentFriendRequest) {
// This can happen if Alice sent Bob a friend request, Bob declined, but then Bob changed his
// mind and sent a friend request to Alice. In this case we want Alice to auto-accept the request
@ -1549,26 +1550,13 @@ NS_ASSUME_NONNULL_BEGIN
// LKThreadFriendRequestStatusRequestSent.
return YES;
}
NSString *senderHexEncodedPublicKey = thread.contactIdentifier;
NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey;
NSString *userMasterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:userHexEncodedPublicKey in:transaction];
NSMutableSet<NSString *> *userLinkedDeviceHexEncodedPublicKeys = [NSMutableSet new];
NSSet<LKDeviceLink *> *userDeviceLinks = [LKDatabaseUtilities getDeviceLinksFor:userMasterHexEncodedPublicKey in:transaction];
for (LKDeviceLink *deviceLink in userDeviceLinks) {
[userLinkedDeviceHexEncodedPublicKeys addObject:deviceLink.master.hexEncodedPublicKey];
[userLinkedDeviceHexEncodedPublicKeys addObject:deviceLink.slave.hexEncodedPublicKey];
}
NSSet<NSString *> *userLinkedDeviceHexEncodedPublicKeys = [LKDatabaseUtilities getLinkedDeviceHexEncodedPublicKeysFor:userHexEncodedPublicKey in:transaction];
if ([userLinkedDeviceHexEncodedPublicKeys containsObject:senderHexEncodedPublicKey]) {
// Auto-accept any friend requests from the user's own linked devices
return YES;
}
NSString *senderMasterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:senderHexEncodedPublicKey in:transaction];
NSMutableSet<TSContactThread *> *senderLinkedDeviceThreads = [NSMutableSet new];
NSSet<LKDeviceLink *> *senderDeviceLinks = [LKDatabaseUtilities getDeviceLinksFor:senderMasterHexEncodedPublicKey in:transaction];
for (LKDeviceLink *deviceLink in senderDeviceLinks) {
[senderLinkedDeviceThreads addObject:[TSContactThread getThreadWithContactId:deviceLink.master.hexEncodedPublicKey transaction:transaction]];
[senderLinkedDeviceThreads addObject:[TSContactThread getThreadWithContactId:deviceLink.slave.hexEncodedPublicKey transaction:transaction]];
}
NSSet<TSContactThread *> *senderLinkedDeviceThreads = [LKDatabaseUtilities getLinkedDeviceThreadsFor:senderHexEncodedPublicKey in:transaction];
if ([senderLinkedDeviceThreads contains:^BOOL(NSObject *object) {
TSContactThread *thread = (TSContactThread *)object;
return thread.isContactFriend;
@ -1611,16 +1599,11 @@ NS_ASSUME_NONNULL_BEGIN
// there's only ever one message with status LKMessageFriendRequestStatusPending in a thread (where a thread is the combination
// of all threads belonging to the linked devices of a user).
NSString *senderID = ((TSIncomingMessage *)message).authorId;
NSMutableSet<TSContactThread *> *threads = [NSMutableSet new];
__block NSSet<TSContactThread *> *linkedDeviceThreads;
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
NSString *masterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:senderID in:transaction] ?: senderID;
NSSet<LKDeviceLink *> *deviceLinks = [LKDatabaseUtilities getDeviceLinksFor:masterHexEncodedPublicKey in:transaction];
for (LKDeviceLink *deviceLink in deviceLinks) {
[threads addObject:[TSContactThread getThreadWithContactId:deviceLink.master.hexEncodedPublicKey transaction:transaction]];
[threads addObject:[TSContactThread getThreadWithContactId:deviceLink.slave.hexEncodedPublicKey transaction:transaction]];
}
linkedDeviceThreads = [LKDatabaseUtilities getLinkedDeviceThreadsFor:senderID in:transaction];
}];
for (TSContactThread *thread in threads) {
for (TSContactThread *thread in linkedDeviceThreads) {
[thread enumerateInteractionsWithTransaction:transaction usingBlock:^(TSInteraction *interaction, YapDatabaseReadTransaction *transaction) {
if (![interaction isKindOfClass:TSIncomingMessage.class]) { return; }
TSIncomingMessage *message = (TSIncomingMessage *)interaction;

Loading…
Cancel
Save