From 47bdad85f7ac2b12daf1da4e340be902a568d976 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 31 Dec 2022 01:23:28 +0300 Subject: Refactor XMPP connection --- src/main/java/com/juick/XMPPManager.java | 65 +++++++++++--------------- src/main/java/com/juick/config/XMPPConfig.java | 42 ++++++++++++++++- 2 files changed, 67 insertions(+), 40 deletions(-) (limited to 'src/main/java/com/juick') diff --git a/src/main/java/com/juick/XMPPManager.java b/src/main/java/com/juick/XMPPManager.java index 624fe73a..1080b1b9 100644 --- a/src/main/java/com/juick/XMPPManager.java +++ b/src/main/java/com/juick/XMPPManager.java @@ -91,38 +91,22 @@ import java.util.concurrent.Executor; public class XMPPManager implements NotificationListener { private static final Logger logger = LoggerFactory.getLogger("XMPP"); private ExternalComponent xmpp; - @Value("${xmppbot_jid:juick@localhost}") - private String botJid; - @Value("${hostname:localhost}") - private String componentName; - @Value("${component_port:5347}") - private int componentPort; - @Value("${component_host:localhost}") - private String componentHost; - @Inject - private CommandsManager commandsManager; - @Value("${xmpp_password:secret}") - private String password; - @Value("classpath:juick.png") - private Resource vCardImage; - @Inject - private StorageService storageService; - @Inject - private MessagesService messagesService; - @Inject - private UserService userService; - @Inject - private PMQueriesService pmQueriesService; - @Inject - private Executor applicationTaskExecutor; - @Inject - private WebApp webApp; - - private Jid jid; - - @PostConstruct - public void init() { + private final CommandsManager commandsManager; + private final MessagesService messagesService; + private final UserService userService; + private final PMQueriesService pmQueriesService; + + private final Jid jid; + + public XMPPManager(String botJid, String componentName, int componentPort, String componentHost, + String password, Resource vCardImage, CommandsManager commandsManager, + StorageService storageService, MessagesService messagesService, UserService userService, + PMQueriesService pmQueriesService, Executor applicationTaskExecutor, WebApp webApp) { jid = Jid.of(botJid); + this.commandsManager = commandsManager; + this.messagesService = messagesService; + this.userService = userService; + this.pmQueriesService = pmQueriesService; logger.info("xmpp component start connecting to {}", componentPort); XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() .extensions(Extension.of(com.juick.model.Message.class), Extension.of(MessageQuery.class)) @@ -155,6 +139,7 @@ public class XMPPManager implements NotificationListener { public java.lang.Class getPayloadClass() { return MessageQuery.class; } + public IQ handleRequest(IQ iq) { Message warningMessage = new Message(iq.getFrom(), Message.Type.CHAT); warningMessage.setFrom(jid); @@ -164,11 +149,12 @@ public class XMPPManager implements NotificationListener { } }); xmpp.addIQHandler(new IQHandler() { - public Class getPayloadClass() { - return VCard.class; - } - @Override - public IQ handleRequest(IQ iq) { + public Class getPayloadClass() { + return VCard.class; + } + + @Override + public IQ handleRequest(IQ iq) { if (iq.getTo().equals(jid) || iq.getTo().asBareJid().equals(jid.asBareJid()) || iq.getTo().asBareJid().toEscapedString().equals(jid.getDomain())) { return iq.createResult(vCard); @@ -349,6 +335,7 @@ public class XMPPManager implements NotificationListener { processLike(activity.getFrom(), activity.getMessage(), activity.getTo()); } } + private void processMessage(com.juick.model.Message msg, List subscribers) { if (msg.isService()) { return; @@ -371,8 +358,7 @@ public class XMPPManager implements NotificationListener { }); } else if (MessageUtils.isReply(msg)) { sendJuickComment(msg, subscribers); - } - else { + } else { sendJuickMessage(msg, subscribers); } } @@ -484,7 +470,7 @@ public class XMPPManager implements NotificationListener { Presence reply = new Presence(); reply.setFrom(p.getTo().withResource(jid.getResource())); reply.setTo(p.getFrom()); - reply.setPriority((byte)10); + reply.setPriority((byte) 10); if (!userService.getActiveJIDs().contains(p.getFrom().asBareJid().toEscapedString())) { reply.setStatus("Send ON to enable notifications"); } @@ -586,6 +572,7 @@ public class XMPPManager implements NotificationListener { errorMessage.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND)); return errorMessage; } + private ClientMessage incomingMessageJuick(User user_from, Jid from, String to, String command, @Nonnull URI attachment) { if (StringUtils.isBlank(command) && attachment.toString().isEmpty()) { return null; diff --git a/src/main/java/com/juick/config/XMPPConfig.java b/src/main/java/com/juick/config/XMPPConfig.java index ee2abab4..73077760 100644 --- a/src/main/java/com/juick/config/XMPPConfig.java +++ b/src/main/java/com/juick/config/XMPPConfig.java @@ -17,16 +17,56 @@ package com.juick.config; +import com.juick.CommandsManager; import com.juick.XMPPManager; +import com.juick.service.MessagesService; +import com.juick.service.PMQueriesService; +import com.juick.service.StorageService; +import com.juick.service.UserService; +import com.juick.www.WebApp; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; + +import javax.inject.Inject; +import java.util.concurrent.Executor; @Configuration @ConditionalOnProperty("xmppbot_jid") public class XMPPConfig { + @Value("${xmppbot_jid:juick@localhost}") + private String botJid; + @Value("${hostname:localhost}") + private String componentName; + @Value("${component_port:5347}") + private int componentPort; + @Value("${component_host:localhost}") + private String componentHost; + @Value("${xmpp_password:secret}") + private String password; + @Value("classpath:juick.png") + private Resource vCardImage; + @Inject + private CommandsManager commandsManager; + @Inject + private StorageService storageService; + @Inject + private MessagesService messagesService; + @Inject + private UserService userService; + @Inject + private PMQueriesService pmQueriesService; + @Inject + private Executor applicationTaskExecutor; + @Inject + private WebApp webApp; + @Bean(destroyMethod = "close") XMPPManager xmppConnection() { - return new XMPPManager(); + return new XMPPManager(botJid, componentName, componentPort, componentHost, password, vCardImage, + commandsManager, storageService, messagesService, userService, pmQueriesService, + applicationTaskExecutor, webApp); } } -- cgit v1.2.3