Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 109 additions & 71 deletions apple/InlineIOS/Features/Compose/PhotoUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,51 @@ extension ComposeView: UIImagePickerControllerDelegate, UINavigationControllerDe
}
}

private func shouldSendAsFile(_ image: UIImage) -> Bool {
private nonisolated static func shouldSendAsFile(_ image: UIImage) -> Bool {
let width = max(image.size.width, 1)
let height = max(image.size.height, 1)
let ratio = max(width / height, height / width)
return ratio > 20 || (width < 50 && height < 50)
}

private func makeImageAttachment(_ image: UIImage, optimizePhoto: Bool) throws -> FileMediaItem {
/// Saves at most this many images at once; each holds a full-resolution encode in memory.
private nonisolated static let imageSaveConcurrency = 2

/// Saves picked images as attachments off the main actor, keeping their order; nil where one
/// failed. Each is a full-resolution JPEG or PNG encode plus a file and a database write, which
/// took the main thread for a noticeable time per photo.
private nonisolated static func imageAttachments(_ images: [UIImage], optimizePhoto: Bool) async -> [FileMediaItem?] {
guard !images.isEmpty else { return [] }
return await Task.detached(priority: .userInitiated) {
await withTaskGroup(of: (Int, FileMediaItem?).self) { group in
@Sendable func save(_ index: Int) -> (Int, FileMediaItem?) {
do {
return (index, try imageAttachment(images[index], optimizePhoto: optimizePhoto))
} catch {
Log.shared.error("Failed to save photo attachment", error: error)
return (index, nil)
}
}
var items = [FileMediaItem?](repeating: nil, count: images.count)
var next = min(imageSaveConcurrency, images.count)
for index in 0 ..< next {
group.addTask { save(index) }
}
for await (index, item) in group {
items[index] = item
if next < images.count {
let index = next
group.addTask { save(index) }
next += 1
}
}
return items
}
}.value
}

/// Synchronous disk and database work: off the main actor, use `imageAttachments`.
private nonisolated static func imageAttachment(_ image: UIImage, optimizePhoto: Bool) throws -> FileMediaItem {
if shouldSendAsFile(image) {
let tempDirectory = FileHelpers.getTrueTemporaryDirectory()
let fileName = "image-\(UUID().uuidString).jpg"
Expand Down Expand Up @@ -325,81 +362,80 @@ extension ComposeView: UIImagePickerControllerDelegate, UINavigationControllerDe

func sendImage(_ image: UIImage, caption: String) {
guard let peerId else { return }
let chatId = chatId
let replyToMsgId = ChatState.shared.getState(peer: peerId).replyingMessageId

clearAttachments()
resetComposeStateAfterPreviewSend()
dismissPreview(dismissAttachmentPicker: currentPreviewUsesAttachmentPicker)
clearAttachments()
// sendMessageHaptic()

do {
let mediaItem = try makeImageAttachment(image, optimizePhoto: false)
let uniqueId = mediaItem.getItemUniqueId()
attachmentItems[uniqueId] = mediaItem
} catch {
Log.shared.error("Failed to save photo", error: error)
}

let didPrepareImage = !attachmentItems.isEmpty
for (_, attachment) in attachmentItems {
// The preview closes now; the message is sent once the photo is saved off the main thread.
Task { @MainActor in
guard let attachment = await Self.imageAttachments([image], optimizePhoto: false).first ?? nil else { return }
Transactions.shared.mutate(
transaction: .sendMessage(
.init(
text: caption,
peerId: peerId,
chatId: chatId ?? 0,
mediaItems: [attachment],
replyToMsgId: ChatState.shared.getState(peer: peerId).replyingMessageId,
replyToMsgId: replyToMsgId,
isSticker: nil,
entities: nil
)
)
)
if let chatId {
IntentDonationCoordinator.donateOutgoing(peerId: peerId, chatId: chatId)
}
}

if didPrepareImage, let chatId {
IntentDonationCoordinator.donateOutgoing(peerId: peerId, chatId: chatId)
}

resetComposeStateAfterPreviewSend()
dismissPreview(dismissAttachmentPicker: currentPreviewUsesAttachmentPicker)
clearAttachments()
// sendMessageHaptic()
}

func sendMultipleImages(_ photoItems: [PhotoItem]) {
guard let peerId else { return }

let chatId = chatId
let replyToMessageId = ChatState.shared.getState(peer: peerId).replyingMessageId

var items: [AlbumSendItem] = []
for (index, photoItem) in photoItems.enumerated() {
do {
let mediaItem = try makeImageAttachment(photoItem.image, optimizePhoto: true)
// Each photo keeps its own caption; the album shows the first one.
let captionText = photoItem.caption?.trimmingCharacters(in: .whitespacesAndNewlines)
items.append(AlbumSendItem(media: mediaItem, text: captionText?.isEmpty == false ? captionText : nil))
} catch {
Log.shared.error("Failed to save and send photo \(index + 1)", error: error)
}
}
// Photos picked together go out as albums of up to ten.
for send in TransactionSendMessage.sends(
peerId: peerId,
chatId: chatId ?? 0,
items: items,
replyToMsgId: replyToMessageId
) {
Transactions.shared.mutate(transaction: .sendMessage(send))
let images = photoItems.map(\.image)
// Each photo keeps its own caption; the album shows the first one.
let captions = photoItems.map { photoItem -> String? in
let captionText = photoItem.caption?.trimmingCharacters(in: .whitespacesAndNewlines)
return captionText?.isEmpty == false ? captionText : nil
}
let didSendImage = !items.isEmpty

if didSendImage, let chatId {
IntentDonationCoordinator.donateOutgoing(peerId: peerId, chatId: chatId)
}

// Clear state and dismiss
// Clear state and dismiss now; the photos are saved off the main thread, then sent.
resetComposeStateAfterPreviewSend()
dismissMultiPreview(dismissAttachmentPicker: currentPreviewUsesAttachmentPicker)
clearAttachments()
ChatState.shared.clearReplyingMessageId(peer: peerId)
// sendMessageHaptic()

Task { @MainActor in
let saved = await Self.imageAttachments(images, optimizePhoto: true)
var items: [AlbumSendItem] = []
for (index, mediaItem) in saved.enumerated() {
guard let mediaItem else {
Log.shared.error("Failed to save and send photo \(index + 1)")
continue
}
items.append(AlbumSendItem(media: mediaItem, text: captions[index]))
}
guard !items.isEmpty else { return }
// Photos picked together go out as albums of up to ten.
for send in TransactionSendMessage.sends(
peerId: peerId,
chatId: chatId ?? 0,
items: items,
replyToMsgId: replyToMessageId
) {
Transactions.shared.mutate(transaction: .sendMessage(send))
}
if let chatId {
IntentDonationCoordinator.donateOutgoing(peerId: peerId, chatId: chatId)
}
}
}

func handlePastedImage() {
Expand Down Expand Up @@ -430,21 +466,20 @@ extension ComposeView: UIImagePickerControllerDelegate, UINavigationControllerDe
func addImages(_ images: [UIImage]) {
guard !images.isEmpty else { return }

var didAddAny = false
for image in images {
do {
let mediaItem = try makeImageAttachment(image, optimizePhoto: true)
let destination = peerId
Task { @MainActor [weak self] in
let saved = await Self.imageAttachments(images, optimizePhoto: true)
guard let self, peerId == destination else { return }
var didAddAny = false
for mediaItem in saved.compactMap({ $0 }) {
let uniqueId = mediaItem.getItemUniqueId()
attachmentItems[uniqueId] = mediaItem
didAddAny = true
Log.shared.debug("Added image attachment with uniqueId: \(uniqueId)")
} catch {
Log.shared.error("Failed to save photo in attachments", error: error)
}
}

if didAddAny {
handleAttachmentItemsChanged(animated: false)
if didAddAny {
handleAttachmentItemsChanged(animated: false)
}
}
}

Expand Down Expand Up @@ -578,7 +613,8 @@ extension ComposeView: UIImagePickerControllerDelegate, UINavigationControllerDe
}

do {
let mediaItem = try makeImageAttachment(image, optimizePhoto: true)
// Rare (a GIF that is not animated), so saved here rather than off the main thread.
let mediaItem = try Self.imageAttachment(image, optimizePhoto: true)
attachmentItems[mediaItem.getItemUniqueId()] = mediaItem
completePendingVideoAttachments([pendingId])
} catch {
Expand Down Expand Up @@ -693,13 +729,10 @@ extension ComposeView: UIImagePickerControllerDelegate, UINavigationControllerDe
return .failure(.message("Couldn't open that recent photo."))
}

do {
let mediaItem = try makeImageAttachment(image, optimizePhoto: true)
return .success(mediaItem)
} catch {
Log.shared.error("Failed to save photo in attachments", error: error)
guard let mediaItem = await Self.imageAttachments([image], optimizePhoto: true).first ?? nil else {
return .failure(.message("Couldn't open that recent photo."))
}
return .success(mediaItem)
}

private func loadRecentImage(from asset: PHAsset) async -> UIImage? {
Expand Down Expand Up @@ -833,6 +866,14 @@ extension ComposeView: PHPickerViewControllerDelegate {
}
}

// Save the photos off the main thread before touching the attachments.
let pickedImages = loadedItems.compactMap { entry -> (index: Int, image: UIImage)? in
guard case let .image(image) = entry.item else { return nil }
return (entry.index, image)
}
let savedImages = await Self.imageAttachments(pickedImages.map(\.image), optimizePhoto: true)
let savedImageByIndex = Dictionary(uniqueKeysWithValues: zip(pickedImages.map(\.index), savedImages))

await MainActor.run { [weak self, weak picker] in
guard let self else { return }

Expand All @@ -856,15 +897,12 @@ extension ComposeView: PHPickerViewControllerDelegate {
for entry in sortedEntries {
let item = entry.item
switch item {
case let .image(image):
do {
let mediaItem = try makeImageAttachment(image, optimizePhoto: true)
let uniqueId = mediaItem.getItemUniqueId()
attachmentItems[uniqueId] = mediaItem
} catch {
Log.shared.error("Failed to save photo in attachments", error: error)
case .image:
guard let mediaItem = savedImageByIndex[entry.index] ?? nil else {
cancelQueuedPendingVideoSend()
continue
}
attachmentItems[mediaItem.getItemUniqueId()] = mediaItem
case let .video(videoItem):
if let pendingId = pendingVideoIdsByIndex[entry.index], isPendingVideoAttachmentCanceled(pendingId) {
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,15 @@ public extension TransactionSendMessage {
) -> [TransactionSendMessage] {
var sends: [TransactionSendMessage] = []
sends.reserveCapacity(items.count)
// Message rows keep dates to the millisecond, and sends made in one go share one. Spacing them
// keeps their order after a relaunch, when interrupted sends resume oldest first.
let firstDate = Date()
for run in albumSendRuns(items.map(\.media)) {
let groupedId: Int64? = run.count > 1 ? Int64.random(in: 1 ... Int64.max) : nil
var ids: [String] = []
for index in run {
let item = items[index]
let send = TransactionSendMessage(
var send = TransactionSendMessage(
text: item.text,
peerId: peerId,
chatId: chatId,
Expand All @@ -148,6 +151,7 @@ public extension TransactionSendMessage {
sendMode: sendMode,
groupedId: groupedId
)
send.date = firstDate.addingTimeInterval(Double(sends.count) / 1_000)
ids.append(send.id)
sends.append(send)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public enum PendingSendRecovery {
/// Pure selection so the policy is testable without a running queue.
static func plan(for messages: [Message], now: Date, window: TimeInterval = automaticResumeWindow) -> Plan {
var plan = Plan()
for message in messages.sorted(by: { $0.date < $1.date }) {
// Insertion order breaks ties: rows store dates to the millisecond, and an album's items, sent
// together before their dates were spaced, can share one.
for message in messages.sorted(by: { ($0.date, $0.globalId ?? 0) < ($1.date, $1.globalId ?? 0) }) {
let isRecoverable = message.randomId != nil
&& !(message.transactionId ?? "").isEmpty
&& now.timeIntervalSince(message.date) <= window
Expand Down
3 changes: 3 additions & 0 deletions apple/InlineKit/Tests/InlineKitTests/MessageAlbumsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ struct MessageAlbumsTests {
let albumId = sends[0].groupedId
#expect(albumId != nil)
#expect(sends.allSatisfy { $0.groupedId == albumId })
// Each send is a millisecond after the one before, so stored rows keep their order.
let milliseconds = sends.map { Int64(($0.date.timeIntervalSince1970 * 1_000).rounded(.down)) }
#expect(zip(milliseconds, milliseconds.dropFirst()).allSatisfy { $0 < $1 })
#expect(AlbumSendBarrier.shared.isRegistered(transactionId: sends[0].id))
for send in sends {
AlbumSendBarrier.shared.settle(transactionId: send.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ struct PendingSendRecoveryTests {
#expect(plan.fail == [-2, -4, -5])
}

@Test("sends that share a stored date resume in the order their rows were written")
@MainActor
func tiesResumeInInsertionOrder() {
let now = Date()
let rows = [(Int64(-30), Int64(12)), (-10, 10), (-20, 11)].map { messageId, globalId in
var message = outgoing(id: messageId, age: 5, now: now)
message.globalId = globalId
return message
}
#expect(PendingSendRecovery.plan(for: rows, now: now).resume == [-10, -20, -30])
}

@Test("recovered send reuses the interrupted row's identity")
@MainActor
func recoveredTransactionKeepsIdentity() throws {
Expand Down
8 changes: 5 additions & 3 deletions server/src/modules/transfers/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
countTransferWork,
recordSqlStatement,
recordStorageOperation,
recordStoragePutTime,
} from "./trace"

describe("transfer traces", () => {
Expand All @@ -14,10 +15,11 @@ describe("transfer traces", () => {
await Promise.resolve()
recordSqlStatement()
recordStorageOperation("put")
recordStoragePutTime(12)
})
expect(inner.counters).toEqual({ sqlStatements: 1, storagePut: 1, storageGet: 0, storageDelete: 0 })
expect(inner.counters).toEqual({ sqlStatements: 1, storagePut: 1, storageGet: 0, storageDelete: 0, storagePutMs: 12 })
})
expect(outer.counters).toEqual({ sqlStatements: 2, storagePut: 1, storageGet: 0, storageDelete: 0 })
expect(outer.counters).toEqual({ sqlStatements: 2, storagePut: 1, storageGet: 0, storageDelete: 0, storagePutMs: 12 })
// Outside any scope nothing is recorded and nothing throws.
recordSqlStatement()
})
Expand All @@ -43,7 +45,7 @@ describe("transfer traces", () => {
traces.finish("upload:7", "complete")
expect(lines).toEqual([{
direction: "upload", carrier: "v3", bytes: 150, parts: 2, durationMs: 42, peakInFlightBytes: 150,
sqlStatements: 1, storageOps: { put: 0, get: 1, delete: 0 }, retries: 0, outcome: "complete",
sqlStatements: 1, storageOps: { put: 0, get: 1, delete: 0 }, putMs: 0, retries: 0, outcome: "complete",
}])
expect(JSON.stringify(lines)).not.toContain("upload:7")
expect(traces.openCount).toBe(0)
Expand Down
Loading
Loading