Skip to content

feat(chat): send and load photo messages - #352

Merged
Mamel0uk merged 27 commits into
mainfrom
feature/photo-messages
Nov 18, 2026
Merged

Mamel0uk merged 27 commits into
mainfrom
feature/photo-messages

Conversation

@Mamel0uk

@Mamel0uk Mamel0uk commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #280. Part of #36. Builds on the photo rules from #350 (merged).

The chat repository can now send a photo message (with an optional caption) and load a message's photo. No UI yet, that's #281.

Update (Nov 18): the first version uploaded the photo before writing its message, but the #350 Storage rules only accept the upload once the message exists, so every upload got a 403 on the real project. I found it while testing #281 on the emulator. Fixed in 3d77b93: the message is written first, then the photo is uploaded.

Update 2 (Nov 18, second review): a photo message now has a pending stage, as peng-0110 suggested. It's written with photoPending: true, and the sender sets it to false once the upload is done. So the other person can tell "still uploading" apart from a broken photo, and a retry after a timeout no longer gets refused. This changes 3 files that were already on main: firestore.rules, rules-tests/firestore.test.js and ChatMessage.kt. ⚠️ The live Firestore rules need to be republished when this merges, or the new photo messages get refused.

What

  • ChatRepository gets three methods:
    • newMessageId(conversationId): a fresh message id, made locally, so it works offline.
    • sendPhoto(conversation, messageId, photo, caption): writes the message (with photoPending: true) and the conversation update in one batch and waits for the server to confirm, uploads the photo to conversations/{conversationId}/{messageId}.jpg, then sets photoPending to false. Every failure is thrown, so there's no onWriteFailure callback.
    • loadMessagePhoto(imagePath): downloads the photo as the signed-in user, like loadListingImage.
  • Pending photo: ChatMessage gets photoPending. It's true from the moment the message is written until the photo is uploaded, so the screen can show "uploading" instead of trying to download a photo that isn't there yet. If the send is never retried (the app was closed, for example), it stays pending. Messages from before this field existed read as false.
  • Rules: a photo message must be created with photoPending: true (a text never has it). Its sender can then update it once, and the only allowed change is photoPending going from true to false. Messages still can't be deleted. Storage rules are unchanged.
  • Retry: the caller keeps the message id and passes it again. If the message is already on the server, it isn't written again, since the rules refuse rewriting a message. If the first try timed out while a write was still queued on the phone, the server read already includes it, so the retry checks metadata.hasPendingWrites() on that same read, waits for the queue (waitForPendingWrites) and reads again, instead of writing a second copy or uploading too early. Then the photo is uploaded only if it's not in Storage yet, and marked uploaded only if it's still pending. If the mark is refused because an earlier one already reached the server, it counts as done when the server shows the photo as uploaded.
  • Offline: sendPhoto first reads the message from the server, so if it's offline when the send starts, it fails straight away with AppError.NETWORK and nothing is queued. The whole send waits at most 30 s, then it's cancelled and fails with NETWORK, instead of the Storage SDK retrying for about 10 minutes. If the timeout fires while the message is being written, Firestore may still send it later: it then shows up as pending until the retry finishes it.
  • Compression happens first, before anything is sent and outside the 30 s timeout: a slow phone isn't reported as offline, and a photo that can't be read (deleted, not an image, permission lost) fails with AppError.NOT_FOUND with nothing written.
  • Loading a photo also gives up after 30 s with NETWORK, instead of showing it as loading for about 10 minutes offline.
  • A photo can't be the first message of a conversation, because the rules refuse a photo message until the conversation exists.
  • sendMessage and sendPhoto now share one private messageBatch that builds the batch. sendMessage still queues it without waiting, so it behaves the same and its tests are unchanged.
  • FirebaseChatRepository now takes a PhotoCompressor and FirebaseStorage (wired in MainActivity).
  • FakeChatRepository implements the new methods (sentPhotos, photos, photoLoadFailure, ids m1, m2, …), so the chat tests keep compiling.
