session-ios/SessionMessagingKit/Jobs/Types/NotifyPushServerJob.swift
Morgan Pretty 3baeb981d9 Further work on the JobRunner
Moved the JobRunner into SessionUtilitiesKit so it can be used by SessionSnodeKit
Exposed a 'sharedLokiProject' value on UserDefaults to remove the hard-coded group name used everywhere
Added "blocking" job support for 'OnLaunch' and 'OnActive' jobs to the JobRunner (will retry until it succeeds)
Added the UpdateProfilePicture and RetrieveDefaultOpenGroupRooms jobs
2022-04-27 10:48:54 +10:00

67 lines
2 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import PromiseKit
import SessionSnodeKit
import SessionUtilitiesKit
public enum NotifyPushServerJob: JobExecutor {
public static var maxFailureCount: Int = 20
public static var requiresThreadId: Bool = false
public static let requiresInteractionId: Bool = false
public static func run(
_ job: Job,
success: @escaping (Job, Bool) -> (),
failure: @escaping (Job, Error?, Bool) -> (),
deferred: @escaping (Job) -> ()
) {
let server: String = PushNotificationAPI.server
guard
let url: URL = URL(string: "\(server)/notify"),
let detailsData: Data = job.details,
let details: Details = try? JSONDecoder().decode(Details.self, from: detailsData)
else {
failure(job, JobRunnerError.missingRequiredDetails, false)
return
}
let parameters: JSON = [
"data": details.message.data.description,
"send_to": details.message.recipient
]
let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [
"Content-Type": "application/json"
]
attempt(maxRetryCount: 4, recoveringOn: DispatchQueue.global()) {
OnionRequestAPI
.sendOnionRequest(
request,
to: server,
target: "/loki/v2/lsrpc",
using: PushNotificationAPI.serverPublicKey
)
.map { _ in }
}
.done { _ in
success(job, false)
}
.`catch` { error in
failure(job, error, false)
}
.retainUntilComplete()
}
}
// MARK: - NotifyPushServerJob.Details
extension NotifyPushServerJob {
public struct Details: Codable {
public let message: SnodeMessage
}
}