Skip to content

Commit

Permalink
Just store buddyUniqueId on room occupant object
Browse files Browse the repository at this point in the history
And add an accessor function for the buddy object.
  • Loading branch information
N-Pex committed Nov 2, 2017
1 parent 35a8874 commit 1bc4b81
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
7 changes: 7 additions & 0 deletions ChatSecure/Classes/Controllers/XMPP/OTRXMPPRoomYapStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ - (OTRXMPPRoomOccupant *)roomOccupantForJID:(NSString *)jid realJID:(NSString *)
occupant.realJID = realJID;
}

// Fill in related buddy object if we have it
if (occupant.realJID) {
OTRBuddy *buddy = [OTRBuddy fetchBuddyWithUsername:occupant.realJID withAccountUniqueId:accountId transaction:transaction];
if (buddy) {
occupant.buddyUniqueId = buddy.uniqueId;
}
}
return occupant;
}

Expand Down
44 changes: 16 additions & 28 deletions ChatSecure/Classes/Model/Yap Storage/OTRXMPPRoomOccupant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,20 @@ open class OTRXMPPRoomOccupant: OTRYapDatabaseObject, YapDatabaseRelationshipNod
@objc open var affiliation:RoomOccupantAffiliation = .none

/**When given by the server we get the room participants reall JID*/
@objc open var realJID:String? {
didSet {
if let realJid = self.realJID, let roomUniqueId = self.roomUniqueId {
OTRDatabaseManager.shared.readOnlyDatabaseConnection?.asyncRead({ (transaction) in
if let room = OTRXMPPRoom.fetchObject(withUniqueID: roomUniqueId, transaction: transaction), let accountUniqueId = room.accountUniqueId {
self.realBuddy = OTRBuddy.fetch(withUsername: realJid, withAccountUniqueId: accountUniqueId, transaction: transaction)
}
})
}
}
}
@objc open var realBuddy:OTRBuddy?

@objc open var realJID:String?

@objc open var buddyUniqueId:String?
@objc open var roomUniqueId:String?

@objc open func avatarImage() -> UIImage {
if let buddy = self.realBuddy {
return buddy.avatarImage
if self.buddyUniqueId != nil {
var buddy:OTRXMPPBuddy?
OTRDatabaseManager.shared.readOnlyDatabaseConnection?.read({ (transaction) in
buddy = self.buddy(with: transaction)
})
if let buddy = buddy {
return buddy.avatarImage
}
}
return OTRImages.avatarImage(withUniqueIdentifier: self.uniqueId, avatarData: nil, displayName: roomName ?? realJID ?? jid, username: self.realJID)
}
Expand All @@ -95,19 +91,11 @@ open class OTRXMPPRoomOccupant: OTRYapDatabaseObject, YapDatabaseRelationshipNod
}
return nil
}

// MARK: Disable Mantle Storage of Dynamic Properties

override open class func encodingBehaviorsByPropertyKey() -> [AnyHashable:Any]? {
var ret = super.encodingBehaviorsByPropertyKey()
ret?["realBuddy"] = MTLModelEncodingBehaviorExcluded
return ret
}

override open class func storageBehaviorForProperty(withKey propertyKey:String) -> MTLPropertyStorage {
if propertyKey.compare("realBuddy") == .orderedSame {
return MTLPropertyStorageNone

@objc open func buddy(with transaction: YapDatabaseReadTransaction) -> OTRXMPPBuddy? {
if let buddyUniqueId = self.buddyUniqueId {
return OTRXMPPBuddy.fetchObject(withUniqueID: buddyUniqueId, transaction: transaction)
}
return super.storageBehaviorForProperty(withKey: propertyKey)
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1751,8 +1751,9 @@ - (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionVi
[self.readOnlyDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
OTRXMPPRoomOccupant *occupant = [self occupantWithTransaction:transaction forFullJid:roomMessage.senderJID inRoom:roomMessage.roomUniqueId];
if (occupant) {
if ([occupant realBuddy] != nil) {
displayName = [[occupant realBuddy] displayName];
OTRXMPPBuddy *buddy = [occupant buddyWith:transaction];
if (buddy) {
displayName = [buddy displayName];
} else {
displayName = [[XMPPJID jidWithString:occupant.jid] resource];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ open class OTRRoomOccupantsViewController: UIViewController {
default: break
}
}
func didChangeMuteSwitch(_ sender: UIControl!) {
@objc func didChangeMuteSwitch(_ sender: UIControl!) {
if let room = self.room {
if room.isMuted {
room.muteExpiration = nil
Expand Down Expand Up @@ -360,13 +360,9 @@ extension OTRRoomOccupantsViewController: UITableViewDataSource {
cell.setCheckImage(image: self.crownImage)
var buddy:OTRXMPPBuddy? = nil
if let roomOccupant = self.viewHandler?.object(indexPath) as? OTRXMPPRoomOccupant, let room = self.room, let jid = roomOccupant.realJID ?? roomOccupant.jid, let account = room.accountUniqueId {
buddy = roomOccupant.realBuddy as? OTRXMPPBuddy
if buddy == nil {
OTRDatabaseManager.shared.readOnlyDatabaseConnection?.read({ (transaction) in
buddy = OTRXMPPBuddy.fetch(withUsername: jid, withAccountUniqueId: account, transaction: transaction)
})
roomOccupant.realBuddy = buddy
}
OTRDatabaseManager.shared.readOnlyDatabaseConnection?.read({ (transaction) in
buddy = OTRXMPPBuddy.fetch(withUsername: jid, withAccountUniqueId: account, transaction: transaction)
})
if let buddy = buddy {
cell.setThread(buddy, account: nil)
if let occupantJid = roomOccupant.jid, let ownJid = ownOccupant?.jid, occupantJid.compare(ownJid) == .orderedSame {
Expand Down

0 comments on commit 1bc4b81

Please sign in to comment.