diff options
author | 2016-06-29 15:09:47 +0300 | |
---|---|---|
committer | 2016-06-29 15:09:47 +0300 | |
commit | 797942249b0d5e38285e806bc0e73e755e6ffede (patch) | |
tree | 6d785d6b59fbc57d6e0e82af157df1abf3e853be /src/main/java/com/juick | |
parent | 87da42e3385e368f05d88ff125a78605a18f9cc3 (diff) |
add more bot queries
Diffstat (limited to 'src/main/java/com/juick')
-rw-r--r-- | src/main/java/com/juick/server/MessagesQueries.java | 11 | ||||
-rw-r--r-- | src/main/java/com/juick/server/SubscriptionsQueries.java | 22 | ||||
-rw-r--r-- | src/main/java/com/juick/server/TagQueries.java | 65 |
3 files changed, 81 insertions, 17 deletions
diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index cd098ec6..880e94e2 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -645,4 +645,15 @@ public class MessagesQueries { return sql.update("UPDATE messages SET privacy=1 WHERE message_id=?", mid) > 0; } + public static boolean deleteMessage(JdbcTemplate sql, int uid, int mid) { + if (sql.update("DELETE FROM messages WHERE message_id=? AND user_id=?", mid, uid) > 0) { + sql.update("DELETE FROM messages_txt WHERE message_id=?", mid); + sql.update("DELETE FROM replies WHERE message_id=?", mid); + sql.update("DELETE FROM subscr_messages WHERE message_id=?", mid); + sql.update("DELETE FROM messages_tags WHERE message_id=?", mid); + return true; + } + return false; + } + } diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java index feebfb22..9f0372ff 100644 --- a/src/main/java/com/juick/server/SubscriptionsQueries.java +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -4,6 +4,7 @@ */ package com.juick.server; +import com.juick.Tag; import com.juick.User; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.StringUtils; @@ -78,4 +79,25 @@ public class SubscriptionsQueries { public static boolean subscribeMessage(JdbcTemplate sql, int mid, int vuid) { return sql.update("INSERT IGNORE INTO subscr_messages(suser_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; } + public static boolean unSubscribeMessage(JdbcTemplate sql, int mid, int vuid) { + return sql.update("DELETE FROM subscr_messages WHERE message_id=? AND suser_id=?", + mid, vuid) > 0; + } + public static boolean subscribeUser(JdbcTemplate sql, User user, User toUser) { + return sql.update("INSERT IGNORE INTO subscr_users(user_id,suser_id) VALUES (?,?)", + user.getUID(), toUser.getUID()) == 1; + } + public static boolean unSubscribeUser(JdbcTemplate sql, User user, User fromUser) { + return sql.update("DELETE FROM subscr_users WHERE suser_id=? AND user_id=?", + user.getUID(), fromUser.getUID()) > 0; + } + public static boolean subscribeTag(JdbcTemplate sql, User user, Tag toTag) { + return sql.update("INSERT IGNORE INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", + toTag.TID, user.getUID()) == 1; + } + public static boolean unSubscribeTag(JdbcTemplate sql, User user, Tag toTag) { + return sql.update("DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", + toTag.TID, user.getUID()) > 0; + } + } diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java index 0094fc22..2258a8ea 100644 --- a/src/main/java/com/juick/server/TagQueries.java +++ b/src/main/java/com/juick/server/TagQueries.java @@ -20,18 +20,17 @@ package com.juick.server; import com.juick.Tag; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; -import java.sql.Connection; import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.springframework.jdbc.support.GeneratedKeyHolder; -import org.springframework.jdbc.support.KeyHolder; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * @@ -39,17 +38,19 @@ import org.springframework.jdbc.support.KeyHolder; */ public class TagQueries { - private static final Logger LOGGER = Logger.getLogger(TagQueries.class.getName()); - public static com.juick.Tag getTag(JdbcTemplate sql, int tid) { - return sql.queryForObject("SELECT synonym_id,name FROM tags WHERE tag_id=?", - new Object[]{tid}, (rs, num) -> { - Tag ret = new Tag(); - ret.TID = tid; - ret.SynonymID = rs.getInt(1); - ret.Name = rs.getString(2); - return ret; - }); + try { + return sql.queryForObject("SELECT synonym_id,name FROM tags WHERE tag_id=?", + new Object[]{tid}, (rs, num) -> { + Tag ret = new Tag(); + ret.TID = tid; + ret.SynonymID = rs.getInt(1); + ret.Name = rs.getString(2); + return ret; + }); + } catch (EmptyResultDataAccessException e) { + return null; + } } public static com.juick.Tag getTag(JdbcTemplate sql, String tag, boolean autoCreate) { @@ -132,4 +133,34 @@ public class TagQueries { public static List<String> getPopularTags(JdbcTemplate sql) { return sql.queryForList("SELECT name FROM tags WHERE top=1 ORDER BY name ASC", String.class); } + public static List<Tag> updateTags(JdbcTemplate sql, int mid, List<Tag> newTags) { + List<Tag> currentTags = MessagesQueries.getMessageTags(sql, mid); + newTags.stream().filter(currentTags::contains) + .forEach(t -> sql.update("DELETE FROM messages_tags WHERE message_id=? AND tag_id=?", mid, t.TID)); + newTags.stream().filter(t -> !currentTags.contains(t)) + .forEach(t -> sql.update("INSERT INTO messages_tags(message_id,tag_id) VALUES (?,?)", mid, t.TID)); + return MessagesQueries.getMessageTags(sql, mid); + } + + public static List<Tag> fromString(JdbcTemplate sql, String txt, boolean tagsOnly) { + String patternString = tagsOnly ? "^(?:(?:\\*[^ \\r\\n\\t]+)|\\s)+$" : "^\\*([^ \\r\\n\\t]+)\\s+([\\s\\S]+)"; + Pattern tagsPattern = Pattern.compile(patternString); + if (tagsPattern.matcher(txt).matches()) { + Pattern tagPattern = Pattern.compile("\\*([^ \\r\\n\\t]+)"); + Matcher tagMatcher = tagPattern.matcher(txt); + List<Tag> tags = new ArrayList<>(); + // TODO: process readonly, private, friends, public + while (tagMatcher.find()) { + for (int i = 1; i <= tagMatcher.groupCount(); i++) { + tags.add(getTag(sql, tagMatcher.group(i), true)); + } + } + return tags; + } + return Collections.emptyList(); + } + public static String toString(List<Tag> tags) { + return tags.stream().map(t -> " *" + t.Name) + .collect(Collectors.joining()); + } } |