diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index cfe50349d..680c600e4 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -653,6 +653,16 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc audioRecorder?.stop() audioSession.endAudioActivity(recordVoiceMessageActivity) } + + // MARK: Screenshot Notifications + @objc func sendScreenshotNotificationIfNeeded() { + guard thread is TSContactThread else { return } + let message = DataExtractionNotification() + message.kind = .screenshot + Storage.write { transaction in + MessageSender.send(message, in: self.thread, using: transaction) + } + } // MARK: Requesting Permission func requestCameraPermissionIfNeeded() -> Bool { diff --git a/Session/Conversations/ConversationVC.swift b/Session/Conversations/ConversationVC.swift index a899b4656..4f8a69a17 100644 --- a/Session/Conversations/ConversationVC.swift +++ b/Session/Conversations/ConversationVC.swift @@ -184,6 +184,7 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat notificationCenter.addObserver(self, selector: #selector(handleAudioDidFinishPlayingNotification(_:)), name: .SNAudioDidFinishPlaying, object: nil) notificationCenter.addObserver(self, selector: #selector(addOrRemoveBlockedBanner), name: NSNotification.Name(rawValue: kNSNotificationName_BlockListDidChange), object: nil) notificationCenter.addObserver(self, selector: #selector(handleGroupUpdatedNotification), name: .groupThreadUpdated, object: nil) + notificationCenter.addObserver(self, selector: #selector(sendScreenshotNotificationIfNeeded), name: UIApplication.userDidTakeScreenshotNotification, object: nil) // Mentions MentionsManager.populateUserPublicKeyCacheIfNeeded(for: thread.uniqueId!) // Draft diff --git a/SessionMessagingKit/Sending & Receiving/Data Extraction/DataExtractionNotificationInfoMessage.swift b/SessionMessagingKit/Sending & Receiving/Data Extraction/DataExtractionNotificationInfoMessage.swift index 6546fb55a..c1f856637 100644 --- a/SessionMessagingKit/Sending & Receiving/Data Extraction/DataExtractionNotificationInfoMessage.swift +++ b/SessionMessagingKit/Sending & Receiving/Data Extraction/DataExtractionNotificationInfoMessage.swift @@ -1,35 +1,29 @@ @objc(SNDataExtractionNotificationInfoMessage) final class DataExtractionNotificationInfoMessage : TSInfoMessage { - private let kind: DataExtractionNotification.Kind - init(kind: DataExtractionNotification.Kind, timestamp: UInt64, thread: TSThread) { - self.kind = kind - let infoMessageType: TSInfoMessageType - switch kind { - case .screenshot: infoMessageType = .screenshotNotification - case .mediaSaved: infoMessageType = .mediaSavedNotification - } - super.init(timestamp: timestamp, in: thread, messageType: infoMessageType) + init(type: TSInfoMessageType, sentTimestamp: UInt64, thread: TSThread, referencedAttachmentTimestamp: UInt64?) { + super.init(timestamp: sentTimestamp, in: thread, messageType: type) } required init(coder: NSCoder) { - preconditionFailure("Not implemented.") + super.init(coder: coder) } required init(dictionary dictionaryValue: [String:Any]!) throws { - preconditionFailure("Not implemented.") + try super.init(dictionary: dictionaryValue) } override func previewText(with transaction: YapDatabaseReadTransaction) -> String { guard let thread = thread as? TSContactThread else { return "" } // Should never occur let sessionID = thread.contactIdentifier() let displayName = Storage.shared.getContact(with: sessionID)?.displayName(for: .regular) ?? sessionID - switch kind { - case .screenshot: return "\(displayName) took a screenshot." - case .mediaSaved: - // TODO: Use the timestamp and tell the user * which * media was saved + switch messageType { + case .screenshotNotification: return "\(displayName) took a screenshot." + case .mediaSavedNotification: + // TODO: Use referencedAttachmentTimestamp to tell the user * which * media was saved return "Media saved by \(displayName)." + default: preconditionFailure() } } } diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 09d370e82..4ae6838a3 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -108,7 +108,12 @@ extension MessageReceiver { // MARK: - Data Extraction Notification private static func handleDataExtractionNotification(_ message: DataExtractionNotification, using transaction: Any) { - + let transaction = transaction as! YapDatabaseReadWriteTransaction + guard message.groupPublicKey == nil, + let thread = TSContactThread.getWithContactId(message.sender!, transaction: transaction) else { return } + // TODO: Handle media saved type notifications + let message = DataExtractionNotificationInfoMessage(type: .screenshotNotification, sentTimestamp: message.sentTimestamp!, thread: thread, referencedAttachmentTimestamp: nil) + message.save(with: transaction) }