mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			68 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Swift
		
	
			
		
		
	
	
			68 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Swift
		
	
import SessionUtilitiesKit
 | 
						|
 | 
						|
public final class MessageReceiveJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
 | 
						|
    public let data: Data
 | 
						|
    public let messageServerID: UInt64?
 | 
						|
    public var delegate: JobDelegate?
 | 
						|
    public var id: String?
 | 
						|
    public var failureCount: UInt = 0
 | 
						|
 | 
						|
    // MARK: Settings
 | 
						|
    public class var collection: String { return "MessageReceiveJobCollection" }
 | 
						|
    public static let maxFailureCount: UInt = 10
 | 
						|
 | 
						|
    // MARK: Initialization
 | 
						|
    public init(data: Data, messageServerID: UInt64? = nil) {
 | 
						|
        self.data = data
 | 
						|
        self.messageServerID = messageServerID
 | 
						|
    }
 | 
						|
 | 
						|
    // MARK: Coding
 | 
						|
    public init?(coder: NSCoder) {
 | 
						|
        guard let data = coder.decodeObject(forKey: "data") as! Data?,
 | 
						|
            let id = coder.decodeObject(forKey: "id") as! String? else { return nil }
 | 
						|
        self.data = data
 | 
						|
        self.messageServerID = coder.decodeObject(forKey: "messageServerUD") as! UInt64?
 | 
						|
        self.id = id
 | 
						|
        self.failureCount = coder.decodeObject(forKey: "failureCount") as! UInt? ?? 0
 | 
						|
    }
 | 
						|
 | 
						|
    public func encode(with coder: NSCoder) {
 | 
						|
        coder.encode(data, forKey: "data")
 | 
						|
        coder.encode(messageServerID, forKey: "messageServerID")
 | 
						|
        coder.encode(id, forKey: "id")
 | 
						|
        coder.encode(failureCount, forKey: "failureCount")
 | 
						|
    }
 | 
						|
 | 
						|
    // MARK: Running
 | 
						|
    public func execute() {
 | 
						|
        Configuration.shared.storage.withAsync({ transaction in // Intentionally capture self
 | 
						|
            do {
 | 
						|
                let (message, proto) = try MessageReceiver.parse(self.data, messageServerID: self.messageServerID, using: transaction)
 | 
						|
                try MessageReceiver.handle(message, associatedWithProto: proto, using: transaction)
 | 
						|
                self.handleSuccess()
 | 
						|
            } catch {
 | 
						|
                SNLog("Couldn't parse message due to error: \(error).")
 | 
						|
                if let error = error as? MessageReceiver.Error, !error.isRetryable {
 | 
						|
                    self.handlePermanentFailure(error: error)
 | 
						|
                } else {
 | 
						|
                    self.handleFailure(error: error)
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }, completion: { })
 | 
						|
    }
 | 
						|
 | 
						|
    private func handleSuccess() {
 | 
						|
        delegate?.handleJobSucceeded(self)
 | 
						|
    }
 | 
						|
 | 
						|
    private func handlePermanentFailure(error: Error) {
 | 
						|
        delegate?.handleJobFailedPermanently(self, with: error)
 | 
						|
    }
 | 
						|
 | 
						|
    private func handleFailure(error: Error) {
 | 
						|
        delegate?.handleJobFailed(self, with: error)
 | 
						|
    }
 | 
						|
}
 | 
						|
 |