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() {
private suspend fun retrySendingMessages(params: JobParameters?, serverUrl: String) {
val dbManager = dbFactory.create(serverUrl)
val messageRepository = DatabaseMessagesRepository(dbManager, DatabaseMessageMapper())
val messageRepository = DatabaseMessagesRepository(dbManager, DatabaseMessageMapper(dbManager))
val temporaryMessages = messageRepository.getAllUnsent()
.sortedWith(compareBy(Message::timestamp))
if (temporaryMessages.isNotEmpty()) {
......
......@@ -267,7 +267,7 @@ class AppModule {
@Provides
fun provideMessageRepository(databaseManager: DatabaseManager): MessagesRepository {
return DatabaseMessagesRepository(databaseManager, DatabaseMessageMapper())
return DatabaseMessagesRepository(databaseManager, DatabaseMessageMapper(databaseManager))
}
@Provides
......
......@@ -168,6 +168,9 @@ abstract class MessageDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun saveLastSync(entity: MessagesSync)
@Query("SELECT * FROM attachment_fields WHERE attachmentId = :id")
abstract fun getAttachmentFields(id: String): List<AttachmentFieldEntity>
companion object {
const val BASE_MESSAGE_QUERY = """
SELECT
......
......@@ -9,6 +9,7 @@ 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.ColorAttachment
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
......@@ -37,6 +38,7 @@ data class AttachmentEntity(
@ColumnInfo(name = "thumb_url")
val thumbUrl: String? = null,
val color: String? = null,
val fallback: String? = null,
@ColumnInfo(name = "title_link")
val titleLink: String? = null,
@ColumnInfo(name = "title_link_download")
......@@ -86,6 +88,7 @@ fun Attachment.asEntity(msgId: String): List<BaseMessageEntity> {
is AuthorAttachment -> asEntity(msgId)
is ColorAttachment -> listOf(asEntity(msgId))
is MessageAttachment -> listOf(asEntity(msgId))
is GenericFileAttachment -> listOf(asEntity(msgId))
// TODO - Action Attachments
else -> {
Timber.d("Missing conversion for: ${javaClass.canonicalName}")
......@@ -163,7 +166,8 @@ fun ColorAttachment.asEntity(msgId: String): AttachmentEntity =
AttachmentEntity(
_id = "${msgId}_${hashCode()}",
messageId = msgId,
color = color.rawColor
color = color.rawColor,
fallback = fallback
)
// TODO - how to model An message attachment with attachments???
......@@ -179,3 +183,14 @@ fun MessageAttachment.asEntity(msgId: String): AttachmentEntity =
messageLink = url,
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
import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.db.model.AttachmentEntity
import chat.rocket.android.db.model.FullMessage
import chat.rocket.android.db.model.ReactionEntity
......@@ -10,15 +11,26 @@ import chat.rocket.common.model.SimpleUser
import chat.rocket.core.model.Message
import chat.rocket.core.model.Reactions
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.url.Meta
import chat.rocket.core.model.url.ParsedUrl
import chat.rocket.core.model.url.Url
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.withContext
class DatabaseMessageMapper {
fun map(message: FullMessage): Message? = map(listOf(message)).firstOrNull()
class DatabaseMessageMapper(private val dbManager: DatabaseManager) {
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>()
messages.forEach { message ->
val favorites = mutableListOf<SimpleUser>()
......@@ -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>()
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
}
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