diff options
author | Vitaly Takmazov | 2016-12-19 15:36:13 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-12-19 15:36:13 +0300 |
commit | 75cb488222629a36d20765f5dada5fea1c83b6cf (patch) | |
tree | 97b998b1abdaa9b302c679c61dd0c26ec98d5e20 /juick-xmpp-bot/src/main | |
parent | 17eaf21941db6aef1e40b3ab5912b2d34f231cb1 (diff) |
juick-server: JuickProtocol refactoring
Diffstat (limited to 'juick-xmpp-bot/src/main')
-rw-r--r-- | juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java b/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java index 9f5d076a..234d04f8 100644 --- a/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java +++ b/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java @@ -1,10 +1,11 @@ package com.juick.components; -import com.fasterxml.jackson.core.JsonProcessingException; import com.juick.User; import com.juick.server.helpers.UserInfo; import com.juick.server.protocol.JuickProtocol; +import com.juick.server.protocol.ProtocolListener; import com.juick.server.protocol.ProtocolReply; +import com.juick.service.PMQueriesService; import com.juick.service.UserService; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -24,24 +25,30 @@ import javax.inject.Inject; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URL; +import java.util.List; /** * Created by vt on 12/11/2016. */ -public class XMPPBot implements AutoCloseable { +public class XMPPBot implements AutoCloseable, ProtocolListener { private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class); @Inject UserService userService; @Inject + PMQueriesService pmQueriesService; + @Inject JuickProtocol juickProtocol; + Jid juickJid; + private ExternalComponent component; public XMPPBot(Environment env) { component = ExternalComponent.create(env.getProperty("component_name", "juick.com"), env.getProperty("component_password", "secret"), env.getProperty("component_host", "localhost"), NumberUtils.toInt(env.getProperty("component_port", "5347"), 5347)); - Jid juickJid = Jid.of(env.getProperty("xmppbot_jid", "juick@juick.com/Juick")); + juickJid = Jid.of(env.getProperty("xmppbot_jid", "juick@juick.com/Juick")); + juickProtocol.setListener(this); try { SoftwareVersionManager softwareVersionManager = component.getManager(SoftwareVersionManager.class); softwareVersionManager.setSoftwareVersion(new SoftwareVersion("Juick", "git", System.getProperty("os.name", "generic"))); @@ -79,7 +86,7 @@ public class XMPPBot implements AutoCloseable { replyMessage.setBody(reply.getResult()); replyMessage.setFrom(juickJid); component.send(replyMessage); - } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | JsonProcessingException ex) { + } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException ex) { logger.warn("unhandled error", ex); } } @@ -97,4 +104,37 @@ public class XMPPBot implements AutoCloseable { logger.info("ExternalComponent on xmpp-bot destroyed"); } + + @Override + public void privateMessage(User from, User to, String body) { + List<String> toJids = userService.getJIDsbyUID(to.getUid()); + toJids.forEach(jid -> { + Message mm = new Message(); + mm.setTo(Jid.of(jid)); + mm.setType(Message.Type.CHAT); + boolean haveInRoster = pmQueriesService.havePMinRoster(from.getUid(), jid); + if (haveInRoster) { + mm.setFrom(Jid.of(from.getName(), juickJid.getDomain(), "Juick")); + mm.setBody(body); + } else { + mm.setFrom(Jid.of("juick", juickJid.getDomain(), "Juick")); + mm.setBody("Private message from @" + from.getName() + ":\n" + body); + } + component.send(mm); + }); + } + + @Override + public void userSubscribed(User from, User to) { + String notification = String.format("%s subscribed to your blog", from.getName()); + List<String> toJids = userService.getJIDsbyUID(to.getUid()); + toJids.forEach(jid -> { + Message mm = new Message(); + mm.setTo(Jid.of(jid)); + mm.setType(Message.Type.CHAT); + mm.setFrom(juickJid); + mm.setBody(notification); + component.send(mm); + }); + } } |