package com.juick.api.controllers; import com.juick.Tag; import com.juick.User; import com.juick.api.ApiServer; import com.juick.Status; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.util.UserUtils; 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.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 org.springframework.web.bind.annotation.RestController; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.stanza.model.Message; import javax.inject.Inject; import java.util.Collections; import java.util.List; /** * @author ugnich */ @RestController @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) { User visitor = UserUtils.getCurrentUser(); if (!visitor.isAnonym()) { 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( @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) { User visitor = UserUtils.getCurrentUser(); int vuid = visitor.getUid(); 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( @RequestParam(defaultValue = "0") int mid) { User visitor = UserUtils.getCurrentUser(); int vuid = visitor.getUid(); 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( @RequestParam(defaultValue = "0") int before_mid) { User visitor = UserUtils.getCurrentUser(); int vuid = visitor.getUid(); if (vuid == 0) { return FORBIDDEN; } 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 ResponseEntity doSetPrivacy( @RequestParam(defaultValue = "0") int mid) { User visitor = UserUtils.getCurrentUser(); int vuid = visitor.getUid(); if (vuid == 0) { throw new HttpForbiddenException(); } com.juick.User user = messagesService.getMessageAuthor(mid); if (user != null && user.getUid() == vuid && messagesService.setMessagePrivacy(mid)) { return ResponseEntity.ok(Status.OK); } throw new HttpForbiddenException(); } @RequestMapping("/messages/set_popular") public Status doSetPopular( @RequestParam(defaultValue = "0") int mid, @RequestParam(defaultValue = "0") int popular) { 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(); } }