blob: 8f74fc21f56386619075545b0f6f8980df95d49f (
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
53
54
55
56
57
|
package com.juick.command;
import com.juick.Tag;
import com.juick.User;
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, MessageListener 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";
}
}
|