diff options
author | Anatoliy Sablin | 2017-11-06 16:27:11 +0300 |
---|---|---|
committer | Anatoliy Sablin | 2017-11-06 16:27:11 +0300 |
commit | d77ff16de5543b5820ee3b185660a052c5f57a15 (patch) | |
tree | ba701d56a441dc7e9caea89bad96929bc3a70d95 /juick-commands/src/main/java | |
parent | 41b885d84958d07ade536850829b3ba394a4f8f9 (diff) |
Added rest commands.
Diffstat (limited to 'juick-commands/src/main/java')
15 files changed, 776 insertions, 8 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/BlacklistTag.java b/juick-commands/src/main/java/com/juick/command/BlacklistTag.java new file mode 100644 index 00000000..2ce455dc --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/BlacklistTag.java @@ -0,0 +1,61 @@ +package com.juick.command; + +import com.juick.Tag; +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.PrivacyQueriesService; +import com.juick.service.TagService; +import com.juick.service.UserService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class BlacklistTag extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^bl\\s\\*(\\S+)$", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + private final TagService tagService; + private final PrivacyQueriesService privacyQueriesService; + + @Autowired + public BlacklistTag(UserService userService, TagService tagService, PrivacyQueriesService privacyQueriesService) { + this.userService = userService; + this.tagService = tagService; + this.privacyQueriesService = privacyQueriesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "BL *tag - add *tag to your blacklist"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + User blUser = getUserService().getUserByName(arguments[0]); + if (blUser != null) { + Tag tag = getTagService().getTag(arguments[0], false); + if (tag != null) { + PrivacyQueriesService.PrivacyResult result = getPrivacyQueriesService().blacklistTag(sender, tag); + if (result == PrivacyQueriesService.PrivacyResult.Added) { + return "Tag added to your blacklist"; + } else { + return "Tag removed from your blacklist"; + } + } + } + return "Tag not found"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/BlacklistUser.java b/juick-commands/src/main/java/com/juick/command/BlacklistUser.java new file mode 100644 index 00000000..f2682c6a --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/BlacklistUser.java @@ -0,0 +1,54 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.PrivacyQueriesService; +import com.juick.service.UserService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class BlacklistUser extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^bl\\s+@([^\\s\\n+]+)", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + private final PrivacyQueriesService privacyQueriesService; + + @Autowired + public BlacklistUser(UserService userService, PrivacyQueriesService privacyQueriesService) { + this.userService = userService; + this.privacyQueriesService = privacyQueriesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "BL @username - add @username to your blacklist"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + User blUser = getUserService().getUserByName(arguments[0]); + if (blUser != null) { + PrivacyQueriesService.PrivacyResult result = getPrivacyQueriesService().blacklistUser(sender, blUser); + if (result == PrivacyQueriesService.PrivacyResult.Added) { + return "User added to your blacklist"; + } else { + return "User removed from your blacklist"; + } + } + return "User not found"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/LastMessages.java b/juick-commands/src/main/java/com/juick/command/LastMessages.java new file mode 100644 index 00000000..77ba8a81 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/LastMessages.java @@ -0,0 +1,46 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.formatters.PlainTextFormatter; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.MessagesService; +import lombok.Getter; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * @author ma1uta + */ +@Component +@Getter +public class LastMessages implements Command { + + private static final Pattern PATTERN = Pattern.compile("^#\\+$", Pattern.CASE_INSENSITIVE); + + private final MessagesService messagesService; + + public LastMessages(MessagesService messagesService) { + this.messagesService = messagesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "#+ - Show last Juick messages"; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + return "Last messages:\n" + + getMessagesService().getMessages(getMessagesService().getAll(sender.getUid(), 0)).stream() + .sorted(Collections.reverseOrder()) + .map(PlainTextFormatter::formatPostSummary).collect(Collectors.joining("\n\n")); + } +} diff --git a/juick-commands/src/main/java/com/juick/command/Login.java b/juick-commands/src/main/java/com/juick/command/Login.java new file mode 100644 index 00000000..0dc6a021 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/Login.java @@ -0,0 +1,42 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.UserService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class Login implements Command { + + private static final Pattern PATTERN = Pattern.compile("^login$", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + + @Autowired + public Login(UserService userService) { + this.userService = userService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "LOGIN - log in to Juick website"; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + return "http://juick.com/login?hash=" + getUserService().getHashByUID(sender.getUid()); + } +} diff --git a/juick-commands/src/main/java/com/juick/command/PrivateMessage.java b/juick-commands/src/main/java/com/juick/command/PrivateMessage.java index e41b0037..069bc367 100644 --- a/juick-commands/src/main/java/com/juick/command/PrivateMessage.java +++ b/juick-commands/src/main/java/com/juick/command/PrivateMessage.java @@ -6,8 +6,12 @@ import com.juick.service.PMQueriesService; import com.juick.service.UserService; import lombok.Getter; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.stanza.model.Message; +import java.util.List; import java.util.regex.Pattern; /** @@ -22,6 +26,9 @@ public class PrivateMessage extends MultiArgsCommand { private final UserService userService; private final PMQueriesService pmQueriesService; + @Value("${xmppbot_jid}") + private Jid jid; + @Autowired public PrivateMessage(UserService userService, PMQueriesService pmQueriesService) { this.userService = userService; @@ -42,17 +49,61 @@ public class PrivateMessage extends MultiArgsCommand { protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { String addressee = arguments[0]; String body = arguments[1]; + int ret; + + int addresseeUid; + List<String> jids_to = null; + boolean haveInRoster; - User toUser = getUserService().getUserByName(addressee); + addresseeUid = addressee.indexOf('@') > 0 ? getUserService().getUIDbyJID(addressee) : getUserService().getUIDbyName(addressee); - if (toUser.getUid() > 0) { - if (!getUserService().isInBLAny(toUser.getUid(), sender.getUid())) { - if (getPmQueriesService().createPM(sender.getUid(), toUser.getUid(), body)) { - protocolListener.privateMessage(sender, toUser, body); - return "Private message sent"; + if (addresseeUid > 0) { + if (!getUserService().isInBLAny(addresseeUid, sender.getUid())) { + if (getPmQueriesService().createPM(sender.getUid(), addresseeUid, body)) { + jids_to = getUserService().getJIDsbyUID(addresseeUid); + ret = 200; + } else { + ret = 500; } + } else { + ret = 403; } + } else { + ret = 404; } - return "Error"; + + if (ret == 200) { + Message msg = new Message(); + msg.setFrom(getJid().asBareJid()); + msg.setTo(Jid.of(Integer.toString(addresseeUid), "push.juick.com", null)); + com.juick.Message jmsg = new com.juick.Message(); + jmsg.setUser(sender); + jmsg.setText(body); + msg.addExtension(jmsg); + //TODO fix external dependency + //router.sendStanza(msg); + + msg.setTo(Jid.of(Integer.toString(addresseeUid), "ws.juick.com", null)); + //TODO fix external dependency + //router.sendStanza(msg); + + for (String userJid : jids_to) { + Message mm = new Message(); + mm.setTo(Jid.of(userJid)); + mm.setType(Message.Type.CHAT); + haveInRoster = getPmQueriesService().havePMinRoster(sender.getUid(), userJid); + if (haveInRoster) { + mm.setFrom(Jid.of(sender.getName(), "juick.com", "Juick")); + mm.setBody(body); + } else { + mm.setFrom(getJid()); + mm.setBody("Private message from @" + sender.getName() + ":\n" + body); + } + //TODO fix external dependency + //xmpp.sendOut(ClientMessage.from(mm)); + } + } + + return ret == 200 ? "Private message sent" : "Error " + ret; } } diff --git a/juick-commands/src/main/java/com/juick/command/Recommendations.java b/juick-commands/src/main/java/com/juick/command/Recommendations.java new file mode 100644 index 00000000..c7f38003 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/Recommendations.java @@ -0,0 +1,62 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.ShowQueriesService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class Recommendations implements Command { + + private static final Pattern PATTERN = Pattern.compile("@", Pattern.CASE_INSENSITIVE); + + private final ShowQueriesService showQueriesService; + + @Autowired + public Recommendations(ShowQueriesService showQueriesService) { + this.showQueriesService = showQueriesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "@ - Show recommendations and popular personal blogs"; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + StringBuilder msg = new StringBuilder(); + msg.append("Recommended blogs"); + List<String> recommendedUsers = getShowQueriesService().getRecommendedUsers(sender); + if (recommendedUsers.size() > 0) { + for (String user : recommendedUsers) { + msg.append("\n@").append(user); + } + } else { + msg.append("\nNo recommendations now. Subscribe to more blogs. ;)"); + } + msg.append("\n\nTop 10 personal blogs:"); + List<String> topUsers = getShowQueriesService().getTopUsers(); + if (topUsers.size() > 0) { + for (String user : topUsers) { + msg.append("\n@").append(user); + } + } else { + msg.append("\nNo top users. Empty DB? ;)"); + } + return msg.toString(); + } +} diff --git a/juick-commands/src/main/java/com/juick/command/ShowBlacklist.java b/juick-commands/src/main/java/com/juick/command/ShowBlacklist.java new file mode 100644 index 00000000..bd4df7df --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/ShowBlacklist.java @@ -0,0 +1,68 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.TagService; +import com.juick.service.UserService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class ShowBlacklist implements Command { + + private static final Pattern PATTERN = Pattern.compile("^bl$", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + private final TagService tagService; + + @Autowired + public ShowBlacklist(UserService userService, TagService tagService) { + this.userService = userService; + this.tagService = tagService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "BL - Show your blacklist"; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + List<User> blusers = getUserService().getUserBLUsers(sender.getUid()); + List<String> bltags = getTagService().getUserBLTags(sender.getUid()); + + StringBuilder txt = new StringBuilder(); + if (bltags.size() > 0) { + for (String bltag : bltags) { + txt.append("*").append(bltag).append("\n"); + } + + if (blusers.size() > 0) { + txt.append("\n"); + } + } + if (blusers.size() > 0) { + for (User bluser : blusers) { + txt.append("@").append(bluser.getName()).append("\n"); + } + } + if (txt.length() == 0) { + txt.append("You don't have any users or tags in your blacklist."); + } + + return txt.toString(); + } +} diff --git a/juick-commands/src/main/java/com/juick/command/ShowFavorites.java b/juick-commands/src/main/java/com/juick/command/ShowFavorites.java new file mode 100644 index 00000000..ced28936 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/ShowFavorites.java @@ -0,0 +1,54 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.formatters.PlainTextFormatter; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.MessagesService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * @author ma1uta + */ +@Component +@Getter +public class ShowFavorites implements Command { + + private static final Pattern PATTERN = Pattern.compile("!", Pattern.CASE_INSENSITIVE); + + private final MessagesService messagesService; + + @Autowired + public ShowFavorites(MessagesService messagesService) { + this.messagesService = messagesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "! - Show your favorite messages"; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + List<Integer> mids = getMessagesService().getUserRecommendations(sender.getUid(), 0); + if (mids.size() > 0) { + List<com.juick.Message> messages = getMessagesService().getMessages(mids); + return "Favorite messages: \n" + String.join("\n", messages.stream() + .sorted(Collections.reverseOrder()) + .map(PlainTextFormatter::formatPost) + .collect(Collectors.toList())); + } + return "No favorite messages, try to \"like\" something ;)"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/ShowSubscriptions.java b/juick-commands/src/main/java/com/juick/command/ShowSubscriptions.java new file mode 100644 index 00000000..ad35a4c2 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/ShowSubscriptions.java @@ -0,0 +1,53 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.SubscriptionService; +import com.juick.service.UserService; +import lombok.Getter; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * @author ma1uta + */ +@Component +@Getter +public class ShowSubscriptions implements Command { + + private static final Pattern PATTERN = Pattern.compile("S", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + private final SubscriptionService subscriptionService; + + public ShowSubscriptions(UserService userService, SubscriptionService subscriptionService) { + this.userService = userService; + this.subscriptionService = subscriptionService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "S - Show your subscriptions"; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + List<User> friends = getUserService().getUserFriends(sender.getUid()); + List<String> tags = getSubscriptionService().getSubscribedTags(sender); + String msg = friends.size() > 0 ? "You are subscribed to users:" + friends.stream().map(u -> "\n@" + u.getName()) + .collect(Collectors.joining()) + : "You are not subscribed to any user."; + msg += tags.size() > 0 ? "\nYou are subscribed to tags:" + tags.stream().map(t -> "\n*" + t) + .collect(Collectors.joining()) + : "\nYou are not subscribed to any tag."; + return msg; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/ShowTags.java b/juick-commands/src/main/java/com/juick/command/ShowTags.java new file mode 100644 index 00000000..186919ad --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/ShowTags.java @@ -0,0 +1,48 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.helpers.TagStats; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.TagService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * @author ma1uta + */ +@Component +@Getter +public class ShowTags implements Command { + + private static final Pattern PATTERN = Pattern.compile("\\*", Pattern.CASE_INSENSITIVE); + + private final TagService tagService; + + @Autowired + public ShowTags(TagService tagService) { + this.tagService = tagService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "* - Show your tags"; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + List<TagStats> tags = getTagService().getUserTagStats(sender.getUid()); + return "Your tags: (tag - messages)\n" + + tags.stream() + .map(t -> String.format("\n*%s - %d", t.getTag().getName(), t.getUsageCount())).collect(Collectors.joining()); + } +} diff --git a/juick-commands/src/main/java/com/juick/command/ShowUserInfo.java b/juick-commands/src/main/java/com/juick/command/ShowUserInfo.java new file mode 100644 index 00000000..336b2e0d --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/ShowUserInfo.java @@ -0,0 +1,55 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.formatters.PlainTextFormatter; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.MessagesService; +import com.juick.service.UserService; +import lombok.Getter; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * @author ma1uta + */ +@Component +@Getter +public class ShowUserInfo extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^@([^\\s\\n+]+)(\\+?)$", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + private final MessagesService messagesService; + + public ShowUserInfo(UserService userService, MessagesService messagesService) { + this.userService = userService; + this.messagesService = messagesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + User blogUser = getUserService().getUserByName(arguments[0]); + int page = arguments[1].length(); + if (blogUser.getUid() > 0) { + List<Integer> mids = getMessagesService().getUserBlog(blogUser.getUid(), 0, page); + List<com.juick.Message> messages = getMessagesService().getMessages(mids); + return String.format("Last messages from @%s:\n%s", arguments[0], + String.join("\n", messages.stream() + .map(PlainTextFormatter::formatPost).collect(Collectors.toList()))); + } + return "User not found"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/SubscribeMessage.java b/juick-commands/src/main/java/com/juick/command/SubscribeMessage.java new file mode 100644 index 00000000..893c0b65 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/SubscribeMessage.java @@ -0,0 +1,60 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.MessagesService; +import com.juick.service.SubscriptionService; +import lombok.Getter; +import org.apache.commons.lang3.math.NumberUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class SubscribeMessage extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^([su])\\s+#(\\d+)$", Pattern.CASE_INSENSITIVE); + + private final MessagesService messagesService; + private final SubscriptionService subscriptionService; + + @Autowired + public SubscribeMessage(MessagesService messagesService, SubscriptionService subscriptionService) { + this.messagesService = messagesService; + this.subscriptionService = subscriptionService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "S #1234 - subscribe to comments\nU #1234 - unsubscribe to comments"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + boolean subscribe = arguments[0].equalsIgnoreCase("s"); + int mid = NumberUtils.toInt(arguments[1], 0); + if (getMessagesService().getMessage(mid) != null) { + if (subscribe) { + if (getSubscriptionService().subscribeMessage(mid, sender.getUid())) { + return "Subscribed"; + } + } else { + if (getSubscriptionService().unSubscribeMessage(mid, sender.getUid())) { + return "Unsubscribed from #" + mid; + } + return "You was not subscribed to #" + mid; + } + } + return "Error"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/SubscribeTag.java b/juick-commands/src/main/java/com/juick/command/SubscribeTag.java new file mode 100644 index 00000000..ce9ebc20 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/SubscribeTag.java @@ -0,0 +1,58 @@ +package com.juick.command; + +import com.juick.Tag; +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.SubscriptionService; +import com.juick.service.TagService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class SubscribeTag extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^([su])\\s+\\*(\\S+)$", Pattern.CASE_INSENSITIVE); + + private final TagService tagService; + private final SubscriptionService subscriptionService; + + @Autowired + public SubscribeTag(TagService tagService, SubscriptionService subscriptionService) { + this.tagService = tagService; + this.subscriptionService = subscriptionService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "S *tag - subscribe to tag\nU *tag - unsubscribe from tag"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + boolean subscribe = arguments[0].equalsIgnoreCase("s"); + Tag tag = getTagService().getTag(arguments[1], true); + if (subscribe) { + if (getSubscriptionService().subscribeTag(sender, tag)) { + return "Subscribed"; + } + } else { + if (getSubscriptionService().unSubscribeTag(sender, tag)) { + return "Unsubscribed from " + tag.getName(); + } + return "You was not subscribed to " + tag.getName(); + } + return "Error"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/SubscribeUser.java b/juick-commands/src/main/java/com/juick/command/SubscribeUser.java index 7589a954..f8204525 100644 --- a/juick-commands/src/main/java/com/juick/command/SubscribeUser.java +++ b/juick-commands/src/main/java/com/juick/command/SubscribeUser.java @@ -35,7 +35,7 @@ public class SubscribeUser extends MultiArgsCommand { @Override public String help() { - return "S @user - subscribe to user's posts"; + return "S @user - subscribe to user's posts\n U @user - unsubscribe to user's posts"; } @Override diff --git a/juick-commands/src/main/java/com/juick/command/ToggleSubscriptions.java b/juick-commands/src/main/java/com/juick/command/ToggleSubscriptions.java new file mode 100644 index 00000000..737565f9 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/ToggleSubscriptions.java @@ -0,0 +1,56 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.UserService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class ToggleSubscriptions extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^(on|off)$", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + + @Autowired + public ToggleSubscriptions(UserService userService) { + this.userService = userService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "ON/OFF - Enable/disable subscriptions delivery"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + UserService.ActiveStatus newStatus; + String retValUpdated; + if (arguments[0].toLowerCase().equals("on")) { + newStatus = UserService.ActiveStatus.Active; + retValUpdated = "Notifications are activated for " + sender.getJid(); + } else { + newStatus = UserService.ActiveStatus.Inactive; + retValUpdated = "Notifications are disabled for " + sender.getJid(); + } + + if (getUserService().setActiveStatusForJID(sender.getJid(), newStatus)) { + return retValUpdated; + } else { + return String.format("Subscriptions status for %s was not changed", sender.getJid()); + } + } +} |