sequenceDiagram
    participant VM as Caller
    participant R as ChatRepository
    participant F as Firestore
    participant S as Storage
    VM->>R: newMessageId()
    VM->>R: sendPhoto(messageId, photo, caption)
    R->>R: compress to JPEG (before anything is sent, outside the timeout)
    rect rgb(240,240,240)
    note over R,S: at most 30 s, else cancel + NETWORK
    R->>F: get message (server only, fails at once offline)
    opt not on the server but still queued on the phone (retry after a timeout)
        R->>F: waitForPendingWrites, then get message again
    end
    alt not on the server
        R->>F: batch: message (photoPending true) + conversation preview (awaited)
    else already there (retry)
        note over R,F: skip the write
    end
    R->>S: getMetadata(path)
    alt not found
        R->>S: putBytes (awaited)
    else already there (retry)
        note over R,S: skip the upload
    end
    opt message still pending
        R->>F: update photoPending = false (awaited)
    end
    end
    R-->>VM: returns, or throws the failure
Loading

Not in this PR: nothing reads photoPending yet besides the model. loadMessagePhoto stays a plain download, and showing photos (with the pending placeholder) is in #374, stacked on this PR, so until then a photo still uploading would read as NOT_FOUND. The fake ignores the flag too, since no screen here uses it.

Why

#281 needs this to send and show photos from the conversation screen. Keeping it in the repository leaves the ViewModel with no Firebase code, and lets it retry a failed send with the same id.

Validation

  • ./gradlew ktfmtCheck assemble lint check jacocoTestReport passes.
  • ./gradlew compileDebugAndroidTestKotlin passes (the fake is shared with androidTest).
  • 89 tests in FirebaseChatRepositoryTest (1411 unit tests in total), all green. They cover sending with and without a caption, the message written and confirmed before the upload, a rejected message, offline, a failed upload, the retries (message on the server, message or mark still queued, photo already stored, photo already marked), marking the photo uploaded and a mark sent twice, reading photoPending, slow and unreadable photos, the timeouts (virtual time, no real waits) and loading.
  • Rules tests (cd rules-tests && npm test): 204 pass, including the new ones: a photo message must start pending, a text can't be pending, only the sender can mark it uploaded, only once, and nothing else can change.
  • Coverage: every new line of FirebaseChatRepository.kt is covered (228/228 lines and 91/98 branches for the file; the missed branches were there before).
  • Tested by hand on the emulator against the real project, with Send photos from the conversation screen #281 on top: photos with and without a caption are sent, and the files are in Storage under conversations/{conversationId}/.

AI use

Claude Code (Claude Opus 5.5) wrote the implementation and the tests, step by step with my approval of each commit, then reviewed the branch and drafted this description. I found the upload bug while testing the #281 UI on the emulator, then tracked down where it came from with Claude Code (from the emulator logs and the rules) and wrote the fix with it. I tested the fix on the emulator before committing. I checked the batch against the #279 rules and the retry/timeout behaviour myself. For the second review, Claude Code implemented the pending stage I chose from the reviewer's suggestion (rules, rules tests, model, repository, tests, doc), one approved commit at a time.

Generated with Claude Code

@Mamel0uk Mamel0uk self-assigned this Nov 16, 2026
Base automatically changed from feature/photo-message-rules to main November 17, 2026 11:39
@Mamel0uk
Mamel0uk force-pushed the feature/photo-messages branch from 63d8fa3 to a700302 Compare November 17, 2026 11:46
@Mamel0uk
Mamel0uk marked this pull request as ready for review November 17, 2026 11:57
@peng-0110
peng-0110 self-requested a review November 17, 2026 17:35

@peng-0110 peng-0110 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work on a very important but tricky part. I can commented 2 fixes that i think should be included and below are some other fixes that can be included in this PR.

  1. Slow compression gets reported as "offline" (:98). Compressing the photo counts against the 30s upload timeout. On a slow phone with a large photo, the timeout fires and the user is told they're offline, and every retry fails the same way.
  2. Compression errors aren't handled (:294). If the user deleted the photo, the app lost access to it, or the file isn't an image, the error isn't caught. It also isn't one of the errors sendPhoto's doc comment lists.
  3. Loading photos has no timeout (:119). Offline, loadMessagePhoto waits through the Storage SDK's retries, about 10 minutes, so the photo shows as loading the whole time. sendPhoto already works around this with its timeout.

