From 8289a0b1097f02a99d62f4f7750e6b46a54a54fc Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 9 Feb 2024 16:02:43 +0300 Subject: Append IDEA code suggestions --- src/main/java/com/juick/CommandsManager.java | 2 +- src/main/java/com/juick/XMPPManager.java | 2 +- src/main/java/com/juick/config/AppConfig.java | 3 +-- src/main/java/com/juick/model/Message.java | 3 +-- src/main/java/com/juick/service/ChatServiceImpl.java | 1 - .../java/com/juick/service/FileSystemStorageService.java | 7 +++---- src/main/java/com/juick/service/InfoService.java | 4 ++-- src/main/java/com/juick/service/StorageService.java | 4 ++-- src/main/java/com/juick/service/TelegramServiceImpl.java | 3 +-- src/main/java/com/juick/util/HttpUtils.java | 4 ++-- .../com/juick/util/formatters/PlainTextFormatter.java | 2 +- src/main/java/com/juick/www/api/Mastodon.java | 2 -- src/main/java/com/juick/www/api/SystemActivity.java | 2 +- src/main/java/com/juick/www/api/activity/Profile.java | 15 ++++++--------- .../java/com/juick/www/api/activity/model/Activity.java | 2 +- src/main/java/com/juick/www/filters/AnythingFilter.java | 1 - .../pebble/extension/filters/FormatMessageFilter.java | 3 +-- src/test/java/com/juick/server/tests/ServerTests.java | 15 +++++++-------- 18 files changed, 31 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/juick/CommandsManager.java b/src/main/java/com/juick/CommandsManager.java index 588d0073..7488e331 100644 --- a/src/main/java/com/juick/CommandsManager.java +++ b/src/main/java/com/juick/CommandsManager.java @@ -417,7 +417,7 @@ public class CommandsManager { public CommandResult commandOnOff(User user, URI attachment, String[] input) { UserService.ActiveStatus newStatus; String retValUpdated; - if (input[0].toLowerCase().equals("on")) { + if (input[0].equalsIgnoreCase("on")) { newStatus = UserService.ActiveStatus.Active; retValUpdated = "XMPP notifications are activated"; } else { diff --git a/src/main/java/com/juick/XMPPManager.java b/src/main/java/com/juick/XMPPManager.java index 0c5cb2f4..9e24c347 100644 --- a/src/main/java/com/juick/XMPPManager.java +++ b/src/main/java/com/juick/XMPPManager.java @@ -239,7 +239,7 @@ public class XMPPManager implements NotificationListener { return isBotJid || isDomainJid; } return isBotJid; - }; + } private void sendJuickMessage(com.juick.model.Message jmsg, List users) { List jids = new ArrayList<>(); diff --git a/src/main/java/com/juick/config/AppConfig.java b/src/main/java/com/juick/config/AppConfig.java index 77229d1b..27622195 100644 --- a/src/main/java/com/juick/config/AppConfig.java +++ b/src/main/java/com/juick/config/AppConfig.java @@ -85,8 +85,7 @@ public class AppConfig { HtmlRenderer helpRenderer() { return HtmlRenderer.builder() .attributeProviderFactory(context -> (node, tagName, attributes) -> { - if (node instanceof Link) { - Link link = (Link) node; + if (node instanceof Link link) { if (link.getDestination().startsWith("/")) { String destination = "/" + helpService().getHelpPath() + link.getDestination(); link.setDestination(destination); diff --git a/src/main/java/com/juick/model/Message.java b/src/main/java/com/juick/model/Message.java index 32d888a7..915d5f21 100644 --- a/src/main/java/com/juick/model/Message.java +++ b/src/main/java/com/juick/model/Message.java @@ -118,10 +118,9 @@ public class Message implements Comparable, Serializable { if (obj == this) return true; - if (!(obj instanceof Message)) + if (!(obj instanceof Message jmsg)) return false; - Message jmsg = (Message) obj; return (this.getMid() == jmsg.getMid() && this.getRid() == jmsg.getRid()); } diff --git a/src/main/java/com/juick/service/ChatServiceImpl.java b/src/main/java/com/juick/service/ChatServiceImpl.java index c0d2f17b..4247e49a 100644 --- a/src/main/java/com/juick/service/ChatServiceImpl.java +++ b/src/main/java/com/juick/service/ChatServiceImpl.java @@ -47,7 +47,6 @@ public class ChatServiceImpl extends BaseJdbcService implements ChatService { return getJdbcTemplate().query( "SELECT l.user_id, users.nick, l.last, pm.txt FROM pm " + "INNER JOIN users ON users.id = pm.user_id " - + "" + "INNER JOIN (SELECT user_id, MAX(ts) AS last FROM pm " + "WHERE user_id_to=? GROUP BY user_id) l ON l.last = pm.ts " + "WHERE pm.user_id_to=? " diff --git a/src/main/java/com/juick/service/FileSystemStorageService.java b/src/main/java/com/juick/service/FileSystemStorageService.java index 89669283..c3ec9333 100644 --- a/src/main/java/com/juick/service/FileSystemStorageService.java +++ b/src/main/java/com/juick/service/FileSystemStorageService.java @@ -145,8 +145,7 @@ public class FileSystemStorageService implements StorageService { try { ImageMetadata metadata = Imaging.getMetadata(imageFile); - if (metadata instanceof JpegImageMetadata) { - JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata; + if (metadata instanceof JpegImageMetadata jpegMetadata) { TiffField orientationField = jpegMetadata.findEXIFValue(TiffTagConstants.TIFF_TAG_ORIENTATION); if (orientationField != null) { @@ -228,10 +227,10 @@ public class FileSystemStorageService implements StorageService { String targetExt = "png"; if (ImageIO.write(Scalr.resize(originalImage, 96), targetExt, tmpPath.toFile())) { Files.move(tmpPath, getAvatarPath(user), copyOptions); - }; + } if (ImageIO.write(Scalr.resize(originalImage, 32), targetExt, tmpPath.toFile())) { Files.move(tmpPath, Paths.get(avatarSmallDir, getAvatarFileName(user, targetExt)), copyOptions); - }; + } } public Attachment getAttachment(File imgFile) throws IOException { diff --git a/src/main/java/com/juick/service/InfoService.java b/src/main/java/com/juick/service/InfoService.java index b10e325e..9540243e 100644 --- a/src/main/java/com/juick/service/InfoService.java +++ b/src/main/java/com/juick/service/InfoService.java @@ -58,9 +58,9 @@ public class InfoService { UserStats userStats = new UserStats(); userStats.setTotal(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM users WHERE banned=0", Integer.class)); userStats.setActiveMonth(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM users WHERE banned=0 AND last_seen > ?", - Integer.class, ZonedDateTime.now().minus(1, ChronoUnit.MONTHS).toLocalDateTime())); + Integer.class, ZonedDateTime.now().minusMonths(1).toLocalDateTime())); userStats.setActiveHalfyear(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM users WHERE banned=0 AND last_seen > ?", - Integer.class, ZonedDateTime.now().minus(6, ChronoUnit.MONTHS).toLocalDateTime())); + Integer.class, ZonedDateTime.now().minusMonths(6).toLocalDateTime())); Usage usage = new Usage(); usage.setUsers(userStats); usage.setLocalPosts(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM messages WHERE privacy=1", diff --git a/src/main/java/com/juick/service/StorageService.java b/src/main/java/com/juick/service/StorageService.java index f1f72a60..1574c809 100644 --- a/src/main/java/com/juick/service/StorageService.java +++ b/src/main/java/com/juick/service/StorageService.java @@ -43,9 +43,9 @@ public interface StorageService { */ void saveAvatar(String tempFilename, User user) throws IOException; - public Attachment getAttachment(File imgFile) throws IOException; + Attachment getAttachment(File imgFile) throws IOException; - public Attachment getAvatarMetadata(User user) throws IOException; + Attachment getAvatarMetadata(User user) throws IOException; String getBaseDirectory(); diff --git a/src/main/java/com/juick/service/TelegramServiceImpl.java b/src/main/java/com/juick/service/TelegramServiceImpl.java index 3cd6c27d..3f11a47a 100644 --- a/src/main/java/com/juick/service/TelegramServiceImpl.java +++ b/src/main/java/com/juick/service/TelegramServiceImpl.java @@ -65,8 +65,7 @@ public class TelegramServiceImpl extends BaseJdbcService implements TelegramServ if (uids.isEmpty()) { return Collections.emptyList(); } - return getNamedParameterJdbcTemplate().queryForList("" + - "SELECT tg_id FROM telegram WHERE user_id IN(:uids)", new MapSqlParameterSource() + return getNamedParameterJdbcTemplate().queryForList("SELECT tg_id FROM telegram WHERE user_id IN(:uids)", new MapSqlParameterSource() .addValue("uids", uids), Long.class); } } diff --git a/src/main/java/com/juick/util/HttpUtils.java b/src/main/java/com/juick/util/HttpUtils.java index 46bb3e2d..22598977 100644 --- a/src/main/java/com/juick/util/HttpUtils.java +++ b/src/main/java/com/juick/util/HttpUtils.java @@ -97,7 +97,7 @@ public class HttpUtils { try { urlConn = url.openConnection(); } catch (IOException e) { - logger.error(String.format("Failed open url: %s", url.toString())); + logger.error(String.format("Failed open url: %s", url)); throw e; } @@ -108,7 +108,7 @@ public class HttpUtils { Files.copy(is, Paths.get(tmpDir, attachmentFName)); return URI.create(String.format("juick://%s", attachmentFName)); } catch (IOException e) { - logger.error(String.format("Failed download image by url: %s", url.toString()), e); + logger.error(String.format("Failed download image by url: %s", url), e); throw e; } } diff --git a/src/main/java/com/juick/util/formatters/PlainTextFormatter.java b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java index b1b1a44a..9a885bca 100644 --- a/src/main/java/com/juick/util/formatters/PlainTextFormatter.java +++ b/src/main/java/com/juick/util/formatters/PlainTextFormatter.java @@ -67,7 +67,7 @@ public class PlainTextFormatter { sb.append(txt); } return String.format("@%s:%s\n%s\n#%s (%s%s) %s", - m.getUser().getName(), MessageUtils.getTagsString(m), sb.toString(), formatPostNumber(m), timeAgo, repliesCount, formatUrl(m)); + m.getUser().getName(), MessageUtils.getTagsString(m), sb, formatPostNumber(m), timeAgo, repliesCount, formatUrl(m)); } public static String formatUrl(Message jmsg) { diff --git a/src/main/java/com/juick/www/api/Mastodon.java b/src/main/java/com/juick/www/api/Mastodon.java index eeca1fda..190ee5ef 100644 --- a/src/main/java/com/juick/www/api/Mastodon.java +++ b/src/main/java/com/juick/www/api/Mastodon.java @@ -182,8 +182,6 @@ public class Mastodon { public record Preferences() { } - ; - @GetMapping("/api/v1/preferences") public Preferences preferences() { return new Preferences(); diff --git a/src/main/java/com/juick/www/api/SystemActivity.java b/src/main/java/com/juick/www/api/SystemActivity.java index 59d5cc73..a75ef3b6 100644 --- a/src/main/java/com/juick/www/api/SystemActivity.java +++ b/src/main/java/com/juick/www/api/SystemActivity.java @@ -72,7 +72,7 @@ public class SystemActivity { follow } - private ActivityType type; + private final ActivityType type; private User from; private List to; private Message message; diff --git a/src/main/java/com/juick/www/api/activity/Profile.java b/src/main/java/com/juick/www/api/activity/Profile.java index aa8f0394..d815c9d6 100644 --- a/src/main/java/com/juick/www/api/activity/Profile.java +++ b/src/main/java/com/juick/www/api/activity/Profile.java @@ -190,7 +190,7 @@ public class Profile { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri); uriComponentsBuilder.path(String.format("/u/%s/followers", userName)); List followers = userService.getUserReaders(user.getUid()); - Stream followersPage = followers.stream().skip(20 * page).limit(20); + Stream followersPage = followers.stream().skip(20L * page).limit(20); OrderedCollectionPage result = new OrderedCollectionPage(); result.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString()); @@ -234,7 +234,7 @@ public class Profile { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri); uriComponentsBuilder.path(String.format("/u/%s/following", userName)); List following = userService.getUserFriends(user.getUid()); - Stream followingPage = following.stream().skip(20 * page).limit(20); + Stream followingPage = following.stream().skip(20L * page).limit(20); OrderedCollectionPage result = new OrderedCollectionPage(); result.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString()); @@ -268,7 +268,7 @@ public class Profile { } private String formatNote(Note note) { - String markdown = remarkConverter.convertFragment((String) note.getContent()); + String markdown = remarkConverter.convertFragment(note.getContent()); // combine note text with attachment urls return note.getAttachment() == null ? markdown : note.getAttachment().stream().map(attachment -> { @@ -289,8 +289,7 @@ public class Profile { if (context instanceof Activity activity) { if ((StringUtils.isNotEmpty(visitor.getUri().toString()) && visitor.getUri().equals(URI.create(activity.getActor()))) || !visitor.isAnonymous()) { - if (activity instanceof Follow) { - Follow followRequest = (Follow) activity; + if (activity instanceof Follow followRequest) { applicationEventPublisher.publishEvent(new FollowEvent(this, followRequest)); return new ResponseEntity<>(CommandResult.fromString("Follow request accepted"), HttpStatus.ACCEPTED); } @@ -309,8 +308,7 @@ public class Profile { } } if (activity instanceof Create) { - if (activity.getObject() instanceof Note) { - Note note = (Note) activity.getObject(); + if (activity.getObject() instanceof Note note) { URI noteId = URI.create(note.getId()); if (messagesService.replyExists(noteId)) { return new ResponseEntity<>(CommandResult.fromString("Reply already exists"), HttpStatus.OK); @@ -386,8 +384,7 @@ public class Profile { logger.info("{} update their profile", activity.getActor()); return new ResponseEntity<>(CommandResult.fromString("Update accepted"), HttpStatus.ACCEPTED); } - if (activity.getObject() instanceof Note) { - Note note = (Note) activity.getObject(); + if (activity.getObject() instanceof Note note) { logger.info("Got update to {}", note.getId()); if (activity.getActor().equals(note.getAttributedTo())) { Message reply = messagesService.getReplyByUri(note.getId()); diff --git a/src/main/java/com/juick/www/api/activity/model/Activity.java b/src/main/java/com/juick/www/api/activity/model/Activity.java index 7cc0b13f..427990c6 100644 --- a/src/main/java/com/juick/www/api/activity/model/Activity.java +++ b/src/main/java/com/juick/www/api/activity/model/Activity.java @@ -38,7 +38,7 @@ public abstract class Activity extends Context { public void setActor(String actor) { this.actor = actor; if (StringUtils.isEmpty(getId())) { - setId(String.format("%s#%s", this.actor, UUID.randomUUID().toString())); + setId(String.format("%s#%s", this.actor, UUID.randomUUID())); } } diff --git a/src/main/java/com/juick/www/filters/AnythingFilter.java b/src/main/java/com/juick/www/filters/AnythingFilter.java index ffd11b29..91422807 100644 --- a/src/main/java/com/juick/www/filters/AnythingFilter.java +++ b/src/main/java/com/juick/www/filters/AnythingFilter.java @@ -73,7 +73,6 @@ public class AnythingFilter extends OncePerRequestFilter { } } } - ; if (userService.getUsernames().stream().anyMatch(name -> name.equals(anything))) { servletResponse.sendRedirect("/" + anything + "/"); } else { diff --git a/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java b/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java index 009cc83a..1ecfd40d 100644 --- a/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java +++ b/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java @@ -34,8 +34,7 @@ import java.util.Map; public class FormatMessageFilter implements Filter { @Override public Object apply(Object input, Map args, PebbleTemplate self, EvaluationContext context, int lineNumber) { - if (input instanceof Message) { - Message msg = (Message) input; + if (input instanceof Message msg) { if (msg.isHtml()) { return new SafeString(msg.getText()); } diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index 64fbd030..13f8b1f2 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -795,9 +795,8 @@ public class ServerTests { (rs, rowNum) -> ((UserServiceImpl) userService).getOffsetDateTime(rs, 1).toInstant(), user.getUid()); var last = list.get(0); assertThat(last, equalTo(yoyoMsg.getNewMessage().get().getCreated())); - assertEquals(true, - commandsManager.processCommand(user, String.format("#%d", mid), emptyUri).getText() - .startsWith("@me")); + assertTrue(commandsManager.processCommand(user, String.format("#%d", mid), emptyUri).getText() + .startsWith("@me")); User readerUser = userService.createUser("dummyReader", "dummySecret") .orElseThrow(IllegalStateException::new); assertThat( @@ -1909,7 +1908,7 @@ public class ServerTests { public void escapeSqlTests() { String sql = String.format("SELECT * FROM table WHERE data='%s'", WebUtils.encodeSphinx("';-- DROP TABLE table")); - assertThat(sql, is("SELECT * FROM table WHERE data='\\';-- DROP TABLE table\'")); + assertThat(sql, is("SELECT * FROM table WHERE data='\\';-- DROP TABLE table'")); } @Test @@ -1967,7 +1966,7 @@ public class ServerTests { Create create = jsonMapper.readValue(createJsonStr, Create.class); Note note = (Note) create.getObject(); Context attachmentObj = note.getAttachment().get(0); - String attachment = attachmentObj != null ? (String) attachmentObj.getUrl() : StringUtils.EMPTY; + String attachment = attachmentObj != null ? attachmentObj.getUrl() : StringUtils.EMPTY; String deleteJsonStr = IOUtils.toString(new ClassPathResource("delete.json").getURI(), StandardCharsets.UTF_8); Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class); @@ -2259,7 +2258,7 @@ public class ServerTests { String noteString = IOUtils.toString(noteWithAttachment.getInputStream(), StandardCharsets.UTF_8); Create create = jsonMapper.readValue(noteString, Create.class); Note note = (Note) create.getObject(); - String markdown = remarkConverter.convertFragment((String) note.getContent()); + String markdown = remarkConverter.convertFragment(note.getContent()); String commandBody = note.getContent() == null ? markdown : note.getAttachment().stream().map(attachment -> { String attachmentUrl = attachment.getUrl(); @@ -2515,8 +2514,8 @@ public class ServerTests { @Test public void ldRequestToBlogShouldRedirect() throws Exception { - var htmlUri = String.format("/ugnich/"); - var ldUri = String.format("/u/ugnich"); + var htmlUri = "/ugnich/"; + var ldUri = "/u/ugnich"; mockMvc.perform(get(htmlUri).accept(Context.LD_JSON_MEDIA_TYPE)).andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl(ldUri)); mockMvc.perform(get(htmlUri).accept(MediaType.APPLICATION_JSON)).andExpect(status().is3xxRedirection()) -- cgit v1.2.3