Keep track of who added us to a group

This commit is contained in:
Nora Trapp 2020-06-08 18:32:16 -07:00
parent 899f2177e2
commit 0be05b41eb
2 changed files with 22 additions and 1 deletions

View File

@ -56,7 +56,7 @@ public class ThreadViewModel: NSObject {
self.hasPendingMessageRequest = thread.hasPendingMessageRequest(transaction: transaction.unwrapGrdbRead)
if let groupThread = thread as? TSGroupThread, let addedByAddress = groupThread.groupModel.addedByAddress {
self.addedToGroupByName = Environment.shared.contactsManager.displayName(for: addedByAddress, transaction: transaction)
self.addedToGroupByName = Environment.shared.contactsManager.shortDisplayName(for: addedByAddress, transaction: transaction)
} else {
self.addedToGroupByName = nil
}

View File

@ -339,6 +339,9 @@ public class GroupManager: NSObject {
return createdGroupModel
}
}.then(on: .global()) { (groupModel: TSGroupModel) -> Promise<TSGroupThread> in
// We're creating this thread, we added ourselves
groupModel.addedByAddress = self.tsAccountManager.localAddress
let thread = databaseStorage.write { (transaction: SDSAnyWriteTransaction) -> TSGroupThread in
return self.insertGroupThreadInDatabaseAndCreateInfoMessage(groupModel: groupModel,
disappearingMessageToken: disappearingMessageToken,
@ -1083,6 +1086,9 @@ public class GroupManager: NSObject {
builder.groupMembership = newGroupMembership
let newGroupModel = try builder.build(transaction: transaction)
// We're leaving, so clear out who added us. If we're re-added it may change.
newGroupModel.addedByAddress = nil
let groupUpdateSourceAddress = localAddress
let result = try self.updateExistingGroupThreadInDatabaseAndCreateInfoMessage(newGroupModel: newGroupModel,
newDisappearingMessageToken: nil,
@ -1550,6 +1556,13 @@ public class GroupManager: NSObject {
guard canInsert else {
throw OWSAssertionError("Missing groupThread.")
}
// This thread didn't previously exist, so if we're a member we
// have to assume we were just added.
if let localAddress = tsAccountManager.localAddress, newGroupModel.groupMembers.contains(localAddress) {
newGroupModel.addedByAddress = groupUpdateSourceAddress
}
let thread = insertGroupThreadInDatabaseAndCreateInfoMessage(groupModel: newGroupModel,
disappearingMessageToken: newDisappearingMessageToken,
groupUpdateSourceAddress: groupUpdateSourceAddress,
@ -1630,6 +1643,14 @@ public class GroupManager: NSObject {
let hasUserFacingChange = !oldGroupModel.isEqual(to: newGroupModel, ignoreRevision: true)
// If we weren't previously a member and are now a member, assume whoever
// triggered this update added us to the group.
if let localAddress = tsAccountManager.localAddress,
!oldGroupModel.groupMembers.contains(localAddress),
newGroupModel.groupMembers.contains(localAddress) {
newGroupModel.addedByAddress = groupUpdateSourceAddress
}
groupThread.update(with: newGroupModel, transaction: transaction)
let action: UpsertGroupResult.Action = (hasUserFacingChange