val photoRef = storage.reference.child(path)
// Awaited: the message must not point to a photo that was never uploaded. Offline, the Storage
// SDK keeps retrying for minutes, so the user is told sooner that the photo was not sent.
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every photo send will be refused. sendPhoto uploads the photo and only then writes the message. But storage.rules (from #350) only allow the upload once the message already exists, sent by the same user and pointing at that photo path. So putBytes fails with ERROR_NOT_AUTHORIZED and the message is never written. The rules comment and storage.test.js both expect the message to be written first.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, #350's rules changed before it merged and I'd built this on the older version. Fixed in 3d77b93: the message is written and confirmed on the server first, then the photo is uploaded. A retry with the same id only uploads.

) {
val senderUid = participantUid(conversation)
// Storage only accepts a chat photo once the conversation exists.
require(conversation.lastMessageAt != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment that explains the order is wrong. It says Storage needs the conversation to exist, when it actually needs the message. That mistake is what led to uploading first, and to the retry and exists() logic built around it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, fixed in 0564fc9. The 'no photo as first message' rule now comes from firestore.rules (a photo message needs the conversation to exist). I also fixed the photoPath and exists() comments. I kept exists(): if an upload reaches the server after the send timed out, the rules won't let a retry overwrite it.

@Mamel0uk

Copy link
Copy Markdown
Contributor Author

Thanks! Did the other 3 too, each with tests:

  1. the photo is compressed first, outside the 30 s timeout, so slow compression isn't reported as offline (0ed75d2).
  2. a deleted, unreadable or non-image photo now fails with NOT_FOUND before anything is sent, and the KDoc lists it (2c4d856).
  3. loadMessagePhoto gives up after 30 s with NETWORK (cc386d6).

Ready for another look.

@peng-0110 peng-0110 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making the fixes. A few more problems to address and then it is good to go

  1. Messages whose photo never arrives (FirebaseChatRepository.kt:139). If the upload fails, times out, is cancelled, or the app is killed, the message stays with a photo that doesn't exist. firestore.rules don't allow updating or deleting a message, and the messageId needed to retry is only kept in memory. So the other person sees a broken photo for good, and the inbox preview text is empty.
  2. A timeout doesn't stop the write (:139). If the 30s timeout fires while the message is being saved, Firestore keeps the write queued and sends it once the connection is back. The user is told the send failed (NETWORK), but the message still shows up without its photo. That contradicts the ChatRepository doc, which says nothing is written offline.
  3. A retry after that timeout is refused (:124). The retry asks the server whether the message exists, and the server can't see the first attempt's queued write yet. So the retry queues a second write of the same message. Once the first write lands, the second one counts as an update, and allow update: if false rejects it. The retry fails and the photo still isn't uploaded.
  4. No state for "message saved, photo still uploading" (:124). The other person gets the message before the upload finishes, and loading its photo returns NOT_FOUND. The app can't tell that apart from a photo that will never arrive, so it has no reason to try again.
  5. The sendPhoto doc is out of date (ChatRepository.kt:35). It still says the photo is compressed after the message is written. It also says nothing is written offline, which problem 2 shows isn't always true. A ViewModel author who trusts it may discard the messageId and lose the chance to retry.

Maybe we can consider a pending stage? Save the message with photoPending: true, and let the sender update it once to mark the photo uploaded. The other person's app shows a placeholder until then. A retry becomes "upload, then mark uploaded", which fixes problem 3. A message stuck as pending at least looks like an unfinished upload rather than a broken one. If you also want those stuck messages removed, the rules could let the sender delete their own message while it's still pending.

@Mamel0uk

Copy link
Copy Markdown
Contributor Author

Thanks, good catches. I went with your pending idea: a photo message is now written with photoPending: true, and once the upload is done the sender flips it to false (the rules only allow that one change, once, by the sender). A retry now checks if the message write is still queued on the phone and waits for it instead of writing it again, so the "update refused" case is gone, then uploads if needed and marks it. The receiver can tell "still uploading" from a real error with ChatMessage.photoPending. I skipped the delete part to keep the rules simple: a stuck message just shows as pending. Keeping the messageId across an app restart is the outbox issue (#372). Doc updated too.

@Mamel0uk
Mamel0uk requested a review from peng-0110 November 18, 2026 12:42

@peng-0110 peng-0110 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for addressing the issues, found a couple more problems that need to be addressed before this can be merged.

  1. A retry doesn't wait for a queued message (FirebaseChatRepository.kt:125). The retry asks the server whether the message exists, but a get(Source.SERVER) read also includes this phone's own queued writes. The Firestore docs say so for Source.SERVER. So a message that is still queued already looks like it exists, and the wait is skipped. The upload then starts before the server has the message, and storage.rules refuse it. The tests mock this read, so they can't catch it. A fix is to check onServer.metadata.hasPendingWrites() on the snapshot it already has.
  2. The queued message can land mid-check (:129). If the queued message reaches the server between the server read and the cache check, the retry writes the message again. The rules refuse that as an update, and the user sees an error for a message that was actually sent.
  3. Marking the photo uploaded can be sent twice (:155). If that update times out and the user retries, it runs a second time. The rules allow it only once, so the retry fails even though the other person already has the photo. Treat a refusal as success when the server already shows the photo as uploaded.
  4. Nothing reads the pending flag yet (ChatRepository.kt:72). photoPending is in the model, but loadMessagePhoto, the UI and FakeChatRepository all ignore it. Recipients will still get "photo not found" for a photo that's still uploading. That's acceptable if showing photos comes in a later PR, but the PR description should say so.

@Mamel0uk

Copy link
Copy Markdown
Contributor Author

Thanks, you're right about Source.SERVER, I missed that it merges the local queued writes. The retry now checks metadata.hasPendingWrites() on that same server read and waits for the queue before reading again, so there's only one check and a write landing in between can't cause a double write anymore (1 and 2). For 3, if the mark gets refused I read the message again and treat it as done when it's already photoPending: false. For 4, I added a note in the description: reading the flag and showing photos is in #374, which is stacked on this one. Tests updated so the mocked server read now includes the pending write like the real SDK.

@Mamel0uk
Mamel0uk requested a review from peng-0110 November 18, 2026 13:51
Mamel0uk and others added 13 commits November 18, 2026 16:12
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
storage.rules only accept a chat photo once the server has the message
that names it, so sendPhoto now reads the message from the server,
writes and confirms it if missing, then uploads. Offline the server read
fails at once with NETWORK, so nothing is queued. A retry with the same
message ID only uploads. sendPhoto throws every failure, so its
onWriteFailure callback is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A photo that can't be read now sends nothing, and a slow compression
no longer counts against the 30 s network timeout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A deleted photo, one that isn't an image, or one the app can no longer
read now fails sendPhoto with NOT_FOUND instead of an unmapped error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mamel0uk and others added 14 commits November 18, 2026 16:12
The comments still described the rules from before #350 changed: a
photo can't open a conversation because of firestore.rules, and Storage
only accepts the upload from the sender of the message that names it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
loadMessagePhoto now fails with NETWORK after the same timeout as
sendPhoto, instead of waiting through the Storage SDK's retries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mamel0uk
Mamel0uk force-pushed the feature/photo-messages branch from 144e8a8 to f9b1749 Compare November 18, 2026 15:16
@sonarqubecloud

Copy link
Copy Markdown

@peng-0110 peng-0110 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the works. LGTM

@Mamel0uk
Mamel0uk merged commit 48420d7 into main Nov 18, 2026
4 checks passed
@Mamel0uk
Mamel0uk deleted the feature/photo-messages branch November 18, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Send and load photo messages

2 participants