Skip to content

Commit

Permalink
Fetch occupant either by jid or real jid, to avoid dups
Browse files Browse the repository at this point in the history
And use members list. Show members with role "none" as grayed out,
since they are not currently occupants.

Also, don't show local notifications for archived groups and when
fetching history on re-join.
  • Loading branch information
N-Pex committed Oct 2, 2017
1 parent 72be2f0 commit e891156
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 20 deletions.
8 changes: 6 additions & 2 deletions ChatSecure/Classes/Controllers/XMPP/OTRXMPPRoomManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ - (OTRXMPPRoomOccupant * _Nullable) roomOccupantForUser:(XMPPJID *)user inRoom:(
[self.databaseConnection readWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
NSString *roomJID = room.roomJID.bare;
NSString *accountId = room.xmppStream.tag;
occupant = [(OTRXMPPRoomYapStorage*)room.xmppRoomStorage roomOccupantForJID:user.full roomJID:roomJID accountId:accountId inTransaction:transaction];
occupant = [(OTRXMPPRoomYapStorage*)room.xmppRoomStorage roomOccupantForJID:user.full realJID:nil roomJID:roomJID accountId:accountId inTransaction:transaction];
}];
}
return occupant;
Expand Down Expand Up @@ -339,7 +339,11 @@ - (void) xmppRoom:(XMPPRoom *)room didFetchMembersList:(NSArray *)items {
[items enumerateObjectsUsingBlock:^(NSXMLElement *item, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *jid = [item attributeStringValueForName:@"jid"];
if ([jid length]) {
[storage roomOccupantForJID:jid roomJID:room.roomJID.bare accountId:accountId inTransaction:transaction];
OTRXMPPRoomOccupant *occupant = [storage roomOccupantForJID:nil realJID:jid roomJID:room.roomJID.bare accountId:accountId inTransaction:transaction];
if (occupant.jid == nil) {
// Not currently an occupant, we just created this one, need to save.
[occupant saveWithTransaction:transaction];
}
}
}];
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

- (instancetype)initWithDatabaseConnection:(YapDatabaseConnection *)databaseConnection;

- (OTRXMPPRoomOccupant *)roomOccupantForJID:(NSString *)jid roomJID:(NSString *)roomJID accountId:(NSString *)accountId inTransaction:(YapDatabaseReadTransaction *)transaction;
- (OTRXMPPRoomOccupant *)roomOccupantForJID:(NSString *)jid realJID:(NSString *)realJID roomJID:(NSString *)roomJID accountId:(NSString *)accountId inTransaction:(YapDatabaseReadTransaction *)transaction;

- (id <OTRMessageProtocol>)lastMessageInRoom:(XMPPRoom *)room accountKey:(NSString *)accountKey;
@end
30 changes: 16 additions & 14 deletions ChatSecure/Classes/Controllers/XMPP/OTRXMPPRoomYapStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ - (instancetype)initWithDatabaseConnection:(YapDatabaseConnection *)databaseConn
return self;
}

