package com.juick.command; import com.juick.Tag; import com.juick.User; import com.juick.server.util.TagUtils; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import com.juick.service.TagService; import lombok.Getter; 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 PostMessage implements Command { private final TagService tagService; private final MessagesService messagesService; private final SubscriptionService subscriptionService; @Value("${baseuri}") private String baseUri = "http://juick.com"; @Autowired public PostMessage(TagService tagService, MessagesService messagesService, SubscriptionService subscriptionService) { this.tagService = tagService; this.messagesService = messagesService; this.subscriptionService = subscriptionService; } @Override public Pattern pattern() { return null; } @Override public String help() { return ""; } @Override public String execute(User sender, MessageListener protocolListener, String command) { List tags = getTagService().fromString(command, false); String body = command.substring(TagUtils.toString(tags).length()); int mid = getMessagesService().createMessage(sender.getUid(), body, null, tags); getSubscriptionService().subscribeMessage(mid, sender.getUid()); protocolListener.messagePosted(getMessagesService().getMessage(mid)); return String.format("New message posted.\n#%d %s%d", mid, getBaseUri(), mid); } }