2020-11-25 06:15:16 +01:00
|
|
|
import SignalCoreKit
|
2020-11-18 05:53:45 +01:00
|
|
|
|
2020-11-25 06:15:16 +01:00
|
|
|
extension MessageReceiver {
|
2020-11-18 05:53:45 +01:00
|
|
|
|
2020-11-25 06:15:16 +01:00
|
|
|
internal static func isBlocked(_ publicKey: String) -> Bool {
|
2020-11-18 05:53:45 +01:00
|
|
|
return SSKEnvironment.shared.blockingManager.isRecipientIdBlocked(publicKey)
|
|
|
|
}
|
|
|
|
|
2020-12-07 01:21:24 +01:00
|
|
|
public static func handle(_ message: Message, associatedWithProto proto: SNProtoContent, openGroupID: String?, isBackgroundPoll: Bool, using transaction: Any) throws {
|
2020-11-25 06:15:16 +01:00
|
|
|
switch message {
|
|
|
|
case let message as ReadReceipt: handleReadReceipt(message, using: transaction)
|
|
|
|
case let message as TypingIndicator: handleTypingIndicator(message, using: transaction)
|
2021-01-22 00:28:26 +01:00
|
|
|
case let message as ClosedGroupControlMessage: handleClosedGroupControlMessage(message, using: transaction)
|
2021-03-02 04:25:21 +01:00
|
|
|
case let message as DataExtractionNotification: handleDataExtractionNotification(message, using: transaction)
|
2020-11-25 06:15:16 +01:00
|
|
|
case let message as ExpirationTimerUpdate: handleExpirationTimerUpdate(message, using: transaction)
|
2021-01-13 03:38:07 +01:00
|
|
|
case let message as ConfigurationMessage: handleConfigurationMessage(message, using: transaction)
|
2020-12-07 01:21:24 +01:00
|
|
|
case let message as VisibleMessage: try handleVisibleMessage(message, associatedWithProto: proto, openGroupID: openGroupID, isBackgroundPoll: isBackgroundPoll, using: transaction)
|
2020-11-25 06:15:16 +01:00
|
|
|
default: fatalError()
|
2020-11-18 05:53:45 +01:00
|
|
|
}
|
2021-01-22 04:25:23 +01:00
|
|
|
var isMainAppAndActive = false
|
|
|
|
if let sharedUserDefaults = UserDefaults(suiteName: "group.com.loki-project.loki-messenger") {
|
|
|
|
isMainAppAndActive = sharedUserDefaults.bool(forKey: "isMainAppActive")
|
|
|
|
}
|
|
|
|
guard isMainAppAndActive else { return }
|
2021-01-20 06:28:36 +01:00
|
|
|
// Touch the thread to update the home screen preview
|
|
|
|
let storage = SNMessagingKitConfiguration.shared.storage
|
|
|
|
guard let threadID = storage.getOrCreateThread(for: message.sender!, groupPublicKey: message.groupPublicKey, openGroupID: openGroupID, using: transaction) else { return }
|
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
guard let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) else { return }
|
|
|
|
thread.touch(with: transaction)
|
2020-11-18 05:53:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 04:06:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Read Receipts
|
|
|
|
|
2020-11-25 06:15:16 +01:00
|
|
|
private static func handleReadReceipt(_ message: ReadReceipt, using transaction: Any) {
|
|
|
|
SSKEnvironment.shared.readReceiptManager.processReadReceipts(fromRecipientId: message.sender!, sentTimestamps: message.timestamps!.map { NSNumber(value: $0) }, readTimestamp: message.receivedTimestamp!)
|
|
|
|
}
|
2020-11-18 05:53:45 +01:00
|
|
|
|
2021-02-25 04:06:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Typing Indicators
|
|
|
|
|
2020-11-25 06:15:16 +01:00
|
|
|
private static func handleTypingIndicator(_ message: TypingIndicator, using transaction: Any) {
|
|
|
|
switch message.kind! {
|
|
|
|
case .started: showTypingIndicatorIfNeeded(for: message.sender!)
|
|
|
|
case .stopped: hideTypingIndicatorIfNeeded(for: message.sender!)
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 05:53:45 +01:00
|
|
|
|
2020-11-25 06:15:16 +01:00
|
|
|
public static func showTypingIndicatorIfNeeded(for senderPublicKey: String) {
|
2020-11-18 05:53:45 +01:00
|
|
|
var threadOrNil: TSContactThread?
|
|
|
|
Storage.read { transaction in
|
|
|
|
threadOrNil = TSContactThread.getWithContactId(senderPublicKey, transaction: transaction)
|
|
|
|
}
|
|
|
|
guard let thread = threadOrNil else { return }
|
|
|
|
func showTypingIndicatorsIfNeeded() {
|
|
|
|
SSKEnvironment.shared.typingIndicators.didReceiveTypingStartedMessage(inThread: thread, recipientId: senderPublicKey, deviceId: 1)
|
|
|
|
}
|
|
|
|
if Thread.current.isMainThread {
|
|
|
|
showTypingIndicatorsIfNeeded()
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
showTypingIndicatorsIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:15:16 +01:00
|
|
|
public static func hideTypingIndicatorIfNeeded(for senderPublicKey: String) {
|
2020-11-18 05:53:45 +01:00
|
|
|
var threadOrNil: TSContactThread?
|
|
|
|
Storage.read { transaction in
|
|
|
|
threadOrNil = TSContactThread.getWithContactId(senderPublicKey, transaction: transaction)
|
|
|
|
}
|
|
|
|
guard let thread = threadOrNil else { return }
|
|
|
|
func hideTypingIndicatorsIfNeeded() {
|
|
|
|
SSKEnvironment.shared.typingIndicators.didReceiveTypingStoppedMessage(inThread: thread, recipientId: senderPublicKey, deviceId: 1)
|
|
|
|
}
|
|
|
|
if Thread.current.isMainThread {
|
|
|
|
hideTypingIndicatorsIfNeeded()
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
hideTypingIndicatorsIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:15:16 +01:00
|
|
|
public static func cancelTypingIndicatorsIfNeeded(for senderPublicKey: String) {
|
2020-11-18 05:53:45 +01:00
|
|
|
var threadOrNil: TSContactThread?
|
|
|
|
Storage.read { transaction in
|
|
|
|
threadOrNil = TSContactThread.getWithContactId(senderPublicKey, transaction: transaction)
|
|
|
|
}
|
|
|
|
guard let thread = threadOrNil else { return }
|
|
|
|
func cancelTypingIndicatorsIfNeeded() {
|
|
|
|
SSKEnvironment.shared.typingIndicators.didReceiveIncomingMessage(inThread: thread, recipientId: senderPublicKey, deviceId: 1)
|
|
|
|
}
|
|
|
|
if Thread.current.isMainThread {
|
|
|
|
cancelTypingIndicatorsIfNeeded()
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
cancelTypingIndicatorsIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-25 04:06:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-02 04:25:21 +01:00
|
|
|
// MARK: - Data Extraction Notification
|
|
|
|
|
|
|
|
private static func handleDataExtractionNotification(_ message: DataExtractionNotification, using transaction: Any) {
|
2021-03-02 04:59:07 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
guard message.groupPublicKey == nil,
|
2021-03-02 05:15:57 +01:00
|
|
|
let thread = TSContactThread.getWithContactId(message.sender!, transaction: transaction), case .screenshot = message.kind else { return }
|
2021-03-02 05:14:00 +01:00
|
|
|
let type: TSInfoMessageType
|
|
|
|
switch message.kind! {
|
|
|
|
case .screenshot: type = .screenshotNotification
|
|
|
|
case .mediaSaved: type = .mediaSavedNotification
|
|
|
|
}
|
|
|
|
let message = DataExtractionNotificationInfoMessage(type: type, sentTimestamp: message.sentTimestamp!, thread: thread, referencedAttachmentTimestamp: nil)
|
2021-03-02 04:59:07 +01:00
|
|
|
message.save(with: transaction)
|
2021-03-02 04:25:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-25 04:06:40 +01:00
|
|
|
// MARK: - Expiration Timers
|
2020-11-18 05:53:45 +01:00
|
|
|
|
2020-11-25 06:15:16 +01:00
|
|
|
private static func handleExpirationTimerUpdate(_ message: ExpirationTimerUpdate, using transaction: Any) {
|
|
|
|
if message.duration! > 0 {
|
2021-02-25 04:06:40 +01:00
|
|
|
setExpirationTimer(to: message.duration!, for: message.sender!, syncTarget: message.syncTarget, groupPublicKey: message.groupPublicKey, messageSentTimestamp: message.sentTimestamp!, using: transaction)
|
2020-11-25 06:15:16 +01:00
|
|
|
} else {
|
2021-02-25 04:06:40 +01:00
|
|
|
disableExpirationTimer(for: message.sender!, syncTarget: message.syncTarget, groupPublicKey: message.groupPublicKey, messageSentTimestamp: message.sentTimestamp!, using: transaction)
|
2020-11-18 05:53:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 04:06:40 +01:00
|
|
|
public static func setExpirationTimer(to duration: UInt32, for senderPublicKey: String, syncTarget: String?, groupPublicKey: String?, messageSentTimestamp: UInt64, using transaction: Any) {
|
2020-11-18 05:53:45 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
var threadOrNil: TSThread?
|
2020-11-26 05:51:12 +01:00
|
|
|
if let groupPublicKey = groupPublicKey {
|
|
|
|
guard Storage.shared.isClosedGroup(groupPublicKey) else { return }
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
threadOrNil = TSGroupThread.fetch(uniqueId: TSGroupThread.threadId(fromGroupId: groupID), transaction: transaction)
|
|
|
|
} else {
|
2021-02-24 05:19:50 +01:00
|
|
|
threadOrNil = TSContactThread.getWithContactId(syncTarget ?? senderPublicKey, transaction: transaction)
|
2020-11-18 05:53:45 +01:00
|
|
|
}
|
|
|
|
guard let thread = threadOrNil else { return }
|
|
|
|
let configuration = OWSDisappearingMessagesConfiguration(threadId: thread.uniqueId!, enabled: true, durationSeconds: duration)
|
|
|
|
configuration.save(with: transaction)
|
2021-02-26 05:56:41 +01:00
|
|
|
let senderDisplayName = Storage.shared.getContact(with: senderPublicKey)?.displayName(for: .regular) ?? senderPublicKey
|
2021-02-25 04:06:40 +01:00
|
|
|
let message = OWSDisappearingConfigurationUpdateInfoMessage(timestamp: messageSentTimestamp, thread: thread,
|
2021-01-28 01:16:43 +01:00
|
|
|
configuration: configuration, createdByRemoteName: senderDisplayName, createdInExistingGroup: false)
|
2020-11-18 05:53:45 +01:00
|
|
|
message.save(with: transaction)
|
|
|
|
SSKEnvironment.shared.disappearingMessagesJob.startIfNecessary()
|
|
|
|
}
|
|
|
|
|
2021-02-25 04:06:40 +01:00
|
|
|
public static func disableExpirationTimer(for senderPublicKey: String, syncTarget: String?, groupPublicKey: String?, messageSentTimestamp: UInt64, using transaction: Any) {
|
2020-11-18 05:53:45 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
var threadOrNil: TSThread?
|
2020-11-26 05:51:12 +01:00
|
|
|
if let groupPublicKey = groupPublicKey {
|
|
|
|
guard Storage.shared.isClosedGroup(groupPublicKey) else { return }
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
threadOrNil = TSGroupThread.fetch(uniqueId: TSGroupThread.threadId(fromGroupId: groupID), transaction: transaction)
|
|
|
|
} else {
|
2021-02-24 05:19:50 +01:00
|
|
|
threadOrNil = TSContactThread.getWithContactId(syncTarget ?? senderPublicKey, transaction: transaction)
|
2020-11-18 05:53:45 +01:00
|
|
|
}
|
|
|
|
guard let thread = threadOrNil else { return }
|
|
|
|
let configuration = OWSDisappearingMessagesConfiguration(threadId: thread.uniqueId!, enabled: false, durationSeconds: 24 * 60 * 60)
|
|
|
|
configuration.save(with: transaction)
|
2021-02-26 05:56:41 +01:00
|
|
|
let senderDisplayName = Storage.shared.getContact(with: senderPublicKey)?.displayName(for: .regular) ?? senderPublicKey
|
2021-02-25 04:06:40 +01:00
|
|
|
let message = OWSDisappearingConfigurationUpdateInfoMessage(timestamp: messageSentTimestamp, thread: thread,
|
2021-01-28 01:16:43 +01:00
|
|
|
configuration: configuration, createdByRemoteName: senderDisplayName, createdInExistingGroup: false)
|
2020-11-18 05:53:45 +01:00
|
|
|
message.save(with: transaction)
|
|
|
|
SSKEnvironment.shared.disappearingMessagesJob.startIfNecessary()
|
|
|
|
}
|
2021-01-13 06:10:06 +01:00
|
|
|
|
2021-02-25 04:06:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Configuration Messages
|
|
|
|
|
2021-01-13 06:10:06 +01:00
|
|
|
private static func handleConfigurationMessage(_ message: ConfigurationMessage, using transaction: Any) {
|
2021-03-03 23:58:19 +01:00
|
|
|
let userPublicKey = getUserHexEncodedPublicKey()
|
|
|
|
guard message.sender == userPublicKey else { return }
|
|
|
|
SNLog("Configuration message received.")
|
2021-01-13 06:10:06 +01:00
|
|
|
let storage = SNMessagingKitConfiguration.shared.storage
|
2021-02-23 06:01:06 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
// Profile
|
2021-03-04 04:40:58 +01:00
|
|
|
let userProfile = SNMessagingKitConfiguration.shared.storage.getUserProfile(using: transaction)
|
|
|
|
updateProfileIfNeeded(publicKey: userPublicKey, name: message.displayName, profilePictureURL: message.profilePictureURL,
|
|
|
|
profileKey: given(message.profileKey) { OWSAES256Key(data: $0)! }, sentTimestamp: message.sentTimestamp!, transaction: transaction)
|
2021-02-23 06:01:06 +01:00
|
|
|
transaction.addCompletionQueue(DispatchQueue.main) {
|
|
|
|
SSKEnvironment.shared.profileManager.downloadAvatar(for: userProfile)
|
|
|
|
}
|
|
|
|
// Initial configuration sync
|
|
|
|
if !UserDefaults.standard[.hasSyncedInitialConfiguration] {
|
|
|
|
UserDefaults.standard[.hasSyncedInitialConfiguration] = true
|
2021-02-23 06:09:22 +01:00
|
|
|
NotificationCenter.default.post(name: .initialConfigurationMessageReceived, object: nil)
|
2021-02-25 01:32:53 +01:00
|
|
|
// Contacts
|
|
|
|
for contact in message.contacts {
|
|
|
|
let sessionID = contact.publicKey!
|
|
|
|
let userProfile = OWSUserProfile.getOrBuild(forRecipientId: sessionID, transaction: transaction)
|
|
|
|
userProfile.profileKey = given(contact.profileKey) { OWSAES256Key(data: $0)! }
|
|
|
|
userProfile.avatarUrlPath = contact.profilePictureURL
|
|
|
|
userProfile.profileName = contact.displayName
|
|
|
|
userProfile.save(with: transaction)
|
|
|
|
let thread = TSContactThread.getOrCreateThread(withContactId: sessionID, transaction: transaction)
|
|
|
|
thread.shouldThreadBeVisible = true
|
|
|
|
thread.save(with: transaction)
|
|
|
|
}
|
2021-02-23 06:01:06 +01:00
|
|
|
// Closed groups
|
|
|
|
let allClosedGroupPublicKeys = storage.getUserClosedGroupPublicKeys()
|
|
|
|
for closedGroup in message.closedGroups {
|
|
|
|
guard !allClosedGroupPublicKeys.contains(closedGroup.publicKey) else { continue }
|
|
|
|
handleNewClosedGroup(groupPublicKey: closedGroup.publicKey, name: closedGroup.name, encryptionKeyPair: closedGroup.encryptionKeyPair,
|
|
|
|
members: [String](closedGroup.members), admins: [String](closedGroup.admins), messageSentTimestamp: message.sentTimestamp!, using: transaction)
|
|
|
|
}
|
|
|
|
// Open groups
|
|
|
|
let allOpenGroups = Set(storage.getAllUserOpenGroups().keys)
|
|
|
|
for openGroupURL in message.openGroups {
|
|
|
|
guard !allOpenGroups.contains(openGroupURL) else { continue }
|
|
|
|
OpenGroupManager.shared.add(with: openGroupURL, using: transaction).retainUntilComplete()
|
|
|
|
}
|
2021-01-13 06:10:06 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-25 04:06:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Visible Messages
|
2020-11-18 05:53:45 +01:00
|
|
|
|
2020-12-01 09:45:42 +01:00
|
|
|
@discardableResult
|
2020-12-07 01:21:24 +01:00
|
|
|
public static func handleVisibleMessage(_ message: VisibleMessage, associatedWithProto proto: SNProtoContent, openGroupID: String?, isBackgroundPoll: Bool, using transaction: Any) throws -> String {
|
2020-12-02 06:25:16 +01:00
|
|
|
let storage = SNMessagingKitConfiguration.shared.storage
|
2020-11-25 06:15:16 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
2020-12-03 00:12:29 +01:00
|
|
|
var isMainAppAndActive = false
|
|
|
|
if let sharedUserDefaults = UserDefaults(suiteName: "group.com.loki-project.loki-messenger") {
|
|
|
|
isMainAppAndActive = sharedUserDefaults.bool(forKey: "isMainAppActive")
|
|
|
|
}
|
2020-11-25 06:15:16 +01:00
|
|
|
// Parse & persist attachments
|
|
|
|
let attachments: [VisibleMessage.Attachment] = proto.dataMessage!.attachments.compactMap { proto in
|
|
|
|
guard let attachment = VisibleMessage.Attachment.fromProto(proto) else { return nil }
|
|
|
|
return attachment.isValid ? attachment : nil
|
|
|
|
}
|
2020-11-28 03:57:03 +01:00
|
|
|
let attachmentIDs = storage.persist(attachments, using: transaction)
|
2020-11-25 06:15:16 +01:00
|
|
|
message.attachmentIDs = attachmentIDs
|
2020-11-28 03:57:03 +01:00
|
|
|
var attachmentsToDownload = attachmentIDs
|
2020-11-25 06:15:16 +01:00
|
|
|
// Update profile if needed
|
2020-11-26 05:16:35 +01:00
|
|
|
if let newProfile = message.profile {
|
2020-12-18 00:35:16 +01:00
|
|
|
let sessionID = message.sender!
|
2021-03-04 04:40:58 +01:00
|
|
|
updateProfileIfNeeded(publicKey: sessionID, name: newProfile.displayName, profilePictureURL: newProfile.profilePictureURL,
|
|
|
|
profileKey: given(newProfile.profileKey) { OWSAES256Key(data: $0)! }, sentTimestamp: message.sentTimestamp!, transaction: transaction)
|
2020-11-30 05:44:07 +01:00
|
|
|
if let rawDisplayName = newProfile.displayName, let openGroupID = openGroupID {
|
2020-12-18 00:35:16 +01:00
|
|
|
let endIndex = sessionID.endIndex
|
|
|
|
let cutoffIndex = sessionID.index(endIndex, offsetBy: -8)
|
|
|
|
let displayName = "\(rawDisplayName) (...\(sessionID[cutoffIndex..<endIndex]))"
|
|
|
|
Storage.shared.setOpenGroupDisplayName(to: displayName, for: sessionID, inOpenGroupWithID: openGroupID, using: transaction)
|
2020-11-30 05:44:07 +01:00
|
|
|
}
|
2020-11-25 06:15:16 +01:00
|
|
|
}
|
2020-11-27 06:22:15 +01:00
|
|
|
// Get or create thread
|
2021-01-14 04:57:32 +01:00
|
|
|
guard let threadID = storage.getOrCreateThread(for: message.syncTarget ?? message.sender!, groupPublicKey: message.groupPublicKey, openGroupID: openGroupID, using: transaction) else { throw Error.noThread }
|
2020-11-27 06:22:15 +01:00
|
|
|
// Parse quote if needed
|
|
|
|
var tsQuotedMessage: TSQuotedMessage? = nil
|
2020-11-27 05:13:42 +01:00
|
|
|
if message.quote != nil && proto.dataMessage?.quote != nil, let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) {
|
2020-11-27 06:22:15 +01:00
|
|
|
tsQuotedMessage = TSQuotedMessage(for: proto.dataMessage!, thread: thread, transaction: transaction)
|
2020-11-27 07:13:37 +01:00
|
|
|
if let id = tsQuotedMessage?.thumbnailAttachmentStreamId() ?? tsQuotedMessage?.thumbnailAttachmentPointerId() {
|
2020-11-28 03:57:03 +01:00
|
|
|
attachmentsToDownload.append(id)
|
2020-11-27 07:13:37 +01:00
|
|
|
}
|
2020-11-27 05:13:42 +01:00
|
|
|
}
|
2020-11-28 01:48:08 +01:00
|
|
|
// Parse link preview if needed
|
|
|
|
var owsLinkPreview: OWSLinkPreview?
|
|
|
|
if message.linkPreview != nil && proto.dataMessage?.preview.isEmpty == false {
|
|
|
|
owsLinkPreview = try? OWSLinkPreview.buildValidatedLinkPreview(dataMessage: proto.dataMessage!, body: message.text, transaction: transaction)
|
|
|
|
if let id = owsLinkPreview?.imageAttachmentId {
|
2020-11-28 03:57:03 +01:00
|
|
|
attachmentsToDownload.append(id)
|
2020-11-28 01:48:08 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-27 06:22:15 +01:00
|
|
|
// Persist the message
|
2021-01-14 04:57:32 +01:00
|
|
|
guard let tsMessageID = storage.persist(message, quotedMessage: tsQuotedMessage, linkPreview: owsLinkPreview,
|
2020-11-30 01:00:28 +01:00
|
|
|
groupPublicKey: message.groupPublicKey, openGroupID: openGroupID, using: transaction) else { throw Error.noThread }
|
2020-11-27 06:22:15 +01:00
|
|
|
message.threadID = threadID
|
2020-11-25 06:15:16 +01:00
|
|
|
// Start attachment downloads if needed
|
2020-12-01 09:45:42 +01:00
|
|
|
attachmentsToDownload.forEach { attachmentID in
|
2021-01-14 04:57:32 +01:00
|
|
|
let downloadJob = AttachmentDownloadJob(attachmentID: attachmentID, tsMessageID: tsMessageID)
|
2020-12-03 00:12:29 +01:00
|
|
|
if isMainAppAndActive {
|
2020-12-01 09:45:42 +01:00
|
|
|
JobQueue.shared.add(downloadJob, using: transaction)
|
|
|
|
} else {
|
|
|
|
JobQueue.shared.addWithoutExecuting(downloadJob, using: transaction)
|
2020-11-25 06:15:16 +01:00
|
|
|
}
|
2020-12-01 09:45:42 +01:00
|
|
|
}
|
|
|
|
// Cancel any typing indicators if needed
|
2020-12-03 00:12:29 +01:00
|
|
|
if isMainAppAndActive {
|
2020-12-01 09:45:42 +01:00
|
|
|
cancelTypingIndicatorsIfNeeded(for: message.sender!)
|
|
|
|
}
|
2021-01-21 01:27:52 +01:00
|
|
|
// Keep track of the open group server message ID ↔ message ID relationship
|
|
|
|
if let serverID = message.openGroupServerMessageID {
|
2021-01-25 03:50:18 +01:00
|
|
|
storage.setIDForMessage(withServerID: serverID, to: tsMessageID, using: transaction)
|
2021-01-21 01:27:52 +01:00
|
|
|
}
|
2020-11-25 06:15:16 +01:00
|
|
|
// Notify the user if needed
|
2021-01-14 04:57:32 +01:00
|
|
|
guard (isMainAppAndActive || isBackgroundPoll), let tsIncomingMessage = TSMessage.fetch(uniqueId: tsMessageID, transaction: transaction) as? TSIncomingMessage,
|
|
|
|
let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) else { return tsMessageID }
|
2020-11-30 22:35:13 +01:00
|
|
|
SSKEnvironment.shared.notificationsManager!.notifyUser(for: tsIncomingMessage, in: thread, transaction: transaction)
|
2021-01-14 04:57:32 +01:00
|
|
|
return tsMessageID
|
2020-11-25 06:15:16 +01:00
|
|
|
}
|
2021-03-04 04:40:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Profile Updating
|
|
|
|
private static func updateProfileIfNeeded(publicKey: String, name: String?, profilePictureURL: String?,
|
|
|
|
profileKey: OWSAES256Key?, sentTimestamp: UInt64, transaction: YapDatabaseReadWriteTransaction) {
|
|
|
|
let isCurrentUser = (publicKey == getUserHexEncodedPublicKey())
|
|
|
|
let profileManager = SSKEnvironment.shared.profileManager
|
|
|
|
let userDefaults = UserDefaults.standard
|
|
|
|
let owsProfile = isCurrentUser ? SNMessagingKitConfiguration.shared.storage.getUserProfile(using: transaction)
|
|
|
|
: OWSUserProfile.fetch(uniqueId: publicKey, transaction: transaction) // Old API
|
|
|
|
let contact = Storage.shared.getContact(with: publicKey) ?? Contact(sessionID: publicKey) // New API
|
|
|
|
// Name
|
|
|
|
if let name = name, name != owsProfile?.profileName {
|
|
|
|
let shouldUpdate: Bool
|
|
|
|
if isCurrentUser {
|
|
|
|
shouldUpdate = given(userDefaults[.lastDisplayNameUpdate]) { sentTimestamp > UInt64($0.timeIntervalSince1970 * 1000) } ?? true
|
|
|
|
} else {
|
|
|
|
shouldUpdate = true
|
|
|
|
}
|
|
|
|
if shouldUpdate {
|
|
|
|
if isCurrentUser {
|
|
|
|
owsProfile?.profileName = name
|
|
|
|
userDefaults[.lastDisplayNameUpdate] = Date(timeIntervalSince1970: TimeInterval(sentTimestamp / 1000))
|
|
|
|
} else {
|
|
|
|
profileManager.updateProfileForContact(withID: publicKey, displayName: name, with: transaction)
|
|
|
|
}
|
|
|
|
contact.name = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Profile picture & profile key
|
|
|
|
if let profileKey = profileKey, let profilePictureURL = profilePictureURL, profileKey.keyData.count == kAES256_KeyByteLength,
|
|
|
|
profileKey != owsProfile?.profileKey {
|
|
|
|
let shouldUpdate: Bool
|
|
|
|
if isCurrentUser {
|
|
|
|
shouldUpdate = given(userDefaults[.lastProfilePictureUpdate]) { sentTimestamp > UInt64($0.timeIntervalSince1970 * 1000) } ?? true
|
|
|
|
} else {
|
|
|
|
shouldUpdate = true
|
|
|
|
}
|
|
|
|
if shouldUpdate {
|
|
|
|
if isCurrentUser {
|
|
|
|
owsProfile?.avatarUrlPath = profilePictureURL
|
|
|
|
owsProfile?.profileKey = profileKey
|
|
|
|
userDefaults[.lastProfilePictureUpdate] = Date(timeIntervalSince1970: TimeInterval(sentTimestamp / 1000))
|
|
|
|
} else {
|
|
|
|
profileManager.setProfileKeyData(profileKey.keyData, forRecipientId: publicKey, avatarURL: profilePictureURL)
|
|
|
|
}
|
|
|
|
contact.profilePictureURL = profilePictureURL
|
|
|
|
contact.profilePictureEncryptionKey = profileKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Persist changes
|
2021-03-04 04:45:17 +01:00
|
|
|
if isCurrentUser { // In the case where it's someone else the profile will already be saved (updateProfileForContact and setProfileKeyData to that internally)
|
2021-03-04 04:40:58 +01:00
|
|
|
owsProfile?.save(with: transaction)
|
|
|
|
}
|
|
|
|
Storage.shared.setContact(contact, using: transaction)
|
|
|
|
}
|
2020-11-18 05:53:45 +01:00
|
|
|
|
2021-02-25 04:06:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Closed Groups
|
2021-01-22 00:28:26 +01:00
|
|
|
private static func handleClosedGroupControlMessage(_ message: ClosedGroupControlMessage, using transaction: Any) {
|
2021-01-04 05:30:13 +01:00
|
|
|
switch message.kind! {
|
2021-01-13 03:38:07 +01:00
|
|
|
case .new: handleNewClosedGroup(message, using: transaction)
|
|
|
|
case .encryptionKeyPair: handleClosedGroupEncryptionKeyPair(message, using: transaction)
|
2021-01-22 00:28:26 +01:00
|
|
|
case .nameChange: handleClosedGroupNameChanged(message, using: transaction)
|
2021-01-22 01:07:22 +01:00
|
|
|
case .membersAdded: handleClosedGroupMembersAdded(message, using: transaction)
|
|
|
|
case .membersRemoved: handleClosedGroupMembersRemoved(message, using: transaction)
|
|
|
|
case .memberLeft: handleClosedGroupMemberLeft(message, using: transaction)
|
2021-02-08 01:33:47 +01:00
|
|
|
case .encryptionKeyPairRequest: handleClosedGroupEncryptionKeyPairRequest(message, using: transaction)
|
2021-01-04 05:30:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 00:28:26 +01:00
|
|
|
private static func handleNewClosedGroup(_ message: ClosedGroupControlMessage, using transaction: Any) {
|
2021-01-04 05:30:13 +01:00
|
|
|
// Prepare
|
|
|
|
guard case let .new(publicKeyAsData, name, encryptionKeyPair, membersAsData, adminsAsData) = message.kind else { return }
|
|
|
|
let groupPublicKey = publicKeyAsData.toHexString()
|
|
|
|
let members = membersAsData.map { $0.toHexString() }
|
|
|
|
let admins = adminsAsData.map { $0.toHexString() }
|
2021-02-07 23:37:39 +01:00
|
|
|
handleNewClosedGroup(groupPublicKey: groupPublicKey, name: name, encryptionKeyPair: encryptionKeyPair,
|
|
|
|
members: members, admins: admins, messageSentTimestamp: message.sentTimestamp!, using: transaction)
|
2021-01-13 03:38:07 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:37:39 +01:00
|
|
|
private static func handleNewClosedGroup(groupPublicKey: String, name: String, encryptionKeyPair: ECKeyPair, members: [String], admins: [String], messageSentTimestamp: UInt64, using transaction: Any) {
|
2021-01-13 03:38:07 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
2021-01-04 05:30:13 +01:00
|
|
|
// Create the group
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
let group = TSGroupModel(title: name, memberIds: members, image: nil, groupId: groupID, groupType: .closedGroup, adminIds: admins)
|
|
|
|
let thread: TSGroupThread
|
|
|
|
if let t = TSGroupThread.fetch(uniqueId: TSGroupThread.threadId(fromGroupId: groupID), transaction: transaction) {
|
|
|
|
thread = t
|
|
|
|
thread.setGroupModel(group, with: transaction)
|
|
|
|
} else {
|
|
|
|
thread = TSGroupThread.getOrCreateThread(with: group, transaction: transaction)
|
|
|
|
thread.save(with: transaction)
|
2021-02-10 00:56:46 +01:00
|
|
|
// Notify the user
|
2021-03-02 04:25:21 +01:00
|
|
|
let infoMessage = TSInfoMessage(timestamp: messageSentTimestamp, in: thread, messageType: .groupUpdate)
|
2021-02-10 00:56:46 +01:00
|
|
|
infoMessage.save(with: transaction)
|
2021-01-04 05:30:13 +01:00
|
|
|
}
|
|
|
|
// Add the group to the user's set of public keys to poll for
|
|
|
|
Storage.shared.addClosedGroupPublicKey(groupPublicKey, using: transaction)
|
2021-01-08 00:32:54 +01:00
|
|
|
// Store the key pair
|
2021-01-04 05:30:13 +01:00
|
|
|
Storage.shared.addClosedGroupEncryptionKeyPair(encryptionKeyPair, for: groupPublicKey, using: transaction)
|
2021-02-07 23:37:39 +01:00
|
|
|
// Store the formation timestamp
|
|
|
|
Storage.shared.setClosedGroupFormationTimestamp(to: messageSentTimestamp, for: groupPublicKey, using: transaction)
|
2021-01-04 05:30:13 +01:00
|
|
|
// Notify the PN server
|
|
|
|
let _ = PushNotificationAPI.performOperation(.subscribe, for: groupPublicKey, publicKey: getUserHexEncodedPublicKey())
|
|
|
|
}
|
2021-01-22 01:02:19 +01:00
|
|
|
|
|
|
|
private static func handleClosedGroupEncryptionKeyPair(_ message: ClosedGroupControlMessage, using transaction: Any) {
|
|
|
|
// Prepare
|
2021-02-08 01:33:47 +01:00
|
|
|
guard case let .encryptionKeyPair(explicitGroupPublicKey, wrappers) = message.kind,
|
|
|
|
let groupPublicKey = explicitGroupPublicKey?.toHexString() ?? message.groupPublicKey else { return }
|
2021-01-22 01:02:19 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
let userPublicKey = getUserHexEncodedPublicKey()
|
|
|
|
guard let userKeyPair = SNMessagingKitConfiguration.shared.storage.getUserKeyPair() else {
|
|
|
|
return SNLog("Couldn't find user X25519 key pair.")
|
|
|
|
}
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
let threadID = TSGroupThread.threadId(fromGroupId: groupID)
|
|
|
|
guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else {
|
|
|
|
return SNLog("Ignoring closed group encryption key pair for nonexistent group.")
|
|
|
|
}
|
2021-02-08 01:33:47 +01:00
|
|
|
guard thread.groupModel.groupMemberIds.contains(message.sender!) else {
|
|
|
|
return SNLog("Ignoring closed group encryption key pair from non-member.")
|
2021-01-22 01:02:19 +01:00
|
|
|
}
|
|
|
|
// Find our wrapper and decrypt it if possible
|
|
|
|
guard let wrapper = wrappers.first(where: { $0.publicKey == userPublicKey }), let encryptedKeyPair = wrapper.encryptedKeyPair else { return }
|
|
|
|
let plaintext: Data
|
|
|
|
do {
|
|
|
|
plaintext = try MessageReceiver.decryptWithSessionProtocol(ciphertext: encryptedKeyPair, using: userKeyPair).plaintext
|
|
|
|
} catch {
|
|
|
|
return SNLog("Couldn't decrypt closed group encryption key pair.")
|
|
|
|
}
|
|
|
|
// Parse it
|
2021-01-25 03:50:18 +01:00
|
|
|
let proto: SNProtoKeyPair
|
2021-01-22 01:02:19 +01:00
|
|
|
do {
|
2021-01-25 03:50:18 +01:00
|
|
|
proto = try SNProtoKeyPair.parseData(plaintext)
|
2021-01-22 01:02:19 +01:00
|
|
|
} catch {
|
|
|
|
return SNLog("Couldn't parse closed group encryption key pair.")
|
|
|
|
}
|
|
|
|
let keyPair: ECKeyPair
|
|
|
|
do {
|
|
|
|
keyPair = try ECKeyPair(publicKeyData: proto.publicKey.removing05PrefixIfNeeded(), privateKeyData: proto.privateKey)
|
|
|
|
} catch {
|
|
|
|
return SNLog("Couldn't parse closed group encryption key pair.")
|
|
|
|
}
|
2021-02-08 01:33:47 +01:00
|
|
|
// Store it if needed
|
|
|
|
let closedGroupEncryptionKeyPairs = Storage.shared.getClosedGroupEncryptionKeyPairs(for: groupPublicKey)
|
|
|
|
guard !closedGroupEncryptionKeyPairs.contains(keyPair) else {
|
|
|
|
return SNLog("Ignoring duplicate closed group encryption key pair.")
|
|
|
|
}
|
2021-01-22 01:02:19 +01:00
|
|
|
Storage.shared.addClosedGroupEncryptionKeyPair(keyPair, for: groupPublicKey, using: transaction)
|
|
|
|
SNLog("Received a new closed group encryption key pair.")
|
|
|
|
}
|
2021-01-04 05:30:13 +01:00
|
|
|
|
2021-01-22 01:02:19 +01:00
|
|
|
private static func handleClosedGroupNameChanged(_ message: ClosedGroupControlMessage, using transaction: Any) {
|
|
|
|
guard case let .nameChange(name) = message.kind else { return }
|
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
performIfValid(for: message, using: transaction) { groupID, thread, group in
|
|
|
|
// Update the group
|
|
|
|
let newGroupModel = TSGroupModel(title: name, memberIds: group.groupMemberIds, image: nil, groupId: groupID, groupType: .closedGroup, adminIds: group.groupAdminIds)
|
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
|
|
|
// Notify the user if needed
|
|
|
|
guard name != group.groupName else { return }
|
|
|
|
let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel)
|
2021-03-02 04:25:21 +01:00
|
|
|
let infoMessage = TSInfoMessage(timestamp: message.sentTimestamp!, in: thread, messageType: .groupUpdate, customMessage: updateInfo)
|
2021-01-22 01:02:19 +01:00
|
|
|
infoMessage.save(with: transaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func handleClosedGroupMembersAdded(_ message: ClosedGroupControlMessage, using transaction: Any) {
|
2021-01-22 01:07:22 +01:00
|
|
|
guard case let .membersAdded(membersAsData) = message.kind else { return }
|
2021-01-22 01:02:19 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
performIfValid(for: message, using: transaction) { groupID, thread, group in
|
|
|
|
// Update the group
|
2021-01-22 03:25:23 +01:00
|
|
|
let members = Set(group.groupMemberIds).union(membersAsData.map { $0.toHexString() })
|
|
|
|
let newGroupModel = TSGroupModel(title: group.groupName, memberIds: [String](members), image: nil, groupId: groupID, groupType: .closedGroup, adminIds: group.groupAdminIds)
|
2021-01-22 01:02:19 +01:00
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
2021-02-11 06:14:03 +01:00
|
|
|
// Send the latest encryption key pair to the added members if the current user is the admin of the group
|
|
|
|
let isCurrentUserAdmin = group.groupAdminIds.contains(getUserHexEncodedPublicKey())
|
|
|
|
if isCurrentUserAdmin {
|
|
|
|
for member in membersAsData.map({ $0.toHexString() }) {
|
|
|
|
MessageSender.sendLatestEncryptionKeyPair(to: member, for: message.groupPublicKey!, using: transaction)
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 01:02:19 +01:00
|
|
|
// Notify the user if needed
|
2021-01-22 03:25:23 +01:00
|
|
|
guard members != Set(group.groupMemberIds) else { return }
|
2021-01-22 01:02:19 +01:00
|
|
|
let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel)
|
2021-03-02 04:25:21 +01:00
|
|
|
let infoMessage = TSInfoMessage(timestamp: message.sentTimestamp!, in: thread, messageType: .groupUpdate, customMessage: updateInfo)
|
2021-01-22 01:02:19 +01:00
|
|
|
infoMessage.save(with: transaction)
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 03:50:18 +01:00
|
|
|
|
2021-01-22 01:02:19 +01:00
|
|
|
private static func handleClosedGroupMembersRemoved(_ message: ClosedGroupControlMessage, using transaction: Any) {
|
2021-01-22 01:07:22 +01:00
|
|
|
guard case let .membersRemoved(membersAsData) = message.kind else { return }
|
2021-01-22 01:02:19 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
guard let groupPublicKey = message.groupPublicKey else { return }
|
|
|
|
performIfValid(for: message, using: transaction) { groupID, thread, group in
|
2021-01-22 03:25:23 +01:00
|
|
|
// Check that the admin wasn't removed
|
|
|
|
let members = Set(group.groupMemberIds).subtracting(membersAsData.map { $0.toHexString() })
|
|
|
|
guard members.contains(group.groupAdminIds.first!) else {
|
2021-01-22 01:02:19 +01:00
|
|
|
return SNLog("Ignoring invalid closed group update.")
|
|
|
|
}
|
|
|
|
// If the current user was removed:
|
|
|
|
// • Stop polling for the group
|
|
|
|
// • Remove the key pairs associated with the group
|
|
|
|
// • Notify the PN server
|
|
|
|
let userPublicKey = getUserHexEncodedPublicKey()
|
|
|
|
let wasCurrentUserRemoved = !members.contains(userPublicKey)
|
|
|
|
if wasCurrentUserRemoved {
|
|
|
|
Storage.shared.removeClosedGroupPublicKey(groupPublicKey, using: transaction)
|
|
|
|
Storage.shared.removeAllClosedGroupEncryptionKeyPairs(for: groupPublicKey, using: transaction)
|
|
|
|
let _ = PushNotificationAPI.performOperation(.unsubscribe, for: groupPublicKey, publicKey: userPublicKey)
|
|
|
|
}
|
|
|
|
// Generate and distribute a new encryption key pair if needed
|
2021-01-25 04:54:18 +01:00
|
|
|
// NOTE: If we're the admin we can be sure at this point that we weren't removed
|
2021-01-22 01:02:19 +01:00
|
|
|
let isCurrentUserAdmin = group.groupAdminIds.contains(getUserHexEncodedPublicKey())
|
2021-01-22 03:25:23 +01:00
|
|
|
if isCurrentUserAdmin {
|
2021-01-22 01:02:19 +01:00
|
|
|
do {
|
|
|
|
try MessageSender.generateAndSendNewEncryptionKeyPair(for: groupPublicKey, to: Set(members), using: transaction)
|
|
|
|
} catch {
|
|
|
|
SNLog("Couldn't distribute new encryption key pair.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update the group
|
2021-01-22 03:25:23 +01:00
|
|
|
let newGroupModel = TSGroupModel(title: group.groupName, memberIds: [String](members), image: nil, groupId: groupID, groupType: .closedGroup, adminIds: group.groupAdminIds)
|
2021-01-22 01:02:19 +01:00
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
|
|
|
// Notify the user if needed
|
2021-01-22 03:25:23 +01:00
|
|
|
guard members != Set(group.groupMemberIds) else { return }
|
2021-03-02 04:25:21 +01:00
|
|
|
let infoMessageType: TSInfoMessageType = wasCurrentUserRemoved ? .groupQuit : .groupUpdate
|
2021-01-22 01:02:19 +01:00
|
|
|
let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel)
|
2021-02-25 01:38:30 +01:00
|
|
|
let infoMessage = TSInfoMessage(timestamp: message.sentTimestamp!, in: thread, messageType: infoMessageType, customMessage: updateInfo)
|
2021-01-22 01:02:19 +01:00
|
|
|
infoMessage.save(with: transaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func handleClosedGroupMemberLeft(_ message: ClosedGroupControlMessage, using transaction: Any) {
|
2021-01-22 01:07:22 +01:00
|
|
|
guard case .memberLeft = message.kind else { return }
|
2021-01-22 01:02:19 +01:00
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
guard let groupPublicKey = message.groupPublicKey else { return }
|
|
|
|
performIfValid(for: message, using: transaction) { groupID, thread, group in
|
2021-01-22 03:25:23 +01:00
|
|
|
let didAdminLeave = group.groupAdminIds.contains(message.sender!)
|
|
|
|
let members: Set<String> = didAdminLeave ? [] : Set(group.groupMemberIds).subtracting([ message.sender! ]) // If the admin leaves the group is disbanded
|
2021-02-01 23:21:50 +01:00
|
|
|
let userPublicKey = getUserHexEncodedPublicKey()
|
|
|
|
let isCurrentUserAdmin = group.groupAdminIds.contains(userPublicKey)
|
|
|
|
// If a regular member left:
|
|
|
|
// • Distribute a new encryption key pair if we're the admin of the group
|
|
|
|
// If the admin left:
|
|
|
|
// • Don't distribute a new encryption key pair
|
|
|
|
// • Unsubscribe from PNs, delete the group public key, etc. as the group will be disbanded
|
|
|
|
if didAdminLeave {
|
|
|
|
// Remove the group from the database and unsubscribe from PNs
|
|
|
|
Storage.shared.removeAllClosedGroupEncryptionKeyPairs(for: groupPublicKey, using: transaction)
|
|
|
|
Storage.shared.removeClosedGroupPublicKey(groupPublicKey, using: transaction)
|
|
|
|
let _ = PushNotificationAPI.performOperation(.unsubscribe, for: groupPublicKey, publicKey: userPublicKey)
|
|
|
|
} else if isCurrentUserAdmin {
|
|
|
|
// Generate and distribute a new encryption key pair if needed
|
2021-01-22 01:02:19 +01:00
|
|
|
do {
|
|
|
|
try MessageSender.generateAndSendNewEncryptionKeyPair(for: groupPublicKey, to: members, using: transaction)
|
|
|
|
} catch {
|
|
|
|
SNLog("Couldn't distribute new encryption key pair.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update the group
|
|
|
|
let newGroupModel = TSGroupModel(title: group.groupName, memberIds: [String](members), image: nil, groupId: groupID, groupType: .closedGroup, adminIds: group.groupAdminIds)
|
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
|
|
|
// Notify the user if needed
|
|
|
|
guard members != Set(group.groupMemberIds) else { return }
|
|
|
|
let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel)
|
2021-03-02 04:25:21 +01:00
|
|
|
let infoMessage = TSInfoMessage(timestamp: message.sentTimestamp!, in: thread, messageType: .groupUpdate, customMessage: updateInfo)
|
2021-01-22 01:02:19 +01:00
|
|
|
infoMessage.save(with: transaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 01:33:47 +01:00
|
|
|
private static func handleClosedGroupEncryptionKeyPairRequest(_ message: ClosedGroupControlMessage, using transaction: Any) {
|
|
|
|
guard case .encryptionKeyPairRequest = message.kind else { return }
|
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
guard let groupPublicKey = message.groupPublicKey else { return }
|
|
|
|
performIfValid(for: message, using: transaction) { groupID, _, group in
|
|
|
|
let publicKey = message.sender!
|
|
|
|
// Guard against self-sends
|
|
|
|
guard publicKey != getUserHexEncodedPublicKey() else {
|
|
|
|
return SNLog("Ignoring invalid closed group update.")
|
|
|
|
}
|
2021-02-11 06:14:03 +01:00
|
|
|
MessageSender.sendLatestEncryptionKeyPair(to: publicKey, for: groupPublicKey, using: transaction)
|
2021-02-08 01:33:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:02:19 +01:00
|
|
|
private static func performIfValid(for message: ClosedGroupControlMessage, using transaction: Any, _ update: (Data, TSGroupThread, TSGroupModel) -> Void) {
|
|
|
|
// Prepare
|
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
// Get the group
|
|
|
|
guard let groupPublicKey = message.groupPublicKey else { return }
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
let threadID = TSGroupThread.threadId(fromGroupId: groupID)
|
|
|
|
guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else {
|
|
|
|
return SNLog("Ignoring closed group update for nonexistent group.")
|
|
|
|
}
|
|
|
|
let group = thread.groupModel
|
|
|
|
// Check that the message isn't from before the group was created
|
2021-02-07 23:37:39 +01:00
|
|
|
if let formationTimestamp = Storage.shared.getClosedGroupFormationTimestamp(for: groupPublicKey) {
|
|
|
|
guard message.sentTimestamp! > formationTimestamp else {
|
|
|
|
return SNLog("Ignoring closed group update from before thread was created.")
|
|
|
|
}
|
2021-01-22 01:02:19 +01:00
|
|
|
}
|
|
|
|
// Check that the sender is a member of the group
|
|
|
|
guard Set(group.groupMemberIds).contains(message.sender!) else {
|
|
|
|
return SNLog("Ignoring closed group update from non-member.")
|
|
|
|
}
|
|
|
|
// Perform the update
|
|
|
|
update(groupID, thread, group)
|
|
|
|
}
|
2020-11-18 05:53:45 +01:00
|
|
|
}
|