package com.juick.www.controllers; import com.juick.Message; import com.juick.User; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.service.PMQueriesService; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.util.MessageUtils; import com.juick.util.UserUtils; import com.juick.www.WebApp; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import rocks.xmpp.addr.Jid; import javax.inject.Inject; import java.security.Principal; import java.util.List; /** * Created by vitalyster on 09.12.2016. */ @Controller public class PMController { private static final Logger logger = LoggerFactory.getLogger(PMController.class); @Inject PMQueriesService pmQueriesService; @Inject UserService userService; @Inject TagService tagService; @Inject WebApp webApp; @RequestMapping("/pm/inbox") public String doGetInbox(Principal principal, ModelMap context) { String name = UserUtils.getUsername(principal, null); User visitor = userService.getUserByName(name); String title = "PM: Inbox"; List msgs = pmQueriesService.getLastPMInbox(visitor.getUid()); msgs.forEach(m -> m.setText(MessageUtils.formatMessage(m.getText()))); context.put("title", title); context.put("visitor", visitor); context.put("msgs", msgs); context.put("tags", tagService.getPopularTags()); return "views/pm_inbox"; } @RequestMapping(value = "/pm/sent", method = RequestMethod.GET) public String doGetSent( Principal principal, @RequestParam String uname, ModelMap context) { String title = "PM: Sent"; String name = UserUtils.getUsername(principal, null); User visitor = userService.getUserByName(name); List msgs = pmQueriesService.getLastPMSent(visitor.getUid()); if (!UserUtils.checkUserNameValid(uname)) { uname = ""; } context.put("title", title); context.put("visitor", visitor); context.put("msgs", msgs); context.put("tags", tagService.getPopularTags()); context.put("uname", uname); return "views/pm_sent"; } @RequestMapping(value = "/pm/sent", method = RequestMethod.POST) public String doPostPM( Principal principal, @RequestParam String uname, @RequestParam String body, ModelMap context) { String name = UserUtils.getUsername(principal, null); User visitor = userService.getUserByName(name); if (uname.startsWith("@")) { uname = uname.substring(1); } int uid = 0; if (UserUtils.checkUserNameValid(uname)) { uid = userService.getUIDbyName(uname); } if (uid == 0 || body == null || body.length() < 1 || body.length() > 10240) { throw new HttpBadRequestException(); } if (userService.isInBLAny(uid, visitor.getUid())) { throw new HttpForbiddenException(); } if (pmQueriesService.createPM(visitor.getUid(), uid, body)) { if (webApp.getXmpp() != null) { rocks.xmpp.core.stanza.model.Message msg = new rocks.xmpp.core.stanza.model.Message(); msg.setFrom(Jid.of("juick@juick.com")); msg.setTo(Jid.of(String.format("%d@push.juick.com", uid))); com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(visitor); jmsg.setText(body); msg.addExtension(jmsg); webApp.getXmpp().send(msg); msg.setTo(Jid.of(String.format("%d@ws.juick.com", uid))); webApp.getXmpp().send(msg); List jids = userService.getJIDsbyUID(uid); for (String jid : jids) { rocks.xmpp.core.stanza.model.Message mm = new rocks.xmpp.core.stanza.model.Message(); mm.setTo(Jid.of(jid)); mm.setType(rocks.xmpp.core.stanza.model.Message.Type.CHAT); if (pmQueriesService.havePMinRoster(visitor.getUid(), jid)) { mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick")); mm.setBody(body); } else { mm.setFrom(Jid.of("juick", "juick.com", "Juick")); mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + body); } webApp.getXmpp().send(mm); } } else { logger.warn("XMPP unavailable"); } return "redirect:/pm/sent"; } else { throw new HttpBadRequestException(); } } }