Merge branch 'develop' into SendMessage
# Conflicts: # src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java # src/main/java/org/to/telegramfinalproject/Database/PrivateChatDatabase.java
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.to.telegramfinalproject.Database;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.to.telegramfinalproject.Models.Contact;
|
||||
import org.to.telegramfinalproject.Models.ContactEntry;
|
||||
import org.to.telegramfinalproject.Models.User;
|
||||
@@ -99,6 +100,32 @@ public class ContactDatabase {
|
||||
}
|
||||
|
||||
|
||||
public static List<JSONObject> getBlockedUsers(UUID userId) {
|
||||
String sql = """
|
||||
SELECT u.internal_uuid, u.user_id, u.profile_name
|
||||
FROM contacts c
|
||||
JOIN users u ON u.internal_uuid = c.contact_id
|
||||
WHERE c.user_id = ? AND c.is_blocked = TRUE
|
||||
ORDER BY u.profile_name NULLS LAST, u.user_id
|
||||
""";
|
||||
List<JSONObject> out = new ArrayList<>();
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
JSONObject j = new JSONObject();
|
||||
j.put("internal_uuid", rs.getObject("internal_uuid").toString());
|
||||
j.put("user_id", rs.getString("user_id"));
|
||||
j.put("profile_name", rs.getString("profile_name"));
|
||||
out.add(j);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public boolean unblockContact(UUID user_id, UUID contact_id) {
|
||||
String sql = "UPDATE contacts SET is_blocked = FALSE WHERE user_id = ? AND contact_id = ?";
|
||||
|
||||
@@ -307,14 +307,87 @@ public class PrivateChatDatabase {
|
||||
return null;
|
||||
}
|
||||
|
||||
// public static UUID getOtherParticipant(UUID chatId, UUID me) {
|
||||
// List<UUID> members = getMembers(chatId);
|
||||
// for (UUID u : members) {
|
||||
// if (!u.equals(me)) return u;
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
public static UUID getOtherParticipant(UUID chatId, UUID me) {
|
||||
List<UUID> members = getMembers(chatId);
|
||||
List<UUID> members = getMembers(chatId); // باید [user1_id, user2_id] بده
|
||||
if (members == null || members.isEmpty()) return null;
|
||||
|
||||
boolean isMember = false;
|
||||
UUID other = null;
|
||||
|
||||
for (UUID u : members) {
|
||||
if (!u.equals(me)) return u;
|
||||
if (u == null) continue;
|
||||
if (u.equals(me)) {
|
||||
isMember = true;
|
||||
} else {
|
||||
other = u;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isMember) return null;
|
||||
return (other != null) ? other
|
||||
: me;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static PrivateChat findSelfChat(UUID userId) {
|
||||
String sql = """
|
||||
SELECT chat_id, user1_id, user2_id, user1_deleted, user2_deleted
|
||||
FROM private_chat
|
||||
WHERE user1_id = ? AND user2_id = ?
|
||||
LIMIT 1
|
||||
""";
|
||||
try (Connection c = ConnectionDb.connect();
|
||||
PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
ps.setObject(2, userId);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) return map(rs);
|
||||
}
|
||||
} catch (SQLException e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
public static UUID createSelfChat(UUID userId) {
|
||||
String sql = """
|
||||
INSERT INTO private_chat (chat_id, user1_id, user2_id, user1_deleted, user2_deleted, created_at)
|
||||
VALUES (gen_random_uuid(), ?, ?, FALSE, FALSE, NOW())
|
||||
RETURNING chat_id
|
||||
""";
|
||||
try (Connection c = ConnectionDb.connect();
|
||||
PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
ps.setObject(2, userId);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) return (UUID) rs.getObject("chat_id");
|
||||
}
|
||||
} catch (SQLException e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static PrivateChat map(ResultSet rs) throws SQLException {
|
||||
return new PrivateChat(
|
||||
(UUID) rs.getObject("chat_id"),
|
||||
(UUID) rs.getObject("user1_id"),
|
||||
(UUID) rs.getObject("user2_id"),
|
||||
rs.getBoolean("user1_deleted"),
|
||||
rs.getBoolean("user2_deleted")
|
||||
);
|
||||
}
|
||||
public static boolean isParticipant(java.util.UUID chatId, java.util.UUID userId) {
|
||||
String sql = """
|
||||
SELECT 1
|
||||
@@ -337,5 +410,4 @@ public class PrivateChatDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -409,5 +409,42 @@ public class userDatabase {
|
||||
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
public static String getPasswordHash(UUID userId) {
|
||||
String sql = "SELECT password FROM users WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setObject(1, userId);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) return rs.getString("password");
|
||||
}
|
||||
} catch (SQLException e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean updateUsername(UUID userId, String newUsername) throws SQLException {
|
||||
String sql = "UPDATE users SET username = ? WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, newUsername);
|
||||
ps.setObject(2, userId);
|
||||
ps.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
// 23505 = unique_violation در PostgreSQL
|
||||
if ("23505".equals(e.getSQLState())) throw e;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean updatePasswordHash(UUID userId, String newHash) {
|
||||
String sql = "UPDATE users SET password = ? WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, newHash);
|
||||
ps.setObject(2, userId);
|
||||
return ps.executeUpdate() > 0;
|
||||
} catch (SQLException e) { e.printStackTrace(); return false; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user