From 2c1ea441673d9d6370e2ea419f2d0f9a863cb0d3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 21 Mar 2018 14:49:52 +0300 Subject: xmpp: remaining xmpp commands --- .../src/main/java/com/juick/server/XMPPBot.java | 47 +++++++++++++++++++- .../xmpp/helpers/annotation/UserCommand.java | 50 ++++++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 juick-server/src/main/java/com/juick/server/xmpp/helpers/annotation/UserCommand.java (limited to 'juick-server') 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 b8789276..d8901fbb 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPBot.java +++ b/juick-server/src/main/java/com/juick/server/XMPPBot.java @@ -20,11 +20,13 @@ package com.juick.server; import com.juick.Tag; import com.juick.User; import com.juick.server.component.LikeEvent; +import com.juick.server.component.MessageEvent; import com.juick.server.component.SubscribeEvent; +import com.juick.server.util.TagUtils; import com.juick.server.xmpp.s2s.StanzaListener; import com.juick.formatters.PlainTextFormatter; import com.juick.server.helpers.TagStats; -import com.juick.server.protocol.annotation.UserCommand; +import com.juick.server.xmpp.helpers.annotation.UserCommand; import com.juick.service.*; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -34,6 +36,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.stanza.model.*; @@ -85,11 +88,24 @@ public class XMPPBot implements StanzaListener, AutoCloseable { @Inject private ApplicationEventPublisher applicationEventPublisher; + private ApplicationListener onMessage; + @PostConstruct public void init() { xmpp.addStanzaListener(this); broadcastPresence(null); pt = new PrettyTime(new Locale("ru")); + onMessage = event -> { + com.juick.Message msg = event.getMessage(); + if (msg.getRid() == 0 && msg.getMid() > 0) { + String notify = "New message posted.\n#" + msg.getMid() + " https://juick.com/" + msg.getMid(); + List activeJids = userService.getActiveJIDs(); + userService.getJIDsbyUID(msg.getUser().getUid()) + .stream() + .filter(activeJids::contains) + .forEach(jid -> sendReply(Jid.of(jid), notify)); + } + }; } public Jid getJid() { @@ -322,7 +338,13 @@ public class XMPPBot implements StanzaListener, AutoCloseable { Optional result = processCommand(user_from, msg.getFrom(), command); result.ifPresent(r -> sendReply(msg.getFrom(), r)); - return result.isPresent(); + // new message + List tags = tagService.fromString(command, false); + String body = command.substring(TagUtils.toString(tags).length()); + int mid = messagesService.createMessage(user_from.getUid(), body, null, tags); + subscriptionService.subscribeMessage(mid, user_from.getUid()); + applicationEventPublisher.publishEvent(new MessageEvent(this, messagesService.getMessage(mid))); + return true; } @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE, @@ -552,6 +574,7 @@ public class XMPPBot implements StanzaListener, AutoCloseable { User toUser = userService.getUserByName(args[1]); if (subscribe) { if (subscriptionService.subscribeUser(user, toUser)) { + // TODO: already subscribed case applicationEventPublisher.publishEvent(new SubscribeEvent(this, user, toUser)); return "Subscribed to @" + toUser.getName(); } @@ -699,6 +722,26 @@ public class XMPPBot implements StanzaListener, AutoCloseable { } return "Your feed is empty"; } + @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)", + help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message") + public String EditOrReply(User user, String... args) { + int mid = NumberUtils.toInt(args[1]); + int rid = NumberUtils.toInt(args[4], 0); + String txt = args[5]; + List messageTags = tagService.fromString(txt, true); + if (messageTags.size() > 0) { + if (user.getUid() != messagesService.getMessageAuthor(mid).getUid()) { + return "It is not your message"; + } + tagService.updateTags(mid, messageTags); + return "Tags are updated"; + } else { + int newrid = messagesService.createReply(mid, rid, user.getUid(), txt, null); + applicationEventPublisher.publishEvent(new MessageEvent(this, messagesService.getReply(mid, newrid))); + return "Reply posted.\n#" + mid + "/" + newrid + " " + + "https://juick.com/" + mid + "#" + newrid; + } + } void sendReply(Jid jidTo, String txt) { Message reply = new Message(); diff --git a/juick-server/src/main/java/com/juick/server/xmpp/helpers/annotation/UserCommand.java b/juick-server/src/main/java/com/juick/server/xmpp/helpers/annotation/UserCommand.java new file mode 100644 index 00000000..383383c9 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/xmpp/helpers/annotation/UserCommand.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2008-2017, 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 + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.server.xmpp.helpers.annotation; + +import org.apache.commons.lang3.StringUtils; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Created by oxpa on 22.03.16. + */ +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface UserCommand { + /** + * + * @return a command pattern + */ + String pattern() default StringUtils.EMPTY; + + /** + * + * @return pattern flags + */ + int patternFlags() default 0; + + /** + * + * @return a string used in HELP command output. Basically, only 1 string + */ + String help() default StringUtils.EMPTY; +} -- cgit v1.2.3