diff --git a/app/build.gradle b/app/build.gradle index b874edb7e..5649086c0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -199,6 +199,7 @@ dependencies { implementation 'uk.co.chrisjenx:calligraphy:2.3.0' + implementation group: 'eu.medsea.mimeutil', name: 'mime-util', version: '2.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation ('androidx.test.espresso:espresso-core:3.1.0-alpha4', { exclude group: 'com.android.support', module: 'support-annotations' diff --git a/app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.java b/app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.java index 178c32276..cd0e239a0 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.java +++ b/app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.java @@ -40,6 +40,7 @@ import com.bumptech.glide.request.RequestOptions; import com.nextcloud.talk.R; import com.nextcloud.talk.application.NextcloudTalkApplication; import com.nextcloud.talk.models.database.UserEntity; +import com.nextcloud.talk.models.json.chat.ChatMessage; import com.nextcloud.talk.models.json.rooms.Conversation; import com.nextcloud.talk.utils.ApiUtils; import com.nextcloud.talk.utils.TextMatchers; @@ -126,7 +127,7 @@ public class ConversationItem extends AbstractFlexibleItem messageTypesToIgnore = Arrays.asList(MessageType.REGULAR_TEXT_MESSAGE, + MessageType.SYSTEM_MESSAGE, MessageType.SINGLE_LINK_VIDEO_MESSAGE, + MessageType.SINGLE_LINK_AUDIO_MESSAGE, MessageType.SINGLE_LINK_MESSAGE); private boolean hasFileAttachment() { if (messageParameters != null && messageParameters.size() > 0) { @@ -78,14 +81,38 @@ public class ChatMessage implements IMessage, MessageContentType, MessageContent } } - if (!getSpecialURLType().equals(TextMatchers.SpecialURLType.NONE) && - !getSpecialURLType().equals(TextMatchers.SpecialURLType.REGULAR)) { + if (!messageTypesToIgnore.contains(getMessageType())) { return getMessage().trim(); } return null; } + public MessageType getMessageType() { + if (!TextUtils.isEmpty(getSystemMessage())) { + return MessageType.SYSTEM_MESSAGE; + } + + if (hasFileAttachment()) { + return MessageType.SINGLE_NC_ATTACHMENT_MESSAGE; + } + + return TextMatchers.getMessageTypeFromString(getText()); + } + + public enum MessageType { + REGULAR_TEXT_MESSAGE, + SYSTEM_MESSAGE, + SINGLE_LINK_GIPHY_MESSAGE, + SINGLE_LINK_TENOR_MESSAGE, + SINGLE_LINK_GIF_MESSAGE, + SINGLE_LINK_MESSAGE, + SINGLE_LINK_VIDEO_MESSAGE, + SINGLE_LINK_IMAGE_MESSAGE, + SINGLE_LINK_AUDIO_MESSAGE, + SINGLE_NC_ATTACHMENT_MESSAGE, + } + public enum SystemMessageType { DUMMY, CONVERSATION_CREATED, @@ -164,18 +191,23 @@ public class ChatMessage implements IMessage, MessageContentType, MessageContent } public String getLastMessageDisplayText() { - if (getSpecialURLType().equals(TextMatchers.SpecialURLType.NONE)) { + if (getMessageType().equals(MessageType.REGULAR_TEXT_MESSAGE) || getMessageType().equals(MessageType.SYSTEM_MESSAGE)) { return getText(); } else { - if (getSpecialURLType().equals(TextMatchers.SpecialURLType.GIPHY) - || getSpecialURLType().equals(TextMatchers.SpecialURLType.TENOR)) { + if (getMessageType().equals(MessageType.SINGLE_LINK_GIPHY_MESSAGE) + || getMessageType().equals(MessageType.SINGLE_LINK_TENOR_MESSAGE) + || getMessageType().equals(MessageType.SINGLE_LINK_GIF_MESSAGE)) { return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_a_gif)); - } else if (getSpecialURLType().equals(TextMatchers.SpecialURLType.REGULAR)) { - if (hasFileAttachment()) { - return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_an_attachment)); - } else { - return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_a_link)); - } + } else if (getMessageType().equals(MessageType.SINGLE_NC_ATTACHMENT_MESSAGE)) { + return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_an_attachment)); + } else if (getMessageType().equals(MessageType.SINGLE_LINK_MESSAGE)) { + return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_a_link)); + } else if (getMessageType().equals(MessageType.SINGLE_LINK_AUDIO_MESSAGE)) { + return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_an_audio)); + } else if (getMessageType().equals(MessageType.SINGLE_LINK_VIDEO_MESSAGE)) { + return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_a_video)); + } else if (getMessageType().equals(MessageType.SINGLE_LINK_IMAGE_MESSAGE)) { + return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_an_image)); } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java b/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java index 55859a574..cf4f99e0f 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java +++ b/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java @@ -28,6 +28,7 @@ import android.util.Log; import android.util.Patterns; import com.nextcloud.talk.R; +import com.nextcloud.talk.models.json.chat.ChatMessage; import java.io.BufferedReader; import java.io.IOException; @@ -40,6 +41,10 @@ import java.util.regex.Pattern; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import eu.medsea.mimeutil.MimeUtil; +import eu.medsea.mimeutil.detector.ExtensionMimeDetector; +import eu.medsea.mimeutil.detector.MagicMimeMimeDetector; +import eu.medsea.mimeutil.detector.OpendesktopMimeDetector; public final class TextMatchers { @@ -47,14 +52,7 @@ public final class TextMatchers { private static Pattern regexPattern; - public enum SpecialURLType { - NONE, - REGULAR, - GIPHY, - TENOR, - } - - public static SpecialURLType getSpecialUrlTypeMessage(@NonNull final String text) { + public static ChatMessage.MessageType getMessageTypeFromString(@NonNull final String text) { List links = new ArrayList<>(); Matcher m = Patterns.WEB_URL.matcher(text); while (m.find()) { @@ -65,18 +63,38 @@ public final class TextMatchers { if (links.size() == 1 && text.trim().length() == links.get(0).length()) { String specialLink = links.get(0); if (specialLink.startsWith("https://media.giphy.com/") && specialLink.endsWith(".gif")) { - return SpecialURLType.GIPHY; - } else if (specialLink.contains("tenor.com/")) { - Pattern pattern = Pattern.compile("https://media.*\\.tenor\\.com.*\\.gif.*", Pattern.CASE_INSENSITIVE); - if (pattern.matcher(specialLink).matches()) { - return SpecialURLType.TENOR; - } + return ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE; + } else if (specialLink.contains("tenor.com/") && + Pattern.compile("https://media.*\\.tenor\\.com.*\\.gif.*", + Pattern.CASE_INSENSITIVE).matcher(specialLink).matches()) { + return ChatMessage.MessageType.SINGLE_LINK_TENOR_MESSAGE; } else { - return SpecialURLType.REGULAR; + if (specialLink.contains("?")) { + specialLink = specialLink.substring(0, specialLink.indexOf("?")); + } + MimeUtil.registerMimeDetector(MagicMimeMimeDetector.class.getName()); + MimeUtil.registerMimeDetector(ExtensionMimeDetector.class.getName()); + MimeUtil.registerMimeDetector(OpendesktopMimeDetector.class.getName()); + + String mimeType = MimeUtil.getMostSpecificMimeType(MimeUtil.getMimeTypes(specialLink)).toString(); + if (mimeType.startsWith("image/")) { + if (mimeType.equalsIgnoreCase("image/gif")) { + return ChatMessage.MessageType.SINGLE_LINK_GIF_MESSAGE; + } else { + return ChatMessage.MessageType.SINGLE_LINK_IMAGE_MESSAGE; + } + } else if (mimeType.startsWith("video/")) { + return ChatMessage.MessageType.SINGLE_LINK_VIDEO_MESSAGE; + } else if (mimeType.startsWith("audio/")) { + return ChatMessage.MessageType.SINGLE_LINK_AUDIO_MESSAGE; + } + + return ChatMessage.MessageType.SINGLE_LINK_MESSAGE; } } - return SpecialURLType.NONE; + // if we have 0 or more than 1 link, we're a regular message + return ChatMessage.MessageType.REGULAR_TEXT_MESSAGE; } public static boolean isMessageWithSingleEmoticonOnly(@NonNull final Context context, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ff84c2321..d7aa5b45d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -183,6 +183,10 @@ sent a link. sent a GIF. sent an attachment. + sent an audio. + sent a video. + sent an image. + No more items to load. Refresh to retry.