From d77ff16de5543b5820ee3b185660a052c5f57a15 Mon Sep 17 00:00:00 2001 From: Anatoliy Sablin Date: Mon, 6 Nov 2017 16:27:11 +0300 Subject: Added rest commands. --- .../java/com/juick/command/ShowSubscriptions.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 juick-commands/src/main/java/com/juick/command/ShowSubscriptions.java (limited to 'juick-commands/src/main/java/com/juick/command/ShowSubscriptions.java') 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 friends = getUserService().getUserFriends(sender.getUid()); + List 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; + } +} -- cgit v1.2.3