- (OTRXMPPRoomOccupant *)roomOccupantForJID:(NSString *)jid roomJID:(NSString *)roomJID accountId:(NSString *)accountId inTransaction:(YapDatabaseReadTransaction *)transaction
- (OTRXMPPRoomOccupant *)roomOccupantForJID:(NSString *)jid realJID:(NSString *)realJID roomJID:(NSString *)roomJID accountId:(NSString *)accountId inTransaction:(YapDatabaseReadTransaction *)transaction
{
__block OTRXMPPRoomOccupant *occupant = nil;

Expand All @@ -40,8 +40,7 @@ - (OTRXMPPRoomOccupant *)roomOccupantForJID:(NSString *)jid roomJID:(NSString *)
[[transaction ext:extensionName] enumerateEdgesWithName:[OTRXMPPRoomOccupant roomEdgeName] destinationKey:databaseRoom.uniqueId collection:[OTRXMPPRoom collection] usingBlock:^(YapDatabaseRelationshipEdge *edge, BOOL *stop) {

OTRXMPPRoomOccupant *tempOccupant = [transaction objectForKey:edge.sourceKey inCollection:edge.sourceCollection];
if([tempOccupant.jid isEqualToString:jid]) {

if((realJID != nil && [tempOccupant.realJID isEqualToString:realJID]) || (jid != nil && [tempOccupant.jid isEqualToString:jid])) {
occupant = tempOccupant;
*stop = YES;
}
Expand All @@ -50,6 +49,7 @@ - (OTRXMPPRoomOccupant *)roomOccupantForJID:(NSString *)jid roomJID:(NSString *)
if(!occupant) {
occupant = [[OTRXMPPRoomOccupant alloc] init];
occupant.jid = jid;
occupant.realJID = realJID;
occupant.roomUniqueId = [OTRXMPPRoom createUniqueId:accountId jid:roomJID];
}
return occupant;
Expand Down Expand Up @@ -98,6 +98,7 @@ - (void)insertIncomingMessage:(XMPPMessage *)message intoRoom:(XMPPRoom *)room
XMPPJID *fromJID = [message from];

__block OTRXMPPRoomMessage *databaseMessage = nil;
__block OTRXMPPRoom *databaseRoom = nil;
__block OTRAccount *account = nil;
[self.databaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction * _Nonnull transaction) {
account = [OTRAccount fetchObjectWithUniqueID:accountId transaction:transaction];
Expand All @@ -108,7 +109,7 @@ - (void)insertIncomingMessage:(XMPPMessage *)message intoRoom:(XMPPRoom *)room
DDLogVerbose(@"%@: %@ - Duplicate MUC message", THIS_FILE, THIS_METHOD);
return;
}
OTRXMPPRoom *databaseRoom = [self fetchRoomWithXMPPRoomJID:roomJID accountId:accountId inTransaction:transaction];
databaseRoom = [self fetchRoomWithXMPPRoomJID:roomJID accountId:accountId inTransaction:transaction];
if(!databaseRoom) {
databaseRoom = [[OTRXMPPRoom alloc] init];
databaseRoom.lastRoomMessageId = @""; // Hack to make it show up in list
Expand All @@ -134,7 +135,7 @@ - (void)insertIncomingMessage:(XMPPMessage *)message intoRoom:(XMPPRoom *)room
databaseMessage.roomJID = databaseRoom.jid;
databaseMessage.state = RoomMessageStateReceived;
databaseMessage.roomUniqueId = databaseRoom.uniqueId;
OTRXMPPRoomOccupant *occupant = [self roomOccupantForJID:databaseMessage.senderJID roomJID:databaseMessage.roomJID accountId:accountId inTransaction:transaction];
OTRXMPPRoomOccupant *occupant = [self roomOccupantForJID:databaseMessage.senderJID realJID:nil roomJID:databaseMessage.roomJID accountId:accountId inTransaction:transaction];
databaseMessage.displayName = occupant.realJID;

databaseRoom.lastRoomMessageId = [databaseMessage uniqueId];
Expand All @@ -151,7 +152,12 @@ - (void)insertIncomingMessage:(XMPPMessage *)message intoRoom:(XMPPRoom *)room
if(databaseMessage) {
OTRXMPPManager *xmpp = (OTRXMPPManager*)[OTRProtocolManager.shared protocolForAccount:account];
[xmpp.fileTransferManager createAndDownloadItemsIfNeededWithMessage:databaseMessage readConnection:OTRDatabaseManager.shared.readOnlyDatabaseConnection force:NO];
[[UIApplication sharedApplication] showLocalNotification:databaseMessage];
// If delayedDeliveryDate is set we are retrieving history. Don't show
// notifications in that case. Also, don't show notifications for archived
// rooms.
if (!message.delayedDeliveryDate && !databaseRoom.isArchived) {
[[UIApplication sharedApplication] showLocalNotification:databaseMessage];
}
}
}];
}
Expand Down Expand Up @@ -204,7 +210,7 @@ - (void)handlePresence:(XMPPPresence *)presence room:(XMPPRoom *)room {
[self.databaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction * _Nonnull transaction) {


OTRXMPPRoomOccupant *occupant = [self roomOccupantForJID:[presenceJID full] roomJID:room.roomJID.bare accountId:accountId inTransaction:transaction];
OTRXMPPRoomOccupant *occupant = [self roomOccupantForJID:[presenceJID full] realJID:nil roomJID:room.roomJID.bare accountId:accountId inTransaction:transaction];
if ([[presence type] isEqualToString:@"unavailable"]) {
occupant.available = NO;
} else {
Expand All @@ -225,6 +231,8 @@ - (void)handlePresence:(XMPPPresence *)presence room:(XMPPRoom *)room {
occupant.role = RoomOccupantRoleModerator;
} else if ([buddyRole isEqualToString:@"participant"]) {
occupant.role = RoomOccupantRoleParticipant;
} else if ([buddyRole isEqualToString:@"visitor"]) {
occupant.role = RoomOccupantRoleVisitor;
} else {
occupant.role = RoomOccupantRoleNone;
}
Expand All @@ -241,13 +249,7 @@ - (void)handlePresence:(XMPPPresence *)presence room:(XMPPRoom *)room {
} else {
occupant.affiliation = RoomOccupantAffiliationNone;
}

if ([[presence type] isEqualToString:@"unavailable"] && [buddyRole isEqualToString:@"none"]) {
// Buddy left the room!
[occupant removeWithTransaction:transaction];
} else {
[occupant saveWithTransaction:transaction];
}
[occupant saveWithTransaction:transaction];
}];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import YapDatabase
case none = 0
case participant = 1
case moderator = 2
case visitor = 3

public func canModifySubject() -> Bool {
switch self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,12 @@ open class OTRComposeGroupViewController: UIViewController, UICollectionViewDele
for row in 0..<mappings.numberOfItems(inSection: section) {
var buddy:OTRXMPPBuddy? = nil
if let roomOccupant = viewHandler.object(IndexPath(row: Int(row), section: Int(section))) as? OTRXMPPRoomOccupant,

let jid = roomOccupant.realJID ?? roomOccupant.jid, let account = room.accountUniqueId {

// Don't exclude people not currently in the room, we
// want to be able to reinvite them.
guard roomOccupant.role != .none else { continue }

OTRDatabaseManager.shared.readOnlyDatabaseConnection?.read({ (transaction) in
buddy = OTRXMPPBuddy.fetch(withUsername: jid, withAccountUniqueId: account, transaction: transaction)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ open class OTRRoomOccupantsViewController: UIViewController {
public func setupViewHandler(databaseConnection:YapDatabaseConnection, roomKey:String) {
databaseConnection.read({ (transaction) in
self.room = OTRXMPPRoom.fetchObject(withUniqueID: roomKey, transaction: transaction)
if let room = self.room, let manager = self.xmppRoomManager() {
self.ownOccupant = manager.roomOccupant(forUser: XMPPJID(string:room.ownJID), inRoom: XMPPJID(string:room.jid))
if let room = self.room, let manager = self.xmppRoomManager(), let roomJid = room.jid, let ownJid = room.ownJID {
self.ownOccupant = manager.roomOccupant(forUser: XMPPJID(string:ownJid), inRoom: XMPPJID(string:roomJid))
}
self.fetchMembersList()
})
Expand Down Expand Up @@ -363,6 +363,11 @@ extension OTRRoomOccupantsViewController: UITableViewDataSource {
cell.accountLabel.text?.append(Locale.current.groupingSeparator ?? ", ")
}
cell.accountLabel.text?.append(GROUP_ROLE_MODERATOR())
} else if roomOccupant.role == .none {
// Not present in the room
cell.nameLabel.textColor = UIColor.lightGray
cell.identifierLabel.textColor = UIColor.lightGray
cell.accountLabel.textColor = UIColor.lightGray
}
} else if let roomJid = roomOccupant.jid,
let jidStr = roomOccupant.realJID,
Expand Down
7 changes: 7 additions & 0 deletions ChatSecure/Classes/Views/Cells/OTRBuddyInfoCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ - (void)updateConstraints
[super updateConstraints];
}

- (void)prepareForReuse {
[super prepareForReuse];
self.nameLabel.textColor = [UIColor blackColor];
self.identifierLabel.textColor = [UIColor darkTextColor];
self.accountLabel.textColor = [UIColor lightGrayColor];
}

@end

0 comments on commit e891156

Please sign in to comment.