From 6b805c47528d34695a52aeece7162c4cc33ab856 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 12 Oct 2022 10:02:36 +0300 Subject: API: add `showReplies` parameter to `/api/thread` endpoint --- src/main/java/com/juick/www/api/Messages.java | 24 ++++++++++++++-------- .../java/com/juick/www/api/SystemActivity.java | 3 +++ 2 files changed, 18 insertions(+), 9 deletions(-) (limited to 'src/main/java/com') diff --git a/src/main/java/com/juick/www/api/Messages.java b/src/main/java/com/juick/www/api/Messages.java index a79088d0..2993e805 100644 --- a/src/main/java/com/juick/www/api/Messages.java +++ b/src/main/java/com/juick/www/api/Messages.java @@ -162,7 +162,8 @@ public class Messages { } @GetMapping("/api/thread") - public List getThread(@Visitor User visitor, @RequestParam(defaultValue = "0") int mid) { + public List getThread(@Visitor User visitor, @RequestParam(defaultValue = "0") int mid, + @RequestParam(defaultValue = "true") boolean showReplies) { Optional message = messagesService.getMessage(mid); if (message.isPresent()) { Message msg = message.get(); @@ -171,15 +172,20 @@ public class Messages { } else { msg.getUser().setAvatar(webApp.getAvatarUrl(msg.getUser())); msg.getRecommendations().forEach(r -> r.setAvatar(webApp.getAvatarUrl(r))); - List replies = messagesService.getReplies(visitor, mid); - // update replies counter to exclude banned replies - msg.setReplies(replies.size()); - if (!visitor.isAnonymous()) { - userService.updateLastSeen(visitor); - applicationEventPublisher.publishEvent(new SystemEvent(this, SystemActivity.read(visitor, msg))); + if (showReplies) { + List replies = messagesService.getReplies(visitor, mid); + // update replies counter to exclude banned replies + msg.setReplies(replies.size()); + if (!visitor.isAnonymous()) { + userService.updateLastSeen(visitor); + applicationEventPublisher + .publishEvent(new SystemEvent(this, SystemActivity.read(visitor, msg))); + } + replies.add(0, msg); + return replies; + } else { + return List.of(msg); } - replies.add(0, msg); - return replies; } } throw new HttpNotFoundException(); diff --git a/src/main/java/com/juick/www/api/SystemActivity.java b/src/main/java/com/juick/www/api/SystemActivity.java index 36bc3ad4..59d5cc73 100644 --- a/src/main/java/com/juick/www/api/SystemActivity.java +++ b/src/main/java/com/juick/www/api/SystemActivity.java @@ -23,6 +23,8 @@ import com.juick.model.User; import java.util.Collections; import java.util.List; +import javax.annotation.Nonnull; + public class SystemActivity { private SystemActivity(ActivityType type) { this.type = type; @@ -43,6 +45,7 @@ public class SystemActivity { return msg; } + @Nonnull public static SystemActivity read(User from, Message message) { var read = new SystemActivity(ActivityType.message); read.setFrom(from); -- cgit v1.2.3