Merge pull request #5 from PartowRoshani/Real-Time-update
Real-Time-update
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package org.to.telegramfinalproject.Database;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.to.telegramfinalproject.Models.Channel;
|
||||
import org.to.telegramfinalproject.Models.Group;
|
||||
|
||||
@@ -7,6 +9,7 @@ import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -75,4 +78,527 @@ public class ChannelDatabase {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static List<UUID> getSubscriberUUIDs(UUID channelInternalUUID) {
|
||||
List<UUID> subscriberIds = new ArrayList<>();
|
||||
String sql = "SELECT user_id FROM channel_subscribers WHERE channel_id = ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, channelInternalUUID);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
subscriberIds.add((UUID) rs.getObject("user_id"));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return subscriberIds;
|
||||
}
|
||||
|
||||
public static Channel findByChannelId(String channelId) {
|
||||
String sql = "SELECT * FROM channels WHERE channel_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setString(1, channelId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
Channel channel = new Channel();
|
||||
channel.setInternal_uuid(UUID.fromString(rs.getString("internal_uuid")));
|
||||
channel.setChannel_id(rs.getString("channel_id"));
|
||||
channel.setChannel_name(rs.getString("channel_name"));
|
||||
channel.setImage_url(rs.getString("image_url"));
|
||||
channel.setCreator_id(UUID.fromString(rs.getString("creator_id")));
|
||||
channel.setDescription(rs.getString("description"));
|
||||
channel.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
|
||||
return channel;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static boolean isUserSubscribed(UUID userId, UUID channelInternalId) {
|
||||
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);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static UUID findInternalUUIDByChannelId(String channelId) {
|
||||
String sql = "SELECT internal_uuid FROM channels WHERE channel_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, channelId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
return (UUID) rs.getObject("internal_uuid");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static boolean addSubscriberToChannel(UUID userId, UUID channelUUID) {
|
||||
String sql = """
|
||||
INSERT INTO channel_subscribers (channel_id, user_id)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT DO NOTHING
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, channelUUID);
|
||||
stmt.setObject(2, userId);
|
||||
return stmt.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean createChannel(Channel channel, UUID creatorId) {
|
||||
String sql = """
|
||||
INSERT INTO channels (
|
||||
internal_uuid, channel_id, channel_name,
|
||||
creator_id, image_url, description, created_at
|
||||
)
|
||||
VALUES (gen_random_uuid(), ?, ?, ?, ?, ?, ?)
|
||||
RETURNING internal_uuid
|
||||
""";
|
||||
|
||||
String subscriberSql = """
|
||||
INSERT INTO channel_subscribers (channel_id, user_id) VALUES (?, ?)
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect()) {
|
||||
// مرحله اول: ساخت کانال
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, channel.getChannel_id());
|
||||
stmt.setString(2, channel.getChannel_name());
|
||||
stmt.setObject(3, creatorId);
|
||||
stmt.setString(4, channel.getImage_url());
|
||||
stmt.setString(5, channel.getDescription());
|
||||
stmt.setObject(6, channel.getCreated_at());
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if (!rs.next()) return false;
|
||||
|
||||
UUID internalUUID = (UUID) rs.getObject("internal_uuid");
|
||||
channel.setInternal_uuid(internalUUID); // اختیاری برای پیگیری بعدی
|
||||
|
||||
// مرحله دوم: افزودن کاربر به لیست سابسکرایبرها
|
||||
PreparedStatement subStmt = conn.prepareStatement(subscriberSql);
|
||||
subStmt.setObject(1, internalUUID);
|
||||
subStmt.setObject(2, creatorId);
|
||||
subStmt.executeUpdate();
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean insertChannel(UUID internalUUID, String channelId, String channelName, UUID creatorId, String imageUrl, LocalDateTime createdAt) {
|
||||
String sql = "INSERT INTO channels (internal_uuid, channel_id, channel_name, creator_id, image_url, created_at) VALUES (?, ?, ?, ?, ?, ?)";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, internalUUID);
|
||||
stmt.setString(2, channelId);
|
||||
stmt.setString(3, channelName);
|
||||
stmt.setObject(4, creatorId);
|
||||
stmt.setString(5, imageUrl);
|
||||
stmt.setObject(6, createdAt);
|
||||
stmt.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Channel findByInternalUUID(UUID internalUUID) {
|
||||
String sql = "SELECT * FROM channels WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setObject(1, internalUUID);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
Channel channel = new Channel();
|
||||
channel.setInternal_uuid(UUID.fromString(rs.getString("internal_uuid")));
|
||||
channel.setChannel_id(rs.getString("channel_id"));
|
||||
channel.setChannel_name(rs.getString("channel_name"));
|
||||
channel.setImage_url(rs.getString("image_url"));
|
||||
channel.setCreator_id(UUID.fromString(rs.getString("creator_id")));
|
||||
channel.setDescription(rs.getString("description"));
|
||||
channel.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
|
||||
return channel;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
List<JSONObject> admins = new ArrayList<>();
|
||||
|
||||
String sql = """
|
||||
SELECT u.internal_uuid, u.profile_name, u.user_id, cs.role, cs.permissions
|
||||
FROM channel_subscribers cs
|
||||
JOIN users u ON cs.user_id = u.internal_uuid
|
||||
WHERE cs.channel_id = ? AND (cs.role = 'owner' OR cs.role = 'admin')
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setObject(1, channelId);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("internal_uuid", rs.getObject("internal_uuid").toString());
|
||||
obj.put("profile_name", rs.getString("profile_name"));
|
||||
obj.put("user_id", rs.getString("user_id"));
|
||||
obj.put("role", rs.getString("role"));
|
||||
obj.put("permissions", new JSONObject(rs.getString("permissions")));
|
||||
admins.add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException 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;
|
||||
}
|
||||
|
||||
|
||||
public static boolean isUserInChannel(UUID userId, UUID channelId) {
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(
|
||||
"SELECT 1 FROM channel_subscribers WHERE channel_id = ? AND user_id = ?")) {
|
||||
stmt.setObject(1, channelId);
|
||||
stmt.setObject(2, userId);
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean removeSubscriberFromChannel(UUID channelId, UUID userId) {
|
||||
String sql = "DELETE 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);
|
||||
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
return affectedRows > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error removing subscriber from channel: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static JSONArray getChannelSubscribers(UUID channelId) {
|
||||
JSONArray subscribers = new JSONArray();
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(
|
||||
"SELECT u.internal_uuid, u.user_id, u.profile_name, " +
|
||||
"CASE WHEN cs.role = 'owner' THEN 'owner' " +
|
||||
" WHEN cs.role = 'admin' THEN 'admin' " +
|
||||
" ELSE 'subscriber' END AS role " +
|
||||
"FROM channel_subscribers cs " +
|
||||
"JOIN users u ON cs.user_id = u.internal_uuid " +
|
||||
"WHERE cs.channel_id = ?")) {
|
||||
|
||||
stmt.setObject(1, channelId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("internal_uuid", rs.getObject("internal_uuid").toString());
|
||||
obj.put("user_id", rs.getString("user_id"));
|
||||
obj.put("profile_name", rs.getString("profile_name"));
|
||||
obj.put("role", rs.getString("role"));
|
||||
subscribers.put(obj);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return subscribers;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean updateChannelInfo(UUID channelId, String newId, String name, String description, String imageUrl) {
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(
|
||||
"UPDATE channels SET channel_id = ?, channel_name = ?, description = ?, image_url = ? WHERE internal_uuid = ?")) {
|
||||
|
||||
stmt.setString(1, newId);
|
||||
stmt.setString(2, name);
|
||||
stmt.setString(3, description);
|
||||
stmt.setString(4, imageUrl);
|
||||
stmt.setObject(5, channelId);
|
||||
|
||||
int rows = stmt.executeUpdate();
|
||||
return rows > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean isChannelIdUnique(String channelId, UUID excludeChannelUUID) {
|
||||
String query = "SELECT COUNT(*) FROM channels WHERE channel_id = ? AND internal_uuid != ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(query)) {
|
||||
|
||||
stmt.setString(1, channelId);
|
||||
stmt.setObject(2, excludeChannelUUID);
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
int count = rs.getInt(1);
|
||||
return count == 0;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static boolean demoteAdminToSubscriber(UUID channelId, UUID userId) {
|
||||
String sql = "UPDATE channel_subscribers SET role = 'member', permissions = '{}'::jsonb WHERE channel_id = ? AND user_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setObject(1, channelId);
|
||||
stmt.setObject(2, userId);
|
||||
|
||||
int affected = stmt.executeUpdate();
|
||||
return affected > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean deleteChannel(UUID channelId) {
|
||||
String sql = "DELETE FROM channels WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setObject(1, channelId);
|
||||
int affected = stmt.executeUpdate();
|
||||
return affected > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean transferOwnership(UUID channelId, UUID newOwnerUUID) {
|
||||
String sql = """
|
||||
UPDATE channel_subscribers
|
||||
SET role = CASE
|
||||
WHEN user_id = ? THEN 'owner'
|
||||
WHEN role = 'owner' THEN 'admin'
|
||||
ELSE role
|
||||
END
|
||||
WHERE channel_id = ?
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setObject(1, newOwnerUUID);
|
||||
stmt.setObject(2, channelId);
|
||||
|
||||
stmt.executeUpdate();
|
||||
return true;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,13 +16,17 @@ public class ContactDatabase {
|
||||
return ConnectionDb.connect();
|
||||
}
|
||||
|
||||
public static boolean addContact(UUID userId, UUID contactId) {
|
||||
String sql = """
|
||||
INSERT INTO contacts (user_id, contact_id)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT DO NOTHING
|
||||
""";
|
||||
|
||||
public boolean addContact(UUID user_id, UUID contact_id) {
|
||||
String sql = "INSERT INTO contacts (user_id, contact_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement stmt = connection.prepareStatement(sql);
|
||||
stmt.setObject(1, user_id);
|
||||
stmt.setObject(2,contact_id);
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, userId);
|
||||
stmt.setObject(2, contactId);
|
||||
return stmt.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
@@ -31,6 +35,7 @@ public class ContactDatabase {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean removeContact(UUID user_id, UUID contact_id) {
|
||||
String sql = "DELETE FROM contacts WHERE user_id = ? AND contact_id = ?";
|
||||
try (Connection connection = getConnection()) {
|
||||
@@ -44,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()) {
|
||||
@@ -94,7 +127,7 @@ public class ContactDatabase {
|
||||
}
|
||||
|
||||
|
||||
public boolean existsContact(UUID user_id, UUID contact_id) {
|
||||
public static boolean existsContact(UUID user_id, UUID contact_id) {
|
||||
String sql = "SELECT 1 FROM contacts WHERE user_id = ? AND contact_id = ? LIMIT 1"; // stop searching when find the first item in DB(LIMIT 1)
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement stmt = connection.prepareStatement(sql);
|
||||
@@ -155,4 +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,11 +1,11 @@
|
||||
package org.to.telegramfinalproject.Database;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.to.telegramfinalproject.Models.Group;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -76,5 +76,528 @@ public class GroupDatabase {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<UUID> getMemberUUIDs(UUID groupInternalUUID) {
|
||||
List<UUID> memberIds = new ArrayList<>();
|
||||
String sql = "SELECT user_id FROM group_members WHERE group_id = ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, groupInternalUUID);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
memberIds.add((UUID) rs.getObject("user_id"));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return memberIds;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Group findByGroupId(String groupId) {
|
||||
String sql = "SELECT * FROM groups WHERE group_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setString(1, groupId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
Group group = new Group();
|
||||
group.setInternal_uuid(UUID.fromString(rs.getString("internal_uuid")));
|
||||
group.setGroup_id(rs.getString("group_id"));
|
||||
group.setGroup_name(rs.getString("group_name"));
|
||||
group.setImage_url(rs.getString("image_url"));
|
||||
group.setCreator_id(UUID.fromString(rs.getString("creator_id")));
|
||||
group.setDescription(rs.getString("description"));
|
||||
group.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
|
||||
return group;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean isUserInGroup(UUID userId, UUID groupInternalId) {
|
||||
String sql = "SELECT * FROM group_members WHERE user_id = ? AND group_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, userId);
|
||||
stmt.setObject(2, groupInternalId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static boolean updateGroupInfo(UUID internalUUID, String newGroupId, String name, String description, String imageUrl) {
|
||||
String sql = "UPDATE groups SET group_id = ?, group_name = ?, description = ?, image_url = ? WHERE internal_uuid = ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, newGroupId);
|
||||
stmt.setString(2, name);
|
||||
stmt.setString(3, description);
|
||||
if (imageUrl == null) {
|
||||
stmt.setNull(4, Types.VARCHAR);
|
||||
} else {
|
||||
stmt.setString(4, imageUrl);
|
||||
}
|
||||
stmt.setObject(5, internalUUID);
|
||||
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
return affectedRows > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean isGroupIdUnique(String groupId, UUID excludeUUID) {
|
||||
String sql = "SELECT COUNT(*) FROM groups WHERE group_id = ? AND internal_uuid != ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, groupId);
|
||||
stmt.setObject(2, excludeUUID);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1) == 0;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void addMember(UUID groupInternalId, UUID userId) {
|
||||
String sql = "INSERT INTO group_members (group_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, groupInternalId);
|
||||
stmt.setObject(2, userId);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static UUID findInternalUUIDByGroupId(String groupId) {
|
||||
String sql = "SELECT internal_uuid FROM groups WHERE group_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, groupId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
return (UUID) rs.getObject("internal_uuid");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static boolean addMemberToGroup(UUID userId, UUID groupUUID) {
|
||||
String sql = """
|
||||
INSERT INTO group_members (group_id, user_id)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT DO NOTHING
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, groupUUID);
|
||||
stmt.setObject(2, userId);
|
||||
return stmt.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static boolean createGroup(Group group, UUID creatorId) {
|
||||
String sql = """
|
||||
INSERT INTO groups (
|
||||
internal_uuid, group_id, group_name,
|
||||
creator_id, image_url, description, created_at
|
||||
)
|
||||
VALUES (gen_random_uuid(), ?, ?, ?, ?, ?, ?)
|
||||
RETURNING internal_uuid
|
||||
""";
|
||||
|
||||
String memberSql = """
|
||||
INSERT INTO group_members (group_id, user_id) VALUES (?, ?)
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect()) {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, group.getGroup_id());
|
||||
stmt.setString(2, group.getGroup_name());
|
||||
stmt.setObject(3, creatorId);
|
||||
stmt.setString(4, group.getImage_url());
|
||||
stmt.setString(5, group.getDescription());
|
||||
stmt.setObject(6, group.getCreated_at());
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if (!rs.next()) return false;
|
||||
|
||||
UUID internalUUID = (UUID) rs.getObject("internal_uuid");
|
||||
group.setInternal_uuid(internalUUID);
|
||||
|
||||
PreparedStatement memberStmt = conn.prepareStatement(memberSql);
|
||||
memberStmt.setObject(1, internalUUID);
|
||||
memberStmt.setObject(2, creatorId);
|
||||
memberStmt.executeUpdate();
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean insertGroup(UUID internalUUID, String groupId, String groupName, UUID creatorId, String imageUrl, LocalDateTime createdAt) {
|
||||
String sql = "INSERT INTO groups (internal_uuid, group_id, group_name, creator_id, image_url, created_at) VALUES (?, ?, ?, ?, ?, ?)";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, internalUUID);
|
||||
stmt.setString(2, groupId);
|
||||
stmt.setString(3, groupName);
|
||||
stmt.setObject(4, creatorId);
|
||||
stmt.setString(5, imageUrl);
|
||||
stmt.setObject(6, createdAt);
|
||||
stmt.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void addMember(UUID groupId, UUID userId, String role) {
|
||||
String sql = "INSERT INTO group_members (group_id, user_id, role) VALUES (?, ?, ?) ON CONFLICT DO NOTHING";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, groupId);
|
||||
stmt.setObject(2, userId);
|
||||
stmt.setString(3, role);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Group findByInternalUUID(UUID internalUUID) {
|
||||
String sql = "SELECT * FROM groups WHERE internal_uuid = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setObject(1, internalUUID);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
Group group = new Group();
|
||||
group.setInternal_uuid(UUID.fromString(rs.getString("internal_uuid")));
|
||||
group.setGroup_id(rs.getString("group_id"));
|
||||
group.setGroup_name(rs.getString("group_name"));
|
||||
group.setImage_url(rs.getString("image_url"));
|
||||
group.setCreator_id(UUID.fromString(rs.getString("creator_id")));
|
||||
group.setDescription(rs.getString("description"));
|
||||
group.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
|
||||
return group;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
List<JSONObject> admins = new ArrayList<>();
|
||||
|
||||
String sql = "SELECT gm.user_id, gm.role, gm.permissions, u.profile_name " +
|
||||
"FROM group_members gm " +
|
||||
"JOIN users u ON gm.user_id = u.internal_uuid " +
|
||||
"WHERE gm.group_id = ? AND gm.role IN ('owner', 'admin')";
|
||||
|
||||
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")));
|
||||
obj.put("profile_name", rs.getString("profile_name"));
|
||||
admins.add(obj);
|
||||
}
|
||||
|
||||
} catch (SQLException 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean transferOwnership(UUID groupId, UUID newOwnerId) {
|
||||
String demoteOldOwner = "UPDATE group_members SET role = 'admin' WHERE group_id = ? AND role = 'owner'";
|
||||
String promoteNewOwner = "UPDATE group_members SET role = 'owner' WHERE group_id = ? AND user_id = ?";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect()) {
|
||||
conn.setAutoCommit(false);
|
||||
|
||||
try (PreparedStatement demoteStmt = conn.prepareStatement(demoteOldOwner);
|
||||
PreparedStatement promoteStmt = conn.prepareStatement(promoteNewOwner)) {
|
||||
|
||||
demoteStmt.setObject(1, groupId);
|
||||
demoteStmt.executeUpdate();
|
||||
|
||||
promoteStmt.setObject(1, groupId);
|
||||
promoteStmt.setObject(2, newOwnerId);
|
||||
promoteStmt.executeUpdate();
|
||||
|
||||
conn.commit();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
conn.rollback();
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static boolean deleteGroup(UUID groupId) {
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement("DELETE FROM groups WHERE internal_uuid = ?")) {
|
||||
|
||||
stmt.setObject(1, groupId);
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
|
||||
return affectedRows > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -12,6 +12,41 @@ import java.util.stream.Collectors;
|
||||
public class MessageDatabase {
|
||||
|
||||
|
||||
public static void save(Message message) {
|
||||
String sql = """
|
||||
INSERT INTO messages (
|
||||
message_id, sender_id, receiver_type, receiver_id, content,
|
||||
message_type, file_url, send_at, status,
|
||||
reply_to_id, is_edited, original_message_id, forwarded_by, forwarded_from
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setObject(1, message.getMessage_id());
|
||||
stmt.setObject(2, message.getSender_id());
|
||||
stmt.setString(3, message.getReceiver_type());
|
||||
stmt.setObject(4, message.getReceiver_id());
|
||||
stmt.setString(5, message.getContent());
|
||||
stmt.setString(6, message.getMessage_type());
|
||||
stmt.setString(7, message.getFile_url());
|
||||
stmt.setObject(8, message.getSend_at());
|
||||
stmt.setString(9, message.getStatus());
|
||||
stmt.setObject(10, message.getReply_to_id());
|
||||
stmt.setBoolean(11, message.isIs_edited());
|
||||
stmt.setObject(12, message.getOriginal_message_id());
|
||||
stmt.setObject(13, message.getForwarded_by());
|
||||
stmt.setObject(14, message.getForwarded_from());
|
||||
|
||||
stmt.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("❌ Error saving message: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void markMessageAsRead(UUID messageId, UUID userId) {
|
||||
String sql = "INSERT INTO message_receipts (message_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
@@ -182,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();
|
||||
@@ -228,7 +228,6 @@ public class userDatabase {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static User findByInternalUUID(UUID internalUuid) {
|
||||
String sql = "SELECT * FROM users WHERE internal_uuid = ?";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user