diff options
author | Anatoliy Sablin | 2017-11-06 15:45:55 +0300 |
---|---|---|
committer | Anatoliy Sablin | 2017-11-06 15:45:55 +0300 |
commit | 41b885d84958d07ade536850829b3ba394a4f8f9 (patch) | |
tree | 93661b9f558c4234afd4e5a84b0442fdb27454a2 /juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java | |
parent | ce0ed9ac9e82b19763b7dfd4814d327e0b49ddb7 (diff) |
Added a few commands.
Diffstat (limited to 'juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java')
-rw-r--r-- | juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java b/juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java new file mode 100644 index 00000000..00ec4be9 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/EditOrReplyMessage.java @@ -0,0 +1,65 @@ +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<Tag> 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); + } + } +} |