package com.juick.command; import com.juick.Tag; import com.juick.User; import com.juick.server.protocol.ProtocolListener; import com.juick.service.MessagesService; import com.juick.service.TagService; import lombok.Getter; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.List; import java.util.regex.Pattern; /** * @author ma1uta */ @Component @Getter public class EditOrReplyMessage extends MultiArgsCommand { private static final Pattern PATTERN = Pattern.compile("^([#.])(\\d+)(([.-/])(\\d+))?\\s([\\s\\S]+)", Pattern.CASE_INSENSITIVE); private final TagService tagService; private final MessagesService messagesService; @Value("${baseuri}") private String baseUri = "http://juick.com"; @Autowired public EditOrReplyMessage(TagService tagService, MessagesService messagesService) { this.tagService = tagService; this.messagesService = messagesService; } @Override public Pattern pattern() { return PATTERN; } @Override public String help() { return "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message"; } @Override protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { int mid = NumberUtils.toInt(arguments[1]); int rid = NumberUtils.toInt(arguments[4], 0); String txt = arguments[5]; List messageTags = getTagService().fromString(txt, true); if (messageTags.size() > 0) { if (sender.getUid() != getMessagesService().getMessageAuthor(mid).getUid()) { return "It is not your message"; } getTagService().updateTags(mid, messageTags); return "Tags are updated"; } else { int newrid = getMessagesService().createReply(mid, rid, sender.getUid(), txt, null); protocolListener.messagePosted(getMessagesService().getReply(mid, newrid)); return String.format("Reply posted.\n#%d/%d %s%d#%d", mid, newrid, getBaseUri(), mid, newrid); } } }