diff options
4 files changed, 15 insertions, 47 deletions
diff --git a/src/main/java/com/juick/service/TwitterService.java b/src/main/java/com/juick/service/TwitterService.java index ffcaca13..1cb3ffa1 100644 --- a/src/main/java/com/juick/service/TwitterService.java +++ b/src/main/java/com/juick/service/TwitterService.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2023, Juick + * Copyright (C) 2008-2024, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,7 +17,6 @@ package com.juick.service; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.scribejava.apis.TwitterApi20; import com.github.scribejava.core.builder.ServiceBuilder; @@ -27,7 +26,6 @@ import com.github.scribejava.core.model.Verb; import com.github.scribejava.core.oauth.OAuth20Service; import com.juick.model.Message; import com.juick.util.MessageUtils; -import com.juick.util.formatters.PlainTextFormatter; import jakarta.annotation.PostConstruct; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -68,7 +66,7 @@ public class TwitterService { userService.getTwitterToken(jmsg.getUser().getUid()).ifPresent(t -> { String status = MessageUtils.getMessageHashTags(jmsg) + StringUtils.defaultString(jmsg.getText()); if (status.length() > 253) { - status = PlainTextFormatter.truncateText(status, 252) + "…"; + status = StringUtils.abbreviate(status, "…", 252); } status += " http://juick.com/" + jmsg.getMid(); try { diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java index 308d144b..42c97d84 100644 --- a/src/main/java/com/juick/util/MessageUtils.java +++ b/src/main/java/com/juick/util/MessageUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2024, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,24 +17,17 @@ package com.juick.util; +import com.juick.model.Entity; import com.juick.model.Message; import com.juick.model.Tag; import com.juick.model.User; -import com.juick.model.Entity; -import com.juick.util.formatters.PlainTextFormatter; import org.apache.commons.lang3.StringUtils; import org.springframework.web.util.UriComponentsBuilder; import java.net.URI; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -53,7 +46,7 @@ public class MessageUtils { if (quote != null) { if (quote.length() > 50) { - result = ">" + PlainTextFormatter.truncateText(quote, 47).replace('\n', ' ') + "…\n"; + result = ">" + StringUtils.abbreviate(quote, "…", 47).replace('\n', ' ') + "\n"; } else if (!quote.isEmpty()) { result = ">" + quote.replace('\n', ' ') + "\n"; } diff --git a/src/main/java/com/juick/util/formatters/PlainTextFormatter.java b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java index 2e7722e7..682a15b4 100644 --- a/src/main/java/com/juick/util/formatters/PlainTextFormatter.java +++ b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2024, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -22,7 +22,6 @@ import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; import org.ocpsoft.prettytime.PrettyTime; -import java.text.BreakIterator; import java.util.Date; import java.util.Locale; @@ -63,7 +62,7 @@ public class PlainTextFormatter { sb.append(attachmentUrl).append("\n"); } if (txt.length() >= cropLength) { - sb.append(PlainTextFormatter.truncateText(txt, cropLength)).append(" [...]"); + sb.append(StringUtils.abbreviate(txt, "[…]", cropLength)); } else { sb.append(txt); } @@ -98,26 +97,4 @@ public class PlainTextFormatter { public static String formatTwitterCard(Message jmsg) { return MessageUtils.getMessageHashTags(jmsg) + StringUtils.defaultString(jmsg.getText()); } - /** - * Truncate text to the nearest word, up to a maximum length specified. - * - * @param text - * @param maxLength - * @return - */ - public static String truncateText(String text, int maxLength) { - if(text != null && text.length() > maxLength) { - BreakIterator bi = BreakIterator.getWordInstance(); - bi.setText(text); - - if(bi.isBoundary(maxLength-1)) { - return text.substring(0, maxLength-2); - } else { - int preceding = bi.preceding(maxLength-1); - return text.substring(0, preceding-1); - } - } else { - return text; - } - } } diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index d61725e6..3bfbf6d4 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2019, Juick + * Copyright (C) 2008-2024, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -38,7 +38,10 @@ import com.juick.service.*; import com.juick.service.activities.UpdateEvent; import com.juick.service.component.SystemEvent; import com.juick.test.util.MockUtils; -import com.juick.util.*; +import com.juick.util.DateFormattersHolder; +import com.juick.util.HttpUtils; +import com.juick.util.MessageUtils; +import com.juick.util.WebUtils; import com.juick.util.formatters.PlainTextFormatter; import com.juick.www.WebApp; import com.juick.www.ad.models.Site; @@ -119,9 +122,6 @@ import java.io.*; import java.net.URI; import java.nio.charset.StandardCharsets; import java.nio.file.*; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.spec.InvalidKeySpecException; import java.time.Instant; import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; @@ -2696,8 +2696,8 @@ public class ServerTests { @Test public void textTruncationShouldNotBreakEmojis() { var text = "Так ты же написал, чтоб я сам поправил отступ \uD83D\uDE00\n"; - var expected = "Так ты же написал, чтоб я сам поправил отступ"; - var truncated = PlainTextFormatter.truncateText(text, 47); + var expected = "Так ты же написал, чтоб я сам поправил отступ …"; + var truncated = StringUtils.abbreviate(text, "…", 47); assertThat(truncated, is(expected)); } } |