From 0f87a60c657ae0b82abfc9be557c094a7f3e6eb5 Mon Sep 17 00:00:00 2001 From: Lucio Maciel Date: Tue, 30 Oct 2018 00:05:14 -0300 Subject: [PATCH] Refactor attachments --- .../rocket/core/internal/AttachmentAdapter.kt | 228 ++++++------------ .../core/model/attachment/Attachment.kt | 73 +++++- .../core/model/attachment/AudioAttachment.kt | 21 -- .../core/model/attachment/AuthorAttachment.kt | 16 -- .../core/model/attachment/ColorAttachment.kt | 54 ----- .../core/model/attachment/FileAttachment.kt | 11 - .../model/attachment/GenericFileAttachment.kt | 17 -- .../core/model/attachment/ImageAttachment.kt | 22 -- .../model/attachment/MessageAttachment.kt | 18 -- .../core/model/attachment/VideoAttachment.kt | 21 -- .../attachment/actions/ActionsAttachment.kt | 12 - 11 files changed, 144 insertions(+), 349 deletions(-) delete mode 100644 core/src/main/kotlin/chat/rocket/core/model/attachment/AudioAttachment.kt delete mode 100644 core/src/main/kotlin/chat/rocket/core/model/attachment/AuthorAttachment.kt delete mode 100644 core/src/main/kotlin/chat/rocket/core/model/attachment/ColorAttachment.kt delete mode 100644 core/src/main/kotlin/chat/rocket/core/model/attachment/FileAttachment.kt delete mode 100644 core/src/main/kotlin/chat/rocket/core/model/attachment/GenericFileAttachment.kt delete mode 100644 core/src/main/kotlin/chat/rocket/core/model/attachment/ImageAttachment.kt delete mode 100644 core/src/main/kotlin/chat/rocket/core/model/attachment/MessageAttachment.kt delete mode 100644 core/src/main/kotlin/chat/rocket/core/model/attachment/VideoAttachment.kt delete mode 100644 core/src/main/kotlin/chat/rocket/core/model/attachment/actions/ActionsAttachment.kt diff --git a/core/src/main/kotlin/chat/rocket/core/internal/AttachmentAdapter.kt b/core/src/main/kotlin/chat/rocket/core/internal/AttachmentAdapter.kt index f1c4afd1..5d248a1a 100644 --- a/core/src/main/kotlin/chat/rocket/core/internal/AttachmentAdapter.kt +++ b/core/src/main/kotlin/chat/rocket/core/internal/AttachmentAdapter.kt @@ -3,25 +3,14 @@ package chat.rocket.core.internal import chat.rocket.common.internal.ISO8601Date import chat.rocket.common.util.Logger 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.DEFAULT_COLOR import chat.rocket.core.model.attachment.Field -import chat.rocket.core.model.attachment.FileAttachment -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 com.squareup.moshi.JsonAdapter -import com.squareup.moshi.JsonDataException import com.squareup.moshi.JsonReader import com.squareup.moshi.JsonWriter import com.squareup.moshi.Moshi import com.squareup.moshi.Types 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 java.lang.reflect.Type @@ -143,45 +132,50 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter< } reader.endObject() - return when { - imageUrl != null -> { - var preview: String? = null - imagePreview?.let { - preview = "data:${imageType!!};base64,$it" - } - ImageAttachment(title, description, text, titleLink, titleLinkDownload, imageUrl, imageType, imageSize, preview) - } - videoUrl != null -> { - VideoAttachment(title, description, text, titleLink, titleLinkDownload, videoUrl, videoType, videoSize) - } - audioUrl != null -> { - AudioAttachment(title, description, text, titleLink, titleLinkDownload, audioUrl, audioType, audioSize) - } - titleLink != null -> { - GenericFileAttachment(title, description, text, titleLink, titleLink, titleLinkDownload) - } - text != null && color != null && fallback != null -> { - ColorAttachment(color, text, fallback, fields) - } - text != null -> { - MessageAttachment(authorName, authorIcon, text, thumbUrl, color, messageLink, attachments, timestamp) - } - authorLink != null -> { - AuthorAttachment(authorLink, authorIcon, authorName, fields) - } - fields != null -> { - ColorAttachment(color ?: DEFAULT_COLOR, text ?: "", fallback, fields) - } - actions != null -> { - ActionsAttachment(title, actions, buttonAlignment = buttonAlignment ?: "vertical") - } - else -> { - logger.debug { - "Invalid Attachment type: supported are file and message at ${reader.path} - type: $type" - } - null + if (isAllNull(title, type, description, authorName, text, thumbUrl, color, titleLink, titleLinkDownload, + imageUrl, imageType, imageSize, videoUrl, videoType, videoSize, audioUrl, audioType, audioSize, + messageLink, attachments, timestamp, authorIcon, authorLink, imagePreview, fields, fallback, + buttonAlignment, actions)) { + logger.debug { + "Empty attachment" } + return null } + + return Attachment( + title = title, + type = type, + description = description, + authorName = authorName, + text = text, + thumbUrl = thumbUrl, + color = color, + titleLink = titleLink, + titleLinkDownload = titleLinkDownload, + imageUrl = imageUrl, + imageType = imageType, + imageSize = imageSize, + videoUrl = videoUrl, + videoType = videoType, + videoSize = videoSize, + audioUrl = audioUrl, + audioType = audioType, + audioSize = audioSize, + messageLink = messageLink, + attachments = attachments, + timestamp = timestamp, + authorIcon = authorIcon, + authorLink = authorLink, + imagePreview = imagePreview, + fields = fields, + fallback = fallback, + buttonAlignment = buttonAlignment, + actions = actions + ) + } + + private fun isAllNull(vararg params: Any?): Boolean { + return params.isEmpty() } private fun parseFields(reader: JsonReader): List? { @@ -258,103 +252,41 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter< } } - override fun toJson(writer: JsonWriter, value: Attachment?) { - if (value == null) { + override fun toJson(writer: JsonWriter, attachment: Attachment?) { + if (attachment == null) { writer.nullValue() } else { - when (value) { - is ColorAttachment -> writeColorAttachment(writer, value) - is MessageAttachment -> writeMessageAttachment(writer, value) - is FileAttachment -> writeFileAttachment(writer, value) - is AuthorAttachment -> writeAuthorAttachment(writer, value) - is ActionsAttachment -> writeActionsAttachment(writer, value) - } - } - } - - private fun writeColorAttachment(writer: JsonWriter, attachment: ColorAttachment) { - writer.beginObject() - with(writer) { - name("color").value(attachment.color.rawColor) - name("text").value(attachment.text) - name("fallback").value(attachment.fallback) - attachment.fields?.let { writeFields(writer, it) } - } - writer.endObject() - } + with(writer) { + beginObject() + name("title").value(attachment.title) + name("description").value(attachment.description) + name("text").value(attachment.text) + name("title_link").value(attachment.titleLink) + name("title_link_download").value(attachment.titleLinkDownload) + name("image_url").value(attachment.imageUrl) + name("image_size").value(attachment.imageSize) + name("image_type").value(attachment.imageType) + name("image_preview").value(attachment.imagePreview) + name("video_url").value(attachment.videoUrl) + name("video_size").value(attachment.videoType) + name("video_type").value(attachment.videoUrl) + name("audio_url").value(attachment.audioUrl) + name("audio_size").value(attachment.audioSize) + name("audio_type").value(attachment.audioType) + name("author_link").value(attachment.authorLink) + name("author_icon").value(attachment.authorIcon) + name("author_name").value(attachment.authorName) + name("button_alignment").value(attachment.buttonAlignment) + name("color").value(attachment.color?.rawColor) + name("fallback").value(attachment.fallback) + name("thumbUrl").value(attachment.thumbUrl) + name("message_link").value(attachment.messageLink) + name("ts").value(attachment.timestamp) - private fun writeMessageAttachment(writer: JsonWriter, attachment: MessageAttachment) { - writer.beginObject() - with(writer) { - name("author_name").value(attachment.author) - name("author_icon").value(attachment.icon) - name("text").value(attachment.text) - name("thumbUrl").value(attachment.thumbUrl) - name("color").value(attachment.color?.toString()) - name("message_link").value(attachment.url) - name("ts").value(attachment.timestamp) - } - writer.endObject() - } - - private fun writeFileAttachment(writer: JsonWriter, attachment: FileAttachment) { - writer.beginObject() - writer.name("title").value(attachment.title) - writer.name("description").value(attachment.description) - writer.name("text").value(attachment.text) - writer.name("title_link").value(attachment.titleLink) - writer.name("title_link_download").value(attachment.titleLinkDownload) - when (attachment) { - is AudioAttachment -> writeAudioAttachment(writer, attachment) - is VideoAttachment -> writeVideoAttachment(writer, attachment) - is ImageAttachment -> writeImageAttachment(writer, attachment) - is GenericFileAttachment -> writeGenericFileAttachment(writer, attachment) - } - writer.endObject() - } - - private fun writeGenericFileAttachment(writer: JsonWriter, attachment: GenericFileAttachment) { - with(writer) { - name("title").value(attachment.title) - name("titleLink").value(attachment.url) - name("titleLinkDownload").value(attachment.titleLinkDownload) - } - } - - private fun writeAudioAttachment(writer: JsonWriter, attachment: AudioAttachment) { - with(writer) { - name("audio_url").value(attachment.url) - name("audio_size").value(attachment.size) - name("audio_type").value(attachment.type) - } - } - - private fun writeVideoAttachment(writer: JsonWriter, attachment: VideoAttachment) { - with(writer) { - name("video_url").value(attachment.url) - name("video_size").value(attachment.size) - name("video_type").value(attachment.type) - } - } - - private fun writeImageAttachment(writer: JsonWriter, attachment: ImageAttachment) { - with(writer) { - name("image_url").value(attachment.url) - name("image_size").value(attachment.size) - name("image_type").value(attachment.type) - name("image_preview").value(attachment.imagePreview) - } - } - - private fun writeAuthorAttachment(writer: JsonWriter, attachment: AuthorAttachment) { - writer.beginObject() - with(writer) { - name("author_link").value(attachment.url) - name("author_icon").value(attachment.authorIcon) - name("author_name").value(attachment.authorName) - attachment.fields?.let { writeFields(writer, it) } + attachment.actions?.let { writeActions(writer, it) } + attachment.fields?.let { writeFields(writer, it) } + } } - writer.endObject() } private fun writeFields(writer: JsonWriter, fields: List) { @@ -371,16 +303,6 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter< } } - private fun writeActionsAttachment(writer: JsonWriter, attachment: ActionsAttachment) { - writer.beginObject() - with(writer) { - name("title").value(attachment.title) - name("button_alignment").value(attachment.buttonAlignment) - attachment.actions?.let { writeActions(writer, it) } - } - writer.endObject() - } - private fun writeActions(writer: JsonWriter, actions: List) { if (actions.isNotEmpty()) { writer.name("actions") @@ -402,10 +324,6 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter< writer.endArray() } } - - private fun checkNonNull(field: Any?, fieldName: String) { - if (field == null) throw JsonDataException("$fieldName is null") - } } class AttachmentAdapterFactory(private val logger: Logger) : JsonAdapter.Factory { diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/Attachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/Attachment.kt index 2b5fd62a..76097f8e 100644 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/Attachment.kt +++ b/core/src/main/kotlin/chat/rocket/core/model/attachment/Attachment.kt @@ -1,5 +1,74 @@ package chat.rocket.core.model.attachment -interface Attachment { - val url: String +import chat.rocket.common.internal.FallbackSealedClass +import chat.rocket.core.model.attachment.actions.Action +import com.squareup.moshi.Json + +data class Attachment( + var title: String? = null, + var type: String? = null, + var description: String? = null, + var authorName: String? = null, + var text: String? = null, + var thumbUrl: String? = null, + var color: Color? = null, + var titleLink: String? = null, + var titleLinkDownload: Boolean = false, + var imageUrl: String? = null, + var imageType: String? = null, + var imageSize: Long? = null, + var videoUrl: String? = null, + var videoType: String? = null, + var videoSize: Long? = null, + var audioUrl: String? = null, + var audioType: String? = null, + var audioSize: Long? = null, + var messageLink: String? = null, + var attachments: List? = null, + var timestamp: Long? = null, + var authorIcon: String? = null, + var authorLink: String? = null, + var imagePreview: String? = null, + var fields: List? = null, + var fallback: String? = null, + var buttonAlignment: String? = null, + var actions: List? = null +) + +@FallbackSealedClass(name = "Custom", fieldName = "colorValue") +sealed class Color(val color: Int, val rawColor: String) { + @Json(name = "good") class Good : Color(parseColor("#35AC19"), "#35AC19") + @Json(name = "warning") class Warning : Color(parseColor("#FCB316"), "#FCB316") + @Json(name = "danger") class Danger : Color(parseColor("#D30230"), "#D30230") + class Custom(private val colorValue: String) : Color(parseColor(colorValue), colorValue) + + override fun toString(): String { + return color.toString(16) + } +} + +private const val DEFAULT_COLOR_INT = 0xA0A0A0 +const val DEFAULT_COLOR_STR = "#A0A0A0" +val DEFAULT_COLOR = Color.Custom(DEFAULT_COLOR_STR) + +fun String?.asColorInt() = parseColor(this ?: DEFAULT_COLOR_STR) +fun String?.asColor() = Color.Custom(this ?: DEFAULT_COLOR_STR) + +private fun parseColor(toParseColor: String): Int { + var rawColor = toParseColor.toUpperCase() + if (rawColor.startsWith("0X")) { + rawColor = "#${rawColor.drop(2)}" + } + return if (rawColor.startsWith('#')) { + var color = toParseColor.substring(1).toLong(16) + if (toParseColor.length == 7) { + // Set the alpha value + color = color or -0x1000000 + } else if (toParseColor.length != 9) { + color = DEFAULT_COLOR_INT.toLong() + } + color.toInt() + } else { + DEFAULT_COLOR_INT + } } \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/AudioAttachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/AudioAttachment.kt deleted file mode 100644 index d7b72d45..00000000 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/AudioAttachment.kt +++ /dev/null @@ -1,21 +0,0 @@ -package chat.rocket.core.model.attachment - -import com.squareup.moshi.Json - -data class AudioAttachment( - override val title: String? = null, - override val description: String? = null, - override val text: String? = null, - @Json(name = "title_link") override val titleLink: String? = null, - @Json(name = "title_link_download") override val titleLinkDownload: Boolean? = null, - @Json(name = "audio_url") private val audioUrl: String, - @Json(name = "audio_type") private val audioType: String? = null, - @Json(name = "audio_size") private val audioSize: Long? = null -) : FileAttachment { - override val url: String - get() = audioUrl - override val size: Long? - get() = audioSize - override val type: String? - get() = audioType -} \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/AuthorAttachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/AuthorAttachment.kt deleted file mode 100644 index 608c49d9..00000000 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/AuthorAttachment.kt +++ /dev/null @@ -1,16 +0,0 @@ -package chat.rocket.core.model.attachment - -import com.squareup.moshi.Json - -data class AuthorAttachment( - @Json(name = "author_link") - private val authorLink: String, - @Json(name = "author_icon") - val authorIcon: String?, - @Json(name = "author_name") - val authorName: String?, - val fields: List? -) : Attachment { - override val url: String - get() = authorLink -} \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/ColorAttachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/ColorAttachment.kt deleted file mode 100644 index 96c3b53c..00000000 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/ColorAttachment.kt +++ /dev/null @@ -1,54 +0,0 @@ -package chat.rocket.core.model.attachment - -import chat.rocket.common.internal.FallbackSealedClass -import com.squareup.moshi.Json -import se.ansman.kotshi.JsonSerializable - -@JsonSerializable -data class ColorAttachment( - val color: Color, - val text: String, - val fallback: String? = null, - val fields: List? = null -) : Attachment { - override val url: String - get() = "#$color" -} - -@FallbackSealedClass(name = "Custom", fieldName = "colorValue") -sealed class Color(val color: Int, val rawColor: String) { - @Json(name = "good") class Good : Color(parseColor("#35AC19"), "#35AC19") - @Json(name = "warning") class Warning : Color(parseColor("#FCB316"), "#FCB316") - @Json(name = "danger") class Danger : Color(parseColor("#D30230"), "#D30230") - class Custom(private val colorValue: String) : Color(parseColor(colorValue), colorValue) - - override fun toString(): String { - return color.toString(16) - } -} - -private const val DEFAULT_COLOR_INT = 0xA0A0A0 -const val DEFAULT_COLOR_STR = "#A0A0A0" -val DEFAULT_COLOR = Color.Custom(DEFAULT_COLOR_STR) - -fun String?.asColorInt() = parseColor(this ?: DEFAULT_COLOR_STR) -fun String?.asColor() = Color.Custom(this ?: DEFAULT_COLOR_STR) - -private fun parseColor(toParseColor: String): Int { - var rawColor = toParseColor.toUpperCase() - if (rawColor.startsWith("0X")) { - rawColor = "#${rawColor.drop(2)}" - } - return if (rawColor.startsWith('#')) { - var color = toParseColor.substring(1).toLong(16) - if (toParseColor.length == 7) { - // Set the alpha value - color = color or -0x1000000 - } else if (toParseColor.length != 9) { - color = DEFAULT_COLOR_INT.toLong() - } - color.toInt() - } else { - DEFAULT_COLOR_INT - } -} \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/FileAttachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/FileAttachment.kt deleted file mode 100644 index d8362d83..00000000 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/FileAttachment.kt +++ /dev/null @@ -1,11 +0,0 @@ -package chat.rocket.core.model.attachment - -interface FileAttachment : Attachment { - val title: String? - val description: String? - val text: String? - val titleLink: String? - val titleLinkDownload: Boolean? - val type: String? - val size: Long? -} \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/GenericFileAttachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/GenericFileAttachment.kt deleted file mode 100644 index 82827e92..00000000 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/GenericFileAttachment.kt +++ /dev/null @@ -1,17 +0,0 @@ -package chat.rocket.core.model.attachment - -data class GenericFileAttachment( - override val title: String? = null, - override val description: String? = null, - override val text: String? = null, - override val titleLink: String? = null, - val fileUrl: String, - override val titleLinkDownload: Boolean? = null -) : FileAttachment { - override val url: String - get() = fileUrl - override val size: Long? - get() = null - override val type: String? - get() = null -} \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/ImageAttachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/ImageAttachment.kt deleted file mode 100644 index 6f1a06cf..00000000 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/ImageAttachment.kt +++ /dev/null @@ -1,22 +0,0 @@ -package chat.rocket.core.model.attachment - -import com.squareup.moshi.Json - -data class ImageAttachment( - override val title: String? = null, - override val description: String? = null, - override val text: String? = null, - @Json(name = "title_link") override val titleLink: String? = null, - @Json(name = "title_link_download") override val titleLinkDownload: Boolean? = null, - @Json(name = "image_url") private val imageUrl: String, - @Json(name = "image_type") private val imageType: String? = null, - @Json(name = "image_size") private val imageSize: Long? = null, - @Json(name = "image_preview") val imagePreview: String? = null -) : FileAttachment { - override val url: String - get() = imageUrl - override val size: Long? - get() = imageSize - override val type: String? - get() = imageType -} \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/MessageAttachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/MessageAttachment.kt deleted file mode 100644 index 5e506993..00000000 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/MessageAttachment.kt +++ /dev/null @@ -1,18 +0,0 @@ -package chat.rocket.core.model.attachment - -import chat.rocket.common.internal.ISO8601Date -import com.squareup.moshi.Json - -data class MessageAttachment( - @Json(name = "author_name") val author: String?, - @Json(name = "author_icon") val icon: String?, - val text: String?, - val thumbUrl: String?, - val color: Color?, - @Json(name = "message_link") private val messageLink: String?, - val attachments: List?, - @Json(name = "ts") @ISO8601Date val timestamp: Long? -) : Attachment { - override val url: String - get() = messageLink ?: "" -} \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/VideoAttachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/VideoAttachment.kt deleted file mode 100644 index aa3dbaef..00000000 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/VideoAttachment.kt +++ /dev/null @@ -1,21 +0,0 @@ -package chat.rocket.core.model.attachment - -import com.squareup.moshi.Json - -data class VideoAttachment( - override val title: String? = null, - override val description: String? = null, - override val text: String? = null, - @Json(name = "title_link") override val titleLink: String? = null, - @Json(name = "title_link_download") override val titleLinkDownload: Boolean? = null, - @Json(name = "video_url") private val videoUrl: String, - @Json(name = "video_type") private val videoType: String? = null, - @Json(name = "video_size") private val videoSize: Long? = null -) : FileAttachment { - override val url: String - get() = videoUrl - override val size: Long? - get() = videoSize - override val type: String? - get() = videoType -} \ No newline at end of file diff --git a/core/src/main/kotlin/chat/rocket/core/model/attachment/actions/ActionsAttachment.kt b/core/src/main/kotlin/chat/rocket/core/model/attachment/actions/ActionsAttachment.kt deleted file mode 100644 index d4090219..00000000 --- a/core/src/main/kotlin/chat/rocket/core/model/attachment/actions/ActionsAttachment.kt +++ /dev/null @@ -1,12 +0,0 @@ -package chat.rocket.core.model.attachment.actions - -import chat.rocket.core.model.attachment.Attachment - -data class ActionsAttachment( - val title: String?, - val actions: List, - val buttonAlignment: String = "vertical" -) : Attachment { - override val url: String - get() = "" -}