session-ios/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift

128 lines
5.6 KiB
Swift
Raw Normal View History

2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-05 23:17:05 +01:00
internal enum MessageReceiver {
internal enum Error : LocalizedError {
2020-11-06 06:31:56 +01:00
case invalidMessage
case unknownMessage
case unknownEnvelopeType
case noUserPublicKey
case noData
2020-11-17 06:23:13 +01:00
case senderBlocked
// Shared sender keys
case invalidGroupPublicKey
case noGroupPrivateKey
case sharedSecretGenerationFailed
case selfSend
2020-11-06 06:31:56 +01:00
internal var errorDescription: String? {
2020-11-06 06:31:56 +01:00
switch self {
case .invalidMessage: return "Invalid message."
case .unknownMessage: return "Unknown message type."
case .unknownEnvelopeType: return "Unknown envelope type."
case .noUserPublicKey: return "Couldn't find user key pair."
case .noData: return "Received an empty envelope."
2020-11-17 06:23:13 +01:00
case .senderBlocked: return "Received a message from a blocked user."
// Shared sender keys
case .invalidGroupPublicKey: return "Invalid group public key."
case .noGroupPrivateKey: return "Missing group private key."
case .sharedSecretGenerationFailed: return "Couldn't generate a shared secret."
case .selfSend: return "Message addressed at self."
2020-11-06 06:31:56 +01:00
}
}
}
2020-11-17 06:23:13 +01:00
internal static func parse(_ data: Data, using transaction: Any) throws -> Message {
// Parse the envelope
2020-11-17 06:23:13 +01:00
let envelope = try SNProtoEnvelope.parseData(data)
// Decrypt the contents
let plaintext: Data
2020-11-17 06:23:13 +01:00
let sender: String
switch envelope.type {
2020-11-17 06:23:13 +01:00
case .unidentifiedSender: (plaintext, sender) = try decryptWithSignalProtocol(envelope: envelope, using: transaction)
case .closedGroupCiphertext: (plaintext, sender) = try decryptWithSharedSenderKeys(envelope: envelope, using: transaction)
default: throw Error.unknownEnvelopeType
}
2020-11-17 06:23:13 +01:00
// Don't process the envelope any further if the sender is blocked
guard !Configuration.shared.storage.isBlocked(sender) else { throw Error.senderBlocked }
// Parse the proto
2020-11-06 04:05:45 +01:00
let proto: SNProtoContent
do {
proto = try SNProtoContent.parseData((plaintext as NSData).removePadding())
2020-11-06 04:05:45 +01:00
} catch {
SNLog("Couldn't parse proto due to error: \(error).")
throw error
2020-11-06 04:05:45 +01:00
}
// Parse the message
2020-11-06 06:31:56 +01:00
let message: Message? = {
if let readReceipt = ReadReceipt.fromProto(proto) { return readReceipt }
if let sessionRequest = SessionRequest.fromProto(proto) { return sessionRequest }
if let typingIndicator = TypingIndicator.fromProto(proto) { return typingIndicator }
if let closedGroupUpdate = ClosedGroupUpdate.fromProto(proto) { return closedGroupUpdate }
if let expirationTimerUpdate = ExpirationTimerUpdate.fromProto(proto) { return expirationTimerUpdate }
if let visibleMessage = VisibleMessage.fromProto(proto) { return visibleMessage }
return nil
}()
if let message = message {
2020-11-17 06:23:13 +01:00
message.sender = sender
message.recipient = Configuration.shared.storage.getUserPublicKey()
2020-11-10 05:48:47 +01:00
message.receivedTimestamp = NSDate.millisecondTimestamp()
guard message.isValid else { throw Error.invalidMessage }
2020-11-06 06:31:56 +01:00
return message
} else {
2020-11-07 23:03:08 +01:00
throw Error.unknownMessage
2020-11-06 06:31:56 +01:00
}
2020-11-06 04:05:45 +01:00
}
2020-11-17 06:23:13 +01:00
internal static func handle(_ message: Message, messageServerID: UInt64?, using transaction: Any) {
switch message {
case let message as ReadReceipt: handleReadReceipt(message, using: transaction)
case let message as SessionRequest: handleSessionRequest(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)
2020-11-17 06:23:13 +01:00
case let message as VisibleMessage: handleVisibleMessage(message, using: transaction)
default: fatalError()
}
}
private static func handleReadReceipt(_ message: ReadReceipt, using transaction: Any) {
}
private static func handleSessionRequest(_ message: SessionRequest, using transaction: Any) {
}
private static func handleTypingIndicator(_ message: TypingIndicator, using transaction: Any) {
let storage = Configuration.shared.storage
switch message.kind! {
case .started: storage.showTypingIndicatorIfNeeded(for: message.sender!)
case .stopped: storage.hideTypingIndicatorIfNeeded(for: message.sender!)
}
}
private static func handleClosedGroupUpdate(_ message: ClosedGroupUpdate, using transaction: Any) {
}
private static func handleExpirationTimerUpdate(_ message: ExpirationTimerUpdate, using transaction: Any) {
}
2020-11-17 06:23:13 +01:00
private static func handleVisibleMessage(_ message: VisibleMessage, using transaction: Any) {
let storage = Configuration.shared.storage
// Update profile if needed
if let profile = message.profile {
storage.updateProfile(for: message.sender!, from: profile, using: transaction)
}
// Persist the message
let (threadID, tsIncomingMessage) = storage.persist(message, using: transaction)
message.threadID = threadID
// Cancel any typing indicators
storage.cancelTypingIndicatorsIfNeeded(for: message.sender!)
2020-11-17 06:23:13 +01:00
// Notify the user if needed
storage.notifyUserIfNeeded(for: tsIncomingMessage, threadID: threadID)
}
2020-11-05 23:17:05 +01:00
}