Commit ac348b69 authored by Lucio Maciel's avatar Lucio Maciel

Tentative Actions Attachment support on DB

parent 2c64b3f4
This diff is collapsed.
......@@ -5,6 +5,7 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import chat.rocket.android.db.model.AttachmentActionEntity
import chat.rocket.android.db.model.AttachmentEntity
import chat.rocket.android.db.model.AttachmentFieldEntity
import chat.rocket.android.db.model.BaseMessageEntity
......@@ -171,6 +172,9 @@ abstract class MessageDao {
@Query("SELECT * FROM attachment_fields WHERE attachmentId = :id")
abstract fun getAttachmentFields(id: String): List<AttachmentFieldEntity>
@Query("SELECT * FROM attachment_action WHERE attachmentId = :id")
abstract fun getAttachmentActions(id: String): List<AttachmentActionEntity>
companion object {
const val BASE_MESSAGE_QUERY = """
SELECT
......
......@@ -2,6 +2,7 @@ package chat.rocket.android.db
import androidx.room.Database
import androidx.room.RoomDatabase
import chat.rocket.android.db.model.AttachmentActionEntity
import chat.rocket.android.db.model.AttachmentEntity
import chat.rocket.android.db.model.AttachmentFieldEntity
import chat.rocket.android.db.model.ChatRoomEntity
......@@ -19,10 +20,10 @@ import chat.rocket.android.db.model.UserEntity
UserEntity::class, ChatRoomEntity::class, MessageEntity::class,
MessageFavoritesRelation::class, MessageMentionsRelation::class,
MessageChannels::class, AttachmentEntity::class,
AttachmentFieldEntity::class, UrlEntity::class, ReactionEntity::class,
MessagesSync::class
AttachmentFieldEntity::class, AttachmentActionEntity::class, UrlEntity::class,
ReactionEntity::class, MessagesSync::class
],
version = 7,
version = 8,
exportSchema = true
)
abstract class RCDatabase : RoomDatabase() {
......
......@@ -3,6 +3,7 @@ package chat.rocket.android.db.model
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import chat.rocket.android.util.extension.orFalse
import chat.rocket.core.model.attachment.Attachment
......@@ -13,6 +14,8 @@ 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.attachment.actions.ActionsAttachment
import chat.rocket.core.model.attachment.actions.ButtonAction
import timber.log.Timber
@Entity(tableName = "attachments",
......@@ -63,13 +66,20 @@ data class AttachmentEntity(
val audioSize: Long? = null,
@ColumnInfo(name = "message_link")
val messageLink: String? = null,
val timestamp: Long? = null
val timestamp: Long? = null,
@ColumnInfo(name = "has_actions")
val hasActions: Boolean = false,
@ColumnInfo(name = "button_alignment")
val buttonAlignment: String? = null
) : BaseMessageEntity
@Entity(tableName = "attachment_fields",
foreignKeys = [
ForeignKey(entity = AttachmentEntity::class, parentColumns = ["_id"],
childColumns = ["attachmentId"], onDelete = ForeignKey.CASCADE)
],
indices = [
Index(value = ["attachmentId"])
])
data class AttachmentFieldEntity(
val attachmentId: String,
......@@ -80,6 +90,29 @@ data class AttachmentFieldEntity(
var id: Long? = null
}
@Entity(tableName = "attachment_action",
foreignKeys = [
ForeignKey(entity = AttachmentEntity::class, parentColumns = ["_id"],
childColumns = ["attachmentId"], onDelete = ForeignKey.CASCADE)
],
indices = [
Index(value = ["attachmentId"])
])
data class AttachmentActionEntity(
val attachmentId: String,
val type: String,
val text: String? = null,
val url: String? = null,
val isWebView: Boolean? = null,
val webViewHeightRatio: String? = null,
val imageUrl: String? = null,
val message: String? = null,
val isMessageInChatWindow: Boolean? = null
) : BaseMessageEntity {
@PrimaryKey(autoGenerate = true)
var id: Long? = null
}
fun Attachment.asEntity(msgId: String): List<BaseMessageEntity> {
return when(this) {
is ImageAttachment -> listOf(asEntity(msgId))
......@@ -89,7 +122,7 @@ fun Attachment.asEntity(msgId: String): List<BaseMessageEntity> {
is ColorAttachment -> listOf(asEntity(msgId))
is MessageAttachment -> listOf(asEntity(msgId))
is GenericFileAttachment -> listOf(asEntity(msgId))
// TODO - Action Attachments
is ActionsAttachment -> asEntity(msgId)
else -> {
Timber.d("Missing conversion for: ${javaClass.canonicalName}")
emptyList()
......@@ -193,4 +226,35 @@ fun GenericFileAttachment.asEntity(msgId: String): AttachmentEntity =
text = text,
titleLink = titleLink,
titleLinkDownload = titleLinkDownload ?: false
)
\ No newline at end of file
)
fun ActionsAttachment.asEntity(msgId: String): List<BaseMessageEntity> {
val list = mutableListOf<BaseMessageEntity>()
val attachmentId = "${msgId}_${hashCode()}"
val attachment = AttachmentEntity(
_id = attachmentId,
messageId = msgId,
title = title,
hasActions = true,
buttonAlignment = buttonAlignment
)
list.add(attachment)
actions.forEach { action ->
when (action) {
is ButtonAction -> AttachmentActionEntity(
attachmentId = attachmentId,
type = action.type,
text = action.text,
url = action.url,
isWebView = action.isWebView,
webViewHeightRatio = action.webViewHeightRatio,
imageUrl = action.imageUrl,
message = action.message,
isMessageInChatWindow = action.isMessageInChatWindow
)
else -> null
}?.let { list.add(it) }
}
return list
}
\ No newline at end of file
package chat.rocket.android.server.infraestructure
import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.db.model.AttachmentActionEntity
import chat.rocket.android.db.model.AttachmentEntity
import chat.rocket.android.db.model.FullMessage
import chat.rocket.android.db.model.ReactionEntity
......@@ -20,6 +21,9 @@ 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.attachment.actions.Action
import chat.rocket.core.model.attachment.actions.ActionsAttachment
import chat.rocket.core.model.attachment.actions.ButtonAction
import chat.rocket.core.model.messageTypeOf
import chat.rocket.core.model.url.Meta
import chat.rocket.core.model.url.ParsedUrl
......@@ -160,6 +164,9 @@ class DatabaseMessageMapper(private val dbManager: DatabaseManager) {
authorLink != null -> {
mapAuthorAttachment(this)
}
hasActions -> {
mapActionAttachment(this)
}
else -> null
}?.let { list.add(it) }
}
......@@ -167,6 +174,25 @@ class DatabaseMessageMapper(private val dbManager: DatabaseManager) {
return list
}
private suspend fun mapActionAttachment(attachment: AttachmentEntity): ActionsAttachment {
val actions = withContext(CommonPool) {
dbManager.messageDao().getAttachmentActions(attachment._id)
}.mapNotNull { mapAction(it) }
return with(attachment) {
// TODO - remove the default "vertical" value from here...
ActionsAttachment(title, actions, buttonAlignment ?: "vertical")
}
}
private fun mapAction(action: AttachmentActionEntity): Action? {
return when (action.type) {
"button" -> ButtonAction(action.type, action.text, action.url, action.isWebView,
action.webViewHeightRatio, action.imageUrl, action.message,
action.isMessageInChatWindow)
else -> null
}
}
private suspend fun mapAuthorAttachment(attachment: AttachmentEntity): AuthorAttachment {
val fields = withContext(CommonPool) {
dbManager.messageDao().getAttachmentFields(attachment._id)
......
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