import SessionProtocolKit import SignalCoreKit extension MessageReceiver { internal static func isBlocked(_ publicKey: String) -> Bool { return SSKEnvironment.shared.blockingManager.isRecipientIdBlocked(publicKey) } public static func handle(_ message: Message, associatedWithProto proto: SNProtoContent, openGroupID: String?, using transaction: Any) throws { switch message { case let message as ReadReceipt: handleReadReceipt(message, using: transaction) case let message as TypingIndicator: handleTypingIndicator(message, using: transaction) case let message as ClosedGroupUpdate: handleClosedGroupUpdate(message, using: transaction) case let message as ExpirationTimerUpdate: handleExpirationTimerUpdate(message, using: transaction) case let message as VisibleMessage: try handleVisibleMessage(message, associatedWithProto: proto, openGroupID: openGroupID, using: transaction) default: fatalError() } } 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!) } private static func handleTypingIndicator(_ message: TypingIndicator, using transaction: Any) { switch message.kind! { case .started: showTypingIndicatorIfNeeded(for: message.sender!) case .stopped: hideTypingIndicatorIfNeeded(for: message.sender!) } } public static func showTypingIndicatorIfNeeded(for senderPublicKey: String) { 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() } } } public static func hideTypingIndicatorIfNeeded(for senderPublicKey: String) { 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() } } } public static func cancelTypingIndicatorsIfNeeded(for senderPublicKey: String) { 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() } } } private static func handleExpirationTimerUpdate(_ message: ExpirationTimerUpdate, using transaction: Any) { if message.duration! > 0 { setExpirationTimer(to: message.duration!, for: message.sender!, groupPublicKey: message.groupPublicKey, using: transaction) } else { disableExpirationTimer(for: message.sender!, groupPublicKey: message.groupPublicKey, using: transaction) } } public static func setExpirationTimer(to duration: UInt32, for senderPublicKey: String, groupPublicKey: String?, using transaction: Any) { let transaction = transaction as! YapDatabaseReadWriteTransaction var isGroup = false var threadOrNil: TSThread? 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) isGroup = true } else { threadOrNil = TSContactThread.getWithContactId(senderPublicKey, transaction: transaction) } guard let thread = threadOrNil else { return } let configuration = OWSDisappearingMessagesConfiguration(threadId: thread.uniqueId!, enabled: true, durationSeconds: duration) configuration.save(with: transaction) let senderDisplayName = SSKEnvironment.shared.profileManager.profileNameForRecipient(withID: senderPublicKey, transaction: transaction) ?? senderPublicKey let message = OWSDisappearingConfigurationUpdateInfoMessage(timestamp: NSDate.millisecondTimestamp(), thread: thread, configuration: configuration, createdByRemoteName: senderDisplayName, createdInExistingGroup: isGroup) message.save(with: transaction) SSKEnvironment.shared.disappearingMessagesJob.startIfNecessary() } public static func disableExpirationTimer(for senderPublicKey: String, groupPublicKey: String?, using transaction: Any) { let transaction = transaction as! YapDatabaseReadWriteTransaction var isGroup = false var threadOrNil: TSThread? 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) isGroup = true } else { threadOrNil = TSContactThread.getWithContactId(senderPublicKey, transaction: transaction) } guard let thread = threadOrNil else { return } let configuration = OWSDisappearingMessagesConfiguration(threadId: thread.uniqueId!, enabled: false, durationSeconds: 24 * 60 * 60) configuration.save(with: transaction) let senderDisplayName = SSKEnvironment.shared.profileManager.profileNameForRecipient(withID: senderPublicKey, transaction: transaction) ?? senderPublicKey let message = OWSDisappearingConfigurationUpdateInfoMessage(timestamp: NSDate.millisecondTimestamp(), thread: thread, configuration: configuration, createdByRemoteName: senderDisplayName, createdInExistingGroup: isGroup) message.save(with: transaction) SSKEnvironment.shared.disappearingMessagesJob.startIfNecessary() } @discardableResult public static func handleVisibleMessage(_ message: VisibleMessage, associatedWithProto proto: SNProtoContent, openGroupID: String?, using transaction: Any) throws -> String { let storage = SNMessagingKitConfiguration.shared.storage let transaction = transaction as! YapDatabaseReadWriteTransaction // 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 } let attachmentIDs = storage.persist(attachments, using: transaction) message.attachmentIDs = attachmentIDs var attachmentsToDownload = attachmentIDs // Update profile if needed if let newProfile = message.profile { let profileManager = SSKEnvironment.shared.profileManager let oldProfile = OWSUserProfile.fetch(uniqueId: message.sender!, transaction: transaction) if let displayName = newProfile.displayName, displayName != oldProfile?.profileName { profileManager.updateProfileForContact(withID: message.sender!, displayName: displayName, with: transaction) } if let profileKey = newProfile.profileKey, let profilePictureURL = newProfile.profilePictureURL, profileKey.count == kAES256_KeyByteLength, profileKey != oldProfile?.profileKey?.keyData { profileManager.setProfileKeyData(profileKey, forRecipientId: message.sender!, avatarURL: profilePictureURL) } if let rawDisplayName = newProfile.displayName, let openGroupID = openGroupID { let publicKey = message.sender! let endIndex = publicKey.endIndex let cutoffIndex = publicKey.index(endIndex, offsetBy: -8) let displayName = "\(rawDisplayName) (...\(publicKey[cutoffIndex..