Skip to content

Commit

Permalink
Merge pull request #809 from zom/friending_problems
Browse files Browse the repository at this point in the history
Friending problems
  • Loading branch information
chrisballinger authored Jul 26, 2017
2 parents 1f93359 + dbd958a commit 6bb23ab
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 9 deletions.
10 changes: 9 additions & 1 deletion ChatSecure/Classes/Controllers/OTROMEMOSignalCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import SignalProtocolObjC
* This is the glue between XMPP/OMEMO and Signal
*/
@objc open class OTROMEMOSignalCoordinator: NSObject {

public static let DeviceListUpdateNotificationName = Notification.Name(rawValue: "DeviceListUpdateNotification")

open let signalEncryptionManager:OTRAccountSignalEncryptionManager
open let omemoStorageManager:OTROMEMOStorageManager
Expand Down Expand Up @@ -617,7 +619,13 @@ extension OTROMEMOSignalCoordinator:OMEMOStorageDelegate {
if (isOurDeviceList) {
self.omemoStorageManager.storeOurDevices(deviceIds)
} else {
self.omemoStorageManager.storeBuddyDevices(deviceIds, buddyUsername: jid.bare())
self.omemoStorageManager.storeBuddyDevices(deviceIds, buddyUsername: jid.bare(), completion: {() -> Void in

//Devices updated for buddy
DispatchQueue.main.async {
NotificationCenter.default.post(name: OTROMEMOSignalCoordinator.DeviceListUpdateNotificationName, object: self, userInfo: ["jid":jid])
}
})
}
}

Expand Down
5 changes: 4 additions & 1 deletion ChatSecure/Classes/Controllers/OTROMEMOStorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ open class OTROMEMOStorageManager {
- parameter devices: An array of the device numbers. Should be UInt32.
- parameter buddyUsername: The bare JID for the buddy.
*/
open func storeBuddyDevices(_ devices:[NSNumber], buddyUsername:String) {
open func storeBuddyDevices(_ devices:[NSNumber], buddyUsername:String, completion:(()->Void)?) {
self.databaseConnection.asyncReadWrite { (transaction) in
// Fetch the buddy from the database.
var buddy = OTRBuddy.fetch(withUsername: buddyUsername, withAccountUniqueId: self.accountKey, transaction: transaction)
Expand All @@ -167,6 +167,9 @@ open class OTROMEMOStorageManager {
}
if let bud = buddy {
self.storeDevices(devices, parentYapKey: bud.uniqueId, parentYapCollection: type(of: bud).collection, transaction: transaction)
if let completion = completion {
completion()
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ - (void)handlePresence:(XMPPPresence *)presence xmppStream:(XMPPStream *)stream
if (!buddy) { return; }
buddy.pendingApproval = NO;
[buddy saveWithTransaction:transaction];
[[NSNotificationCenter defaultCenter] postNotificationName:OTRBuddyPendingApprovalDidChangeNotification object:self userInfo:@{@"buddy": buddy}];
}];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ - (void) handleSubscriptionRequest:(OTRXMPPPresenceSubscriptionRequest*)request
buddy.lastMessageId = @"";
}
buddy.displayName = request.jid;
buddy.pendingApproval = YES;
[buddy saveWithTransaction:transaction];
}];
[manager.xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:YES];
Expand Down
64 changes: 57 additions & 7 deletions ChatSecure/Classes/View Controllers/OTRMessagesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ @interface OTRMessagesViewController () <UITextViewDelegate, OTRAttachmentPicker

@property (nonatomic, weak) id didFinishGeneratingPrivateKeyNotificationObject;
@property (nonatomic, weak) id messageStateDidChangeNotificationObject;
@property (nonatomic, weak) id pendingApprovalDidChangeNotificationObject;
@property (nonatomic, weak) id deviceListUpdateNotificationObject;


@property (nonatomic ,strong) UIBarButtonItem *lockBarButtonItem;
@property (nonatomic, strong) OTRLockButton *lockButton;
Expand Down Expand Up @@ -355,7 +358,38 @@ - (void)setThreadKey:(NSString *)key collection:(NSString *)collection
[self updateEncryptionState];
[self updateJIDForwardingHeader];

__weak typeof(self)weakSelf = self;
if (self.pendingApprovalDidChangeNotificationObject == nil) {
self.pendingApprovalDidChangeNotificationObject = [[NSNotificationCenter defaultCenter] addObserverForName:OTRBuddyPendingApprovalDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
__strong typeof(weakSelf)strongSelf = weakSelf;
OTRXMPPBuddy *notificationBuddy = [note.userInfo objectForKey:@"buddy"];
__block NSString *buddyKey = nil;
[strongSelf.readOnlyDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
buddyKey = [strongSelf buddyWithTransaction:transaction].uniqueId;
}];
if ([notificationBuddy.uniqueId isEqualToString:buddyKey]) {
[strongSelf fetchOMEMODeviceList];
[strongSelf sendPresenceProbe];
}
}];
}

