diff options
Diffstat (limited to 'juick-commands/src/main/java/com/juick/command/SubscribeUser.java')
-rw-r--r-- | juick-commands/src/main/java/com/juick/command/SubscribeUser.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/SubscribeUser.java b/juick-commands/src/main/java/com/juick/command/SubscribeUser.java new file mode 100644 index 00000000..7589a954 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/SubscribeUser.java @@ -0,0 +1,61 @@ +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.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class SubscribeUser extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^([su])\\s+@(\\S+)$", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + private final SubscriptionService subscriptionService; + + @Autowired + public SubscribeUser(UserService userService, SubscriptionService subscriptionService) { + this.userService = userService; + this.subscriptionService = subscriptionService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String help() { + return "S @user - subscribe to user's posts"; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + boolean subscribe = arguments[0].equalsIgnoreCase("s"); + User toUser = getUserService().getUserByName(arguments[1]); + if (toUser.getUid() > 0) { + if (subscribe) { + if (getSubscriptionService().subscribeUser(sender, toUser)) { + protocolListener.userSubscribed(sender, toUser); + return "Subscribed"; + // TODO: already subscribed case + } + } else { + if (subscriptionService.unSubscribeUser(sender, toUser)) { + return "Unsubscribed from @" + toUser.getName(); + } + return "You was not subscribed to @" + toUser.getName(); + } + } + return "Error"; + } +} |