Signal-iOS/SignalUI/ViewModels/ThreadUtil+SignalUI.swift
Sasha Weiss 242dfd2bce
Add all JobQueues to environment, via a wrapper
Currently, only some `JobQueue` types are initialized during startup
(as part of `Environment`, or `SSKEnvironment`). Initialization is
required, however, for a `JobQueue` type to restart any latent jobs.
That means that, for example, a durable `SendGiftBadge` job that failed,
and should be reattempted at a later date, will not in fact be restarted
since no `SendGiftBadgeJobQueue` will be initialized at launch.

This change adds `SSKJobQueues` and `SignalMessagingJobQueues` types,
which are intended to be singletons that hold within them a singleton
job queue for each of our `JobQueue` types. These wrappers are added to
`SSKEnvironment` and `Environment` (from SignalMessaging) respectively,
ensuring that all the `JobQueue`s they contain are initialized as part
of environment setup. The wrappers also avoid the need to add a new
property to the (already large) environment types for each new future
`JobQueue`.

This change also updates all existing call sites that accessed a
`JobQueue` from an environment object, to direct that access now through
the wrapper type.
2022-11-09 14:08:44 -06:00

162 lines
6.9 KiB
Swift

//
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import SignalMessaging
@objc
public extension ThreadUtil {
// MARK: - Durable Message Enqueue
@discardableResult
class func enqueueMessage(body messageBody: MessageBody?,
mediaAttachments: [SignalAttachment] = [],
thread: TSThread,
quotedReplyModel: OWSQuotedReplyModel? = nil,
linkPreviewDraft: OWSLinkPreviewDraft? = nil,
persistenceCompletionHandler persistenceCompletion: PersistenceCompletion? = nil,
transaction readTransaction: SDSAnyReadTransaction) -> TSOutgoingMessage {
AssertIsOnMainThread()
let outgoingMessagePreparer = OutgoingMessagePreparer(messageBody: messageBody,
mediaAttachments: mediaAttachments,
thread: thread,
quotedReplyModel: quotedReplyModel,
transaction: readTransaction)
let message: TSOutgoingMessage = outgoingMessagePreparer.unpreparedMessage
BenchManager.startEvent(
title: "Send Message Milestone: Sending (\(message.timestamp))",
eventId: "sendMessageSending-\(message.timestamp)",
logInProduction: true
)
BenchManager.startEvent(
title: "Send Message Milestone: Sent (\(message.timestamp))",
eventId: "sendMessageSentSent-\(message.timestamp)",
logInProduction: true
)
BenchManager.startEvent(
title: "Send Message Milestone: Marked as Sent (\(message.timestamp))",
eventId: "sendMessageMarkedAsSent-\(message.timestamp)",
logInProduction: true
)
BenchManager.benchAsync(title: "Send Message Milestone: Enqueue \(message.timestamp)") { benchmarkCompletion in
Self.enqueueSendAsyncWrite { writeTransaction in
outgoingMessagePreparer.insertMessage(linkPreviewDraft: linkPreviewDraft,
transaction: writeTransaction)
Self.sskJobQueues.messageSenderJobQueue.add(
message: outgoingMessagePreparer,
transaction: writeTransaction
)
writeTransaction.addSyncCompletion {
benchmarkCompletion()
}
if let persistenceCompletion = persistenceCompletion {
writeTransaction.addAsyncCompletionOnMain {
persistenceCompletion()
}
}
}
}
if message.hasRenderableContent() {
thread.donateSendMessageIntent(for: message, transaction: readTransaction)
}
return message
}
class func createUnsentMessage(body messageBody: MessageBody?,
mediaAttachments: [SignalAttachment],
thread: TSThread,
quotedReplyModel: OWSQuotedReplyModel? = nil,
linkPreviewDraft: OWSLinkPreviewDraft? = nil,
transaction: SDSAnyWriteTransaction) throws -> TSOutgoingMessage {
let preparer = OutgoingMessagePreparer(messageBody: messageBody,
mediaAttachments: mediaAttachments,
thread: thread,
quotedReplyModel: quotedReplyModel,
transaction: transaction)
preparer.insertMessage(linkPreviewDraft: linkPreviewDraft, transaction: transaction)
return try preparer.prepareMessage(transaction: transaction)
}
}
// MARK: -
extension OutgoingMessagePreparer {
@objc
public convenience init(messageBody: MessageBody?,
mediaAttachments: [SignalAttachment] = [],
thread: TSThread,
quotedReplyModel: OWSQuotedReplyModel? = nil,
transaction: SDSAnyReadTransaction) {
var attachments = mediaAttachments
let truncatedText: String?
let bodyRanges: MessageBodyRanges?
if let messageBody = messageBody, !messageBody.text.isEmpty {
if messageBody.text.lengthOfBytes(using: .utf8) >= kOversizeTextMessageSizeThreshold {
truncatedText = messageBody.text.truncated(toByteCount: kOversizeTextMessageSizeThreshold)
bodyRanges = messageBody.ranges
if let dataSource = DataSourceValue.dataSource(withOversizeText: messageBody.text) {
let attachment = SignalAttachment.attachment(dataSource: dataSource,
dataUTI: kOversizeTextAttachmentUTI)
attachments.append(attachment)
} else {
owsFailDebug("dataSource was unexpectedly nil")
}
} else {
truncatedText = messageBody.text
bodyRanges = messageBody.ranges
}
} else {
truncatedText = nil
bodyRanges = nil
}
let expiresInSeconds = thread.disappearingMessagesDuration(with: transaction)
assert(attachments.allSatisfy { !$0.hasError && !$0.mimeType.isEmpty })
let isVoiceMessage = attachments.count == 1 && attachments.last?.isVoiceMessage == true
var isViewOnceMessage = false
for attachment in mediaAttachments {
if attachment.isViewOnceAttachment {
assert(mediaAttachments.count == 1)
isViewOnceMessage = true
break
}
if attachment.isBorderless {
assert(mediaAttachments.count == 1)
break
}
}
// Discard quoted reply for view-once messages.
let quotedMessage: TSQuotedMessage? = (isViewOnceMessage
? nil
: quotedReplyModel?.buildQuotedMessageForSending())
let message = TSOutgoingMessageBuilder(
thread: thread,
messageBody: truncatedText,
bodyRanges: bodyRanges,
expiresInSeconds: expiresInSeconds,
isVoiceMessage: isVoiceMessage,
quotedMessage: quotedMessage,
isViewOnceMessage: isViewOnceMessage
).build(transaction: transaction)
let attachmentInfos = attachments.map { $0.buildOutgoingAttachmentInfo(message: message) }
self.init(message, unsavedAttachmentInfos: attachmentInfos)
}
}