diff options
author | Anatoliy Sablin | 2017-11-05 10:33:34 +0300 |
---|---|---|
committer | Anatoliy Sablin | 2017-11-06 15:00:22 +0300 |
commit | ce0ed9ac9e82b19763b7dfd4814d327e0b49ddb7 (patch) | |
tree | 4e63b5a35e162956b548d9415fa8f2c6d38b8e3b /juick-commands/src/main/java/com/juick/command/PrivateMessage.java | |
parent | 83815d291a7945c26a675c8543e8766ad2d1beda (diff) |
juick-commands
Diffstat (limited to 'juick-commands/src/main/java/com/juick/command/PrivateMessage.java')
-rw-r--r-- | juick-commands/src/main/java/com/juick/command/PrivateMessage.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/PrivateMessage.java b/juick-commands/src/main/java/com/juick/command/PrivateMessage.java new file mode 100644 index 00000000..1e02f1da --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/PrivateMessage.java @@ -0,0 +1,53 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.PMQueriesService; +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 PrivateMessage extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^@(\\S+)\\s+([\\s\\S]+)$", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + private final PMQueriesService pmQueriesService; + + @Autowired + public PrivateMessage(UserService userService, PMQueriesService pmQueriesService) { + this.userService = userService; + this.pmQueriesService = pmQueriesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + String addressee = arguments[0]; + String body = arguments[1]; + + User toUser = getUserService().getUserByName(addressee); + + if (toUser.getUid() > 0) { + if (!getUserService().isInBLAny(toUser.getUid(), sender.getUid())) { + if (getPmQueriesService().createPM(sender.getUid(), toUser.getUid(), body)) { + protocolListener.privateMessage(sender, toUser, body); + return "Private message sent"; + } + } + } + return "Error"; + } +} |