aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-04-06 13:21:03 +0300
committerGravatar Vitaly Takmazov2018-04-06 13:21:03 +0300
commitfa698d98e0f24362fe55800ca6a431ada213a78d (patch)
treef55551a09f8bf606b900af820043d25c4ec26197 /juick-common/src
parent7fc4e305e546f30340b8b6a72426723163e91858 (diff)
server: CommandsManager does not depend on Jid
Diffstat (limited to 'juick-common/src')
-rw-r--r--juick-common/src/main/java/com/juick/server/CommandsManager.java73
1 files changed, 35 insertions, 38 deletions
diff --git a/juick-common/src/main/java/com/juick/server/CommandsManager.java b/juick-common/src/main/java/com/juick/server/CommandsManager.java
index 4f115577..3883e677 100644
--- a/juick-common/src/main/java/com/juick/server/CommandsManager.java
+++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java
@@ -38,7 +38,6 @@ import org.apache.commons.lang3.reflect.MethodUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
-import rocks.xmpp.addr.Jid;
import javax.annotation.Nonnull;
import javax.inject.Inject;
@@ -76,7 +75,7 @@ public class CommandsManager {
@Inject
private ApplicationEventPublisher applicationEventPublisher;
- public CommandResult processCommand(User user, Jid from, String input, @Nonnull URI attachment) throws Exception {
+ public CommandResult processCommand(User user, String input, @Nonnull URI attachment) throws Exception {
Optional<Method> cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream()
.filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(),
m.getAnnotation(UserCommand.class).patternFlags()).matcher(input).matches())
@@ -90,8 +89,8 @@ public class CommandsManager {
groups.add(matcher.group(i));
}
}
- CommandResult commandResult = (CommandResult) getClass().getMethod(cmd.get().getName(), User.class, Jid.class, URI.class, String[].class)
- .invoke(this, user, from, attachment, groups.toArray(new String[groups.size()]));
+ CommandResult commandResult = (CommandResult) getClass().getMethod(cmd.get().getName(), User.class, URI.class, String[].class)
+ .invoke(this, user, attachment, groups.toArray(new String[groups.size()]));
if (StringUtils.isNotEmpty(commandResult.getText())) {
return commandResult;
}
@@ -115,14 +114,14 @@ public class CommandsManager {
@UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "PING - returns you a PONG")
- public CommandResult commandPing(User user, Jid from, URI attachment, String[] input) {
+ public CommandResult commandPing(User user, URI attachment, String[] input) {
applicationEventPublisher.publishEvent(new PingEvent(this, user));
return CommandResult.fromString("PONG");
}
@UserCommand(pattern = "^help$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "HELP - returns this help message")
- public CommandResult commandHelp(User user, Jid from, URI attachment, String[] input) {
+ public CommandResult commandHelp(User user, URI attachment, String[] input) {
return CommandResult.fromString(Arrays.stream(getClass().getDeclaredMethods())
.filter(m -> m.isAnnotationPresent(UserCommand.class))
.map(m -> m.getAnnotation(UserCommand.class).help())
@@ -131,11 +130,11 @@ public class CommandsManager {
@UserCommand(pattern = "^login$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "LOGIN - log in to Juick website")
- public CommandResult commandLogin(User user_from, Jid from, URI attachment, String[] input) {
+ public CommandResult commandLogin(User user_from, URI attachment, String[] input) {
return CommandResult.fromString("http://juick.com/login?hash=" + userService.getHashByUID(user_from.getUid()));
}
@UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username")
- public CommandResult commandPM(User user_from, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandPM(User user_from, URI attachment, String... arguments) {
String body = arguments[1];
User user_to = userService.getUserByName(arguments[0]);
@@ -156,7 +155,7 @@ public class CommandsManager {
}
@UserCommand(pattern = "^bl$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "BL - Show your blacklist")
- public CommandResult commandBLShow(User user_from, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandBLShow(User user_from, URI attachment, String... arguments) {
List<User> blusers = userService.getUserBLUsers(user_from.getUid());
List<String> bltags = tagService.getUserBLTags(user_from.getUid());
@@ -183,13 +182,13 @@ public class CommandsManager {
}
@UserCommand(pattern = "^#\\+$", help = "#+ - Show last Juick messages")
- public CommandResult commandLast(User user_from, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandLast(User user_from, URI attachment, String... arguments) {
return CommandResult.fromString("Last messages:\n"
+ printMessages(messagesService.getAll(user_from.getUid(), 0), true));
}
@UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs")
- public CommandResult commandUsers(User user_from, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandUsers(User user_from, URI attachment, String... arguments) {
StringBuilder msg = new StringBuilder();
msg.append("Recommended blogs");
List<String> recommendedUsers = showQueriesService.getRecommendedUsers(user_from);
@@ -213,7 +212,7 @@ public class CommandsManager {
}
@UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE,
help = "BL @username - add @username to your blacklist")
- public CommandResult blacklistUser(User user_from, Jid from, URI attachment, String... arguments) {
+ public CommandResult blacklistUser(User user_from, URI attachment, String... arguments) {
User blUser = userService.getUserByName(arguments[0]);
if (blUser != null) {
PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistUser(user_from, blUser);
@@ -227,7 +226,7 @@ public class CommandsManager {
}
@UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "BL *tag - add *tag to your blacklist")
- public CommandResult blacklistTag(User user_from, Jid from, URI attachment, String... arguments) {
+ public CommandResult blacklistTag(User user_from, URI attachment, String... arguments) {
User blUser = userService.getUserByName(arguments[0]);
if (blUser != null) {
Tag tag = tagService.getTag(arguments[0], false);
@@ -243,7 +242,7 @@ public class CommandsManager {
return CommandResult.fromString("Tag not found");
}
@UserCommand(pattern = "\\*", help = "* - Show your tags")
- public CommandResult commandTags(User currentUser, Jid from, URI attachment, String... args) {
+ public CommandResult commandTags(User currentUser, URI attachment, String... args) {
List<TagStats> tags = tagService.getUserTagStats(currentUser.getUid());
String msg = "Your tags: (tag - messages)\n" +
tags.stream()
@@ -251,7 +250,7 @@ public class CommandsManager {
return CommandResult.fromString(msg);
}
@UserCommand(pattern = "S", help = "S - Show your subscriptions")
- public CommandResult commandSubscriptions(User currentUser, Jid from, URI attachment, String... args) {
+ public CommandResult commandSubscriptions(User currentUser, URI attachment, String... args) {
List<User> friends = userService.getUserFriends(currentUser.getUid());
List<String> tags = subscriptionService.getSubscribedTags(currentUser);
String msg = friends.size() > 0 ? "You are subscribed to users:" + friends.stream().map(u -> "\n@" + u.getName())
@@ -263,7 +262,7 @@ public class CommandsManager {
return CommandResult.fromString(msg);
}
@UserCommand(pattern = "!", help = "! - Show your favorite messages")
- public CommandResult commandFavorites(User currentUser, Jid from, URI attachment, String... args) {
+ public CommandResult commandFavorites(User currentUser, URI attachment, String... args) {
List<Integer> mids = messagesService.getUserRecommendations(currentUser.getUid(), 0);
if (mids.size() > 0) {
return CommandResult.fromString("Favorite messages: \n" + printMessages(mids, false));
@@ -271,7 +270,7 @@ public class CommandsManager {
return CommandResult.fromString("No favorite messages, try to \"like\" something ;)");
}
@UserCommand(pattern = "^\\!\\s+#(\\d+)", help = "! #12345 - recommend message")
- public CommandResult commandRecommend(User user, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandRecommend(User user, URI attachment, String... arguments) {
int mid = NumberUtils.toInt(arguments[0], 0);
if (mid > 0) {
com.juick.Message msg = messagesService.getMessage(mid);
@@ -295,7 +294,7 @@ public class CommandsManager {
// TODO: target notification
@UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @username - subscribe to user" +
"\nU @username - unsubscribe from user", patternFlags = Pattern.CASE_INSENSITIVE)
- public CommandResult commandSubscribeUser(User user, Jid from, URI attachment, String... args) {
+ public CommandResult commandSubscribeUser(User user, URI attachment, String... args) {
boolean subscribe = args[0].equalsIgnoreCase("s");
User toUser = userService.getUserByName(args[1]);
if (subscribe) {
@@ -314,7 +313,7 @@ public class CommandsManager {
}
@UserCommand(pattern = "^(s|u)\\s+\\*(\\S+)$", help = "S *tag - subscribe to tag" +
"\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE)
- public CommandResult commandSubscribeTag(User user, Jid from, URI attachment, String... args) {
+ public CommandResult commandSubscribeTag(User user, URI attachment, String... args) {
boolean subscribe = args[0].equalsIgnoreCase("s");
Tag tag = tagService.getTag(args[1], true);
if (subscribe) {
@@ -331,7 +330,7 @@ public class CommandsManager {
}
@UserCommand(pattern = "^(s|u)\\s+#(\\d+)$", help = "S #1234 - subscribe to comments" +
"\nU #1234 - unsubscribe from comments", patternFlags = Pattern.CASE_INSENSITIVE)
- public CommandResult commandSubscribeMessage(User user, Jid from, URI attachment, String... args) {
+ public CommandResult commandSubscribeMessage(User user, URI attachment, String... args) {
boolean subscribe = args[0].equalsIgnoreCase("s");
int mid = NumberUtils.toInt(args[1], 0);
if (messagesService.getMessage(mid) != null) {
@@ -350,26 +349,24 @@ public class CommandsManager {
}
@UserCommand(pattern = "^(on|off)$", patternFlags = Pattern.CASE_INSENSITIVE,
help = "ON/OFF - Enable/disable subscriptions delivery")
- public CommandResult commandOnOff(User user, Jid from, URI attachment, String[] input) {
+ public CommandResult commandOnOff(User user, URI attachment, String[] input) {
UserService.ActiveStatus newStatus;
String retValUpdated;
if (input[0].toLowerCase().equals("on")) {
newStatus = UserService.ActiveStatus.Active;
- retValUpdated = "Notifications are activated for " + from.asBareJid().toEscapedString();
+ retValUpdated = "XMPP notifications are activated";
} else {
newStatus = UserService.ActiveStatus.Inactive;
- retValUpdated = "Notifications are disabled for " + from.asBareJid().toEscapedString();
+ retValUpdated = "XMPP notifications are disabled";
}
-
- if (userService.setActiveStatusForJID(from.asBareJid().toEscapedString(), newStatus)) {
+ if (userService.getAllJIDs(user).stream().allMatch(jid -> userService.setActiveStatusForJID(jid, newStatus))) {
return CommandResult.fromString(retValUpdated);
- } else {
- return CommandResult.fromString(String.format("Subscriptions status for %s was not changed", from.toEscapedString()));
}
+ return CommandResult.fromString("Error");
}
@UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$",
help = "@username+ - Show user's info and last 20 messages")
- public CommandResult commandUser(User user, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandUser(User user, URI attachment, String... arguments) {
User blogUser = userService.getUserByName(arguments[0]);
int page = arguments[1].length();
if (blogUser.getUid() > 0) {
@@ -380,7 +377,7 @@ public class CommandsManager {
return CommandResult.fromString("User not found");
}
@UserCommand(pattern = "^#(\\d+)(\\+?)$", help = "#1234 - Show message (#1234+ - message with replies)")
- public CommandResult commandShow(User user, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandShow(User user, URI attachment, String... arguments) {
boolean showReplies = arguments[1].length() > 0;
int mid = NumberUtils.toInt(arguments[0], 0);
if (mid == 0) {
@@ -399,7 +396,7 @@ public class CommandsManager {
return CommandResult.fromString("Message not found");
}
@UserCommand(pattern = "^#(\\d+)\\/(\\d+)$", help = "#1234/5 - Show reply")
- public CommandResult commandShowReply(User user, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandShowReply(User user, URI attachment, String... arguments) {
int mid = NumberUtils.toInt(arguments[0], 0);
int rid = NumberUtils.toInt(arguments[1], 0);
com.juick.Message reply = messagesService.getReply(mid, rid);
@@ -409,7 +406,7 @@ public class CommandsManager {
return CommandResult.fromString("Reply not found");
}
@UserCommand(pattern = "^\\*(\\S+)(\\+?)$", help = "*tag - Show last messages with tag")
- public CommandResult commandShowTag(User user, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandShowTag(User user, URI attachment, String... arguments) {
if (StringUtils.isNotEmpty(attachment.toString())) {
// new message with tag
return CommandResult.fromString(StringUtils.EMPTY);
@@ -423,7 +420,7 @@ public class CommandsManager {
return CommandResult.fromString("Tag not found");
}
@UserCommand(pattern = "^D #(\\d+)$", help = "D #1234 - Delete post", patternFlags = Pattern.CASE_INSENSITIVE)
- public CommandResult commandDeletePost(User user, Jid from, URI attachment, String... args) {
+ public CommandResult commandDeletePost(User user, URI attachment, String... args) {
int mid = Integer.valueOf(args[0]);
if (messagesService.deleteMessage(user.getUid(), mid)) {
return CommandResult.fromString("Message deleted");
@@ -431,7 +428,7 @@ public class CommandsManager {
return CommandResult.fromString("This is not your message");
}
@UserCommand(pattern = "^D #(\\d+)(\\.|\\-|\\/)(\\d+)$", help = "D #1234/5 - Delete comment", patternFlags = Pattern.CASE_INSENSITIVE)
- public CommandResult commandDeleteReply(User user, Jid from, URI attachment, String... args) {
+ public CommandResult commandDeleteReply(User user, URI attachment, String... args) {
int mid = Integer.valueOf(args[0]);
int rid = Integer.valueOf(args[2]);
if (messagesService.deleteReply(user.getUid(), mid, rid)) {
@@ -441,19 +438,19 @@ public class CommandsManager {
}
}
@UserCommand(pattern = "^(D L|DL|D LAST)$", help = "D L - Delete last message", patternFlags = Pattern.CASE_INSENSITIVE)
- public CommandResult commandDeleteLast(User user, Jid from, URI attachment, String... args) {
+ public CommandResult commandDeleteLast(User user, URI attachment, String... args) {
return CommandResult.fromString("Temporarily unavailable");
}
@UserCommand(pattern = "^\\?\\s+\\@([a-zA-Z0-9\\-\\.\\@]+)\\s+([\\s\\S]+)$", help = "? @user string - search in user messages")
- public CommandResult commandSearch(User user, Jid from, URI attachment, String... args) {
+ public CommandResult commandSearch(User user, URI attachment, String... args) {
return CommandResult.fromString("Temporarily unavailable");
}
@UserCommand(pattern = "^\\?\\s+([\\s\\S]+)$", help = "? string - search in all messages")
- public CommandResult commandSearchAll(User user, Jid from, URI attachment, String... args) {
+ public CommandResult commandSearchAll(User user, URI attachment, String... args) {
return CommandResult.fromString("Temporarily unavailable");
}
@UserCommand(pattern = "^(#+)$", help = "# - Show last messages from your feed (## - second page, ...)")
- public CommandResult commandMyFeed(User user, Jid from, URI attachment, String... arguments) {
+ public CommandResult commandMyFeed(User user, URI attachment, String... arguments) {
// number of # is the page count
int page = arguments[0].length() - 1;
List<Integer> mids = messagesService.getMyFeed(user.getUid(), page, false);
@@ -464,7 +461,7 @@ public class CommandsManager {
}
@UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)?",
help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message")
- public CommandResult EditOrReply(User user, Jid from, @Nonnull URI attachment, String... args) throws Exception {
+ public CommandResult EditOrReply(User user, @Nonnull URI attachment, String... args) throws Exception {
int mid = NumberUtils.toInt(args[1]);
int rid = NumberUtils.toInt(args[4], 0);
String txt = StringUtils.defaultString(args[5]);