diff options
author | Vitaly Takmazov | 2016-07-07 11:38:54 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-07-07 11:38:54 +0300 |
commit | b1c9bbe04e6300f5bbb9d0015987c4b18dfb2d09 (patch) | |
tree | 83e4f4a05bae0588e4692a32ee82726c365fd59e /src/main/java/com/juick/server/SubscriptionsQueries.java | |
parent | fe8cf8c0093bbbfb053f52a53daea9145705b62e (diff) | |
parent | a0d038f5e6aa28ef67162c591abe64d057a19fa5 (diff) |
merge server
Diffstat (limited to 'src/main/java/com/juick/server/SubscriptionsQueries.java')
-rw-r--r-- | src/main/java/com/juick/server/SubscriptionsQueries.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java new file mode 100644 index 00000000..0cf3f169 --- /dev/null +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -0,0 +1,104 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.juick.server; + +import com.juick.Tag; +import com.juick.User; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Logger; +import java.util.stream.Collectors; + +/** + * + * @author ugnich + */ +public class SubscriptionsQueries { + + private static final Logger logger = Logger.getLogger(SubscriptionsQueries.class.getName()); + + public static List<String> getJIDSubscribedToUser(JdbcTemplate sql, int uid, boolean friendsonly) { + if (friendsonly == false) { + return sql.queryForList("SELECT jids.jid FROM subscr_users INNER JOIN jids " + + "ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1", + String.class, uid); + } else { + return sql.queryForList("SELECT jids.jid FROM subscr_users INNER JOIN jids " + + "ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1 " + + "AND jids.user_id IN (SELECT wl_user_id FROM wl_users WHERE user_id=?)", String.class, uid, uid); + } + } + + public static List<User> getSubscribedUsers(JdbcTemplate sql, int uid, int mid) { + List<User> userids = UserQueries.getUserReaders(sql, uid); + Set<Integer> set = new HashSet<>(); + set.addAll(userids.stream().map(User::getUID).collect(Collectors.toList())); + List<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, mid); + if (tags.size() > 0) { + List<Integer> tagUsers = sql.queryForList("SELECT suser_id FROM subscr_tags " + + "WHERE tag_id IN (" + StringUtils.arrayToCommaDelimitedString(tags.toArray()) + ") AND suser_id!=" + uid, Integer.class); + set.addAll(tagUsers); + } + return UserQueries.getUsersByID(sql, new ArrayList<>(set)); + } + + public static List<User> getUsersSubscribedToComments(JdbcTemplate sql, int mid, int ignore_uid) { + List<Integer> userids = sql.queryForList("SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?", + new Object[] {mid, ignore_uid}, Integer.class); + if (userids.size() > 0) { + return UserQueries.getUsersByID(sql, userids); + } else { + return new ArrayList<>(); + } + } + + public static List<User> getUsersSubscribedToUserRecommendations(JdbcTemplate sql, int uid, int mid, int muid) { + List<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, mid); + + String query = "SELECT user_id IN (SELECT suser_id FROM subscr_users WHERE user_id=" + uid + ")"; + query += " AND user_id NOT IN (SELECT user_id FROM bl_users WHERE bl_user_id=" + muid + ")"; + query += " AND user_id NOT IN (SELECT suser_id FROM subscr_users WHERE user_id=" + muid + ")"; + query += " AND user_id NOT IN (SELECT suser_id FROM subscr_messages WHERE message_id=" + mid + ")"; + query += " AND user_id NOT IN (SELECT user_id FROM favorites WHERE message_id=" + mid + ")"; + query += " AND user_id NOT IN (SELECT subscr_users.suser_id FROM subscr_users INNER JOIN favorites ON (favorites.message_id=" + mid + " AND subscr_users.user_id=favorites.user_id AND favorites.user_id!=" + uid + "))"; + if (!tags.isEmpty()) { + String tagsStr = Utils.convertArrayInt2String(tags); + query += " AND user_id NOT IN (SELECT suser_id FROM subscr_tags WHERE tag_id IN (" + tagsStr + "))"; + query += " AND user_id NOT IN (SELECT user_id FROM bl_tags WHERE tag_id IN (" + tagsStr + "))"; + } + List<Integer> userids = sql.queryForList(query, Integer.class); + return UserQueries.getUsersByID(sql, userids); + } + + 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; + } + +} |