Commit afb2033e authored by Lucio Maciel's avatar Lucio Maciel

Implement Attachments mapping.

parent ac7a6e1e
This diff is collapsed.
...@@ -44,7 +44,7 @@ class MessageService : JobService() { ...@@ -44,7 +44,7 @@ class MessageService : JobService() {
private suspend fun retrySendingMessages(params: JobParameters?, serverUrl: String) { private suspend fun retrySendingMessages(params: JobParameters?, serverUrl: String) {
val dbManager = dbFactory.create(serverUrl) val dbManager = dbFactory.create(serverUrl)
val messageRepository = DatabaseMessagesRepository(dbManager, DatabaseMessageMapper()) val messageRepository = DatabaseMessagesRepository(dbManager, DatabaseMessageMapper(dbManager))
val temporaryMessages = messageRepository.getAllUnsent() val temporaryMessages = messageRepository.getAllUnsent()
.sortedWith(compareBy(Message::timestamp)) .sortedWith(compareBy(Message::timestamp))
if (temporaryMessages.isNotEmpty()) { if (temporaryMessages.isNotEmpty()) {
......
...@@ -267,7 +267,7 @@ class AppModule { ...@@ -267,7 +267,7 @@ class AppModule {
@Provides @Provides
fun provideMessageRepository(databaseManager: DatabaseManager): MessagesRepository { fun provideMessageRepository(databaseManager: DatabaseManager): MessagesRepository {
return DatabaseMessagesRepository(databaseManager, DatabaseMessageMapper()) return DatabaseMessagesRepository(databaseManager, DatabaseMessageMapper(databaseManager))
} }
@Provides @Provides
......
...@@ -168,6 +168,9 @@ abstract class MessageDao { ...@@ -168,6 +168,9 @@ abstract class MessageDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun saveLastSync(entity: MessagesSync) abstract fun saveLastSync(entity: MessagesSync)
@Query("SELECT * FROM attachment_fields WHERE attachmentId = :id")
abstract fun getAttachmentFields(id: String): List<AttachmentFieldEntity>
companion object { companion object {
const val BASE_MESSAGE_QUERY = """ const val BASE_MESSAGE_QUERY = """
SELECT SELECT
......
...@@ -9,6 +9,7 @@ import chat.rocket.core.model.attachment.Attachment ...@@ -9,6 +9,7 @@ import chat.rocket.core.model.attachment.Attachment
import chat.rocket.core.model.attachment.AudioAttachment import chat.rocket.core.model.attachment.AudioAttachment
import chat.rocket.core.model.attachment.AuthorAttachment import chat.rocket.core.model.attachment.AuthorAttachment
import chat.rocket.core.model.attachment.ColorAttachment import chat.rocket.core.model.attachment.ColorAttachment
import chat.rocket.core.model.attachment.GenericFileAttachment
import chat.rocket.core.model.attachment.ImageAttachment import chat.rocket.core.model.attachment.ImageAttachment
import chat.rocket.core.model.attachment.MessageAttachment import chat.rocket.core.model.attachment.MessageAttachment
import chat.rocket.core.model.attachment.VideoAttachment import chat.rocket.core.model.attachment.VideoAttachment
...@@ -37,6 +38,7 @@ data class AttachmentEntity( ...@@ -37,6 +38,7 @@ data class AttachmentEntity(
@ColumnInfo(name = "thumb_url") @ColumnInfo(name = "thumb_url")
val thumbUrl: String? = null, val thumbUrl: String? = null,
val color: String? = null, val color: String? = null,
val fallback: String? = null,
@ColumnInfo(name = "title_link") @ColumnInfo(name = "title_link")
val titleLink: String? = null, val titleLink: String? = null,
@ColumnInfo(name = "title_link_download") @ColumnInfo(name = "title_link_download")
...@@ -86,6 +88,7 @@ fun Attachment.asEntity(msgId: String): List<BaseMessageEntity> { ...@@ -86,6 +88,7 @@ fun Attachment.asEntity(msgId: String): List<BaseMessageEntity> {
is AuthorAttachment -> asEntity(msgId) is AuthorAttachment -> asEntity(msgId)
is ColorAttachment -> listOf(asEntity(msgId)) is ColorAttachment -> listOf(asEntity(msgId))
is MessageAttachment -> listOf(asEntity(msgId)) is MessageAttachment -> listOf(asEntity(msgId))
is GenericFileAttachment -> listOf(asEntity(msgId))
// TODO - Action Attachments // TODO - Action Attachments
else -> { else -> {
Timber.d("Missing conversion for: ${javaClass.canonicalName}") Timber.d("Missing conversion for: ${javaClass.canonicalName}")
...@@ -163,7 +166,8 @@ fun ColorAttachment.asEntity(msgId: String): AttachmentEntity = ...@@ -163,7 +166,8 @@ fun ColorAttachment.asEntity(msgId: String): AttachmentEntity =
AttachmentEntity( AttachmentEntity(
_id = "${msgId}_${hashCode()}", _id = "${msgId}_${hashCode()}",
messageId = msgId, messageId = msgId,
color = color.rawColor color = color.rawColor,
fallback = fallback
) )
// TODO - how to model An message attachment with attachments??? // TODO - how to model An message attachment with attachments???
...@@ -179,3 +183,14 @@ fun MessageAttachment.asEntity(msgId: String): AttachmentEntity = ...@@ -179,3 +183,14 @@ fun MessageAttachment.asEntity(msgId: String): AttachmentEntity =
messageLink = url, messageLink = url,
timestamp = timestamp timestamp = timestamp
) )
fun GenericFileAttachment.asEntity(msgId: String): AttachmentEntity =
AttachmentEntity(
_id = "${msgId}_${hashCode()}",
messageId = msgId,
title = title,
description = description,
text = text,
titleLink = titleLink,
titleLinkDownload = titleLinkDownload ?: false
)
\ No newline at end of file
package chat.rocket.android.server.infraestructure package chat.rocket.android.server.infraestructure
import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.db.model.AttachmentEntity import chat.rocket.android.db.model.AttachmentEntity
import chat.rocket.android.db.model.FullMessage import chat.rocket.android.db.model.FullMessage
import chat.rocket.android.db.model.ReactionEntity import chat.rocket.android.db.model.ReactionEntity
...@@ -10,15 +11,26 @@ import chat.rocket.common.model.SimpleUser ...@@ -10,15 +11,26 @@ import chat.rocket.common.model.SimpleUser
import chat.rocket.core.model.Message import chat.rocket.core.model.Message
import chat.rocket.core.model.Reactions import chat.rocket.core.model.Reactions
import chat.rocket.core.model.attachment.Attachment import chat.rocket.core.model.attachment.Attachment
import chat.rocket.core.model.attachment.AudioAttachment
import chat.rocket.core.model.attachment.AuthorAttachment
import chat.rocket.core.model.attachment.Color
import chat.rocket.core.model.attachment.ColorAttachment
import chat.rocket.core.model.attachment.Field
import chat.rocket.core.model.attachment.GenericFileAttachment
import chat.rocket.core.model.attachment.ImageAttachment
import chat.rocket.core.model.attachment.MessageAttachment
import chat.rocket.core.model.attachment.VideoAttachment
import chat.rocket.core.model.messageTypeOf import chat.rocket.core.model.messageTypeOf
import chat.rocket.core.model.url.Meta import chat.rocket.core.model.url.Meta
import chat.rocket.core.model.url.ParsedUrl import chat.rocket.core.model.url.ParsedUrl
import chat.rocket.core.model.url.Url import chat.rocket.core.model.url.Url
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.withContext
class DatabaseMessageMapper { class DatabaseMessageMapper(private val dbManager: DatabaseManager) {
fun map(message: FullMessage): Message? = map(listOf(message)).firstOrNull() suspend fun map(message: FullMessage): Message? = map(listOf(message)).firstOrNull()
fun map(messages: List<FullMessage>): List<Message> { suspend fun map(messages: List<FullMessage>): List<Message> {
val list = mutableListOf<Message>() val list = mutableListOf<Message>()
messages.forEach { message -> messages.forEach { message ->
val favorites = mutableListOf<SimpleUser>() val favorites = mutableListOf<SimpleUser>()
...@@ -120,12 +132,47 @@ class DatabaseMessageMapper { ...@@ -120,12 +132,47 @@ class DatabaseMessageMapper {
} }
} }
private fun mapAttachments(attachments: List<AttachmentEntity>): List<Attachment> { private suspend fun mapAttachments(attachments: List<AttachmentEntity>): List<Attachment> {
val list = mutableListOf<Attachment>() val list = mutableListOf<Attachment>()
attachments.forEach { attachment -> attachments.forEach { attachment ->
with(attachment) {
when {
imageUrl != null -> {
ImageAttachment(title, description, text, titleLink, titleLinkDownload, imageUrl, type, imageSize)
}
videoUrl != null -> {
VideoAttachment(title, description, text, titleLink, titleLinkDownload, videoUrl, type, videoSize)
}
audioUrl != null -> {
AudioAttachment(title, description, text, titleLink, titleLinkDownload, audioUrl, type, audioSize)
}
titleLink != null -> {
GenericFileAttachment(title, description, text, titleLink, titleLink, titleLinkDownload)
}
text != null && color != null && fallback != null -> {
ColorAttachment(Color.Custom(color), text, fallback)
}
text != null -> {
// TODO how to model message with attachments
MessageAttachment(authorName, authorIcon, text, thumbUrl,
color?.let { Color.Custom(it) }, messageLink, null, timestamp)
}
authorLink != null -> {
mapAuthorAttachment(this)
}
else -> null
}?.let { list.add(it) }
}
} }
// TODO - implement mapping
return list return list
} }
private suspend fun mapAuthorAttachment(attachment: AttachmentEntity): AuthorAttachment {
val fields = withContext(CommonPool) {
dbManager.messageDao().getAttachmentFields(attachment._id)
}.map { Field(it.title, it.value) }
return with(attachment) {
AuthorAttachment(authorLink!!, authorIcon, authorName, fields)
}
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment