aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/www/api/Messages.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/www/api/Messages.java')
-rw-r--r--src/main/java/com/juick/www/api/Messages.java93
1 files changed, 38 insertions, 55 deletions
diff --git a/src/main/java/com/juick/www/api/Messages.java b/src/main/java/com/juick/www/api/Messages.java
index 395d00d8..7b003220 100644
--- a/src/main/java/com/juick/www/api/Messages.java
+++ b/src/main/java/com/juick/www/api/Messages.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2020, Juick
+ * Copyright (C) 2008-2021, 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
@@ -24,6 +24,8 @@ import com.juick.util.WebUtils;
import com.juick.www.WebApp;
import com.juick.model.CommandResult;
import com.juick.util.HttpBadRequestException;
+import com.juick.util.HttpForbiddenException;
+import com.juick.util.HttpNotFoundException;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
@@ -34,9 +36,8 @@ import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.io.Resource;
-import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
+import org.springframework.security.access.annotation.Secured;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
@@ -52,14 +53,6 @@ import java.util.stream.Collectors;
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class Messages {
- private static final ResponseEntity<List<Message>> NOT_FOUND = ResponseEntity
- .status(HttpStatus.NOT_FOUND)
- .body(Collections.emptyList());
-
- private static final ResponseEntity<List<Message>> FORBIDDEN = ResponseEntity
- .status(HttpStatus.FORBIDDEN)
- .body(Collections.emptyList());
-
@Inject
private MessagesService messagesService;
@Inject
@@ -76,31 +69,25 @@ public class Messages {
// TODO: serialize image urls
@GetMapping("/api/home")
- public ResponseEntity<List<Message>> getHome(
- @Visitor User visitor,
+ @Secured("ROLE_USER")
+ public List<Message> getHome(@Visitor User visitor,
@RequestParam(defaultValue = "0") int before_mid) {
- if (!visitor.isAnonymous()) {
- int vuid = visitor.getUid();
- List<Integer> mids = messagesService.getMyFeed(vuid, before_mid, true);
- List<Message> msgs = messagesService.getMessages(visitor, mids);
- msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser())));
- return ResponseEntity.ok(msgs);
- }
- return FORBIDDEN;
+ int vuid = visitor.getUid();
+ List<Integer> mids = messagesService.getMyFeed(vuid, before_mid, true);
+ List<Message> msgs = messagesService.getMessages(visitor, mids);
+ msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser())));
+ return msgs;
}
@GetMapping("/api/messages")
- public ResponseEntity<List<Message>> getMessages(
- @Visitor User visitor,
+ public List<Message> getMessages(@Visitor User visitor,
@RequestParam(required = false) String uname,
@RequestParam(name = "before_mid", defaultValue = "0") Integer before,
@RequestParam(required = false, defaultValue = "0") Integer daysback,
- @RequestParam(required = false) String withrecommended,
- @RequestParam(required = false) String popular,
+ @RequestParam(required = false) String withrecommended, @RequestParam(required = false) String popular,
@RequestParam(required = false) String search,
@RequestParam(required = false, defaultValue = "0") Integer page,
- @RequestParam(required = false) String media,
- @RequestParam(required = false) String tag) {
+ @RequestParam(required = false) String media, @RequestParam(required = false) String tag) {
List<Integer> mids;
if (StringUtils.hasText(uname)) {
User user = userService.getUserByName(uname);
@@ -112,19 +99,20 @@ public class Messages {
if (tagObject != null) {
mids = messagesService.getUserTag(user.getUid(), tagObject.TID, 0, before);
} else {
- return NOT_FOUND;
+ throw new HttpNotFoundException();
}
} else if (StringUtils.hasText(withrecommended)) {
mids = messagesService.getUserBlogWithRecommendations(user.getUid(), 0, before);
} else if (daysback > 0) {
mids = messagesService.getUserBlogAtDay(user.getUid(), 0, daysback);
} else if (StringUtils.hasText(search)) {
- mids = messagesService.getUserSearch(visitor, user.getUid(), WebUtils.encodeSphinx(search), 0, page);
+ mids = messagesService.getUserSearch(visitor, user.getUid(), WebUtils.encodeSphinx(search), 0,
+ page);
} else {
mids = messagesService.getUserBlog(user.getUid(), 0, before);
}
} else {
- return NOT_FOUND;
+ throw new HttpNotFoundException();
}
} else {
if (StringUtils.hasText(popular)) {
@@ -136,7 +124,7 @@ public class Messages {
if (tagObject != null) {
mids = messagesService.getTag(tagObject.TID, visitor.getUid(), before, 20);
} else {
- return NOT_FOUND;
+ throw new HttpNotFoundException();
}
} else if (StringUtils.hasText(search)) {
mids = messagesService.getSearch(visitor, WebUtils.encodeSphinx(search), page);
@@ -146,12 +134,12 @@ public class Messages {
}
List<Message> msgs = messagesService.getMessages(visitor, mids);
msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser())));
- return ResponseEntity.ok(msgs);
+ return msgs;
}
+
@DeleteMapping("/api/messages")
- public CommandResult deleteMessage(
- @Visitor User visitor,
- @RequestParam int mid, @RequestParam(required = false, defaultValue = "0") int rid) {
+ public CommandResult deleteMessage(@Visitor User visitor, @RequestParam int mid,
+ @RequestParam(required = false, defaultValue = "0") int rid) {
if (rid > 0) {
if (messagesService.deleteReply(visitor.getUid(), mid, rid)) {
return CommandResult.fromString("Reply deleted");
@@ -164,52 +152,47 @@ public class Messages {
}
@GetMapping("/api/messages/discussions")
- public List<Message> getDiscussions(
- @Visitor User visitor,
+ public List<Message> getDiscussions(@Visitor User visitor,
@RequestParam(required = false, defaultValue = "0") Long to) {
- List<Message> msgs = messagesService.getMessages(visitor,
- messagesService.getDiscussions(visitor.getUid(), to));
+ List<Message> msgs = messagesService.getMessages(visitor, messagesService.getDiscussions(visitor.getUid(), to));
msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser())));
return msgs;
}
+
@GetMapping("/api/thread")
- public ResponseEntity<List<Message>> getThread(
- @Visitor User visitor,
- @RequestParam(defaultValue = "0") int mid) {
+ public List<Message> getThread(@Visitor User visitor, @RequestParam(defaultValue = "0") int mid) {
Optional<Message> message = messagesService.getMessage(mid);
if (message.isPresent()) {
Message msg = message.get();
if (!messagesService.canViewThread(mid, visitor.getUid())) {
- return FORBIDDEN;
+ throw new HttpForbiddenException();
} else {
msg.getUser().setAvatar(webApp.getAvatarUrl(msg.getUser()));
- msg.setRecommendations(new HashSet<>(messagesService.getMessagesRecommendations(
- Collections.singletonList(msg.getMid()))
- .stream().map(Pair::getRight).collect(Collectors.toList())));
+ msg.setRecommendations(new HashSet<>(
+ messagesService.getMessagesRecommendations(Collections.singletonList(msg.getMid())).stream()
+ .map(Pair::getRight).collect(Collectors.toList())));
msg.getRecommendations().forEach(r -> r.setAvatar(webApp.getAvatarUrl(r)));
List<Message> replies = messagesService.getReplies(visitor, mid);
replies.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser())));
if (!visitor.isAnonymous()) {
userService.updateLastSeen(visitor);
- applicationEventPublisher.publishEvent(
- new SystemEvent(this, SystemActivity.read(visitor, msg)));
+ applicationEventPublisher.publishEvent(new SystemEvent(this, SystemActivity.read(visitor, msg)));
}
replies.add(0, msg);
- return ResponseEntity.ok(replies);
+ return replies;
}
}
- return NOT_FOUND;
+ throw new HttpNotFoundException();
}
+
@GetMapping(value = "/api/thread/mark_read/{mid}-{rid}.gif", produces = MediaType.IMAGE_GIF_VALUE)
- public byte[] markThreadRead(
- @Visitor User visitor,
- @PathVariable int mid, @PathVariable int rid) throws IOException {
+ public byte[] markThreadRead(@Visitor User visitor, @PathVariable int mid, @PathVariable int rid)
+ throws IOException {
if (!visitor.isAnonymous()) {
messagesService.setLastReadComment(visitor, mid, rid);
Message msg = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new);
userService.updateLastSeen(visitor);
- applicationEventPublisher.publishEvent(
- new SystemEvent(this, SystemActivity.read(visitor, msg)));
+ applicationEventPublisher.publishEvent(new SystemEvent(this, SystemActivity.read(visitor, msg)));
return IOUtils.toByteArray(invisiblePixel.getInputStream());
}
throw new HttpBadRequestException();