From 0a9cd317b878d6c2c777ec4eb19173a7d651abe5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 11 May 2016 15:34:44 +0300 Subject: fix fill users, part 2 --- src/main/java/com/juick/server/PMQueries.java | 40 ++++++--------------------- 1 file changed, 8 insertions(+), 32 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index d1d826cc..6de30995 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -100,39 +100,15 @@ public class PMQueries { } public static List getPMLastConversationsUsers(JdbcTemplate sql, int uid, int cnt) { - List users = sql.query("SELECT user_id,unread FROM pm_streams " - + "WHERE user_id_to=? AND unread>0 " - + "ORDER BY lastmessage DESC LIMIT " + cnt, (rs, rowNum) -> { + return sql.query("SELECT users.id, users.nick,pm_streams.unread FROM pm_streams " + + "INNER JOIN users ON users.id = pm_streams.user_id WHERE user_id_to=? " + + "ORDER BY pm_streams.lastmessage DESC LIMIT ?", (rs, rowNum) -> { com.juick.User u = new com.juick.User(); u.setUID(rs.getInt(1)); - u.MessagesCount = rs.getInt(2); + u.setUName(rs.getString(2)); + u.MessagesCount = rs.getInt(3); return u; - }, uid); - - if (users.size() < cnt) { - List addUsers = sql.query("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 ?", (rs, num) -> { - User u = new com.juick.User(); - int uuid = rs.getInt(1) + rs.getInt(2) - uid; - u.setUID(uuid); - return u; - }, uid, uid, cnt * 2); - for (User addUser : addUsers) { - if (!haveUserInArray(users, addUser.getUID())) { - users.add(addUser); - if (users.size() >= cnt) { - break; - } - } - } - } - if (!users.isEmpty()) { - UserQueries.fillUsersByID(sql, users); - } - - return users; + }, uid, cnt); } public static boolean haveUserInArray(List arr, int uid) { @@ -156,10 +132,10 @@ public class PMQueries { msg.setText(rs.getString(2)); msg.setDate(rs.getTimestamp(3)); return msg; - }, new Object[]{uid, uid_to, uid, uid_to}); + }, uid, uid_to, uid, uid_to); sql.update("UPDATE pm_streams SET lastview=NOW(),unread=0 " - + "WHERE user_id_to=? AND user_id=?", new Object[]{uid, uid_to}); + + "WHERE user_id_to=? AND user_id=?", uid, uid_to); return msgs; } -- cgit v1.2.3