Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: attach files to awala messages #82

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:largeHeap="true"
android:hardwareAccelerated="false"
android:theme="@style/Theme.Letro">
<activity
android:name=".ui.MainActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tech.relaycorp.letro.messages.attachments

import kotlinx.coroutines.flow.Flow
import tech.relaycorp.letro.messages.filepicker.FileConverter
import tech.relaycorp.letro.messages.filepicker.FileSaver
import tech.relaycorp.letro.messages.filepicker.model.File
import tech.relaycorp.letro.messages.model.AttachmentAwalaWrapper
import tech.relaycorp.letro.messages.storage.AttachmentsDao
import tech.relaycorp.letro.messages.storage.entity.Attachment
import java.util.UUID
Expand All @@ -11,17 +13,29 @@ import javax.inject.Inject
interface AttachmentsRepository {
val attachments: Flow<List<Attachment>>
suspend fun saveAttachments(messageId: Long, attachments: List<File.FileWithContent>)
suspend fun saveMessageAttachments(messageId: Long, attachments: List<AttachmentAwalaWrapper>)
suspend fun getById(id: UUID): Attachment?
}

class AttachmentsRepositoryImpl @Inject constructor(
private val attachmentsDao: AttachmentsDao,
private val fileSaver: FileSaver,
private val fileConverter: FileConverter,
) : AttachmentsRepository {

override val attachments: Flow<List<Attachment>>
get() = attachmentsDao.getAll()

override suspend fun saveMessageAttachments(
messageId: Long,
attachments: List<AttachmentAwalaWrapper>,
) {
saveAttachments(
messageId = messageId,
attachments = attachments.mapNotNull { fileConverter.getFile(it) },
)
}

override suspend fun saveAttachments(messageId: Long, attachments: List<File.FileWithContent>) {
attachmentsDao.insert(
attachments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import android.webkit.MimeTypeMap
import androidx.core.net.toUri
import tech.relaycorp.letro.messages.filepicker.model.File
import tech.relaycorp.letro.messages.filepicker.model.FileExtension
import tech.relaycorp.letro.messages.model.AttachmentAwalaWrapper
import tech.relaycorp.letro.messages.storage.entity.Attachment
import java.util.UUID
import javax.inject.Inject

interface FileConverter {
suspend fun getFile(uri: Uri): File.FileWithContent?
suspend fun getFile(attachment: Attachment): File.FileWithoutContent?
suspend fun getFile(attachmentAwalaWrapper: AttachmentAwalaWrapper): File.FileWithContent?
}

class FileConverterImpl @Inject constructor(
Expand Down Expand Up @@ -50,6 +52,16 @@ class FileConverterImpl @Inject constructor(
)
}

override suspend fun getFile(attachmentAwalaWrapper: AttachmentAwalaWrapper): File.FileWithContent? {
return File.FileWithContent(
id = UUID.randomUUID(),
name = attachmentAwalaWrapper.fileName,
extension = FileExtension.fromMimeType(attachmentAwalaWrapper.mimeType),
size = attachmentAwalaWrapper.content.size.toLong(),
content = attachmentAwalaWrapper.content,
)
}

private suspend fun getFileName(uri: Uri): String? {
if (uri.scheme.equals("content")) {
contentResolver.query(uri, null, null, null, null)?.use {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,39 @@ data class ConversationAwalaWrapper(
val recipientVeraId: String,
val subject: String?,
val messageText: String,
val attachments: List<AttachmentAwalaWrapper>,
)

data class MessageAwalaWrapper(
val conversationId: String,
val messageText: String,
val senderVeraId: String,
val recipientVeraId: String,
val attachments: List<AttachmentAwalaWrapper>,
)

data class AttachmentAwalaWrapper(
val fileName: String,
val content: ByteArray,
val mimeType: String,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as AttachmentAwalaWrapper

if (fileName != other.fileName) return false
if (!content.contentEquals(other.content)) return false
if (mimeType != other.mimeType) return false

return true
}

override fun hashCode(): Int {
var result = fileName.hashCode()
result = 31 * result + content.contentHashCode()
result = 31 * result + mimeType.hashCode()
return result
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,69 @@
package tech.relaycorp.letro.messages.parser

import com.google.gson.Gson
import tech.relaycorp.letro.messages.filepicker.model.File
import tech.relaycorp.letro.messages.model.AttachmentAwalaWrapper
import tech.relaycorp.letro.messages.model.ConversationAwalaWrapper
import tech.relaycorp.letro.messages.model.MessageAwalaWrapper
import tech.relaycorp.letro.messages.storage.entity.Conversation
import tech.relaycorp.letro.messages.storage.entity.Message
import javax.inject.Inject

interface OutgoingMessageMessageEncoder {
fun encodeNewConversationContent(conversation: Conversation, messageText: String): ByteArray
fun encodeNewMessageContent(message: Message): ByteArray
fun encodeNewConversationContent(
conversation: Conversation,
messageText: String,
attachments: List<File.FileWithContent>,
): ByteArray
fun encodeNewMessageContent(
message: Message,
attachments: List<File.FileWithContent>,
): ByteArray
}

class OutgoingMessageMessageEncoderImpl @Inject constructor() : OutgoingMessageMessageEncoder {

override fun encodeNewConversationContent(conversation: Conversation, messageText: String): ByteArray {
override fun encodeNewConversationContent(
conversation: Conversation,
messageText: String,
attachments: List<File.FileWithContent>,
): ByteArray {
val json = Gson().toJson(
ConversationAwalaWrapper(
conversationId = conversation.conversationId.toString(),
messageText = messageText,
senderVeraId = conversation.ownerVeraId,
recipientVeraId = conversation.contactVeraId,
subject = conversation.subject,
attachments = attachments.map {
AttachmentAwalaWrapper(
fileName = it.name,
content = it.content,
mimeType = it.extension.mimeType,
)
},
),
)
return json.toByteArray()
}

override fun encodeNewMessageContent(message: Message): ByteArray {
override fun encodeNewMessageContent(
message: Message,
attachments: List<File.FileWithContent>,
): ByteArray {
val json = Gson().toJson(
MessageAwalaWrapper(
conversationId = message.conversationId.toString(),
messageText = message.text,
senderVeraId = message.senderVeraId,
recipientVeraId = message.recipientVeraId,
attachments = attachments.map {
AttachmentAwalaWrapper(
fileName = it.name,
content = it.content,
mimeType = it.extension.mimeType,
)
},
),
)
return json.toByteArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tech.relaycorp.letro.messages.processor
import tech.relaycorp.awaladroid.messaging.IncomingMessage
import tech.relaycorp.letro.awala.AwalaManager
import tech.relaycorp.letro.awala.processor.AwalaMessageProcessor
import tech.relaycorp.letro.messages.attachments.AttachmentsRepository
import tech.relaycorp.letro.messages.dto.NewConversationIncomingMessage
import tech.relaycorp.letro.messages.model.ConversationAwalaWrapper
import tech.relaycorp.letro.messages.parser.NewConversationMessageParser
Expand All @@ -24,6 +25,7 @@ class NewConversationProcessorImpl @Inject constructor(
private val newConversationMessageParser: NewConversationMessageParser,
private val conversationsDao: ConversationsDao,
private val messagesDao: MessagesDao,
private val attachmentsRepository: AttachmentsRepository,
) : NewConversationProcessor {

override suspend fun process(message: IncomingMessage, awalaManager: AwalaManager) {
Expand All @@ -50,6 +52,9 @@ class NewConversationProcessorImpl @Inject constructor(
)
conversationsDao.createNewConversation(conversation)
val messageId = messagesDao.insert(message)

attachmentsRepository.saveMessageAttachments(messageId, conversationWrapper.attachments)

pushManager.showPush(
PushData(
title = conversationWrapper.senderVeraId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tech.relaycorp.letro.messages.processor
import tech.relaycorp.awaladroid.messaging.IncomingMessage
import tech.relaycorp.letro.awala.AwalaManager
import tech.relaycorp.letro.awala.processor.AwalaMessageProcessor
import tech.relaycorp.letro.messages.attachments.AttachmentsRepository
import tech.relaycorp.letro.messages.dto.NewMessageIncomingMessage
import tech.relaycorp.letro.messages.parser.NewMessageMessageParser
import tech.relaycorp.letro.messages.storage.ConversationsDao
Expand All @@ -23,6 +24,7 @@ class NewMessageProcessorImpl @Inject constructor(
private val parser: NewMessageMessageParser,
private val conversationsDao: ConversationsDao,
private val messagesDao: MessagesDao,
private val attachmentsRepository: AttachmentsRepository,
) : NewMessageProcessor {

@Suppress("NAME_SHADOWING")
Expand Down Expand Up @@ -55,6 +57,7 @@ class NewMessageProcessorImpl @Inject constructor(
sentAt = LocalDateTime.now(),
)
val messageId = messagesDao.insert(message)
attachmentsRepository.saveMessageAttachments(messageId, messageWrapper.attachments)
pushManager.showPush(
PushData(
title = message.senderVeraId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class ConversationsRepositoryImpl @Inject constructor(
content = outgoingMessageMessageEncoder.encodeNewConversationContent(
conversation = conversation,
messageText = message.text,
attachments = attachments,
),
),
recipient = MessageRecipient.User(
Expand Down Expand Up @@ -182,7 +183,10 @@ class ConversationsRepositoryImpl @Inject constructor(
awalaManager.sendMessage(
outgoingMessage = AwalaOutgoingMessage(
type = MessageType.NewMessage,
content = outgoingMessageMessageEncoder.encodeNewMessageContent(message),
content = outgoingMessageMessageEncoder.encodeNewMessageContent(
message = message,
attachments = attachments,
),
),
recipient = MessageRecipient.User(
nodeId = recipientNodeId,
Expand Down