package com.juick.api.controllers; import com.juick.Tag; import com.juick.User; import com.juick.api.ApiServer; import com.juick.api.util.HttpBadRequestException; import com.juick.api.util.HttpForbiddenException; 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; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import java.security.Principal; import java.util.Collections; import java.util.List; /** * @author ugnich */ @Controller @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> NOT_FOUND = ResponseEntity .status(HttpStatus.NOT_FOUND) .body(Collections.emptyList()); private static final ResponseEntity> FORBIDDEN = ResponseEntity .status(HttpStatus.FORBIDDEN) .body(Collections.emptyList()); @Inject private MessagesService messagesService; @Inject private UserService userService; @Inject private TagService tagService; @Inject private ApiServer apiServer; // TODO: serialize image urls @RequestMapping("/home") public ResponseEntity> getHome( @RequestParam(defaultValue = "0") int before_mid, Principal principal) { String name = UserUtils.getUsername(principal, null); User visitor = userService.getUserByName(name); if (visitor != null) { int vuid = visitor.getUid(); List mids = messagesService.getMyFeed(vuid, before_mid); if (!mids.isEmpty()) return ResponseEntity.ok(messagesService.getMessages(mids)); return NOT_FOUND; } return FORBIDDEN; } @RequestMapping("/messages") public ResponseEntity> 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) return FORBIDDEN; if (vuid == 0) { String hash = request.getParameter("hash"); if (hash != null && hash.length() == 16) vuid = userService.getUIDbyHash(hash); } List mids; if (!StringUtils.isEmpty(uname)) { User user = userService.getUserByName(uname); if (user != null) { if (!StringUtils.isEmpty(media)) { mids = messagesService.getUserPhotos(user.getUid(), 0, before_mid); } else if (!StringUtils.isEmpty(tag)) { Tag tagObject = tagService.getTag(tag, false); if (tagObject != null) { mids = messagesService.getUserTag(user.getUid(), tagObject.TID, 0, before_mid); } else { return NOT_FOUND; } } else { mids = messagesService.getUserBlog(user.getUid(), 0, before_mid); } } else { return NOT_FOUND; } } else { if (!StringUtils.isEmpty(popular)) { mids = messagesService.getPopular(vuid, before_mid); } else if (!StringUtils.isEmpty(media)) { mids = messagesService.getPhotos(vuid, before_mid); } else if (!StringUtils.isEmpty(tag)) { Tag tagObject = tagService.getTag(tag, false); if (tagObject != null) { mids = messagesService.getTag(tagObject.TID, vuid, before_mid, 20); } else { return NOT_FOUND; } } else { mids = messagesService.getAll(vuid, before_mid); } } return ResponseEntity.ok(messagesService.getMessages(mids)); } @RequestMapping("/thread") public ResponseEntity> getThread( HttpServletRequest request) { // TODO: use spring-security String auth = request.getHeader("Authorization"); int vuid = userService.getUIDByHttpAuth(auth); if (vuid == -1) { return FORBIDDEN; } if (vuid == 0) { String hash = request.getParameter("hash"); if (hash != null && hash.length() == 16) { 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)) { return FORBIDDEN; } else { List replies = messagesService.getReplies(mid); replies.add(0, msg); return ResponseEntity.ok(replies); } } return NOT_FOUND; } @RequestMapping("/messages/recommended") public ResponseEntity> doGetRecommended(HttpServletRequest request) { // TODO: use spring-security String auth = request.getHeader("Authorization"); int vuid = userService.getUIDByHttpAuth(auth); if (vuid == -1) { return FORBIDDEN; } if (vuid == 0) { String hash = request.getParameter("hash"); if (hash != null && hash.length() == 16) { vuid = userService.getUIDbyHash(hash); } } if (vuid == 0) { return FORBIDDEN; } int before_mid = NumberUtils.toInt(request.getParameter("before_mid"), 0); List mids = messagesService.getUserRecommendations(vuid, before_mid); if (mids != null && !mids.isEmpty()) { List msgs = messagesService.getMessages(mids); if (msgs != null && !msgs.isEmpty()) { return ResponseEntity.ok(msgs); } else { return FORBIDDEN; } } return NOT_FOUND; } @RequestMapping("/messages/set_privacy") @ResponseBody public Status doSetPrivacy(HttpServletRequest request) { // TODO: use spring-security String auth = request.getHeader("Authorization"); int vuid = userService.getUIDByHttpAuth(auth); if (vuid == -1) { throw new HttpForbiddenException(); } if (vuid == 0) { String hash = request.getParameter("hash"); if (hash != null && hash.length() == 16) { vuid = userService.getUIDbyHash(hash); } } if (vuid == 0) { throw new HttpForbiddenException(); } 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 Status.OK; } else { throw new HttpBadRequestException(); } } @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); if (mid > 0) { boolean ret = messagesService.setMessagePopular(mid, popular); if (ret && popular == 2) { try { com.juick.Message m = messagesService.getMessage(mid); if (m != null) { Message msg = new Message(); msg.setFrom(Jid.of("juick@juick.com")); msg.setTo(Jid.of("crosspost.juick.com")); m.setUser(userService.getUserByUID(11574).get()); msg.addExtension(m); msg.setTo(Jid.of("twitter@crosspost.juick.com")); apiServer.getXmpp().send(msg); msg.setTo(Jid.of("fb@crosspost.juick.com")); apiServer.getXmpp().send(msg); msg.setTo(Jid.of("vk@crosspost.juick.com")); apiServer.getXmpp().send(msg); } else { throw new Exception("Message not found"); } } catch (Exception e) { logger.error("SETPOPULAR ERROR", e); } } return Status.OK; } throw new HttpBadRequestException(); } }