feat(voice): data layer — voice message model, sendVoiceMessage, durationMs field

- QuestionMessage: add durationMs, isVoice, type supports 'voice'
- ConversationRepository: sendVoiceMessage(audioBytes, durationMs)
- FirestoreConversationDataSource: sendVoiceMessage encrypts + uploads audio, decrypts durationMs
- ConversationRepositoryImpl: delegates sendVoiceMessage
This commit is contained in:
null 2026-06-24 16:34:38 -05:00
parent 29beff1702
commit c20745e82a
4 changed files with 27 additions and 3 deletions

View File

@ -122,6 +122,22 @@ class FirestoreConversationDataSource @Inject constructor(
updateLastMessage(coupleId, conversationId, userId, fieldEncryptor.encrypt("📷 Photo", aead, coupleId))
}
suspend fun sendVoiceMessage(coupleId: String, conversationId: String, userId: String, audioBytes: ByteArray, durationMs: Long) {
val aead = encryptionManager.requireAead(coupleId)
val encrypted = aead.encrypt(audioBytes, coupleId.toByteArray(Charsets.UTF_8))
val url = storageDataSource.uploadEncryptedMedia(userId, encrypted)
messagesRef(coupleId, conversationId).add(
mapOf(
"authorUserId" to userId,
"type" to "voice",
"mediaUrl" to url,
"durationMs" to durationMs,
"createdAt" to FieldValue.serverTimestamp()
)
).refAwait()
updateLastMessage(coupleId, conversationId, userId, fieldEncryptor.encrypt("🎤 Voice message", aead, coupleId))
}
private suspend fun updateLastMessage(coupleId: String, conversationId: String, userId: String, encryptedPreview: String) {
conversationsRef(coupleId).document(conversationId).set(
mapOf(
@ -185,7 +201,8 @@ class FirestoreConversationDataSource @Inject constructor(
userId = userId,
type = type,
mediaUrl = getString("mediaUrl") ?: "",
text = if (type == "image") "" else (fieldEncryptor.decryptForDisplay(getString("text"), aead, coupleId) ?: ""),
durationMs = getLong("durationMs") ?: 0L,
text = if (type == "text") (fieldEncryptor.decryptForDisplay(getString("text"), aead, coupleId) ?: "") else "",
createdAt = getTimestamp("createdAt")?.toDate()?.time ?: 0L
)
}

View File

@ -34,6 +34,9 @@ class ConversationRepositoryImpl @Inject constructor(
override suspend fun sendImageMessage(coupleId: String, conversationId: String, userId: String, imageBytes: ByteArray) =
dataSource.sendImageMessage(coupleId, conversationId, userId, imageBytes)
override suspend fun sendVoiceMessage(coupleId: String, conversationId: String, userId: String, audioBytes: ByteArray, durationMs: Long) =
dataSource.sendVoiceMessage(coupleId, conversationId, userId, audioBytes, durationMs)
override suspend fun loadDecryptedMedia(coupleId: String, mediaUrl: String): ByteArray? =
dataSource.loadDecryptedMedia(coupleId, mediaUrl)
}

View File

@ -4,11 +4,14 @@ data class QuestionMessage(
val id: String = "",
val userId: String = "",
val text: String = "",
/** "text" or "image". */
/** "text", "image" (also GIFs/stickers/Bitmoji), or "voice". */
val type: String = "text",
/** Download URL of the ENCRYPTED image bytes in Storage (empty for text messages). */
/** Download URL of the ENCRYPTED media bytes in Storage (empty for text messages). */
val mediaUrl: String = "",
/** Voice-note length in milliseconds (0 for non-voice). */
val durationMs: Long = 0L,
val createdAt: Long = 0L
) {
val isImage: Boolean get() = type == "image" && mediaUrl.isNotBlank()
val isVoice: Boolean get() = type == "voice" && mediaUrl.isNotBlank()
}

View File

@ -12,5 +12,6 @@ interface ConversationRepository {
fun observeMessages(coupleId: String, conversationId: String): Flow<List<QuestionMessage>>
suspend fun sendMessage(coupleId: String, conversationId: String, userId: String, text: String)
suspend fun sendImageMessage(coupleId: String, conversationId: String, userId: String, imageBytes: ByteArray)
suspend fun sendVoiceMessage(coupleId: String, conversationId: String, userId: String, audioBytes: ByteArray, durationMs: Long)
suspend fun loadDecryptedMedia(coupleId: String, mediaUrl: String): ByteArray?
}