if (self.deviceListUpdateNotificationObject == nil) {
self.deviceListUpdateNotificationObject = [[NSNotificationCenter defaultCenter] addObserverForName:OTROMEMOSignalCoordinator.DeviceListUpdateNotificationName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
__strong typeof(weakSelf)strongSelf = weakSelf;
XMPPJID *notificationJid = [note.userInfo objectForKey:@"jid"];
__block NSString *buddyUser = nil;
[strongSelf.readOnlyDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
buddyUser = [strongSelf buddyWithTransaction:transaction].username;
}];
if (notificationJid != nil && [notificationJid.bare isEqualToString:buddyUser]) {
[strongSelf updateEncryptionState];
}
}];
}

[self sendPresenceProbe];
[self fetchOMEMODeviceList];
}


Expand Down Expand Up @@ -390,7 +424,7 @@ - (void) sendPresenceProbe {
xmpp = [self xmppManagerWithTransaction:transaction];
buddy = (OTRXMPPBuddy*)[self buddyWithTransaction:transaction];
}];
if (!xmpp || ![buddy isKindOfClass:[OTRXMPPBuddy class]]) { return; }
if (!xmpp || ![buddy isKindOfClass:[OTRXMPPBuddy class]] || buddy.pendingApproval) { return; }
[xmpp sendPresenceProbeForBuddy:buddy];
}

Expand Down Expand Up @@ -770,12 +804,7 @@ - (void) infoButtonPressed:(id)sender {

// Hack to manually re-fetch OMEMO devicelist because PEP sucks
// TODO: Ideally this should be moved to some sort of manual refresh in the Profile view
id manager = [[OTRProtocolManager sharedInstance] protocolForAccount:account];
if ([manager isKindOfClass:[OTRXMPPManager class]]) {
XMPPJID *jid = [XMPPJID jidWithString:buddy.username];
OTRXMPPManager *xmpp = manager;
[xmpp.omemoSignalCoordinator.omemoModule fetchDeviceIdsForJID:jid elementId:nil];
}
[self fetchOMEMODeviceList];

XLFormDescriptor *form = [UserProfileViewController profileFormDescriptorForAccount:account buddies:@[buddy] connection:self.readOnlyDatabaseConnection];

Expand All @@ -788,6 +817,27 @@ - (void) infoButtonPressed:(id)sender {
[self presentViewController:verifyNav animated:YES completion:nil];
}


// Hack to manually re-fetch OMEMO devicelist because PEP sucks
// TODO: Ideally this should be moved to some sort of manual refresh in the Profile view
-(void) fetchOMEMODeviceList {
__block OTRAccount *account = nil;
__block OTRBuddy *buddy = nil;
[self.readOnlyDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
account = [self accountWithTransaction:transaction];
buddy = [self buddyWithTransaction:transaction];
}];
if (!account || !buddy) {
return;
}
id manager = [[OTRProtocolManager sharedInstance] protocolForAccount:account];
if ([manager isKindOfClass:[OTRXMPPManager class]]) {
XMPPJID *jid = [XMPPJID jidWithString:buddy.username];
OTRXMPPManager *xmpp = manager;
[xmpp.omemoSignalCoordinator.omemoModule fetchDeviceIdsForJID:jid elementId:nil];
}
}

- (UIBarButtonItem *)rightBarButtonItem
{
if (!self.lockBarButtonItem) {
Expand Down

0 comments on commit 6bb23ab

Please sign in to comment.