diff options
Diffstat (limited to 'juick-api/src/main/java/com/juick/api/controllers')
-rw-r--r-- | juick-api/src/main/java/com/juick/api/controllers/Messages.java | 116 |
1 files changed, 69 insertions, 47 deletions
diff --git a/juick-api/src/main/java/com/juick/api/controllers/Messages.java b/juick-api/src/main/java/com/juick/api/controllers/Messages.java index 36882140..414e9ba1 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Messages.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Messages.java @@ -5,19 +5,22 @@ import com.juick.User; import com.juick.api.ApiServer; import com.juick.api.util.HttpBadRequestException; import com.juick.api.util.HttpForbiddenException; -import com.juick.api.util.HttpNotFoundException; import com.juick.server.helpers.Status; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; +import com.juick.util.UserUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.stanza.model.Message; @@ -25,16 +28,25 @@ import rocks.xmpp.core.stanza.model.Message; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import java.security.Principal; +import java.util.Collections; import java.util.List; /** * @author ugnich */ @Controller -@ResponseBody +@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public class Messages { private static final Logger logger = LoggerFactory.getLogger(Messages.class); + private static final ResponseEntity<List<com.juick.Message>> NOT_FOUND = ResponseEntity + .status(HttpStatus.NOT_FOUND) + .body(Collections.emptyList()); + + private static final ResponseEntity<List<com.juick.Message>> FORBIDDEN = ResponseEntity + .status(HttpStatus.FORBIDDEN) + .body(Collections.emptyList()); + @Inject private MessagesService messagesService; @Inject @@ -42,39 +54,48 @@ public class Messages { @Inject private TagService tagService; @Inject - ApiServer apiServer; + private ApiServer apiServer; // TODO: serialize image urls - @RequestMapping(value = "/home", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public List<com.juick.Message> doGetHome(HttpServletRequest request, Principal principal) { - String name = principal.getName(); + @RequestMapping("/home") + public ResponseEntity<List<com.juick.Message>> getHome( + @RequestParam(defaultValue = "0") int before_mid, + Principal principal) { + String name = UserUtils.getUsername(principal, null); User visitor = userService.getUserByName(name); - int vuid = visitor.getUid(); - int before_mid = NumberUtils.toInt(request.getParameter("before_mid"), 0); - List<Integer> mids = messagesService.getMyFeed(vuid, before_mid); - return messagesService.getMessages(mids); + if (visitor != null) { + int vuid = visitor.getUid(); + List<Integer> mids = messagesService.getMyFeed(vuid, before_mid); + + if (!mids.isEmpty()) + return ResponseEntity.ok(messagesService.getMessages(mids)); + + return NOT_FOUND; + } + return FORBIDDEN; } - @RequestMapping(value = "/messages", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public List<com.juick.Message> doGet(HttpServletRequest request) { + @RequestMapping("/messages") + public ResponseEntity<List<com.juick.Message>> getMessages( + HttpServletRequest request, + @RequestParam(required = false) String uname, + @RequestParam(defaultValue = "0") int before_mid, + @RequestParam(required = false) String popular, + @RequestParam(required = false) String media, + @RequestParam(required = false) String tag) { // TODO: use spring-security String auth = request.getHeader("Authorization"); int vuid = userService.getUIDByHttpAuth(auth); - if (vuid == -1) { - throw new HttpForbiddenException(); - } + if (vuid == -1) + return FORBIDDEN; + if (vuid == 0) { String hash = request.getParameter("hash"); - if (hash != null && hash.length() == 16) { - vuid = userService.getUIDbyHash(hash); - } + if (hash != null && hash.length() == 16) + vuid = userService.getUIDbyHash(hash); } - int before_mid = NumberUtils.toInt(request.getParameter("before_mid"), 0); - String uname = request.getParameter("uname"); - String popular = request.getParameter("popular"); - String media = request.getParameter("media"); - String tag = request.getParameter("tag"); + List<Integer> mids; if (!StringUtils.isEmpty(uname)) { User user = userService.getUserByName(uname); @@ -86,13 +107,13 @@ public class Messages { if (tagObject != null) { mids = messagesService.getUserTag(user.getUid(), tagObject.TID, 0, before_mid); } else { - throw new HttpNotFoundException(); + return NOT_FOUND; } } else { mids = messagesService.getUserBlog(user.getUid(), 0, before_mid); } } else { - throw new HttpNotFoundException(); + return NOT_FOUND; } } else { if (!StringUtils.isEmpty(popular)) { @@ -104,59 +125,60 @@ public class Messages { if (tagObject != null) { mids = messagesService.getTag(tagObject.TID, vuid, before_mid, 20); } else { - throw new HttpNotFoundException(); + return NOT_FOUND; } } else { mids = messagesService.getAll(vuid, before_mid); } } - return messagesService.getMessages(mids); + return ResponseEntity.ok(messagesService.getMessages(mids)); } - @RequestMapping(value = "/thread", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public List<com.juick.Message> doThreadGet(HttpServletRequest request) { + @RequestMapping("/thread") + public ResponseEntity<List<com.juick.Message>> getThread( + HttpServletRequest request) { // TODO: use spring-security String auth = request.getHeader("Authorization"); int vuid = userService.getUIDByHttpAuth(auth); if (vuid == -1) { - throw new HttpForbiddenException(); + return FORBIDDEN; } if (vuid == 0) { String hash = request.getParameter("hash"); if (hash != null && hash.length() == 16) { - vuid = userService.getUIDbyHash(hash); + vuid = userService.getUIDbyHash(hash); } } int mid = NumberUtils.toInt(request.getParameter("mid"), 0); com.juick.Message msg = messagesService.getMessage(mid); if (msg != null) { if (!messagesService.canViewThread(mid, vuid)) { - throw new HttpForbiddenException(); + return FORBIDDEN; } else { List<com.juick.Message> replies = messagesService.getReplies(mid); replies.add(0, msg); - return replies; + return ResponseEntity.ok(replies); } } - throw new HttpNotFoundException(); + return NOT_FOUND; } - @RequestMapping(value = "/messages/recommended", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public List<com.juick.Message> doGetRecommended(HttpServletRequest request) { + @RequestMapping("/messages/recommended") + public ResponseEntity<List<com.juick.Message>> doGetRecommended(HttpServletRequest request) { // TODO: use spring-security String auth = request.getHeader("Authorization"); int vuid = userService.getUIDByHttpAuth(auth); if (vuid == -1) { - throw new HttpForbiddenException(); + return FORBIDDEN; } if (vuid == 0) { String hash = request.getParameter("hash"); if (hash != null && hash.length() == 16) { - vuid = userService.getUIDbyHash(hash); + vuid = userService.getUIDbyHash(hash); } } if (vuid == 0) { - throw new HttpForbiddenException(); + return FORBIDDEN; } int before_mid = NumberUtils.toInt(request.getParameter("before_mid"), 0); @@ -164,15 +186,15 @@ public class Messages { if (mids != null && !mids.isEmpty()) { List<com.juick.Message> msgs = messagesService.getMessages(mids); if (msgs != null && !msgs.isEmpty()) { - return msgs; + return ResponseEntity.ok(msgs); } else { - throw new HttpForbiddenException(); + return FORBIDDEN; } } - throw new HttpNotFoundException(); + return NOT_FOUND; } - @RequestMapping(value = "/messages/set_privacy", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping("/messages/set_privacy") @ResponseBody public Status doSetPrivacy(HttpServletRequest request) { // TODO: use spring-security @@ -184,7 +206,7 @@ public class Messages { if (vuid == 0) { String hash = request.getParameter("hash"); if (hash != null && hash.length() == 16) { - vuid = userService.getUIDbyHash(hash); + vuid = userService.getUIDbyHash(hash); } } if (vuid == 0) { @@ -193,13 +215,13 @@ public class Messages { int mid = NumberUtils.toInt(request.getParameter("mid"), 0); com.juick.User user = messagesService.getMessageAuthor(mid); if (user != null && user.getUid() == vuid && messagesService.setMessagePrivacy(mid)) { - return new Status("ok"); + return Status.OK; } else { throw new HttpBadRequestException(); } } - @RequestMapping(value = "/messages/set_popular", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping("/messages/set_popular") public Status doSetPopular(HttpServletRequest request) { int mid = NumberUtils.toInt(request.getParameter("mid"), 0); int popular = NumberUtils.toInt(request.getParameter("popular"), 0); @@ -230,7 +252,7 @@ public class Messages { logger.error("SETPOPULAR ERROR", e); } } - return new Status("ok"); + return Status.OK; } throw new HttpBadRequestException(); } |