From 6c08f98fbbc4268541a2694c0e2cec822b6ba8fd Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 14 Feb 2019 18:36:45 -0700 Subject: [PATCH] replying to notification marks thread as read --- .../Notifications/AppNotifications.swift | 32 ++++++++++--------- .../src/Util/YapDatabase+Promise.swift | 20 ++++++++++++ 2 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 SignalServiceKit/src/Util/YapDatabase+Promise.swift diff --git a/Signal/src/UserInterface/Notifications/AppNotifications.swift b/Signal/src/UserInterface/Notifications/AppNotifications.swift index 9f957e0cf..afe1ebc6b 100644 --- a/Signal/src/UserInterface/Notifications/AppNotifications.swift +++ b/Signal/src/UserInterface/Notifications/AppNotifications.swift @@ -613,15 +613,7 @@ class NotificationActionHandler { throw NotificationError.failDebug("unable to find thread with id: \(threadId)") } - return Promise { resolver in - self.dbConnection.asyncReadWrite({ transaction in - thread.markAllAsRead(with: transaction) - }, - completionBlock: { - self.notificationPresenter.cancelNotifications(threadId: threadId) - resolver.fulfill(()) - }) - } + return markAsRead(thread: thread) } func reply(userInfo: [AnyHashable: Any], replyText: String) throws -> Promise { @@ -633,12 +625,16 @@ class NotificationActionHandler { throw NotificationError.failDebug("unable to find thread with id: \(threadId)") } - return ThreadUtil.sendMessageNonDurably(text: replyText, - thread: thread, - quotedReplyModel: nil, - messageSender: messageSender).recover { error in - Logger.warn("Failed to send reply message from notification with error: \(error)") - self.notificationPresenter.notifyForFailedSend(inThread: thread) + return markAsRead(thread: thread).then { () -> Promise in + let sendPromise = ThreadUtil.sendMessageNonDurably(text: replyText, + thread: thread, + quotedReplyModel: nil, + messageSender: self.messageSender) + + return sendPromise.recover { error in + Logger.warn("Failed to send reply message from notification with error: \(error)") + self.notificationPresenter.notifyForFailedSend(inThread: thread) + } } } @@ -654,6 +650,12 @@ class NotificationActionHandler { signalApp.presentConversation(forThreadId: threadId, animated: shouldAnimate) return Promise.value(()) } + + private func markAsRead(thread: TSThread) -> Promise { + return dbConnection.readWritePromise { transaction in + thread.markAllAsRead(with: transaction) + } + } } extension ThreadUtil { diff --git a/SignalServiceKit/src/Util/YapDatabase+Promise.swift b/SignalServiceKit/src/Util/YapDatabase+Promise.swift new file mode 100644 index 000000000..2fe3351a2 --- /dev/null +++ b/SignalServiceKit/src/Util/YapDatabase+Promise.swift @@ -0,0 +1,20 @@ +// +// Copyright (c) 2019 Open Whisper Systems. All rights reserved. +// + +import Foundation +import PromiseKit + +public extension YapDatabaseConnection { + + @objc + func readWritePromise(_ block: @escaping (YapDatabaseReadWriteTransaction) -> Void) -> AnyPromise { + return AnyPromise(readWritePromise(block) as Promise) + } + + func readWritePromise(_ block: @escaping (YapDatabaseReadWriteTransaction) -> Void) -> Promise { + return Promise { resolver in + self.asyncReadWrite(block, completionBlock: resolver.fulfill) + } + } +}