aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-xmpp/src/main/java')
-rw-r--r--juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java34
1 files changed, 34 insertions, 0 deletions
diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java b/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java
index 986eaed0..71711dce 100644
--- a/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java
+++ b/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java
@@ -3,6 +3,7 @@ package com.juick.components.s2s;
import com.juick.User;
import com.juick.components.XMPPServer;
import org.apache.commons.lang3.StringUtils;
+import org.ocpsoft.prettytime.PrettyTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rocks.xmpp.addr.Jid;
@@ -15,9 +16,12 @@ import rocks.xmpp.core.stanza.model.client.ClientPresence;
import rocks.xmpp.core.stanza.model.errors.Condition;
import javax.inject.Inject;
+import java.util.Collections;
import java.util.List;
+import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import java.util.stream.Collectors;
/**
*
@@ -28,6 +32,7 @@ public class JuickBot implements StanzaListener, AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(JuickBot.class);
XMPPServer xmpp;
+ PrettyTime pt;
@Inject
public JuickBot(XMPPServer xmpp) {
@@ -36,6 +41,7 @@ public class JuickBot implements StanzaListener, AutoCloseable {
if (!xmpp.messagesService.isReadonly()) {
broadcastPresence(null);
}
+ pt = new PrettyTime(new Locale("ru"));
}
private static final String HELPTEXT =
@@ -289,6 +295,8 @@ public class JuickBot implements StanzaListener, AutoCloseable {
} else if (commandlen == 2 && command.equalsIgnoreCase("BL")) {
commandBLShow(msg, user_from);
return true;
+ } else if (commandlen == 2 && command.equalsIgnoreCase("#+")) {
+ return commandLast(msg.getFrom());
} else if (xmpp.messagesService.isReadonly()) {
return commandMaintenance(msg.getFrom());
}
@@ -429,6 +437,17 @@ public class JuickBot implements StanzaListener, AutoCloseable {
xmpp.sendOut(ClientMessage.from(reply));
}
+ boolean commandLast(Jid jidFrom) {
+ User user = xmpp.userService.getUserByJID(jidFrom.asBareJid().toEscapedString());
+ Message reply = new Message();
+ reply.setFrom(xmpp.getJid());
+ reply.setTo(jidFrom);
+ reply.setType(Message.Type.CHAT);
+ reply.setBody("Last messages: " + printMessages(xmpp.messagesService.getAll(user.getUid(), 0), true));
+ xmpp.sendOut(ClientMessage.from(reply));
+ return true;
+ }
+
private boolean commandMaintenance(Jid jidFrom) {
Message reply = new Message();
reply.setFrom(xmpp.getJid());
@@ -454,6 +473,21 @@ public class JuickBot implements StanzaListener, AutoCloseable {
}
}
+ String printMessages(List<Integer> mids, boolean crop) {
+ int cropLength = 384;
+ return xmpp.messagesService.getMessages(mids).stream()
+ .sorted(Collections.reverseOrder())
+ .map(m -> {
+ String timeAgo = pt.format(m.getDate());
+ String repliesCount = m.getReplies() == 1 ? "; 1 reply" : m.getReplies() == 0 ? ""
+ : String.format("; %d replies", m.getReplies());
+ String txt = crop ? m.getText().length() >= cropLength ?
+ StringUtils.substring(m.getText(), 0, cropLength) + " [...]" : m.getText() : m.getText();
+ return String.format("@%s:%s\n%s\n#%d (%s%s) http://juick.com/%d",
+ m.getUser().getName(), m.getTagsString(), txt, m.getMid(), timeAgo, repliesCount, m.getMid());
+ }).collect(Collectors.joining("\n\n"));
+ }
+
void broadcastPresence(Presence.Type type) {
Presence presence = new Presence();
presence.setFrom(xmpp.getJid());