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; } }