Skip to content

Commit a6e1747

Browse files
committed
Added remove buddy action
1 parent 4e52fd5 commit a6e1747

File tree

4 files changed

+59
-36
lines changed

4 files changed

+59
-36
lines changed

ChatSecure/Classes/Controllers/MessageQueueHandler.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,41 @@ public class MessageQueueHandler:NSObject {
318318
completion(false, self.accountRetryTimeout)
319319
}
320320
}
321-
321+
322+
fileprivate func removeBuddyFromRoster(_ removeBuddyAction:OTRYapRemoveBuddyAction, completion:@escaping (_ success: Bool, _ retryTimeout: TimeInterval) -> Void) {
323+
324+
var acc:OTRAccount? = nil
325+
self.databaseConnection.read({ (transaction) in
326+
if let accountKey = removeBuddyAction.accountKey {
327+
acc = OTRAccount.fetchObject(withUniqueID: accountKey, transaction: transaction)
328+
}
329+
})
330+
guard let account = acc else {
331+
completion(true, 0.0)
332+
return
333+
}
334+
335+
//Get the XMPP procol manager associated with this message and therefore account
336+
guard let accountProtocol = OTRProtocolManager.sharedInstance().protocol(for: account) as? OTRXMPPManager else {
337+
completion(true, 0.0)
338+
return
339+
}
340+
341+
//Ensure protocol is connected or if not and autologin then connnect
342+
if (accountProtocol.connectionStatus == .connected) {
343+
// Add the buddy to our roster
344+
let jid = XMPPJID(string: removeBuddyAction.buddyJid)
345+
accountProtocol.xmppRoster.removeUser(jid)
346+
completion(true, 0.0)
347+
} else if (account.autologin == true) {
348+
self.waitingForAccount(account.uniqueId, action: OutstandingActionInfo(action: removeBuddyAction, timer: nil, completion: completion))
349+
accountProtocol.connectUserInitiated(false)
350+
} else {
351+
// Retry later
352+
completion(false, self.accountRetryTimeout)
353+
}
354+
}
355+
322356

323357
//Mark: Callback for Account
324358

@@ -441,6 +475,8 @@ extension MessageQueueHandler: YapTaskQueueHandler {
441475
self.sendMessage(sendMessageAction, completion: completion)
442476
case let addBuddyAction as OTRYapAddBuddyAction:
443477
self.addBuddyToRoster(addBuddyAction, completion: completion)
478+
case let removeBuddyAction as OTRYapRemoveBuddyAction:
479+
self.removeBuddyFromRoster(removeBuddyAction, completion: completion)
444480
default: break
445481
}
446482
}

ChatSecure/Classes/Controllers/XMPP/OTRXMPPManager.m

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,8 @@ -(void)sendChatState:(OTRChatState)chatState withBuddyID:(NSString *)buddyUnique
11491149
- (void) addBuddy:(OTRXMPPBuddy *)newBuddy
11501150
{
11511151
[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
1152-
OTRYapAddBuddyAction *addBuddyAction = [[OTRYapAddBuddyAction alloc] initWithBuddyKey:newBuddy.uniqueId];
1152+
OTRYapAddBuddyAction *addBuddyAction = [[OTRYapAddBuddyAction alloc] init];
1153+
addBuddyAction.buddyKey = newBuddy.uniqueId;
11531154
[addBuddyAction saveWithTransaction:transaction];
11541155
}];
11551156
}
@@ -1162,13 +1163,17 @@ - (void) setDisplayName:(NSString *) newDisplayName forBuddy:(OTRXMPPBuddy *)bud
11621163
}
11631164
-(void)removeBuddies:(NSArray *)buddies
11641165
{
1165-
for (OTRXMPPBuddy *buddy in buddies){
1166-
XMPPJID * jid = [XMPPJID jidWithString:buddy.username];
1167-
[self.xmppRoster removeUser:jid];
1168-
}
1169-
1170-
11711166
[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
1167+
1168+
// Add actions to the queue
1169+
for (OTRXMPPBuddy *buddy in buddies){
1170+
OTRYapRemoveBuddyAction *removeBuddyAction = [[OTRYapRemoveBuddyAction alloc] init];
1171+
removeBuddyAction.buddyKey = buddy.uniqueId;
1172+
removeBuddyAction.buddyJid = buddy.username;
1173+
removeBuddyAction.accountKey = buddy.accountUniqueId;
1174+
[removeBuddyAction saveWithTransaction:transaction];
1175+
}
1176+
11721177
[transaction removeObjectsForKeys:[buddies valueForKey:NSStringFromSelector(@selector(uniqueId))] inCollection:[OTRXMPPBuddy collection]];
11731178
}];
11741179

ChatSecure/Classes/Model/Yap Storage/YapActions/BuddyActions.swift

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,8 @@ open class BuddyAction: OTRYapDatabaseObject, YapActionable {
7575
}
7676

7777
open class OTRYapBuddyAction :OTRYapDatabaseObject, YapTaskQueueAction {
78-
var buddyKey:String = ""
79-
var date:Date = Date()
80-
var accountKey:String?
81-
82-
public override init() {
83-
super.init()
84-
}
85-
86-
public init(buddyKey:String) {
87-
super.init()
88-
self.buddyKey = buddyKey
89-
self.date = Date()
90-
}
78+
open var buddyKey:String = ""
79+
open var date:Date = Date()
9180

9281
override open var uniqueId: String {
9382
return buddyKey
@@ -96,18 +85,6 @@ open class OTRYapBuddyAction :OTRYapDatabaseObject, YapTaskQueueAction {
9685
override open static func collection() -> String {
9786
return OTRYapMessageSendAction.collection()
9887
}
99-
100-
required public init(dictionary dictionaryValue: [AnyHashable : Any]!) throws {
101-
try super.init(dictionary: dictionaryValue)
102-
}
103-
104-
required public init!(coder: NSCoder!) {
105-
super.init(coder: coder)
106-
}
107-
108-
required public init?(uniqueId: String) {
109-
super.init(uniqueId: uniqueId)
110-
}
11188

11289
/// The yap key of this item
11390
public func yapKey() -> String {
@@ -141,3 +118,7 @@ open class OTRYapAddBuddyAction :OTRYapBuddyAction, YapDatabaseRelationshipNode
141118
}
142119
}
143120

121+
open class OTRYapRemoveBuddyAction :OTRYapBuddyAction {
122+
open var accountKey:String?
123+
open var buddyJid:String?
124+
}

ChatSecure/Classes/View Controllers/OTRComposeViewController.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,10 @@ - (void)removeThreadOwner:(id<OTRThreadOwner>)threadOwner {
456456
[self.readWriteConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction * _Nonnull transaction) {
457457
OTRBuddy *dbBuddy = [OTRBuddy fetchObjectWithUniqueID:key transaction:transaction];
458458
if (dbBuddy) {
459-
BuddyAction *action = [[BuddyAction alloc] init];
460-
action.buddy = dbBuddy;
461-
action.action = BuddyActionTypeDelete;
459+
OTRYapRemoveBuddyAction *action = [[OTRYapRemoveBuddyAction alloc] init];
460+
action.buddyKey = dbBuddy.uniqueId;
461+
action.buddyJid = dbBuddy.username;
462+
action.accountKey = dbBuddy.accountUniqueId;
462463
[action saveWithTransaction:transaction];
463464
[dbBuddy removeWithTransaction:transaction];
464465
}

0 commit comments

Comments
 (0)