diff --git a/app/build.gradle b/app/build.gradle index 1e8fe8a1e..9286fc31f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -269,10 +269,6 @@ dependencies { implementation 'com.github.nextcloud:PopupBubble:1.0.6' implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' - implementation('eu.medsea.mimeutil:mime-util:2.1.3', { - exclude group: 'org.slf4j' - }) - implementation "com.afollestad.material-dialogs:core:${materialDialogsVersion}" implementation "com.afollestad.material-dialogs:datetime:${materialDialogsVersion}" implementation "com.afollestad.material-dialogs:bottomsheets:${materialDialogsVersion}" 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 3988f6e31..25fda04e9 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java +++ b/app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java @@ -2,6 +2,8 @@ * Nextcloud Talk application * * @author Mario Danic + * @author Tim Krüger + * Copyright (C) 2021 Tim Krüger * Copyright (C) 2017-2018 Mario Danic * * This program is free software: you can redistribute it and/or modify @@ -22,71 +24,13 @@ package com.nextcloud.talk.utils; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.core.util.PatternsCompat; -import com.nextcloud.talk.models.json.chat.ChatMessage; import com.vanniktech.emoji.EmojiInformation; import com.vanniktech.emoji.EmojiUtils; -import eu.medsea.mimeutil.MimeUtil; -import eu.medsea.mimeutil.detector.ExtensionMimeDetector; -import eu.medsea.mimeutil.detector.MagicMimeMimeDetector; -import eu.medsea.mimeutil.detector.OpendesktopMimeDetector; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import androidx.annotation.Nullable; public final class TextMatchers { - private static final String TAG = "TextMatchers"; - - public static ChatMessage.MessageType getMessageTypeFromString(@NonNull final String text) { - List links = new ArrayList<>(); - Matcher m = PatternsCompat.WEB_URL.matcher(text); - while (m.find()) { - String url = m.group(); - links.add(url); - } - - 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 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 { - 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; - } - } - - // if we have 0 or more than 1 link, we're a regular message - return ChatMessage.MessageType.REGULAR_TEXT_MESSAGE; - } - public static boolean isMessageWithSingleEmoticonOnly(@Nullable final String text) { final EmojiInformation emojiInformation = EmojiUtils.emojiInformation(text); return (emojiInformation.isOnlyEmojis && emojiInformation.emojis.size() == 1); diff --git a/app/src/test/java/com/nextcloud/talk/utils/TextMatchersTest.java b/app/src/test/java/com/nextcloud/talk/utils/TextMatchersTest.java deleted file mode 100644 index fb295f317..000000000 --- a/app/src/test/java/com/nextcloud/talk/utils/TextMatchersTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Nextcloud Talk application - * - * @author Mario Danic - * Copyright (C) 2017-2019 Mario Danic - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.nextcloud.talk.utils; - -import com.nextcloud.talk.models.json.chat.ChatMessage; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class TextMatchersTest { - - @Test - public void getMessageTypeFromString_regularTextGiven_regularTextMessageTypeReturned() { - String simpleMessageText = "Hello world! Have a cookie!"; - String messageContainingLink = "Hello https://nextcloud.com! Have a good day"; - - assertEquals(ChatMessage.MessageType.REGULAR_TEXT_MESSAGE, - TextMatchers.getMessageTypeFromString(simpleMessageText)); - assertEquals(ChatMessage.MessageType.REGULAR_TEXT_MESSAGE, - TextMatchers.getMessageTypeFromString(messageContainingLink)); - } - - @Test - public void getMessageTypeFromString_singleUrlTextGiven_singleLinkMessageTypeReturned() { - String simpleUrlText = "https://nextcloud.com/"; - String complexUrlText = "https://docs.nextcloud.com/server/15/admin_manual/#target-audience"; - - assertEquals(ChatMessage.MessageType.SINGLE_LINK_MESSAGE, - TextMatchers.getMessageTypeFromString(simpleUrlText)); - assertEquals(ChatMessage.MessageType.SINGLE_LINK_MESSAGE, - TextMatchers.getMessageTypeFromString(complexUrlText)); - } - - @Test - public void getMessageTypeFromString_imageLinkGiven_singleLinkImageMessageReturned() { - String simpleImageText = "https://nextcloud.com/image.jpg"; - String complexImageUrlText = "https://nextcloud.com/wp-content/themes/next/assets/img/features/mobileDesktop.png?x22777"; - - assertEquals(ChatMessage.MessageType.SINGLE_LINK_IMAGE_MESSAGE, - TextMatchers.getMessageTypeFromString(simpleImageText)); - assertEquals(ChatMessage.MessageType.SINGLE_LINK_IMAGE_MESSAGE, - TextMatchers.getMessageTypeFromString(complexImageUrlText)); - } - - @Test - public void getMessageTypeFromString_gifLinkGiven_gifMessageTypeReturned() { - String gifImageText = "https://nextcloud.com/funny.gif"; - - assertEquals(ChatMessage.MessageType.SINGLE_LINK_GIF_MESSAGE, - TextMatchers.getMessageTypeFromString(gifImageText)); - } - - @Test - public void getMessageTypeFromString_audioLinkGiven_audioMessageTypeReturned() { - String wavLink = "https://nextcloud.com/message.wav"; - String mp3Link = "https://nextcloud.com/message.mp3"; - - assertEquals(ChatMessage.MessageType.SINGLE_LINK_AUDIO_MESSAGE, - TextMatchers.getMessageTypeFromString(wavLink)); - assertEquals(ChatMessage.MessageType.SINGLE_LINK_AUDIO_MESSAGE, - TextMatchers.getMessageTypeFromString(mp3Link)); - } - - @Test - public void getMessageTypeFromString_videoLinkGiven_videoMessageTypeReturned() { - String mp4Link = "https://nextcloud.com/message.mp4"; - String flvLink = "https://nextcloud.com/message.flv"; - - assertEquals(ChatMessage.MessageType.SINGLE_LINK_VIDEO_MESSAGE, - TextMatchers.getMessageTypeFromString(mp4Link)); - assertEquals(ChatMessage.MessageType.SINGLE_LINK_VIDEO_MESSAGE, - TextMatchers.getMessageTypeFromString(flvLink)); - } - - @Test - public void getMessageTypeFromString_giphyLinkGiven_giphyMessageTypeReturned() { - String giphyLink = "https://media.giphy.com/media/11fucLQCTOdvBS/giphy.gif"; - - assertEquals(ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE, - TextMatchers.getMessageTypeFromString(giphyLink)); - } - - @Test - public void getMessageTypeFromString_tenorLinkGiven_tenorMessageTypeReturned() { - String tenorLink = "https://media.tenor.com/images/d98e76e3930cf171cc39e301c9e974af/tenor.gif"; - - assertEquals(ChatMessage.MessageType.SINGLE_LINK_TENOR_MESSAGE, - TextMatchers.getMessageTypeFromString(tenorLink)); - } -}