Searching system in messages and update status and last seen for users
This commit is contained in:
@@ -7,6 +7,7 @@ import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MessageDatabase {
|
||||
|
||||
@@ -132,4 +133,122 @@ public class MessageDatabase {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static Message extractMessage(ResultSet rs) throws SQLException {
|
||||
return new Message(
|
||||
UUID.fromString(rs.getString("message_id")),
|
||||
UUID.fromString(rs.getString("sender_id")),
|
||||
rs.getString("receiver_type"),
|
||||
UUID.fromString(rs.getString("receiver_id")),
|
||||
rs.getString("content"),
|
||||
rs.getString("message_type"),
|
||||
rs.getString("file_url"),
|
||||
rs.getTimestamp("send_at").toLocalDateTime(),
|
||||
rs.getString("status"),
|
||||
(UUID) rs.getObject("reply_to_id"),
|
||||
rs.getBoolean("is_edited"),
|
||||
(UUID) rs.getObject("original_message_id"),
|
||||
(UUID) rs.getObject("forwarded_by"),
|
||||
(UUID) rs.getObject("forwarded_from")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static List<Message> searchMessagesForUser(UUID userId, String keyword) {
|
||||
List<Message> result = new ArrayList<>();
|
||||
String sql = """
|
||||
SELECT * FROM messages
|
||||
WHERE receiver_type = 'private'
|
||||
AND (sender_id = ? OR receiver_id = ?)
|
||||
AND content ILIKE ?
|
||||
ORDER BY send_at DESC
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, userId);
|
||||
stmt.setObject(2, userId);
|
||||
stmt.setString(3, "%" + keyword + "%");
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
result.add(extractMessage(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static List<Message> searchMessagesInGroups(List<UUID> groupIds, String keyword) {
|
||||
List<Message> result = new ArrayList<>();
|
||||
if (groupIds.isEmpty()) return result;
|
||||
|
||||
String placeholders = groupIds.stream().map(id -> "?").collect(Collectors.joining(", "));
|
||||
String sql = """
|
||||
SELECT * FROM messages
|
||||
WHERE receiver_type = 'group'
|
||||
AND receiver_id IN (%s)
|
||||
AND content ILIKE ?
|
||||
ORDER BY send_at DESC
|
||||
""".formatted(placeholders);
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
int i = 1;
|
||||
for (UUID id : groupIds) {
|
||||
stmt.setObject(i++, id);
|
||||
}
|
||||
stmt.setString(i, "%" + keyword + "%");
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
result.add(extractMessage(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static List<Message> searchMessagesInChannels(List<UUID> channelIds, String keyword) {
|
||||
List<Message> result = new ArrayList<>();
|
||||
if (channelIds.isEmpty()) return result;
|
||||
|
||||
String placeholders = channelIds.stream().map(id -> "?").collect(Collectors.joining(", "));
|
||||
String sql = """
|
||||
SELECT * FROM messages
|
||||
WHERE receiver_type = 'channel'
|
||||
AND receiver_id IN (%s)
|
||||
AND content ILIKE ?
|
||||
ORDER BY send_at DESC
|
||||
""".formatted(placeholders);
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
int i = 1;
|
||||
for (UUID id : channelIds) {
|
||||
stmt.setObject(i++, id);
|
||||
}
|
||||
stmt.setString(i, "%" + keyword + "%");
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
result.add(extractMessage(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -208,12 +208,14 @@ public class userDatabase {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, status);
|
||||
stmt.setObject(2, uuid);
|
||||
stmt.executeUpdate();
|
||||
int rows = stmt.executeUpdate();
|
||||
System.out.println("🔁 updateUserStatus: set '" + status + "' for " + uuid + " → affected rows = " + rows);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void updateLastSeen(UUID uuid) {
|
||||
String sql = "UPDATE users SET last_seen = CURRENT_TIMESTAMP WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
@@ -263,14 +265,21 @@ public class userDatabase {
|
||||
|
||||
return null;
|
||||
}
|
||||
public List<User> searchUsers(String keyword, UUID currentUserId) {
|
||||
String query = """
|
||||
SELECT * FROM users
|
||||
WHERE (user_id ILIKE ? OR profile_name ILIKE ?)
|
||||
AND internal_uuid <> ?
|
||||
""";
|
||||
|
||||
public List<User> searchUsers(String keyword) {
|
||||
String query = "SELECT * FROM users WHERE user_id ILIKE ? OR profile_name ILIKE ?"; //(ILIKE) case_insensitive
|
||||
List<User> result = new ArrayList<>();
|
||||
try (Connection conn = getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(query)) {
|
||||
|
||||
stmt.setString(1, "%" + keyword + "%");
|
||||
stmt.setString(2, "%" + keyword + "%");
|
||||
stmt.setObject(3, currentUserId);
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
result.add(extractUser(rs));
|
||||
@@ -282,5 +291,18 @@ public class userDatabase {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void setAllUsersOffline() {
|
||||
String sql = "UPDATE users SET status = 'offline'";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
int affected = stmt.executeUpdate();
|
||||
System.out.println("🔁 All users set to offline. Rows affected: " + affected);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user