Repository navigation
feat(chat): send and load photo messages - #352
Conversation
63d8fa3 to
a700302
Compare
peng-0110
left a comment
There was a problem hiding this comment.
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.
- 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.
- 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.
- 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 { |
There was a problem hiding this comment.
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.
| ) { | ||
| val senderUid = participantUid(conversation) | ||
| // Storage only accepts a chat photo once the conversation exists. | ||
| require(conversation.lastMessageAt != null) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Thanks! Did the other 3 too, each with tests:
Ready for another look. |
peng-0110
left a comment
There was a problem hiding this comment.
Thank you for making the fixes. A few more problems to address and then it is good to go
- 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.
- 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.
- 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.
- 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.
- 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.
|
Thanks, good catches. I went with your pending idea: a photo message is now written with |
peng-0110
left a comment
There was a problem hiding this comment.
Thank you for addressing the issues, found a couple more problems that need to be addressed before this can be merged.
- 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.
- 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.
- 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.
- 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.
|
Thanks, you're right about Source.SERVER, I missed that it merges the local queued writes. The retry now checks |
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>
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>
144e8a8 to
f9b1749
Compare
|
peng-0110
left a comment
There was a problem hiding this comment.
Thanks for the works. LGTM



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⚠️ The live Firestore rules need to be republished when this merges, or the new photo messages get refused.
photoPending: true, and the sender sets it tofalseonce 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.jsandChatMessage.kt.What
ChatRepositorygets three methods:newMessageId(conversationId): a fresh message id, made locally, so it works offline.sendPhoto(conversation, messageId, photo, caption): writes the message (withphotoPending: true) and the conversation update in one batch and waits for the server to confirm, uploads the photo toconversations/{conversationId}/{messageId}.jpg, then setsphotoPendingtofalse. Every failure is thrown, so there's noonWriteFailurecallback.loadMessagePhoto(imagePath): downloads the photo as the signed-in user, likeloadListingImage.ChatMessagegetsphotoPending. It'struefrom 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 asfalse.photoPending: true(a text never has it). Its sender can then update it once, and the only allowed change isphotoPendinggoing fromtruetofalse. Messages still can't be deleted. Storage rules are unchanged.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.sendPhotofirst reads the message from the server, so if it's offline when the send starts, it fails straight away withAppError.NETWORKand nothing is queued. The whole send waits at most 30 s, then it's cancelled and fails withNETWORK, 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.AppError.NOT_FOUNDwith nothing written.NETWORK, instead of showing it as loading for about 10 minutes offline.sendMessageandsendPhotonow share one privatemessageBatchthat builds the batch.sendMessagestill queues it without waiting, so it behaves the same and its tests are unchanged.FirebaseChatRepositorynow takes aPhotoCompressorandFirebaseStorage(wired inMainActivity).FakeChatRepositoryimplements the new methods (sentPhotos,photos,photoLoadFailure, idsm1,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 failureNot in this PR: nothing reads
photoPendingyet besides the model.loadMessagePhotostays 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 jacocoTestReportpasses../gradlew compileDebugAndroidTestKotlinpasses (the fake is shared withandroidTest).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, readingphotoPending, slow and unreadable photos, the timeouts (virtual time, no real waits) and loading.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.FirebaseChatRepository.ktis covered (228/228 lines and 91/98 branches for the file; the missed branches were there before).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