package com.juick.command; import com.juick.User; import com.juick.server.protocol.ProtocolListener; 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 ToggleSubscriptions extends MultiArgsCommand { private static final Pattern PATTERN = Pattern.compile("^(on|off)$", Pattern.CASE_INSENSITIVE); private final UserService userService; @Autowired public ToggleSubscriptions(UserService userService) { this.userService = userService; } @Override public Pattern pattern() { return PATTERN; } @Override public String help() { return "ON/OFF - Enable/disable subscriptions delivery"; } @Override protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { UserService.ActiveStatus newStatus; String retValUpdated; if (arguments[0].toLowerCase().equals("on")) { newStatus = UserService.ActiveStatus.Active; retValUpdated = "Notifications are activated for " + sender.getJid(); } else { newStatus = UserService.ActiveStatus.Inactive; retValUpdated = "Notifications are disabled for " + sender.getJid(); } if (getUserService().setActiveStatusForJID(sender.getJid(), newStatus)) { return retValUpdated; } else { return String.format("Subscriptions status for %s was not changed", sender.getJid()); } } }