From aa9240e5431c5ee81f3d25d6481c66c445d11711 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 31 Dec 2022 02:20:29 +0300 Subject: Remove roster chats --- src/main/java/com/juick/XMPPManager.java | 100 ++++++--------------- src/main/java/com/juick/config/XMPPConfig.java | 4 +- src/main/java/com/juick/service/ChatService.java | 6 -- .../java/com/juick/service/ChatServiceImpl.java | 26 +----- .../db/migration/V1.28__drop_pm_inroster.sql | 1 + 5 files changed, 28 insertions(+), 109 deletions(-) create mode 100644 src/main/resources/db/migration/V1.28__drop_pm_inroster.sql diff --git a/src/main/java/com/juick/XMPPManager.java b/src/main/java/com/juick/XMPPManager.java index 9bd3b7b5..20b8259b 100644 --- a/src/main/java/com/juick/XMPPManager.java +++ b/src/main/java/com/juick/XMPPManager.java @@ -29,7 +29,6 @@ import com.juick.service.component.SystemEvent; import com.juick.util.MessageUtils; import com.juick.util.formatters.PlainTextFormatter; import com.juick.util.xmpp.iq.MessageQuery; -import com.juick.www.WebApp; import com.juick.www.api.SystemActivity; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FilenameUtils; @@ -80,24 +79,23 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; +import java.util.function.BiFunction; /** * @author ugnich */ public class XMPPManager implements NotificationListener { private static final Logger logger = LoggerFactory.getLogger("XMPP"); - private ExternalComponent xmpp; + private final ExternalComponent xmpp; private final CommandsManager commandsManager; private final MessagesService messagesService; private final UserService userService; private final ChatService chatService; - 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, - ChatService chatService, Executor applicationTaskExecutor, WebApp webApp) { + ChatService chatService, Executor applicationTaskExecutor) { jid = Jid.of(botJid); this.commandsManager = commandsManager; this.messagesService = messagesService; @@ -135,7 +133,6 @@ 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); @@ -148,29 +145,11 @@ public class XMPPManager implements NotificationListener { 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())) { + if (isValidTarget(iq.getTo(), true)) { return iq.createResult(vCard); } - User user = userService.getUserByName(iq.getTo().getLocal()); - if (!user.isAnonymous()) { - User info = userService.getUserInfo(user); - VCard userVCard = new VCard(); - userVCard.setFormattedName(info.getFullName()); - userVCard.setNickname(user.getName()); - try { - userVCard.setPhoto(new VCard.Image(URI.create(webApp.getAvatarUrl(user)))); - if (info.getUrl() != null) { - userVCard.setUrl(new URL(info.getUrl())); - } - } catch (MalformedURLException e) { - logger.warn("url exception", e); - } - return iq.createResult(userVCard); - } return iq.createError(Condition.BAD_REQUEST); } }); @@ -197,9 +176,9 @@ public class XMPPManager implements NotificationListener { logger.info("transfer completed"); try { Jid initiator = e.getInitiator(); - ClientMessage result = incomingMessageJuick( + ClientMessage result = handleCommand( userService.getUserByJID(initiator.asBareJid().toEscapedString()), initiator, - jid.getLocal(), StringUtils.defaultString(e.getDescription()).trim(), URI.create(String.format("juick://%s", targetFilename))); + StringUtils.defaultString(e.getDescription()).trim(), URI.create(String.format("juick://%s", targetFilename))); if (result != null) { xmpp.send(result); } @@ -253,6 +232,15 @@ public class XMPPManager implements NotificationListener { }); } + private boolean isValidTarget (Jid from, boolean allowDomain) { + var isBotJid = from.equals(jid) || from.asBareJid().equals(jid.asBareJid()); + var isDomainJid = from.asBareJid().toEscapedString().equals(jid.getDomain()); + if (allowDomain) { + return isBotJid || isDomainJid; + } + return isBotJid; + }; + private void sendJuickMessage(com.juick.model.Message jmsg, List users) { List jids = new ArrayList<>(); @@ -342,14 +330,8 @@ public class XMPPManager implements NotificationListener { Message mm = new Message(); mm.setTo(Jid.of(userJid)); mm.setType(Message.Type.CHAT); - boolean inroster = chatService.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()); - } + mm.setFrom(jid); + mm.setBody("Private message from @" + msg.getUser().getName() + ":\n" + msg.getText()); xmpp.send(ClientMessage.from(mm)); }); } else if (MessageUtils.isReply(msg)) { @@ -442,9 +424,7 @@ public class XMPPManager implements NotificationListener { } private void incomingPresence(Presence p) { - final String username = p.getTo().getLocal(); - final boolean toJuick = username.equals(jid.getLocal()); - + final boolean toJuick = isValidTarget(p.getTo(), false); if (p.getType() == null) { Presence reply = new Presence(); reply.setFrom(p.getTo().asBareJid()); @@ -452,17 +432,13 @@ public class XMPPManager implements NotificationListener { reply.setType(Presence.Type.UNSUBSCRIBE); xmpp.send(ClientPresence.from(reply)); } else if (p.getType().equals(Presence.Type.PROBE)) { - int uid_to = 0; - if (!toJuick) { - uid_to = userService.getUIDbyName(username); - } else { + if (toJuick) { User visitor = userService.getUserByJID(p.getFrom().asBareJid().toEscapedString()); if (visitor != null) { userService.updateLastSeen(visitor); } } - - if (toJuick || uid_to > 0) { + if (toJuick) { Presence reply = new Presence(); reply.setFrom(p.getTo().withResource(jid.getResource())); reply.setTo(p.getFrom()); @@ -481,17 +457,7 @@ public class XMPPManager implements NotificationListener { xmpp.send(ClientPresence.from(reply)); } } else if (p.getType().equals(Presence.Type.SUBSCRIBE)) { - boolean canSubscribe = false; if (toJuick) { - canSubscribe = true; - } else { - int uid_to = userService.getUIDbyName(username); - if (uid_to > 0) { - chatService.addPMinRoster(uid_to, p.getFrom().asBareJid().toEscapedString()); - canSubscribe = true; - } - } - if (canSubscribe) { Presence reply = new Presence(); reply.setFrom(p.getTo()); reply.setTo(p.getFrom()); @@ -512,13 +478,6 @@ public class XMPPManager implements NotificationListener { xmpp.send(ClientPresence.from(reply)); } } else if (p.getType().equals(Presence.Type.UNSUBSCRIBE)) { - if (!toJuick) { - int uid_to = userService.getUIDbyName(username); - if (uid_to > 0) { - chatService.removePMinRoster(uid_to, p.getFrom().asBareJid().toEscapedString()); - } - } - Presence reply = new Presence(); reply.setFrom(p.getTo()); reply.setTo(p.getFrom()); @@ -547,11 +506,10 @@ public class XMPPManager implements NotificationListener { return null; } Jid to = msg.getTo(); - if (to.getDomain().equals(xmpp.getDomain().toEscapedString()) || to.equals(this.jid)) { + if (isValidTarget(to, false)) { User user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString()); - if ((user_from == null || user_from.isAnonymous()) && !msg.getFrom().equals(jid)) { - String signuphash = userService.getSignUpHashByJID(msg.getFrom().asBareJid().toEscapedString()); - return makeReply(msg.getFrom(), "Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."); + if ((user_from == null || user_from.isAnonymous())) { + return makeReply(msg.getFrom(), "XMPP registrations are disabled"); } URI attachment = URI.create(StringUtils.EMPTY); OobX oobX = msg.getExtension(OobX.class); @@ -559,7 +517,7 @@ public class XMPPManager implements NotificationListener { attachment = oobX.getUri(); } try { - return incomingMessageJuick(user_from, msg.getFrom(), msg.getTo().getLocal(), StringUtils.defaultString(msg.getBody()).trim(), attachment); + return handleCommand(user_from, msg.getFrom(), StringUtils.defaultString(msg.getBody()).trim(), attachment); } catch (Exception e1) { logger.warn("message exception", e1); } @@ -569,7 +527,7 @@ public class XMPPManager implements NotificationListener { return errorMessage; } - private ClientMessage incomingMessageJuick(User user_from, Jid from, String to, String command, @Nonnull URI attachment) { + private ClientMessage handleCommand(User user_from, Jid from, String command, @Nonnull URI attachment) { if (StringUtils.isBlank(command) && attachment.toString().isEmpty()) { return null; } @@ -583,14 +541,6 @@ public class XMPPManager implements NotificationListener { command = command.substring(3); } - if (!jid.getLocal().equals(to)) { - // PM - if (!StringUtils.isEmpty(command)) { - commandsManager.commandPM(user_from, null, to, command); - return null; - } - } - try { CommandResult result = commandsManager.processCommand(user_from, command, attachment); if (StringUtils.isNotBlank(result.getText())) { diff --git a/src/main/java/com/juick/config/XMPPConfig.java b/src/main/java/com/juick/config/XMPPConfig.java index 5593b228..76ccb4a0 100644 --- a/src/main/java/com/juick/config/XMPPConfig.java +++ b/src/main/java/com/juick/config/XMPPConfig.java @@ -60,13 +60,11 @@ public class XMPPConfig { private ChatService chatService; @Inject private Executor applicationTaskExecutor; - @Inject - private WebApp webApp; @Bean(destroyMethod = "close") XMPPManager xmppConnection() { return new XMPPManager(botJid, componentName, componentPort, componentHost, password, vCardImage, commandsManager, storageService, messagesService, userService, chatService, - applicationTaskExecutor, webApp); + applicationTaskExecutor); } } diff --git a/src/main/java/com/juick/service/ChatService.java b/src/main/java/com/juick/service/ChatService.java index b09e93b0..4c0ed214 100644 --- a/src/main/java/com/juick/service/ChatService.java +++ b/src/main/java/com/juick/service/ChatService.java @@ -29,12 +29,6 @@ import java.util.List; public interface ChatService { boolean createMessage(int uidFrom, int uid_to, String body); - boolean addPMinRoster(int uid, String jid); - - boolean removePMinRoster(int uid, String jid); - - boolean havePMinRoster(int uid, String jid); - List getLastChats(User user); List getChat(int uid, int uidTo); diff --git a/src/main/java/com/juick/service/ChatServiceImpl.java b/src/main/java/com/juick/service/ChatServiceImpl.java index 2d3fe31a..9409d397 100644 --- a/src/main/java/com/juick/service/ChatServiceImpl.java +++ b/src/main/java/com/juick/service/ChatServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2022, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -41,30 +41,6 @@ public class ChatServiceImpl extends BaseJdbcService implements ChatService { uidFrom, uid_to, body) > 0; } - @Transactional - @Override - public boolean addPMinRoster(final int uid, final String jid) { - return getJdbcTemplate().update( - "INSERT INTO pm_inroster(user_id, jid) VALUES (?, ?) ON DUPLICATE KEY UPDATE user_id=user_id", uid, jid) > 0; - } - - @Transactional - @Override - public boolean removePMinRoster(final int uid, final String jid) { - return getJdbcTemplate().update( - "DELETE FROM pm_inroster WHERE user_id = ? AND jid = ?", uid, jid) > 0; - } - - @Transactional - @Override - public boolean havePMinRoster(final int uid, final String jid) { - List res = getJdbcTemplate().queryForList( - "SELECT 1 FROM pm_inroster WHERE user_id = ? AND jid = ?", - Integer.class, - uid, jid); - return res.size() > 0; - } - @Transactional(readOnly = true) @Override public List getLastChats(final User user) { diff --git a/src/main/resources/db/migration/V1.28__drop_pm_inroster.sql b/src/main/resources/db/migration/V1.28__drop_pm_inroster.sql new file mode 100644 index 00000000..253c57e7 --- /dev/null +++ b/src/main/resources/db/migration/V1.28__drop_pm_inroster.sql @@ -0,0 +1 @@ +DROP TABLE pm_inroster -- cgit v1.2.3