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.
49 lines
2.2 KiB
Swift
49 lines
2.2 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
import GRDB
|
|
import SessionUtilitiesKit
|
|
|
|
public enum FailedMessageSendsJob: JobExecutor {
|
|
public static let maxFailureCount: Int = -1
|
|
public static let requiresThreadId: Bool = false
|
|
public static let requiresInteractionId: Bool = false
|
|
|
|
public static func run(
|
|
_ job: Job,
|
|
queue: DispatchQueue,
|
|
success: @escaping (Job, Bool) -> Void,
|
|
failure: @escaping (Job, Error, Bool) -> Void,
|
|
deferred: @escaping (Job) -> Void,
|
|
using dependencies: Dependencies
|
|
) {
|
|
guard Identity.userExists(using: dependencies) else { return success(job, false) }
|
|
|
|
var changeCount: Int = -1
|
|
var attachmentChangeCount: Int = -1
|
|
|
|
// Update all 'sending' message states to 'failed'
|
|
dependencies[singleton: .storage]
|
|
.writePublisher { db in
|
|
let sendChangeCount: Int = try RecipientState
|
|
.filter(RecipientState.Columns.state == RecipientState.State.sending)
|
|
.updateAll(db, RecipientState.Columns.state.set(to: RecipientState.State.failed))
|
|
let syncChangeCount: Int = try RecipientState
|
|
.filter(RecipientState.Columns.state == RecipientState.State.syncing)
|
|
.updateAll(db, RecipientState.Columns.state.set(to: RecipientState.State.failedToSync))
|
|
attachmentChangeCount = try Attachment
|
|
.filter(Attachment.Columns.state == Attachment.State.uploading)
|
|
.updateAll(db, Attachment.Columns.state.set(to: Attachment.State.failedUpload))
|
|
changeCount = (sendChangeCount + syncChangeCount)
|
|
}
|
|
.subscribe(on: queue, using: dependencies)
|
|
.receive(on: queue, using: dependencies)
|
|
.sinkUntilComplete(
|
|
receiveCompletion: { _ in
|
|
Log.info("[FailedMessageSendsJob] Marked \(changeCount) message\(plural: changeCount) as failed (\(attachmentChangeCount) upload\(plural: attachmentChangeCount) cancelled)")
|
|
success(job, false)
|
|
}
|
|
)
|
|
}
|
|
}
|