From 4b3ddcd138af69f504a9a08281edc1c2e7eb0c7a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 28 Mar 2018 21:19:07 +0300 Subject: PM events --- .../src/main/java/com/juick/server/XMPPBot.java | 87 ++++++---------------- .../main/java/com/juick/server/XMPPConnection.java | 2 + .../src/main/java/com/juick/server/api/PM.java | 23 ++---- 3 files changed, 31 insertions(+), 81 deletions(-) (limited to 'juick-server/src/main/java/com') diff --git a/juick-server/src/main/java/com/juick/server/XMPPBot.java b/juick-server/src/main/java/com/juick/server/XMPPBot.java index dda7b6de..5f9f83e6 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPBot.java +++ b/juick-server/src/main/java/com/juick/server/XMPPBot.java @@ -259,31 +259,11 @@ public class XMPPBot implements StanzaListener, AutoCloseable { } if (success) { - Message m = new Message(); - m.setFrom(jid.asBareJid()); com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(user_from); jmsg.setTo(userService.getUserByUID(uid_to).get()); jmsg.setText(msg.getBody()); - m.addExtension(jmsg); - - List jids; - boolean inroster = false; - jids = userService.getJIDsbyUID(uid_to); - for (String userJid : jids) { - Message mm = new Message(); - mm.setTo(Jid.of(userJid)); - mm.setType(Message.Type.CHAT); - inroster = pmQueriesService.havePMinRoster(user_from.getUid(), userJid); - if (inroster) { - mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick")); - mm.setBody(msg.getBody()); - } else { - mm.setFrom(jid); - mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + msg.getBody()); - } - xmpp.sendOut(ClientMessage.from(mm)); - } + applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg)); } else { Message reply = new Message(); reply.setFrom(msg.getTo()); @@ -347,6 +327,22 @@ public class XMPPBot implements StanzaListener, AutoCloseable { String notify = "New message posted.\n#" + msg.getMid() + " https://juick.com/" + msg.getMid(); userService.getJIDsbyUID(msg.getUser().getUid()) .forEach(jid -> sendReply(Jid.of(jid), notify)); + } else if (msg.getTo() != null) { + userService.getJIDsbyUID(msg.getTo().getUid()) + .forEach(userJid -> { + Message mm = new Message(); + mm.setTo(Jid.of(userJid)); + mm.setType(Message.Type.CHAT); + boolean inroster = pmQueriesService.havePMinRoster(msg.getUser().getUid(), userJid); + if (inroster) { + mm.setFrom(Jid.of(msg.getUser().getName(), "juick.com", "Juick")); + mm.setBody(msg.getText()); + } else { + mm.setFrom(jid); + mm.setBody("Private message from @" + msg.getUser().getName() + ":\n" + msg.getText()); + } + xmpp.sendOut(ClientMessage.from(mm)); + }); } } @@ -377,57 +373,22 @@ public class XMPPBot implements StanzaListener, AutoCloseable { @UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username") public String commandPM(User user_from, Jid from, String... arguments) { String body = arguments[1]; - int ret = 0; User user_to = userService.getUserByName(arguments[0]); - List jids_to = null; - boolean haveInRoster = false; if (user_to.getUid() > 0) { if (!userService.isInBLAny(user_to.getUid(), user_from.getUid())) { if (pmQueriesService.createPM(user_from.getUid(), user_to.getUid(), body)) { - jids_to = userService.getJIDsbyUID(user_to.getUid()); - ret = 200; - } else { - ret = 500; + com.juick.Message jmsg = new com.juick.Message(); + jmsg.setUser(user_from); + jmsg.setTo(user_to); + jmsg.setText(body); + applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg)); + return "Private message sent"; } - } else { - ret = 403; } - } else { - ret = 404; - } - - if (ret == 200) { - Message msg = new Message(); - msg.setFrom(jid.asBareJid()); - com.juick.Message jmsg = new com.juick.Message(); - jmsg.setUser(user_from); - jmsg.setTo(user_to); - jmsg.setText(body); - msg.addExtension(jmsg); - - for (String userJid : jids_to) { - Message mm = new Message(); - mm.setTo(Jid.of(userJid)); - mm.setType(Message.Type.CHAT); - haveInRoster = pmQueriesService.havePMinRoster(user_from.getUid(), userJid); - if (haveInRoster) { - mm.setFrom(Jid.of(user_from.getName(), "juick.com", "Juick")); - mm.setBody(body); - } else { - mm.setFrom(jid); - mm.setBody("Private message from @" + user_from.getName() + ":\n" + body); - } - xmpp.sendOut(ClientMessage.from(mm)); - } - } - - if (ret == 200) { - return "Private message sent"; - } else { - return "Error " + ret; } + return "Error"; } @UserCommand(pattern = "^bl$", patternFlags = Pattern.CASE_INSENSITIVE, help = "BL - Show your blacklist") diff --git a/juick-server/src/main/java/com/juick/server/XMPPConnection.java b/juick-server/src/main/java/com/juick/server/XMPPConnection.java index 25c77d74..2176317f 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPConnection.java +++ b/juick-server/src/main/java/com/juick/server/XMPPConnection.java @@ -170,6 +170,8 @@ public class XMPPConnection implements AutoCloseable { User fromUser = jmsg.getUser(); com.juick.Message msg = messagesService.getMessage(jmsg.getMid()); applicationEventPublisher.publishEvent(new LikeEvent(this, fromUser, msg)); + } else if (jid.getLocal().equals("pm")) { + applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg)); } else { if (jmsg.getRid() > 0) { // to get quote and attachment diff --git a/juick-server/src/main/java/com/juick/server/api/PM.java b/juick-server/src/main/java/com/juick/server/api/PM.java index 6797c7fd..357b68dc 100644 --- a/juick-server/src/main/java/com/juick/server/api/PM.java +++ b/juick-server/src/main/java/com/juick/server/api/PM.java @@ -19,11 +19,13 @@ package com.juick.server.api; import com.juick.User; import com.juick.server.ServerManager; +import com.juick.server.component.MessageEvent; import com.juick.server.helpers.AnonymousUser; import com.juick.server.helpers.PrivateChats; import com.juick.server.util.*; import com.juick.service.PMQueriesService; import com.juick.service.UserService; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -46,6 +48,8 @@ public class PM { private PMQueriesService pmQueriesService; @Inject private ServerManager serverManager; + @Inject + private ApplicationEventPublisher applicationEventPublisher; @RequestMapping(value = "/pm", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public List doGetPM( @@ -90,28 +94,11 @@ public class PM { } if (pmQueriesService.createPM(vuid, userTo.getUid(), body)) { - Message msg = new Message(); - msg.setFrom(Jid.of("juick@juick.com")); com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(visitor); jmsg.setText(body); jmsg.setTo(userTo); - msg.addExtension(jmsg); - - List jids = userService.getJIDsbyUID(userTo.getUid()); - for (String jid : jids) { - Message mm = new Message(); - mm.setTo(Jid.of(jid)); - mm.setType(Message.Type.CHAT); - if (pmQueriesService.havePMinRoster(vuid, 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); - } - serverManager.sendMessage(mm); - } + applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg)); return jmsg; } -- cgit v1.2.3