package com.juick.command; import com.juick.User; import com.juick.server.protocol.ProtocolListener; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import lombok.Getter; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.regex.Pattern; /** * @author ma1uta */ @Component @Getter public class SubscribeMessage extends MultiArgsCommand { private static final Pattern PATTERN = Pattern.compile("^([su])\\s+#(\\d+)$", Pattern.CASE_INSENSITIVE); private final MessagesService messagesService; private final SubscriptionService subscriptionService; @Autowired public SubscribeMessage(MessagesService messagesService, SubscriptionService subscriptionService) { this.messagesService = messagesService; this.subscriptionService = subscriptionService; } @Override public Pattern pattern() { return PATTERN; } @Override public String help() { return "S #1234 - subscribe to comments\nU #1234 - unsubscribe to comments"; } @Override protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { boolean subscribe = arguments[0].equalsIgnoreCase("s"); int mid = NumberUtils.toInt(arguments[1], 0); if (getMessagesService().getMessage(mid) != null) { if (subscribe) { if (getSubscriptionService().subscribeMessage(mid, sender.getUid())) { return "Subscribed"; } } else { if (getSubscriptionService().unSubscribeMessage(mid, sender.getUid())) { return "Unsubscribed from #" + mid; } return "You was not subscribed to #" + mid; } } return "Error"; } }