Moved the Storage.write call into the `self.approveMessageRequestIfNeeded` call

pull/604/head
Morgan Pretty 3 years ago
parent 212c5e87aa
commit e4def22472

@ -263,10 +263,8 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc
let linkPreviewDraft = snInputView.linkPreviewInfo?.draft
let tsMessage = TSOutgoingMessage.from(message, associatedWith: thread)
Storage.write(with: { transaction in
let promise: Promise<Void> = self.approveMessageRequestIfNeeded(
for: self.thread,
with: transaction,
isNewThread: !oldThreadShouldBeVisible,
timestamp: (sentTimestamp - 1) // Set 1ms earlier as this is used for sorting
)
@ -305,7 +303,6 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc
}
promise.retainUntilComplete()
})
}
func sendAttachments(_ attachments: [SignalAttachment], with text: String, onComplete: (() -> ())? = nil) {
@ -329,10 +326,8 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc
let oldThreadShouldBeVisible: Bool = thread.shouldBeVisible
let tsMessage = TSOutgoingMessage.from(message, associatedWith: thread)
Storage.write(with: { transaction in
let promise: Promise<Void> = self.approveMessageRequestIfNeeded(
for: self.thread,
with: transaction,
isNewThread: !oldThreadShouldBeVisible,
timestamp: (sentTimestamp - 1) // Set 1ms earlier as this is used for sorting
)
@ -366,7 +361,6 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc
}
promise.retainUntilComplete()
})
}
func handleMessageSent() {
@ -1103,7 +1097,7 @@ extension ConversationVC: UIDocumentInteractionControllerDelegate {
extension ConversationVC {
fileprivate func approveMessageRequestIfNeeded(for thread: TSThread?, with transaction: YapDatabaseReadWriteTransaction, isNewThread: Bool, timestamp: UInt64) -> Promise<Void> {
fileprivate func approveMessageRequestIfNeeded(for thread: TSThread?, isNewThread: Bool, timestamp: UInt64) -> Promise<Void> {
guard let contactThread: TSContactThread = thread as? TSContactThread else { return Promise.value(()) }
// If the contact doesn't exist then we should create it so we can store the 'isApproved' state
@ -1134,7 +1128,17 @@ extension ConversationVC {
}
return promise
.then { MessageSender.sendNonDurably(messageRequestResponse, in: contactThread, using: transaction) }
.then { _ -> Promise<Void> in
let (promise, seal) = Promise<Void>.pending()
Storage.writeSync { transaction in
MessageSender.sendNonDurably(messageRequestResponse, in: contactThread, using: transaction)
.done { seal.fulfill(()) }
.catch { _ in seal.fulfill(()) } // Fulfill even if this failed; the configuration in the swarm should be at most 2 days old
.retainUntilComplete()
}
return promise
}
.map { _ in
if self?.presentedViewController is ModalActivityIndicatorViewController {
self?.dismiss(animated: true, completion: nil) // Dismiss the loader
@ -1143,9 +1147,11 @@ extension ConversationVC {
}
.map { _ in
// Default 'didApproveMe' to true for the person approving the message request
Storage.write { transaction in
contact.isApproved = true
contact.didApproveMe = (contact.didApproveMe || !isNewThread)
Storage.shared.setContact(contact, using: transaction)
}
// Hide the 'messageRequestView' since the request has been approved and force a config
// sync to propagate the contact approval state (both must run on the main thread)
@ -1190,10 +1196,8 @@ extension ConversationVC {
}
@objc func acceptMessageRequest() {
Storage.write { transaction in
let promise: Promise<Void> = self.approveMessageRequestIfNeeded(
for: self.thread,
with: transaction,
isNewThread: false,
timestamp: NSDate.millisecondTimestamp()
)
@ -1207,7 +1211,6 @@ extension ConversationVC {
promise.retainUntilComplete()
}
}
@objc func deleteMessageRequest() {
guard let uniqueId: String = thread.uniqueId else { return }

@ -29,7 +29,7 @@ extension AppDelegate {
// Note: SQLite only supports a single write thread so we can be sure this will retrieve the most up-to-date data
Storage.writeSync { transaction in
guard Storage.shared.getUser()?.name != nil, let configurationMessage = ConfigurationMessage.getCurrent(with: transaction) else {
guard Storage.shared.getUser(using: transaction)?.name != nil, let configurationMessage = ConfigurationMessage.getCurrent(with: transaction) else {
seal.fulfill(())
return
}

Loading…
Cancel
Save