RealTime and openChat

This commit is contained in:
2025-06-30 22:22:08 +03:30
parent 5afe3662d4
commit ee037532a1
20 changed files with 2222 additions and 275 deletions
@@ -1,5 +1,6 @@
package org.to.telegramfinalproject.Database;
import org.json.JSONObject;
import org.to.telegramfinalproject.Models.Channel;
import org.to.telegramfinalproject.Models.Group;
@@ -81,7 +82,7 @@ public class ChannelDatabase {
public static List<UUID> getSubscriberUUIDs(UUID channelInternalUUID) {
List<UUID> subscriberIds = new ArrayList<>();
String sql = "SELECT user_id FROM channel_subscribe WHERE channel_id = ?";
String sql = "SELECT user_id FROM channel_subscribers WHERE channel_id = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
@@ -128,7 +129,7 @@ public class ChannelDatabase {
public static boolean isUserSubscribed(UUID userId, UUID channelInternalId) {
String sql = "SELECT * FROM channel_subscribe WHERE user_id = ? AND channel_id = ?";
String sql = "SELECT * FROM channel_subscribers WHERE user_id = ? AND channel_id = ?";
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, userId);
stmt.setObject(2, channelInternalId);
@@ -240,12 +241,14 @@ public class ChannelDatabase {
}
}
public static void addSubscriber(UUID channelId, UUID userId) {
String sql = "INSERT INTO channel_subscribers (channel_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
public static void addSubscriber(UUID channelId, UUID userId, String role) {
String sql = "INSERT INTO channel_subscribers (channel_id, user_id, role) VALUES (?, ?, ?) ON CONFLICT DO NOTHING";
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, channelId);
stmt.setObject(2, userId);
stmt.setString(3, role);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
@@ -279,4 +282,142 @@ public class ChannelDatabase {
}
public static boolean addOwnerToChannel(UUID channelId, UUID userId) {
String sql = "INSERT INTO channel_subscribers (channel_id, user_id, role) VALUES (?, ?, 'owner')";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, channelId);
stmt.setObject(2, userId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean addAdminToChannel(UUID channelId, UUID userId, JSONObject permissions) {
String sql = "UPDATE channel_subscribers SET role = 'admin', permissions = ?::jsonb WHERE channel_id = ? AND user_id = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, permissions.toString());
stmt.setObject(2, channelId);
stmt.setObject(3, userId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static String getChannelRole(UUID channelId, UUID userId) {
String sql = "SELECT role FROM channel_subscribers WHERE channel_id = ? AND user_id = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, channelId);
stmt.setObject(2, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return rs.getString("role");
}
} catch (SQLException e) {
e.printStackTrace();
}
return "subscriber"; // پیش‌فرض
}
public static JSONObject getChannelPermissions(UUID channelId, UUID userId) {
String sql = "SELECT permissions FROM channel_subscribers WHERE channel_id = ? AND user_id = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, channelId);
stmt.setObject(2, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return new JSONObject(rs.getString("permissions"));
}
} catch (Exception e) {
e.printStackTrace();
}
return new JSONObject();
}
public static boolean updateChannelAdminPermissions(UUID channelId, UUID userId, JSONObject permissions) {
String sql = "UPDATE channel_subscribers SET permissions = ?::jsonb WHERE channel_id = ? AND user_id = ? AND role = 'admin'";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, permissions.toString());
stmt.setObject(2, channelId);
stmt.setObject(3, userId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static List<JSONObject> getChannelAdminsAndOwner(UUID channelId) {
String sql = "SELECT user_id, role, permissions FROM channel_subscribers WHERE channel_id = ? AND role IN ('owner', 'admin')";
List<JSONObject> admins = new ArrayList<>();
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, channelId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
JSONObject obj = new JSONObject();
obj.put("user_id", rs.getObject("user_id").toString());
obj.put("role", rs.getString("role"));
obj.put("permissions", new JSONObject(rs.getString("permissions")));
admins.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
}
return admins;
}
public static boolean isOwner(UUID channelId, UUID userId) {
String sql = "SELECT role FROM channel_subscribers WHERE channel_id = ? AND user_id = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, channelId);
stmt.setObject(2, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return "owner".equals(rs.getString("role"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static boolean isAdmin(UUID channelId, UUID userId) {
String sql = "SELECT role FROM channel_subscribers WHERE channel_id = ? AND user_id = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, channelId);
stmt.setObject(2, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String role = rs.getString("role");
return "admin".equals(role) || "owner".equals(role); // owner هم admin هست
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
@@ -49,19 +49,47 @@ public class ContactDatabase {
}
}
public boolean blockContact(UUID user_id, UUID contact_id) {
String sql = "UPDATE contacts SET is_blocked = TRUE WHERE user_id = ? AND contact_id = ?";
try (Connection connection = getConnection()) {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setObject(1, user_id);
stmt.setObject(2, contact_id);
return stmt.executeUpdate() > 0;
public static boolean toggleBlock(UUID userId, UUID targetId) {
String selectSql = "SELECT is_blocked FROM contacts WHERE user_id = ? AND contact_id = ?";
String updateSql = "UPDATE contacts SET is_blocked = ? WHERE user_id = ? AND contact_id = ?";
try (Connection conn = getConnection();
PreparedStatement selectStmt = conn.prepareStatement(selectSql)) {
selectStmt.setObject(1, userId);
selectStmt.setObject(2, targetId);
ResultSet rs = selectStmt.executeQuery();
if (rs.next()) {
boolean currentlyBlocked = rs.getBoolean("is_blocked");
try (PreparedStatement updateStmt = conn.prepareStatement(updateSql)) {
updateStmt.setBoolean(1, !currentlyBlocked);
updateStmt.setObject(2, userId);
updateStmt.setObject(3, targetId);
updateStmt.executeUpdate();
}
return !currentlyBlocked;
} else {
// اگر رابطه وجود نداره، اول باید کاربر رو به contact ها اضافه کنیم
String insertSql = "INSERT INTO contacts (user_id, contact_id, is_blocked) VALUES (?, ?, ?)";
try (PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
insertStmt.setObject(1, userId);
insertStmt.setObject(2, targetId);
insertStmt.setBoolean(3, true);
insertStmt.executeUpdate();
}
return true;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return false;
}
public boolean unblockContact(UUID user_id, UUID contact_id) {
String sql = "UPDATE contacts SET is_blocked = FALSE WHERE user_id = ? AND contact_id = ?";
try (Connection connection = getConnection()) {
@@ -160,6 +188,76 @@ public class ContactDatabase {
}
public static boolean deleteChatOneSide(UUID currentUserId, UUID otherUserId) {
String sql = """
UPDATE private_chat
SET user1_deleted = CASE WHEN user1_id = ? THEN TRUE ELSE user1_deleted END,
user2_deleted = CASE WHEN user2_id = ? THEN TRUE ELSE user2_deleted END
WHERE (user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)
""";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, currentUserId);
stmt.setObject(2, currentUserId);
stmt.setObject(3, currentUserId);
stmt.setObject(4, otherUserId);
stmt.setObject(5, otherUserId);
stmt.setObject(6, currentUserId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean deleteChatBoth(UUID currentUserId, UUID otherUserId) {
String sqlDeleteMessages = """
DELETE FROM messages
WHERE receiver_type = 'private' AND (
(sender_id = ? AND receiver_id = ?) OR
(sender_id = ? AND receiver_id = ?)
)
""";
String sqlDeleteChat = """
DELETE FROM private_chat
WHERE (user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)
""";
try (Connection conn = getConnection()) {
conn.setAutoCommit(false);
try (PreparedStatement stmtMsg = conn.prepareStatement(sqlDeleteMessages);
PreparedStatement stmtChat = conn.prepareStatement(sqlDeleteChat)) {
stmtMsg.setObject(1, currentUserId);
stmtMsg.setObject(2, otherUserId);
stmtMsg.setObject(3, otherUserId);
stmtMsg.setObject(4, currentUserId);
stmtMsg.executeUpdate();
stmtChat.setObject(1, currentUserId);
stmtChat.setObject(2, otherUserId);
stmtChat.setObject(3, otherUserId);
stmtChat.setObject(4, currentUserId);
stmtChat.executeUpdate();
conn.commit();
return true;
} catch (SQLException e) {
conn.rollback();
e.printStackTrace();
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
@@ -1,5 +1,7 @@
package org.to.telegramfinalproject.Database;
import org.json.JSONArray;
import org.json.JSONObject;
import org.to.telegramfinalproject.Models.Group;
import java.sql.Connection;
@@ -284,4 +286,223 @@ public class GroupDatabase {
}
public static boolean addOwnerToGroup(UUID groupId, UUID userId) {
String sql = "INSERT INTO group_members (group_id, user_id, role) VALUES (?, ?, 'owner')";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
stmt.setObject(2, userId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean addAdminToGroup(UUID groupId, UUID userId, JSONObject permissions) {
String sql = """
UPDATE group_members
SET role = 'admin',
permissions = ?::jsonb
WHERE group_id = ? AND user_id = ? AND role = 'member'
""";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, permissions.toString());
stmt.setObject(2, groupId);
stmt.setObject(3, userId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static String getGroupRole(UUID groupId, UUID userId) {
String sql = "SELECT role FROM group_members WHERE group_id = ? AND user_id = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
stmt.setObject(2, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return rs.getString("role");
}
} catch (SQLException e) {
e.printStackTrace();
}
return "member"; // پیش‌فرض
}
public static boolean updateGroupAdminPermissions(UUID groupId, UUID userId, JSONObject permissions) {
String sql = """
UPDATE group_members
SET permissions = ?::jsonb
WHERE group_id = ? AND user_id = ? AND role = 'admin'
""";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, permissions.toString());
stmt.setObject(2, groupId);
stmt.setObject(3, userId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static List<JSONObject> getGroupAdminsAndOwner(UUID groupId) {
String sql = "SELECT user_id, role, permissions FROM group_members WHERE group_id = ? AND role IN ('owner', 'admin')";
List<JSONObject> admins = new ArrayList<>();
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
JSONObject obj = new JSONObject();
obj.put("user_id", rs.getObject("user_id").toString());
obj.put("role", rs.getString("role"));
obj.put("permissions", new JSONObject(rs.getString("permissions")));
admins.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
}
return admins;
}
public static boolean isOwner(UUID groupId, UUID userId) {
String sql = "SELECT 1 FROM group_members WHERE group_id = ? AND user_id = ? AND role = 'owner'";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
stmt.setObject(2, userId);
ResultSet rs = stmt.executeQuery();
return rs.next(); // اگر رکوردی پیدا شد یعنی owner است
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static boolean isAdmin(UUID groupId, UUID userId) {
String sql = "SELECT role FROM group_members WHERE group_id = ? AND user_id = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
stmt.setObject(2, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String role = rs.getString("role");
return "admin".equals(role) || "owner".equals(role); // owner هم admin هست
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static JSONObject getGroupPermissions(UUID groupId, UUID userId) {
String sql = "SELECT permissions FROM group_members WHERE group_id = ? AND user_id = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
stmt.setObject(2, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String permissions = rs.getString("permissions");
if (permissions != null && !permissions.isBlank()) {
return new JSONObject(permissions);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return new JSONObject();
}
public static JSONArray getGroupMembers(UUID groupId) {
String sql = """
SELECT u.profile_name, u.user_id, u.internal_uuid, gm.role, gm.permissions
FROM group_members gm
JOIN users u ON gm.user_id = u.internal_uuid
WHERE gm.group_id = ?
""";
JSONArray members = new JSONArray();
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
JSONObject member = new JSONObject();
member.put("profile_name", rs.getString("profile_name"));
member.put("user_id", rs.getString("user_id")); // آیدی قابل نمایش
member.put("internal_uuid", rs.getObject("internal_uuid").toString());
member.put("role", rs.getString("role"));
String permissions = rs.getString("permissions");
if (permissions != null && !permissions.isBlank()) {
member.put("permissions", new JSONObject(permissions));
}
members.put(member);
}
return members;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static boolean demoteAdminToMember(UUID groupId, UUID userId) {
String sql = "UPDATE group_members SET role = 'member', permissions = '{}'::jsonb WHERE group_id = ? AND user_id = ? AND role = 'admin'";
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
stmt.setObject(2, userId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean removeMemberFromGroup(UUID groupId, UUID userId) {
String sql = "DELETE FROM group_members WHERE group_id = ? AND user_id = ?";
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
stmt.setObject(2, userId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
@@ -217,6 +217,80 @@ public class MessageDatabase {
return result;
}
public static List<Message> privateChatHistory(UUID user1, UUID user2) {
List<Message> result = new ArrayList<>();
String sql = """
SELECT * FROM messages
WHERE receiver_type = 'private'
AND (
(sender_id = ? AND receiver_id = ?)
OR (sender_id = ? AND receiver_id = ?)
)
ORDER BY send_at
""";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, user1);
stmt.setObject(2, user2);
stmt.setObject(3, user2);
stmt.setObject(4, user1);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
result.add(extractMessage(rs));
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public static List<Message> groupChatHistory(UUID groupId) {
List<Message> result = new ArrayList<>();
String sql = """
SELECT * FROM messages
WHERE receiver_type = 'group' AND receiver_id = ?
ORDER BY send_at
""";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, groupId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
result.add(extractMessage(rs));
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public static List<Message> channelChatHistory(UUID channelId) {
List<Message> result = new ArrayList<>();
String sql = """
SELECT * FROM messages
WHERE receiver_type = 'channel' AND receiver_id = ?
ORDER BY send_at
""";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, channelId);
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<>();
@@ -21,7 +21,7 @@ public class userDatabase {
try {
User var6;
try (Connection conn = this.getConnection()) {
try (Connection conn = ConnectionDb.connect()) {
try (PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, userId);
ResultSet rs = stmt.executeQuery();