From 13a68f3b21d2081d5f48b95a537e922c4ab4135b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 19 Oct 2016 16:35:51 +0300 Subject: protocol: some new commands and fixes --- .../main/java/com/juick/server/PrivacyQueries.java | 32 ++++++++++++++ .../com/juick/server/protocol/JuickProtocol.java | 50 +++++++++++++++++++--- 2 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 juick-core/src/main/java/com/juick/server/PrivacyQueries.java (limited to 'juick-core/src/main/java') diff --git a/juick-core/src/main/java/com/juick/server/PrivacyQueries.java b/juick-core/src/main/java/com/juick/server/PrivacyQueries.java new file mode 100644 index 00000000..53b13505 --- /dev/null +++ b/juick-core/src/main/java/com/juick/server/PrivacyQueries.java @@ -0,0 +1,32 @@ +package com.juick.server; + +import com.juick.Tag; +import com.juick.User; +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * Created by vitalyster on 19.10.2016. + */ +public class PrivacyQueries { + public enum PrivacyResult { + Removed, Added + } + public static PrivacyResult blacklistUser(JdbcTemplate jdbc, User user, User target) { + int result = jdbc.update("DELETE FROM bl_users WHERE user_id=? AND bl_user_id=?", user.getUID(), target.getUID()); + if (result > 0) { + return PrivacyResult.Removed; + } else { + jdbc.update("INSERT INTO bl_users(user_id,bl_user_id) VALUES (?,?)", user.getUID(), target.getUID()); + return PrivacyResult.Added; + } + } + public static PrivacyResult blacklistTag(JdbcTemplate jdbc, User user, Tag tag) { + int result = jdbc.update("DELETE FROM bl_tags WHERE user_id=? AND tag_id=?", user.getUID(), tag.TID); + if (result > 0) { + return PrivacyResult.Removed; + } else { + jdbc.update("INSERT INTO bl_tags(user_id,tag_id) VALUES (?,?)", user.getUID(), tag.TID); + return PrivacyResult.Added; + } + } +} diff --git a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java index 024179a7..01f7d691 100644 --- a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java +++ b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -113,6 +113,39 @@ public class JuickProtocol { return new ProtocolReply(txt, Optional.empty()); } + @UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE, + help = "BL @username - add @username to your blacklist") + public ProtocolReply blacklistUser(User from, String... arguments) { + User blUser = UserQueries.getUserByName(sql, arguments[0]); + if (blUser != null) { + PrivacyQueries.PrivacyResult result = PrivacyQueries.blacklistUser(sql, from, blUser); + if (result == PrivacyQueries.PrivacyResult.Added) { + return new ProtocolReply("User added to your blacklist", Optional.empty()); + } else { + return new ProtocolReply("User removed from your blacklist", Optional.empty()); + } + } + return new ProtocolReply("User not found", Optional.empty()); + } + + @UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "BL *tag - add *tag to your blacklist") + public ProtocolReply blacklistTag(User from, String... arguments) { + User blUser = UserQueries.getUserByName(sql, arguments[0]); + if (blUser != null) { + Tag tag = TagQueries.getTag(sql, arguments[0], false); + if (tag != null) { + PrivacyQueries.PrivacyResult result = PrivacyQueries.blacklistTag(sql, from, tag); + if (result == PrivacyQueries.PrivacyResult.Added) { + return new ProtocolReply("Tag added to your blacklist", Optional.empty()); + } else { + return new ProtocolReply("Tag removed from your blacklist", Optional.empty()); + } + } + } + return new ProtocolReply("Tag not found", Optional.empty()); + } + @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$", help = "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)") public ProtocolReply commandUser(User user, String... arguments) { @@ -263,14 +296,17 @@ public class JuickProtocol { return new ProtocolReply("Error", Optional.empty()); } Message msg = MessagesQueries.getMessage(sql, mid); - if (showReplies) { - List replies = MessagesQueries.getReplies(sql, mid); - replies.add(0, msg); - return new ProtocolReply(String.join("\n", - replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())), - Optional.of(json.serializeList(replies))); + if (msg != null) { + if (showReplies) { + List replies = MessagesQueries.getReplies(sql, mid); + replies.add(0, msg); + return new ProtocolReply(String.join("\n", + replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())), + Optional.of(json.serializeList(replies))); + } + return new ProtocolReply(PlainTextFormatter.formatPost(msg), Optional.of(json.serializeList(Collections.singletonList(msg)))); } - return new ProtocolReply(msg.toString(), Optional.of(json.serializeList(Collections.singletonList(msg)))); + return new ProtocolReply("Message not found", Optional.empty()); } @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)", help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message") -- cgit v1.2.3