aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/ShowSubscriptions.java
blob: d4548c1790ed83f72c6c13ee202c15dd29c2f5eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.juick.command;

import com.juick.User;
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, MessageListener 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;
    }
}