@ -1,9 +1,11 @@
import SessionUtilitiesKit
import PromiseKit
public final class MessageReceiveJob : NSObject , Job , NSCoding { // N S O b j e c t / N S C o d i n g c o n f o r m a n c e i s n e e d e d f o r Y a p D a t a b a s e c o m p a t i b i l i t y
public let data : Data
public let openGroupMessageServerID : UInt64 ?
public let openGroupID : String ?
public let isBackgroundPoll : Bool
public var delegate : JobDelegate ?
public var id : String ?
public var failureCount : UInt = 0
@ -13,10 +15,11 @@ public final class MessageReceiveJob : NSObject, Job, NSCoding { // NSObject/NSC
public static let maxFailureCount : UInt = 10
// MARK: I n i t i a l i z a t i o n
public init ( data : Data , openGroupMessageServerID : UInt64 ? = nil , openGroupID : String ? = nil ) {
public init ( data : Data , openGroupMessageServerID : UInt64 ? = nil , openGroupID : String ? = nil , isBackgroundPoll : Bool ) {
self . data = data
self . openGroupMessageServerID = openGroupMessageServerID
self . openGroupID = openGroupID
self . isBackgroundPoll = isBackgroundPoll
#if DEBUG
if openGroupMessageServerID != nil { assert ( openGroupID != nil ) }
if openGroupID != nil { assert ( openGroupMessageServerID != nil ) }
@ -26,10 +29,12 @@ public final class MessageReceiveJob : NSObject, Job, NSCoding { // NSObject/NSC
// MARK: C o d i n g
public init ? ( coder : NSCoder ) {
guard let data = coder . decodeObject ( forKey : " data " ) as ! Data ? ,
let id = coder . decodeObject ( forKey : " id " ) as ! String ? else { return nil }
let id = coder . decodeObject ( forKey : " id " ) as ! String ? ,
let isBackgroundPoll = coder . decodeObject ( forKey : " isBackgroundPoll " ) as ! Bool ? else { return nil }
self . data = data
self . openGroupMessageServerID = coder . decodeObject ( forKey : " openGroupMessageServerID " ) as ! UInt64 ?
self . openGroupID = coder . decodeObject ( forKey : " openGroupID " ) as ! String ?
self . isBackgroundPoll = isBackgroundPoll
self . id = id
self . failureCount = coder . decodeObject ( forKey : " failureCount " ) as ! UInt ? ? ? 0
}
@ -38,17 +43,24 @@ public final class MessageReceiveJob : NSObject, Job, NSCoding { // NSObject/NSC
coder . encode ( data , forKey : " data " )
coder . encode ( openGroupMessageServerID , forKey : " openGroupMessageServerID " )
coder . encode ( openGroupID , forKey : " openGroupID " )
coder . encode ( isBackgroundPoll , forKey : " isBackgroundPoll " )
coder . encode ( id , forKey : " id " )
coder . encode ( failureCount , forKey : " failureCount " )
}
// MARK: R u n n i n g
public func execute ( ) {
let _ : Promise < Void > = execute ( )
}
public func execute ( ) -> Promise < Void > {
let ( promise , seal ) = Promise < Void > . pending ( )
SNMessagingKitConfiguration . shared . storage . withAsync ( { transaction in // I n t e n t i o n a l l y c a p t u r e s e l f
do {
let ( message , proto ) = try MessageReceiver . parse ( self . data , openGroupMessageServerID : self . openGroupMessageServerID , using : transaction )
try MessageReceiver . handle ( message , associatedWithProto : proto , openGroupID : self . openGroupID , using : transaction )
try MessageReceiver . handle ( message , associatedWithProto : proto , openGroupID : self . openGroupID , isBackgroundPoll: self . isBackgroundPoll , using: transaction )
self . handleSuccess ( )
seal . fulfill ( ( ) )
} catch {
SNLog ( " Couldn't receive message due to error: \( error ) . " )
if let error = error as ? MessageReceiver . Error , ! error . isRetryable {
@ -56,8 +68,10 @@ public final class MessageReceiveJob : NSObject, Job, NSCoding { // NSObject/NSC
} else {
self . handleFailure ( error : error )
}
seal . fulfill ( ( ) ) // T h e p r o m i s e i s j u s t u s e d t o k e e p t r a c k o f w h e n w e ' r e d o n e
}
} , completion : { } )
return promise
}
private func handleSuccess ( ) {