diff options
author | 2017-09-27 15:19:06 +0300 | |
---|---|---|
committer | 2017-09-28 12:54:39 +0300 | |
commit | 751b2a9b0fc523d17c9f7a410fadcc82eaaa1fb7 (patch) | |
tree | 1be25ccef6d38b4ec45084e389aed3045c1cdd70 /juick-api/src/main/java/com/juick/api/ApiServer.java | |
parent | e6e25791d6b9c8360247d92340b0dccda33c86ed (diff) |
api: Telegram posting
Diffstat (limited to 'juick-api/src/main/java/com/juick/api/ApiServer.java')
-rw-r--r-- | juick-api/src/main/java/com/juick/api/ApiServer.java | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/juick-api/src/main/java/com/juick/api/ApiServer.java b/juick-api/src/main/java/com/juick/api/ApiServer.java index 4305387b..a5bf5d38 100644 --- a/juick-api/src/main/java/com/juick/api/ApiServer.java +++ b/juick-api/src/main/java/com/juick/api/ApiServer.java @@ -16,16 +16,23 @@ */ package com.juick.api; +import com.juick.User; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import rocks.xmpp.addr.Jid; import rocks.xmpp.core.XmppException; import rocks.xmpp.core.session.Extension; import rocks.xmpp.core.session.XmppSessionConfiguration; import rocks.xmpp.core.session.debug.LogbackDebugger; +import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.extensions.component.accept.ExternalComponent; +import rocks.xmpp.extensions.oob.model.x.OobX; import javax.annotation.PostConstruct; +import java.net.URI; +import java.net.URISyntaxException; /** * @author Ugnich Anton @@ -56,8 +63,8 @@ public class ApiServer implements AutoCloseable { @Override public void close() { try { - if (getXmpp() != null) - getXmpp().close(); + if (xmpp != null) + xmpp.close(); logger.info("ExternalComponent on juick-api destroyed"); } catch (Exception e) { @@ -70,19 +77,33 @@ public class ApiServer implements AutoCloseable { .debugger(LogbackDebugger.class) .extensions(Extension.of(com.juick.Message.class)) .build(); - setXmpp(ExternalComponent.create(jid, password, configuration, host, port)); + xmpp = ExternalComponent.create(jid, password, configuration, host, port); try { - getXmpp().connect(); + xmpp.connect(); } catch (XmppException e) { logger.warn("xmpp extension", e); } } - public ExternalComponent getXmpp() { - return xmpp; + public void sendMessage(Message message) { + if (!isXmppDisabled) { + xmpp.sendMessage(message); + } } - public void setXmpp(ExternalComponent xmpp) { - this.xmpp = xmpp; + public void processMessage(User visitor, String body, String attachmentName) { + Message xmsg = new Message(); + xmsg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl")); + xmsg.setTo(Jid.of("juick@juick.com/Juick")); + xmsg.setBody(body); + try { + if (StringUtils.isNotEmpty(attachmentName)) { + String attachmentUrl = String.format("juick://%s", attachmentName); + xmsg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); + } + sendMessage(xmsg); + } catch (URISyntaxException e1) { + logger.warn("attachment error", e1); + } } } |