Skip to content

Commit 514369b

Browse files
committed
Compacted the function even more.
(cherry picked from commit 5104770e25498869993428a39adeaea4b5ee91f9)
1 parent e47a99a commit 514369b

File tree

1 file changed

+22
-79
lines changed

1 file changed

+22
-79
lines changed

Signal/Notifications/NotificationActionHandler.swift

+22-79
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,27 @@ public class NotificationActionHandler {
120120
private class func reply(userInfo: [AnyHashable: Any], replyText: String) throws -> Promise<Void> {
121121
return firstly { () -> Promise<NotificationMessage> in
122122
self.notificationMessage(forUserInfo: userInfo)
123-
}.then(on: DispatchQueue.global()) { (notificationMessage: NotificationMessage) -> Promise<(DraftQuotedReplyModel?, NotificationMessage)> in
124-
return getDraftQuotedReplyModelTupleFromIncomingMessage(notificationMessage: notificationMessage)
125-
}.then(on: DispatchQueue.global()) { (informationTuple: (draft: DraftQuotedReplyModel?, notificationMessage: NotificationMessage)) -> Promise<(DraftQuotedReplyModel.ForSending?, NotificationMessage)> in
126-
return getDraftQuotedReplyModelForSendingTupleFromDraftAndIncomingMessage(optionalDraftModel: informationTuple.draft, notificationMessage: informationTuple.notificationMessage)
127-
}.then(on: DispatchQueue.global()) { (informationTuple: (draft: DraftQuotedReplyModel.ForSending?, notificationMessage: NotificationMessage)) -> Promise<Void> in
128-
let thread = informationTuple.notificationMessage.thread
129-
let interaction = informationTuple.notificationMessage.interaction
123+
}.then(on: DispatchQueue.global()) { (notificationMessage: NotificationMessage) -> Promise<Void> in
124+
let thread = notificationMessage.thread
125+
let interaction = notificationMessage.interaction
126+
var draftModelForSending: DraftQuotedReplyModel.ForSending?
130127
guard (interaction is TSOutgoingMessage) || (interaction is TSIncomingMessage) else {
131128
throw OWSAssertionError("Unexpected interaction type.")
132129
}
133-
130+
131+
let optionalDraftModel: DraftQuotedReplyModel? = SSKEnvironment.shared.databaseStorageRef.read { transaction in
132+
if
133+
let incomingMessage = notificationMessage.interaction as? TSIncomingMessage,
134+
let draftQuotedReplyModel = DependenciesBridge.shared.quotedReplyManager.buildDraftQuotedReply(originalMessage: incomingMessage, tx: transaction.asV2Read) {
135+
return draftQuotedReplyModel
136+
}
137+
return nil
138+
}
139+
140+
if let draftModel = optionalDraftModel {
141+
draftModelForSending = try? DependenciesBridge.shared.quotedReplyManager.prepareDraftForSending(draftModel)
142+
}
143+
134144
return firstly(on: DispatchQueue.global()) { () -> Promise<Void> in
135145
SSKEnvironment.shared.databaseStorageRef.write { transaction in
136146
let builder: TSOutgoingMessageBuilder = .withDefaultValues(thread: thread)
@@ -139,7 +149,7 @@ public class NotificationActionHandler {
139149
// If we're replying to a group story reply, keep the reply within that context.
140150
if
141151
let incomingMessage = interaction as? TSIncomingMessage,
142-
informationTuple.notificationMessage.isGroupStoryReply,
152+
notificationMessage.isGroupStoryReply,
143153
let storyTimestamp = incomingMessage.storyTimestamp,
144154
let storyAuthorAci = incomingMessage.storyAuthorAci
145155
{
@@ -160,7 +170,7 @@ public class NotificationActionHandler {
160170
explicitRecipients: [],
161171
skippedRecipients: [],
162172
transaction: transaction
163-
), quotedReplyDraft: informationTuple.draft)
173+
), quotedReplyDraft: draftModelForSending)
164174
do {
165175
let preparedMessage = try unpreparedMessage.prepare(tx: transaction)
166176
return ThreadUtil.enqueueMessagePromise(message: preparedMessage, transaction: transaction)
@@ -173,78 +183,11 @@ public class NotificationActionHandler {
173183
SSKEnvironment.shared.notificationPresenterRef.notifyUserOfFailedSend(inThread: thread)
174184
throw error
175185
}.then(on: DispatchQueue.global()) { () -> Promise<Void> in
176-
self.markMessageAsRead(notificationMessage: informationTuple.notificationMessage)
177-
}
178-
}
179-
}
180-
181-
private class func getDraftQuotedReplyModelTupleFromIncomingMessage(notificationMessage: NotificationMessage) -> Promise<(DraftQuotedReplyModel?, NotificationMessage)> {
182-
let (promise, future) = Promise<(DraftQuotedReplyModel?, NotificationMessage)>.pending()
183-
SSKEnvironment.shared.databaseStorageRef.read { readTransaction in
184-
if
185-
let incomingMessage = notificationMessage.interaction as? TSIncomingMessage,
186-
let draftQuotedReplyModel = DependenciesBridge.shared.quotedReplyManager.buildDraftQuotedReply(originalMessage: incomingMessage, tx: readTransaction.asV2Read) {
187-
future.resolve((draftQuotedReplyModel, notificationMessage))
188-
}
189-
return future.resolve((nil, notificationMessage))
190-
}
191-
return promise
192-
}
193-
194-
private class func getDraftQuotedReplyModelForSendingTupleFromDraftAndIncomingMessage(optionalDraftModel: DraftQuotedReplyModel?, notificationMessage: NotificationMessage) -> Promise<(DraftQuotedReplyModel.ForSending?, NotificationMessage)> {
195-
guard let draftModel = optionalDraftModel else {
196-
return Promise.value((nil, notificationMessage))
197-
}
198-
199-
do {
200-
let draftForSending = try DependenciesBridge.shared.quotedReplyManager.prepareDraftForSending(draftModel)
201-
return Promise.value((draftForSending, notificationMessage))
202-
} catch {
203-
return Promise.value((nil, notificationMessage))
204-
}
205-
}
206-
207-
private class func sendReplyToNoficationMessageWithMessageToReplyTo(replyText: String, messageBeingRespondedTo: DraftQuotedReplyModel.ForSending?, notificationMessage: NotificationMessage) throws -> Promise<Void> {
208-
return SSKEnvironment.shared.databaseStorageRef.write { transaction in
209-
let builder: TSOutgoingMessageBuilder = .withDefaultValues(thread: notificationMessage.thread)
210-
builder.messageBody = replyText
211-
212-
// If we're replying to a group story reply, keep the reply within that context.
213-
if
214-
let incomingMessage = notificationMessage.interaction as? TSIncomingMessage,
215-
notificationMessage.isGroupStoryReply,
216-
let storyTimestamp = incomingMessage.storyTimestamp,
217-
let storyAuthorAci = incomingMessage.storyAuthorAci
218-
{
219-
builder.storyTimestamp = storyTimestamp
220-
builder.storyAuthorAci = storyAuthorAci
221-
} else {
222-
// We only use the thread's DM timer for normal messages & 1:1 story
223-
// replies -- group story replies last for the lifetime of the story.
224-
let dmConfigurationStore = DependenciesBridge.shared.disappearingMessagesConfigurationStore
225-
let dmConfig = dmConfigurationStore.fetchOrBuildDefault(for: .thread(notificationMessage.thread), tx: transaction.asV2Read)
226-
builder.expiresInSeconds = dmConfig.durationSeconds
227-
builder.expireTimerVersion = NSNumber(value: dmConfig.timerVersion)
228-
}
229-
230-
let outgoingMessage = TSOutgoingMessage(
231-
outgoingMessageWith: builder,
232-
additionalRecipients: [],
233-
explicitRecipients: [],
234-
skippedRecipients: [],
235-
transaction: transaction
236-
)
237-
238-
let unpreparedMessage = UnpreparedOutgoingMessage.forMessage(outgoingMessage, quotedReplyDraft: messageBeingRespondedTo)
239-
do {
240-
let preparedMessage = try unpreparedMessage.prepare(tx: transaction)
241-
return ThreadUtil.enqueueMessagePromise(message: preparedMessage, transaction: transaction)
242-
} catch {
243-
return Promise(error: error)
186+
self.markMessageAsRead(notificationMessage: notificationMessage)
244187
}
245188
}
246189
}
247-
190+
248191
private class func showThread(userInfo: [AnyHashable: Any]) throws -> Promise<Void> {
249192
return firstly { () -> Promise<NotificationMessage> in
250193
self.notificationMessage(forUserInfo: userInfo)

0 commit comments

Comments
 (0)