From 69866e591c1d5ad89c62ae785886a685d05d34b0 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 19 Apr 2023 08:11:39 +0200 Subject: [PATCH] include mediaType and size in message content --- app/build.gradle | 2 +- .../1.json | 28 ++++++++++++--- .../xmpp/MessageTransformationTest.java | 3 +- .../android/database/Converters.java | 11 ++++++ .../database/entity/MessageContentEntity.java | 12 +++++-- .../database/model/MessageContent.java | 34 ++++++++++++++----- .../model/MessageWithContentReactions.java | 4 +-- .../transformer/MessageContentWrapper.java | 18 ++++++++-- .../android/util/TextContents.java | 3 +- build.gradle | 2 +- 10 files changed, 93 insertions(+), 24 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index cf2ed0c84..a05fff10c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -74,7 +74,7 @@ dependencies { annotationProcessor project(':annotation-processor') // make Java 8 API available - coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.2' + coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3' // Jetpack / AndroidX libraries diff --git a/app/schemas/im.conversations.android.database.ConversationsDatabase/1.json b/app/schemas/im.conversations.android.database.ConversationsDatabase/1.json index 310304d8a..d0ccdec59 100644 --- a/app/schemas/im.conversations.android.database.ConversationsDatabase/1.json +++ b/app/schemas/im.conversations.android.database.ConversationsDatabase/1.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 1, - "identityHash": "8be54d59ea976565ba5a4f7a9faf9109", + "identityHash": "77c7c4ba9aae53855e38b8ce967e2668", "entities": [ { "tableName": "account", @@ -1947,7 +1947,7 @@ }, { "tableName": "message_content", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `messageVersionId` INTEGER NOT NULL, `language` TEXT, `type` TEXT, `body` TEXT, `url` TEXT, FOREIGN KEY(`messageVersionId`) REFERENCES `message_version`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `messageVersionId` INTEGER NOT NULL, `language` TEXT, `partType` TEXT, `mediaType` TEXT, `size` INTEGER, `body` TEXT, `url` TEXT, `cached` INTEGER NOT NULL, FOREIGN KEY(`messageVersionId`) REFERENCES `message_version`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", "fields": [ { "fieldPath": "id", @@ -1968,11 +1968,23 @@ "notNull": false }, { - "fieldPath": "type", - "columnName": "type", + "fieldPath": "partType", + "columnName": "partType", "affinity": "TEXT", "notNull": false }, + { + "fieldPath": "mediaType", + "columnName": "mediaType", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "size", + "columnName": "size", + "affinity": "INTEGER", + "notNull": false + }, { "fieldPath": "body", "columnName": "body", @@ -1984,6 +1996,12 @@ "columnName": "url", "affinity": "TEXT", "notNull": false + }, + { + "fieldPath": "cached", + "columnName": "cached", + "affinity": "INTEGER", + "notNull": true } ], "primaryKey": { @@ -2694,7 +2712,7 @@ "views": [], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '8be54d59ea976565ba5a4f7a9faf9109')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '77c7c4ba9aae53855e38b8ce967e2668')" ] } } \ No newline at end of file diff --git a/app/src/androidTest/java/im/conversations/android/xmpp/MessageTransformationTest.java b/app/src/androidTest/java/im/conversations/android/xmpp/MessageTransformationTest.java index 88fde52c1..4fd1946bc 100644 --- a/app/src/androidTest/java/im/conversations/android/xmpp/MessageTransformationTest.java +++ b/app/src/androidTest/java/im/conversations/android/xmpp/MessageTransformationTest.java @@ -516,7 +516,8 @@ public class MessageTransformationTest extends BaseTransformationTest { final var messages = database.messageDao().getMessagesForTesting(1L); final var message = Iterables.getOnlyElement(messages); Assert.assertEquals(Modification.RETRACTION, message.modification); - Assert.assertEquals(PartType.RETRACTION, Iterables.getOnlyElement(message.contents).type); + Assert.assertEquals( + PartType.RETRACTION, Iterables.getOnlyElement(message.contents).partType); } @Test diff --git a/app/src/main/java/im/conversations/android/database/Converters.java b/app/src/main/java/im/conversations/android/database/Converters.java index 9b68e73e0..b24047713 100644 --- a/app/src/main/java/im/conversations/android/database/Converters.java +++ b/app/src/main/java/im/conversations/android/database/Converters.java @@ -2,6 +2,7 @@ package im.conversations.android.database; import androidx.room.TypeConverter; import com.google.common.base.Strings; +import com.google.common.net.MediaType; import de.measite.minidns.DNSName; import java.io.IOException; import java.net.InetAddress; @@ -176,4 +177,14 @@ public final class Converters { return null; } } + + @TypeConverter + public static String fromMediaType(final MediaType mediaType) { + return mediaType == null ? null : mediaType.toString(); + } + + @TypeConverter + public static MediaType toMediaType(final String mediaType) { + return mediaType == null ? null : MediaType.parse(mediaType); + } } diff --git a/app/src/main/java/im/conversations/android/database/entity/MessageContentEntity.java b/app/src/main/java/im/conversations/android/database/entity/MessageContentEntity.java index 220a46be0..a7a967208 100644 --- a/app/src/main/java/im/conversations/android/database/entity/MessageContentEntity.java +++ b/app/src/main/java/im/conversations/android/database/entity/MessageContentEntity.java @@ -5,6 +5,7 @@ import androidx.room.Entity; import androidx.room.ForeignKey; import androidx.room.Index; import androidx.room.PrimaryKey; +import com.google.common.net.MediaType; import im.conversations.android.database.model.MessageContent; import im.conversations.android.database.model.PartType; @@ -26,20 +27,27 @@ public class MessageContentEntity { public String language; - public PartType type; + public PartType partType; + public MediaType mediaType; + public Long size; public String body; public String url; + public boolean cached; + public static MessageContentEntity of( final long messageVersionId, final MessageContent content) { final var entity = new MessageContentEntity(); entity.messageVersionId = messageVersionId; entity.language = content.language; - entity.type = content.type; + entity.partType = content.partType; + entity.mediaType = content.mediaType; + entity.size = content.size; entity.body = content.body; entity.url = content.url; + entity.cached = content.cached; return entity; } } diff --git a/app/src/main/java/im/conversations/android/database/model/MessageContent.java b/app/src/main/java/im/conversations/android/database/model/MessageContent.java index 5343ac70b..187a3fdee 100644 --- a/app/src/main/java/im/conversations/android/database/model/MessageContent.java +++ b/app/src/main/java/im/conversations/android/database/model/MessageContent.java @@ -1,30 +1,45 @@ package im.conversations.android.database.model; import com.google.common.base.Objects; +import com.google.common.net.MediaType; public class MessageContent { public final String language; - public final PartType type; + public final PartType partType; + public MediaType mediaType; + public Long size; public final String body; public final String url; - public MessageContent(String language, PartType type, String body, String url) { + public boolean cached; + + public MessageContent( + String language, + PartType partType, + MediaType mediaType, + Long size, + String body, + String url, + boolean cached) { this.language = language; - this.type = type; + this.partType = partType; + this.mediaType = mediaType; + this.size = size; this.body = body; this.url = url; + this.cached = cached; } public static MessageContent text(final String body, final String language) { - return new MessageContent(language, PartType.TEXT, body, null); + return new MessageContent(language, PartType.TEXT, null, null, body, null, false); } public static MessageContent file(final String url) { - return new MessageContent(null, PartType.FILE, null, url); + return new MessageContent(null, PartType.FILE, null, null, null, url, false); } @Override @@ -32,14 +47,17 @@ public class MessageContent { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MessageContent that = (MessageContent) o; - return Objects.equal(language, that.language) - && type == that.type + return cached == that.cached + && Objects.equal(language, that.language) + && partType == that.partType + && Objects.equal(mediaType, that.mediaType) + && Objects.equal(size, that.size) && Objects.equal(body, that.body) && Objects.equal(url, that.url); } @Override public int hashCode() { - return Objects.hashCode(language, type, body, url); + return Objects.hashCode(language, partType, mediaType, size, body, url, cached); } } diff --git a/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java b/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java index e303b8026..e5f2bbf74 100644 --- a/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java +++ b/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java @@ -105,7 +105,7 @@ public final class MessageWithContentReactions } public boolean hasPreview() { - return Iterables.tryFind(this.contents, c -> c.type == PartType.FILE).isPresent(); + return Iterables.tryFind(this.contents, c -> c.partType == PartType.FILE).isPresent(); } public boolean hasDownloadButton() { @@ -113,7 +113,7 @@ public final class MessageWithContentReactions } public boolean hasTextContent() { - return Iterables.tryFind(this.contents, c -> c.type == PartType.TEXT).isPresent(); + return Iterables.tryFind(this.contents, c -> c.partType == PartType.TEXT).isPresent(); } public boolean hasInReplyTo() { diff --git a/app/src/main/java/im/conversations/android/transformer/MessageContentWrapper.java b/app/src/main/java/im/conversations/android/transformer/MessageContentWrapper.java index 777393d75..ff0125b33 100644 --- a/app/src/main/java/im/conversations/android/transformer/MessageContentWrapper.java +++ b/app/src/main/java/im/conversations/android/transformer/MessageContentWrapper.java @@ -22,13 +22,22 @@ public class MessageContentWrapper { public static final MessageContentWrapper RETRACTION = new MessageContentWrapper( - ImmutableList.of(new MessageContent(null, PartType.RETRACTION, null, null)), + ImmutableList.of( + new MessageContent( + null, PartType.RETRACTION, null, null, null, null, false)), Encryption.CLEARTEXT, null); private static final List NOT_ENCRYPTED_FOR_THIS_DEVICE = ImmutableList.of( - new MessageContent(null, PartType.NOT_ENCRYPTED_FOR_THIS_DEVICE, null, null)); + new MessageContent( + null, + PartType.NOT_ENCRYPTED_FOR_THIS_DEVICE, + null, + null, + null, + null, + false)); public final List contents; public final Encryption encryption; @@ -104,8 +113,11 @@ public class MessageContentWrapper { new MessageContent( null, PartType.DECRYPTION_FAILURE, + null, + null, exceptionToMessage(cause), - null)), + null, + false)), Encryption.FAILURE, null); } diff --git a/app/src/main/java/im/conversations/android/util/TextContents.java b/app/src/main/java/im/conversations/android/util/TextContents.java index f3ac68c5b..5001e54df 100644 --- a/app/src/main/java/im/conversations/android/util/TextContents.java +++ b/app/src/main/java/im/conversations/android/util/TextContents.java @@ -16,7 +16,8 @@ public final class TextContents { final boolean removeFallback, final int inReplyToFallbackStart, final int inReplyToFallbackEnd) { - final var textContents = Collections2.filter(messageContents, c -> c.type == PartType.TEXT); + final var textContents = + Collections2.filter(messageContents, c -> c.partType == PartType.TEXT); if (textContents.size() == 1 && removeFallback) { final String body = Strings.nullToEmpty(Iterables.getOnlyElement(textContents).body); if (inReplyToFallbackEnd > inReplyToFallbackStart diff --git a/build.gradle b/build.gradle index c2352a174..7dcbc38d2 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { material = "1.8.0" lifecycleVersion = "2.2.0" navVersion = '2.5.3' - roomVersion = "2.5.0" + roomVersion = "2.5.1" preferenceVersion = "1.2.0" espressoVersion = "3.5.1" pagingVersion = "3.1.1"