diff options
author | Vitaly Takmazov | 2015-10-24 18:09:15 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2015-10-24 18:09:15 +0300 |
commit | d00fd7705cb1a4b085eed7f34df2f1c6d9a69f76 (patch) | |
tree | 70a83e0e30a4195855484f820c0322e84405bc04 /src/main/java/com/juick/server | |
parent | b5df0e1c2664a8a1a0f9042745dd608773fedce3 (diff) |
moving to Gradle
Diffstat (limited to 'src/main/java/com/juick/server')
-rw-r--r-- | src/main/java/com/juick/server/AdsQueries.java | 51 | ||||
-rw-r--r-- | src/main/java/com/juick/server/CrosspostQueries.java | 80 | ||||
-rw-r--r-- | src/main/java/com/juick/server/MessagesQueries.java | 1065 | ||||
-rw-r--r-- | src/main/java/com/juick/server/PMQueries.java | 303 | ||||
-rw-r--r-- | src/main/java/com/juick/server/PushQueries.java | 68 | ||||
-rw-r--r-- | src/main/java/com/juick/server/SQLHelpers.java | 163 | ||||
-rw-r--r-- | src/main/java/com/juick/server/SubscriptionsQueries.java | 168 | ||||
-rw-r--r-- | src/main/java/com/juick/server/TagQueries.java | 189 | ||||
-rw-r--r-- | src/main/java/com/juick/server/UserQueries.java | 592 | ||||
-rw-r--r-- | src/main/java/com/juick/server/Utils.java | 86 |
10 files changed, 2765 insertions, 0 deletions
diff --git a/src/main/java/com/juick/server/AdsQueries.java b/src/main/java/com/juick/server/AdsQueries.java new file mode 100644 index 00000000..0fb4c0a6 --- /dev/null +++ b/src/main/java/com/juick/server/AdsQueries.java @@ -0,0 +1,51 @@ +/* + * Juick + * Copyright (C) 2008-2011, ugnich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * + * @author ugnich + */ +public class AdsQueries { + + public static int getAdMID(Connection sql, int uid) { + if (uid > 0) { + return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1", uid, 0); + } else { + return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", 0); + } + } + + public static void logAdMID(Connection sql, int uid, int mid) { + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO ads_messages_log(user_id,message_id,ts) VALUES (?,?,UNIX_TIMESTAMP())"); + stmt.setInt(1, uid); + stmt.setInt(2, mid); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } +} diff --git a/src/main/java/com/juick/server/CrosspostQueries.java b/src/main/java/com/juick/server/CrosspostQueries.java new file mode 100644 index 00000000..fd677048 --- /dev/null +++ b/src/main/java/com/juick/server/CrosspostQueries.java @@ -0,0 +1,80 @@ +/* + * Juick + * Copyright (C) 2008-2013, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * + * @author ugnich + */ +public class CrosspostQueries { + + public static String[] getTwitterTokens(Connection sql, int uid) { + String tokens[] = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT access_token,access_token_secret FROM twitter WHERE user_id=? AND crosspost=1"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + tokens = new String[2]; + tokens[0] = rs.getString(1); + tokens[1] = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return tokens; + } + + public static String getFacebookToken(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT access_token FROM facebook WHERE user_id=? AND access_token IS NOT NULL AND crosspost=1", uid); + } + + public static String[] getVKTokens(Connection sql, int uid) { + String tokens[] = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT vk_id,access_token FROM vk WHERE user_id=? AND crosspost=1"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + tokens = new String[2]; + tokens[0] = rs.getString(1); + tokens[1] = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return tokens; + } +} diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java new file mode 100644 index 00000000..8bef9018 --- /dev/null +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -0,0 +1,1065 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; +import java.util.ArrayList; +import java.util.Collections; + +/** + * + * @author Ugnich Anton + */ +public class MessagesQueries { + + public static int createMessage(Connection sql, int uid, String txt, String attachment, ArrayList<com.juick.Tag> tags) { + int mid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("INSERT INTO messages(user_id,attach) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); + stmt.setInt(1, uid); + if (attachment != null) { + stmt.setString(2, attachment); + } else { + stmt.setNull(2, Types.VARCHAR); + } + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + mid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (mid > 0) { + String tagsNames = ""; + String tagsIDs = ""; + + for (int i = 0; i < tags.size(); i++) { + if (i > 0) { + tagsNames += " "; + tagsIDs += ","; + } + tagsNames += tags.get(i).Name; + tagsIDs += "(" + mid + "," + tags.get(i).TID + ")"; + } + + try { + stmt = sql.prepareStatement("INSERT INTO messages_tags(message_id,tag_id) VALUES " + tagsIDs); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + try { + stmt = sql.prepareStatement("INSERT INTO messages_txt(message_id,tags,txt) VALUES (?,?,?)"); + stmt.setInt(1, mid); + stmt.setString(2, tagsNames); + stmt.setString(3, txt); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return mid; + } + + public static int createReply(Connection sql, int mid, int rid, int uid, String txt, String attachment) { + int ridnew = getReplyIDIncrement(sql, mid); + + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO replies(message_id,reply_id,user_id,replyto,attach,txt) VALUES (?,?,?,?,?,?)"); + stmt.setInt(1, mid); + stmt.setInt(2, ridnew); + stmt.setInt(3, uid); + stmt.setInt(4, rid); + if (attachment != null) { + stmt.setString(5, attachment); + } else { + stmt.setNull(5, Types.VARCHAR); + } + stmt.setString(6, txt); + if (stmt.executeUpdate() == 0) { + ridnew = 0; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (ridnew > 0) { + SQLHelpers.executeInt(sql, "UPDATE messages SET replies=replies+1 WHERE message_id=?", mid); + } + + return ridnew; + } + + public static int getReplyIDIncrement(Connection sql, int mid) { + int rid = 0; + + SQLHelpers.execute(sql, "LOCK TABLES messages WRITE"); + if (SQLHelpers.executeInt(sql, "UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid) == 1) { + rid = SQLHelpers.getInt(sql, "SELECT maxreplyid FROM messages WHERE message_id=?", mid, 0); + } + SQLHelpers.execute(sql, "UNLOCK TABLES"); + + return rid; + } + + public static boolean recommendMessage(Connection sql, int mid, int vuid) { + boolean res = SQLHelpers.execute(sql, "INSERT IGNORE INTO favorites(user_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; + if (res) { + SQLHelpers.executeInt(sql, "UPDATE messages SET likes=likes+1 WHERE message_id=?", mid); + } + return res; + } + + public static boolean canViewThread(Connection sql, int mid, int uid) { + + int privacy = 0; + int owner_uid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT user_id,privacy FROM messages WHERE messages.message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + owner_uid = rs.getInt(1); + privacy = rs.getInt(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return privacy >= 0 + || uid == owner_uid + || ((privacy == -1 || privacy == -2) && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)); + } + + public static boolean isReadOnly(Connection sql, int mid) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT readonly FROM messages WHERE message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static boolean isSubscribed(Connection sql, int uid, int mid) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM subscr_messages WHERE suser_id=? AND message_id=?"); + stmt.setInt(1, uid); + stmt.setInt(2, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static int getMessagePrivacy(Connection sql, int mid) { + return SQLHelpers.getInt(sql, "SELECT privacy FROM messages WHERE message_id=?", mid, -4); + } + + public static com.juick.Message getMessage(Connection sql, int mid) { + com.juick.Message msg = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT messages.user_id,users.nick,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.readonly,messages.privacy,messages.replies,messages.attach,messages.place_id,messages.lat,messages.lon,messages.likes,messages.hidden FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + msg = new com.juick.Message(); + msg.MID = mid; + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.TimeAgo = rs.getInt(3); + msg.TimestampString = rs.getString(4); + msg.ReadOnly = rs.getBoolean(5); + msg.Privacy = rs.getInt(6); + msg.FriendsOnly = msg.Privacy < 0; + msg.Replies = rs.getInt(7); + msg.AttachmentType = rs.getString(8); + //if (rs.getInt(9) > 0) { + // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); + //} + if (rs.getDouble(10) != 0) { + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(10); + msg.Place.lon = rs.getDouble(11); + } + msg.Likes = rs.getInt(12); + msg.Hidden = rs.getBoolean(13); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (msg == null) { + return null; + } + + try { + stmt = sql.prepareStatement("SELECT tags,repliesby,txt FROM messages_txt WHERE message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + if (rs.getString(1) != null) { + msg.parseTags(rs.getString(1)); + } + msg.RepliesBy = rs.getString(2); + msg.Text = rs.getString(3); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msg; + } + + public static com.juick.Message getReply(Connection sql, int mid, int rid) { + com.juick.Message msg = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT replies.user_id,users.nick,replies.replyto,DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s'),replies.attach,replies.txt FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? AND replies.reply_id=?"); + stmt.setInt(1, mid); + stmt.setInt(2, rid); + rs = stmt.executeQuery(); + if (rs.first()) { + msg = new com.juick.Message(); + msg.MID = mid; + msg.RID = rid; + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.ReplyTo = rs.getInt(3); + msg.TimestampString = rs.getString(4); + msg.AttachmentType = rs.getString(5); + msg.Text = rs.getString(6); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msg; + } + + public static com.juick.User getMessageAuthor(Connection sql, int mid) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT messages.user_id,users.nick FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static ArrayList<com.juick.Tag> getMessageTags(Connection sql, int mid) { + ArrayList<com.juick.Tag> tags = new ArrayList<com.juick.Tag>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tags.tag_id,synonym_id,name,stat_messages FROM tags INNER JOIN messages_tags ON (messages_tags.message_id=? AND messages_tags.tag_id=tags.tag_id)"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Tag t = new com.juick.Tag(); + t.TID = rs.getInt(1); + t.SynonymID = rs.getInt(2); + t.Name = rs.getString(3); + t.UsageCnt = rs.getInt(4); + tags.add(t); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return tags; + } + + public static ArrayList<Integer> getMessageTagsIDs(Connection sql, int mid) { + ArrayList<Integer> tids = new ArrayList<Integer>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tag_id FROM messages_tags WHERE message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + tids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return tids; + } + + public static ArrayList<String> getMessageRecommendations(Connection sql, int mid) { + ArrayList<String> users = new ArrayList<String>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.nick FROM favorites INNER JOIN users ON (favorites.message_id=? AND favorites.user_id=users.id)"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + users.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return users; + } + + public static ArrayList<Integer> getAll(Connection sql, int visitor_uid, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (visitor_uid > 1) { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id<? AND hidden=0 AND (privacy>0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + stmt.setInt(2, visitor_uid); + stmt.setInt(3, visitor_uid); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0 AND (privacy>0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, visitor_uid); + stmt.setInt(2, visitor_uid); + } + } else { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id<? AND hidden=0 AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0 AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + } + + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getTag(Connection sql, int tid, int visitor_uid, int before, int cnt) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { +// stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (SELECT tag_id FROM tags WHERE tag_id=? OR synonym_id=?) AND messages.message_id<? AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); + stmt = sql.prepareStatement("SELECT message_id FROM (tags INNER JOIN messages_tags ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) INNER JOIN messages USING(message_id) WHERE messages.message_id<? AND (messages.privacy>0 OR messages.user_id=?) ORDER BY message_id DESC LIMIT ?"); + stmt.setInt(1, tid); + stmt.setInt(2, tid); + stmt.setInt(3, before); + stmt.setInt(4, visitor_uid); + stmt.setInt(5, cnt); + } else { + //stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (SELECT tag_id FROM tags WHERE tag_id=? OR synonym_id=?) AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); + stmt = sql.prepareStatement("SELECT message_id FROM (tags INNER JOIN messages_tags ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) INNER JOIN messages USING(message_id) WHERE messages.privacy>0 OR messages.user_id=? ORDER BY message_id DESC LIMIT ?"); + stmt.setInt(1, tid); + stmt.setInt(2, tid); + stmt.setInt(3, visitor_uid); + stmt.setInt(4, cnt); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getTags(Connection sql, String tids, int visitor_uid, int before, int cnt) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") AND messages.message_id<? AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); + stmt.setInt(1, before); + stmt.setInt(2, visitor_uid); + stmt.setInt(3, cnt); + } else { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); + stmt.setInt(1, visitor_uid); + stmt.setInt(2, cnt); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getPlace(Connection sql, int place_id, int visitor_uid, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE place_id=? AND message_id<? AND (privacy>0 OR user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, place_id); + stmt.setInt(2, before); + stmt.setInt(3, visitor_uid); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE place_id=? AND (privacy>0 OR user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, place_id); + stmt.setInt(2, visitor_uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getMyFeed(Connection sql, int uid, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(40); + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) WHERE message_id<? AND (privacy>=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, before); + stmt.setInt(3, uid); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) WHERE (privacy>=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id<? ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + Collections.sort(mids, Collections.reverseOrder()); + int remove = mids.size() - 20; + for (int i = 0; i < remove; i++) { + mids.remove(20); + } + + return mids; + } + + public static ArrayList<Integer> getPrivate(Connection sql, int uid, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy<0 AND message_id<? ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy<0 ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getDiscussions(Connection sql, int uid, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM subscr_messages WHERE suser_id=? AND message_id<? ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM subscr_messages WHERE suser_id=? ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return mids; + } + + public static ArrayList<Integer> getRecommended(Connection sql, int uid, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM favorites WHERE user_id IN (SELECT user_id FROM subscr_users WHERE suser_id=?) AND message_id<? ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM favorites WHERE user_id IN (SELECT user_id FROM subscr_users WHERE suser_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + + return mids; + } + + public static ArrayList<Integer> getPopular(Connection sql, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id<? AND privacy>0 AND popular>0 ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND popular>0 ORDER BY message_id DESC LIMIT 20"); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getPhotos(Connection sql, int visitor_uid, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id<? AND (privacy>0 OR user_id=?) AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + stmt.setInt(2, visitor_uid); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, visitor_uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getSearch(Connection sql, Connection sqlSearch, String search, int before) { + ArrayList<Integer> mids0 = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE MATCH(?) AND id<? ORDER BY id DESC LIMIT 25"); + stmt.setString(1, search); + stmt.setInt(2, before); + } else { + stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE MATCH(?) ORDER BY id DESC LIMIT 25"); + stmt.setString(1, search); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids0.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + ArrayList<Integer> mids = new ArrayList<Integer>(20); + if (mids0.size() > 0) { + try { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + } + + return mids; + } + + public static ArrayList<Integer> getUserBlog(Connection sql, int UID, int privacy, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id<? AND privacy>=" + privacy + " ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + " ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getUserTag(Connection sql, int UID, int TID, int privacy, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? AND messages.message_id<? AND messages.privacy>=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setInt(2, TID); + stmt.setInt(3, before); + } else { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? AND messages.privacy>=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setInt(2, TID); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getUserRecommendations(Connection sql, int UID, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM favorites WHERE user_id=? AND message_id<? ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM favorites WHERE user_id=? ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getUserPhotos(Connection sql, int UID, int privacy, int before) { + ArrayList<Integer> mids = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id<? AND privacy>=" + privacy + " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList<Integer> getUserSearch(Connection sql, Connection sqlSearch, int UID, String search, int privacy, int before) { + ArrayList<Integer> mids0 = new ArrayList<Integer>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE user_id=? AND MATCH(?) AND id<? ORDER BY id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setString(2, search); + stmt.setInt(3, before); + } else { + stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE user_id=? AND MATCH(?) ORDER BY id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setString(2, search); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids0.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + ArrayList<Integer> mids = new ArrayList<Integer>(20); + if (mids0.size() > 0) { + try { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>=" + privacy + " ORDER BY message_id DESC"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + } + + return mids; + } + + public static ArrayList<com.juick.Message> getMessages(Connection sql, ArrayList<Integer> mids) { + ArrayList<com.juick.Message> msgs = new ArrayList<com.juick.Message>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT STRAIGHT_JOIN messages.message_id,messages.user_id,users.nick,messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.replies,messages_txt.repliesby,messages.attach,messages.lat,messages.lon,messages.likes FROM (messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id IN (" + Utils.convertArrayInt2String(mids) + ") ORDER BY messages.message_id DESC"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + msg.User = new com.juick.User(); + + msg.MID = rs.getInt(1); + msg.User.UID = rs.getInt(2); + msg.User.UName = rs.getString(3); + if (rs.getString(4) != null) { + msg.parseTags(rs.getString(4)); + } + msg.ReadOnly = rs.getInt(5) == 1; + msg.Privacy = rs.getInt(6); + msg.FriendsOnly = msg.Privacy < 0; + msg.Text = rs.getString(7); + msg.TimeAgo = rs.getInt(8); + msg.TimestampString = rs.getString(9); + msg.Replies = rs.getInt(10); + msg.RepliesBy = rs.getString(11); + msg.AttachmentType = rs.getString(12); + if (rs.getDouble(13) != 0) { + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(13); + msg.Place.lon = rs.getDouble(14); + } + msg.Likes = rs.getInt(15); + + msgs.add(msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msgs; + } + + public static ArrayList<com.juick.Message> getReplies(Connection sql, int mid) { + ArrayList<com.juick.Message> replies = new ArrayList<com.juick.Message>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT replies.reply_id,replies.replyto,replies.user_id,users.nick,users.banned,replies.txt,TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s'),replies.attach FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? ORDER BY replies.reply_id ASC"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + msg.MID = mid; + msg.RID = rs.getInt(1); + msg.ReplyTo = rs.getInt(2); + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(3); + msg.User.UName = rs.getString(4); + msg.User.Banned = rs.getBoolean(5); + msg.Text = rs.getString(6); + msg.TimeAgo = rs.getInt(7); + msg.TimestampString = rs.getString(8); + msg.AttachmentType = rs.getString(9); + + replies.add(msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return replies; + } + + public static boolean setMessagePopular(Connection sql, int mid, int popular) { + boolean ret = false; + + PreparedStatement stmt = null; + try { + if (popular == -2) { + stmt = sql.prepareStatement("UPDATE messages SET hidden=1 WHERE message_id=?"); + stmt.setInt(1, mid); + } else if (popular == -1) { + stmt = sql.prepareStatement("UPDATE messages SET popular=0 WHERE message_id=?"); + stmt.setInt(1, mid); + } else { + stmt = sql.prepareStatement("UPDATE messages SET popular=? WHERE message_id=?"); + stmt.setInt(1, popular); + stmt.setInt(2, mid); + } + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (popular == -1) { + try { + stmt = sql.prepareStatement("INSERT INTO top_ignore_messages VALUES (?)"); + stmt.setInt(1, mid); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return ret; + } + + public static boolean setMessagePrivacy(Connection sql, int mid) { + boolean ret = false; + + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("UPDATE messages SET privacy=1 WHERE message_id=?"); + stmt.setInt(1, mid); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + return ret; + } +} diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java new file mode 100644 index 00000000..56b91abf --- /dev/null +++ b/src/main/java/com/juick/server/PMQueries.java @@ -0,0 +1,303 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +/** + * + * @author Ugnich Anton + */ +public class PMQueries { + + public static boolean createPM(Connection sql, int uid_from, int uid_to, String body) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO pm(user_id,user_id_to,txt) VALUES (?,?,?)"); + stmt.setInt(1, uid_from); + stmt.setInt(2, uid_to); + stmt.setString(3, body); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (ret) { + PreparedStatement stmt2 = null; + try { + stmt2 = sql.prepareStatement("INSERT INTO pm_streams(user_id,user_id_to,lastmessage,unread) VALUES (?,?,NOW(),1) ON DUPLICATE KEY UPDATE lastmessage=NOW(),unread=unread+1"); + stmt2.setInt(1, uid_from); + stmt2.setInt(2, uid_to); + stmt2.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt2); + } + } + + return ret; + } + + public static boolean addPMinRoster(Connection sql, int uid, String jid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO pm_inroster(user_id,jid) VALUES (?,?)"); + stmt.setInt(1, uid); + stmt.setString(2, jid); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return ret; + } + + public static boolean removePMinRoster(Connection sql, int uid, String jid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("DELETE FROM pm_inroster WHERE user_id=? AND jid=?"); + stmt.setInt(1, uid); + stmt.setString(2, jid); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return ret; + } + + public static boolean havePMinRoster(Connection sql, int uid, String jid) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM pm_inroster WHERE user_id=? AND jid=?"); + stmt.setInt(1, uid); + stmt.setString(2, jid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = true; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static String getLastView(Connection sql, int uid_from, int uid_to) { + String ret = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT lastview FROM pm_streams WHERE user_id=? AND user_id_to=?"); + stmt.setInt(1, uid_from); + stmt.setInt(2, uid_to); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getString(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static ArrayList<com.juick.User> getPMLastConversationsUsers(Connection sql, int uid, int cnt) { + ArrayList<com.juick.User> users = new ArrayList<com.juick.User>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT user_id,unread FROM pm_streams WHERE user_id_to=? AND unread>0 ORDER BY lastmessage DESC LIMIT " + cnt); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User u = new com.juick.User(); + u.UID = rs.getInt(1); + u.MessagesCount = rs.getInt(2); + users.add(u); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (users.size() < cnt) { + try { + stmt = sql.prepareStatement("SELECT user_id,user_id_to FROM pm_streams WHERE (user_id=? OR user_id_to=?) AND lastmessage>TIMESTAMPADD(MONTH,-1,NOW()) ORDER BY lastmessage DESC LIMIT ?"); + stmt.setInt(1, uid); + stmt.setInt(2, uid); + stmt.setInt(3, cnt * 2); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + int uuid = rs.getInt(1) + rs.getInt(2) - uid; + if (!haveUserInArray(users, uuid)) { + com.juick.User u = new com.juick.User(); + u.UID = uuid; + users.add(u); + if (users.size() >= cnt) { + break; + } + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + } + + if (!users.isEmpty()) { + UserQueries.fillUsersByID(sql, users); + } + + return users; + } + + public static boolean haveUserInArray(ArrayList<com.juick.User> arr, int uid) { + int s = arr.size(); + for (int i = 0; i < s; i++) { + if (arr.get(i).UID == uid) { + return true; + } + } + return false; + } + + public static ArrayList<com.juick.Message> getPMMessages(Connection sql, int uid, int uid_to) { + ArrayList<com.juick.Message> msgs = new ArrayList<com.juick.Message>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT user_id,txt,ts FROM pm WHERE (user_id=? AND user_id_to=?) OR (user_id_to=? AND user_id=?) ORDER BY ts DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, uid_to); + stmt.setInt(3, uid); + stmt.setInt(4, uid_to); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + int uuid = rs.getInt(1); + msg.User = new com.juick.User(); + msg.User.UID = uuid; + msg.Text = rs.getString(2); + msg.TimestampString = rs.getString(3); + msgs.add(0, msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + PreparedStatement stmt2 = null; + try { + stmt2 = sql.prepareStatement("UPDATE pm_streams SET lastview=NOW(),unread=0 WHERE user_id_to=? AND user_id=?"); + stmt2.setInt(1, uid); + stmt2.setInt(2, uid_to); + stmt2.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt2); + } + + return msgs; + } + + public static ArrayList<com.juick.Message> getLastPMInbox(Connection sql, int uid) { + ArrayList<com.juick.Message> msgs = new ArrayList<com.juick.Message>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT pm.user_id,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),DATE_FORMAT(pm.ts,'%Y-%m-%d %H:%i:%s') FROM pm INNER JOIN users ON pm.user_id=users.id WHERE pm.user_id_to=? ORDER BY pm.ts DESC LIMIT 20"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.Text = rs.getString(3); + msg.TimeAgo = rs.getInt(4); + msg.TimestampString = rs.getString(5); + msgs.add(0, msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msgs; + } + + public static ArrayList<com.juick.Message> getLastPMSent(Connection sql, int uid) { + ArrayList<com.juick.Message> msgs = new ArrayList<com.juick.Message>(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT pm.user_id_to,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),DATE_FORMAT(pm.ts,'%Y-%m-%d %H:%i:%s') FROM pm INNER JOIN users ON pm.user_id_to=users.id WHERE pm.user_id=? ORDER BY pm.ts DESC LIMIT 20"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.Text = rs.getString(3); + msg.TimeAgo = rs.getInt(4); + msg.TimestampString = rs.getString(5); + msgs.add(0, msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msgs; + } +} diff --git a/src/main/java/com/juick/server/PushQueries.java b/src/main/java/com/juick/server/PushQueries.java new file mode 100644 index 00000000..84f42722 --- /dev/null +++ b/src/main/java/com/juick/server/PushQueries.java @@ -0,0 +1,68 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +/** + * + * @author ugnich + */ +public class PushQueries { + + public static String getAndroidRegID(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT regid FROM android WHERE user_id=?", uid); + } + + public static ArrayList<String> getAndroidSubscribers(Connection sql, int uid) { + ArrayList<String> regids = new ArrayList<String>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT regid FROM android INNER JOIN subscr_users ON (subscr_users.user_id=? AND android.user_id=subscr_users.suser_id)"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + regids.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return regids; + } + + public static String getWinPhoneURL(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT url FROM winphone WHERE user_id=?", uid); + } + + public static ArrayList<String> getWinPhoneSubscribers(Connection sql, int uid) { + ArrayList<String> urls = new ArrayList<String>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT url FROM winphone INNER JOIN subscr_users ON (subscr_users.user_id=? AND winphone.user_id=subscr_users.suser_id)"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + urls.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return urls; + } +} diff --git a/src/main/java/com/juick/server/SQLHelpers.java b/src/main/java/com/juick/server/SQLHelpers.java new file mode 100644 index 00000000..f5569993 --- /dev/null +++ b/src/main/java/com/juick/server/SQLHelpers.java @@ -0,0 +1,163 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +/** + * + * @author ugnich + */ +public class SQLHelpers { + + public static int execute(Connection sql, String query) { + int ret = -1; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement(query); + ret = stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return ret; + } + + public static int executeInt(Connection sql, String query, int param) { + int ret = -1; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement(query); + stmt.setInt(1, param); + ret = stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return ret; + } + + public static int getInt(Connection sql, String query, int defvalue) { + int ret = defvalue; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static int getInt(Connection sql, String query, int param, int defvalue) { + int ret = defvalue; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setInt(1, param); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static int getInt(Connection sql, String query, String param, int defvalue) { + int ret = defvalue; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setString(1, param); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static String getString(Connection sql, String query, int param) { + String ret = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setInt(1, param); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getString(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static String getString(Connection sql, String query, String param) { + String ret = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setString(1, param); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getString(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static ArrayList<Integer> getArrayInteger(Connection sql, String query, int param) { + ArrayList<Integer> ret = new ArrayList<Integer>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setInt(1, param); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + ret.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } +} 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..d0f5f308 --- /dev/null +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -0,0 +1,168 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +/** + * + * @author ugnich + */ +public class SubscriptionsQueries { + + public static ArrayList<String> getJIDSubscribedToUser(Connection sql, int uid, boolean friendsonly) { + ArrayList<String> jids = new ArrayList<String>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (friendsonly == false) { + stmt = sql.prepareStatement("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"); + stmt.setInt(1, uid); + } else { + stmt = sql.prepareStatement("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=?)"); + stmt.setInt(1, uid); + stmt.setInt(2, uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + jids.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return jids; + } + + public static ArrayList<String> getJIDSubscribedToUserAndTags(Connection sql, int uid, int mid) { + ArrayList<String> jids = new ArrayList<String>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + + String tbl = "subscr_jids_" + mid; + ArrayList<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, mid); + + try { + stmt = sql.prepareStatement("CREATE TEMPORARY TABLE " + tbl + "(user_id INT UNSIGNED NOT NULL) ENGINE=MEMORY"); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + try { + String query = "INSERT INTO " + tbl + " SELECT suser_id FROM subscr_users WHERE user_id=" + uid; + if (!tags.isEmpty()) { + query += " UNION DISTINCT SELECT suser_id FROM subscr_tags WHERE tag_id IN (" + Utils.convertArrayInt2String(tags) + ") AND suser_id!=" + uid; + } + stmt = sql.prepareStatement(query); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + + try { + String query = "SELECT jids.jid FROM " + tbl + " INNER JOIN jids ON (" + tbl + ".user_id=jids.user_id) WHERE jids.active=1 AND " + tbl + ".user_id NOT IN (SELECT user_id FROM bl_users WHERE bl_user_id=" + uid + ")"; + if (!tags.isEmpty()) { + query += " AND " + tbl + ".user_id NOT IN (SELECT user_id FROM bl_tags WHERE tag_id IN (" + Utils.convertArrayInt2String(tags) + "))"; + } + stmt = sql.prepareStatement(query); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + jids.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + try { + stmt = sql.prepareStatement("DROP TABLE " + tbl); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + return jids; + } + + public static ArrayList<String> getJIDSubscribedToComments(Connection sql, int mid, int ignore_uid) { + ArrayList<String> jids = new ArrayList<String>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT jids.jid FROM subscr_messages INNER JOIN jids ON (subscr_messages.message_id=? AND subscr_messages.suser_id=jids.user_id) WHERE jids.user_id!=? AND jids.active=1"); + stmt.setInt(1, mid); + stmt.setInt(2, ignore_uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + jids.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return jids; + } + + public static ArrayList<String> getJIDSubscribedToUserRecommendations(Connection sql, int uid, int mid, int muid) { + ArrayList<String> jids = new ArrayList<String>(); + + ArrayList<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, mid); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + String query = "SELECT jid FROM jids WHERE active=1 AND user_id!=" + uid; + query += " AND 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 + "))"; + } + + stmt = sql.prepareStatement(query); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + jids.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return jids; + } + + public static boolean subscribeMessage(Connection sql, int mid, int vuid) { + return SQLHelpers.execute(sql, "INSERT IGNORE INTO subscr_messages(suser_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; + } +} diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java new file mode 100644 index 00000000..493e8675 --- /dev/null +++ b/src/main/java/com/juick/server/TagQueries.java @@ -0,0 +1,189 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +/** + * + * @author Ugnich Anton + */ +public class TagQueries { + + public static com.juick.Tag getTag(Connection sql, int tid) { + com.juick.Tag ret = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT synonym_id,name FROM tags WHERE tag_id=?"); + stmt.setInt(1, tid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = new com.juick.Tag(); + ret.TID = tid; + ret.SynonymID = rs.getInt(1); + ret.Name = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { + com.juick.Tag ret = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tag_id,synonym_id,name FROM tags WHERE name=?"); + stmt.setString(1, tag); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = new com.juick.Tag(); + ret.TID = rs.getInt(1); + ret.SynonymID = rs.getInt(2); + ret.Name = rs.getString(3); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (ret == null && autoCreate) { + ret = new com.juick.Tag(); + ret.TID = createTag(sql, tag); + ret.Name = tag; + } + + return ret; + } + + public static ArrayList<com.juick.Tag> getTags(Connection sql, String[] tags, boolean autoCreate) { + ArrayList<com.juick.Tag> ret = new ArrayList<com.juick.Tag>(); + + for (int i = 0; i < tags.length; i++) { + if (!tags[i].isEmpty()) { + com.juick.Tag t = getTag(sql, tags[i], autoCreate); + if (t != null) { + ret.add(t); + } + } + } + + return ret; + } + + public static boolean getTagNoIndex(Connection sql, int tag_id) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT noindex FROM tags WHERE tag_id=?"); + stmt.setInt(1, tag_id); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static int createTag(Connection sql, String name) { + int ret = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, name); + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static ArrayList<com.juick.Tag> getUserTagsAll(Connection sql, int uid) { + ArrayList<com.juick.Tag> tags = new ArrayList<com.juick.Tag>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tags.name,COUNT(messages.message_id) FROM (messages INNER JOIN messages_tags ON (messages.user_id=? AND messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id GROUP BY tags.tag_id ORDER BY tags.name ASC"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Tag t = new com.juick.Tag(); + t.Name = rs.getString(1); + t.UsageCnt = rs.getInt(2); + tags.add(t); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return tags; + } + + public static ArrayList<String> getUserBLTags(Connection sql, int uid) { + ArrayList<String> tags = new ArrayList<String>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tags.name FROM tags INNER JOIN bl_tags ON (bl_tags.user_id=? AND bl_tags.tag_id=tags.tag_id) ORDER BY tags.name"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + tags.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return tags; + } +} diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java new file mode 100644 index 00000000..d1313715 --- /dev/null +++ b/src/main/java/com/juick/server/UserQueries.java @@ -0,0 +1,592 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.server; + +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.Random; +import java.util.UUID; + +/** + * + * @author Ugnich Anton + */ +public class UserQueries { + + static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + public static String getSignUpHashByJID(Connection sql, String jid) { + String hash = SQLHelpers.getString(sql, "SELECT loginhash FROM jids WHERE jid=? AND user_id IS NULL", jid); + + if (hash == null) { + hash = UUID.randomUUID().toString(); + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO jids(jid,loginhash) VALUES (?,?)"); + stmt.setString(1, jid); + stmt.setString(2, hash); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return hash; + } + + public static int createUser(Connection sql, String username, String password) { + int uid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("INSERT INTO users(nick,passw) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, username); + stmt.setString(2, password); + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + uid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + SQLHelpers.executeInt(sql, "INSERT INTO useroptions(user_id) VALUES (?)", uid); + SQLHelpers.executeInt(sql, "INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)", uid); + + return uid; + } + + public static com.juick.User getUserByUID(Connection sql, int uid) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT nick,banned FROM users WHERE id=?"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = uid; + user.UName = rs.getString(1); + user.Banned = rs.getBoolean(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static com.juick.User getUserByName(Connection sql, String username) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick,banned FROM users WHERE nick=?"); + stmt.setString(1, username); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + user.Banned = rs.getBoolean(3); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static com.juick.User getUserByJID(Connection sql, String jid) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)"); + stmt.setString(1, jid); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + user.JID = jid; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static ArrayList<com.juick.User> getUsersByName(Connection sql, ArrayList<String> unames) { + ArrayList<com.juick.User> users = new ArrayList<com.juick.User>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + users.add(user); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return users; + } + + public static ArrayList<com.juick.User> getUsersByID(Connection sql, ArrayList<Integer> uids) { + ArrayList<com.juick.User> users = new ArrayList<com.juick.User>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id IN (" + Utils.convertArrayInt2String(uids) + ")"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User u = new com.juick.User(); + u.UID = rs.getInt(1); + u.UName = rs.getString(2); + users.add(u); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return users; + } + + public static boolean fillUsersByID(Connection sql, ArrayList<com.juick.User> users) { + boolean ret = false; + + String uids = ""; + final int usersSize = users.size(); + for (int i = 0; i < usersSize; i++) { + if (i > 0) { + uids += ","; + } + uids += users.get(i).UID; + } + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id IN (" + uids + ")"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + int uid = rs.getInt(1); + for (int i = 0; i < usersSize; i++) { + if (users.get(i).UID == uid) { + users.get(i).UName = rs.getString(2); + ret = true; + } + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static ArrayList<com.juick.User> getUsersByJID(Connection sql, ArrayList<String> jids) { + ArrayList<com.juick.User> users = new ArrayList<com.juick.User>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.id,users.nick,jids.jid FROM users INNER JOIN jids ON jids.user_id=users.id WHERE jids.jid IN (" + Utils.convertArrayString2String(jids) + ")"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + user.JID = rs.getString(3); + users.add(user); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return users; + } + + public static String getJIDbyUID(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT jid FROM jids WHERE user_id=? AND active=1", uid); + } + + public static int getUIDbyJID(Connection sql, String jid) { + return SQLHelpers.getInt(sql, "SELECT user_id FROM jids WHERE jid=?", jid, 0); + } + + public static int getUIDbyName(Connection sql, String uname) { + return SQLHelpers.getInt(sql, "SELECT id FROM users WHERE nick=?", uname, 0); + } + + public static int getUIDbyHash(Connection sql, String hash) { + return SQLHelpers.getInt(sql, "SELECT user_id FROM logins WHERE hash=?", hash, 0); + } + + public static com.juick.User getUserByHash(Connection sql, String hash) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT logins.user_id,users.nick FROM logins INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?"); + stmt.setString(1, hash); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + user.AuthHash = hash; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static String getHashByUID(Connection sql, int uid) { + String hash = SQLHelpers.getString(sql, "SELECT hash FROM logins WHERE user_id=?", uid); + + if (hash == null) { + hash = generateHash(16); + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO logins(user_id,hash) VALUES (?,?)"); + stmt.setInt(1, uid); + stmt.setString(2, hash); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return hash; + } + + public static String generateHash(int len) { + Random rnd = new Random(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + sb.append(ABCDEF.charAt(rnd.nextInt(ABCDEF.length()))); + } + return sb.toString(); + } + + public static boolean checkUserNameValid(String uname) { + return uname != null && uname.length() >= 2 && uname.length() <= 16 && uname.matches("[a-zA-Z0-9\\-]+"); + } + + public static int checkPassword(Connection sql, String username, String password) { + int uid = 0; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,passw FROM users WHERE nick=?"); + stmt.setString(1, username); + rs = stmt.executeQuery(); + if (rs.first()) { + if (password.equals(rs.getString(2))) { + uid = rs.getInt(1); + } else { + uid = -1; + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return uid; + } + + public static int getUserOptionInt(Connection sql, int uid, String option, int defaultValue) { + int ret = defaultValue; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT " + option + " FROM useroptions WHERE user_id=?"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static void setUserOptionInt(Connection sql, int uid, String option, int value) { + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("UPDATE useroptions SET " + option + "=? WHERE user_id=?"); + stmt.setInt(1, value); + stmt.setInt(2, uid); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + public static boolean getCanMedia(Connection sql, int uid) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.lastphoto-UNIX_TIMESTAMP() FROM users WHERE id=?"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) < 3600; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static boolean isInWL(Connection sql, int uid, int check) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM wl_users WHERE user_id=? AND wl_user_id=?"); + stmt.setInt(1, uid); + stmt.setInt(2, check); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static boolean isInBL(Connection sql, int uid, int check) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM bl_users WHERE user_id=? AND bl_user_id=?"); + stmt.setInt(1, uid); + stmt.setInt(2, check); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static boolean isInBLAny(Connection sql, int uid, int uid2) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM bl_users WHERE (user_id=? AND bl_user_id=?) OR (user_id=? AND bl_user_id=?)"); + stmt.setInt(1, uid); + stmt.setInt(2, uid2); + stmt.setInt(3, uid2); + stmt.setInt(4, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static ArrayList<Integer> checkBL(Connection sql, int visitor, ArrayList<Integer> uids) { + ArrayList<Integer> ret = new ArrayList<Integer>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT user_id FROM bl_users WHERE bl_user_id=? and user_id IN (" + Utils.convertArrayInt2String(uids) + ")"); + stmt.setInt(1, visitor); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + ret.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static boolean isSubscribed(Connection sql, int uid, int check) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM subscr_users WHERE suser_id=? AND user_id=?"); + stmt.setInt(1, uid); + stmt.setInt(2, check); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static ArrayList<Integer> getUserRead(Connection sql, int uid) { + return SQLHelpers.getArrayInteger(sql, "SELECT user_id FROM subscr_users WHERE suser_id=?", uid); + } + + public static ArrayList<com.juick.User> getUserReadLeastPopular(Connection sql, int uid, int cnt) { + ArrayList<com.juick.User> users = new ArrayList<com.juick.User>(cnt); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.id,users.nick FROM (subscr_users INNER JOIN users_subscr ON (subscr_users.suser_id=? AND subscr_users.user_id=users_subscr.user_id)) INNER JOIN users ON subscr_users.user_id=users.id ORDER BY cnt LIMIT ?"); + stmt.setInt(1, uid); + stmt.setInt(2, cnt); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User u = new com.juick.User(); + u.UID = rs.getInt(1); + u.UName = rs.getString(2); + users.add(u); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return users; + } + + public static ArrayList<Integer> getUserReaders(Connection sql, int uid) { + return SQLHelpers.getArrayInteger(sql, "SELECT suser_id FROM subscr_users WHERE user_id=?", uid); + } + + public static ArrayList<com.juick.User> getUserBLUsers(Connection sql, int uid) { + ArrayList<com.juick.User> users = new ArrayList<com.juick.User>(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.id,users.nick FROM users INNER JOIN bl_users ON(bl_users.bl_user_id=users.id) WHERE bl_users.user_id=? ORDER BY users.nick"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User u = new com.juick.User(); + u.UID = rs.getInt(1); + u.UName = rs.getString(2); + users.add(u); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return users; + } + + public static int getStatsIRead(Connection sql, int uid) { + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE suser_id=?", uid, 0); + } + + public static int getStatsMyReaders(Connection sql, int uid) { + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE user_id=?", uid, 0); + } + + public static int getStatsMessages(Connection sql, int uid) { + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM messages WHERE user_id=?", uid, 0); + } + + public static int getStatsReplies(Connection sql, int uid) { + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM replies WHERE user_id=?", uid, 0); + } +} diff --git a/src/main/java/com/juick/server/Utils.java b/src/main/java/com/juick/server/Utils.java new file mode 100644 index 00000000..f97797d7 --- /dev/null +++ b/src/main/java/com/juick/server/Utils.java @@ -0,0 +1,86 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.server; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +/** + * + * @author Ugnich Anton + */ +public class Utils { + + public static String convertArrayInt2String(ArrayList<Integer> mids) { + String q = ""; + for (int i = 0; i < mids.size(); i++) { + if (i > 0) { + q += ","; + } + q += mids.get(i); + } + return q; + } + + public static String convertArrayString2String(ArrayList<String> unames) { + String q = ""; + for (int i = 0; i < unames.size(); i++) { + if (i > 0) { + q += ","; + } + q += "\"" + unames.get(i) + "\""; + } + return q; + } + + public static String buildQueryArray(String query1, int length, String query2) { + String ret = query1; + for (int i = 0; i < length; i++) { + if (i > 0) { + ret += ","; + } + ret += "?"; + } + ret += query2; + return ret; + } + + public static void stmtSetStringArray(PreparedStatement stmt, int offset, String strs[]) throws SQLException { + for (int i = 0; i < strs.length; i++) { + stmt.setString(offset + i, strs[i]); + } + } + + public static void finishSQL(ResultSet rs, Statement stmt) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + } + } + } +} |