Work on block/ unblock
This commit is contained in:
@@ -81,7 +81,6 @@ public class ContactDatabase {
|
|||||||
}
|
}
|
||||||
return !currentlyBlocked;
|
return !currentlyBlocked;
|
||||||
} else {
|
} else {
|
||||||
// اگر رابطه وجود نداره، اول باید کاربر رو به contact ها اضافه کنیم
|
|
||||||
String insertSql = "INSERT INTO contacts (user_id, contact_id, is_blocked) VALUES (?, ?, ?)";
|
String insertSql = "INSERT INTO contacts (user_id, contact_id, is_blocked) VALUES (?, ?, ?)";
|
||||||
try (PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
|
try (PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
|
||||||
insertStmt.setObject(1, userId);
|
insertStmt.setObject(1, userId);
|
||||||
@@ -151,8 +150,13 @@ public class ContactDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean eitherBlocks(UUID userA, UUID userB) {
|
||||||
|
return isBlocked(userA, userB) || isBlocked(userB, userA);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isBlocked(UUID user_id, UUID contact_id) {
|
|
||||||
|
|
||||||
|
public static boolean isBlocked(UUID user_id, UUID contact_id) {
|
||||||
String sql = "SELECT is_blocked FROM contacts WHERE user_id = ? AND contact_id = ?";
|
String sql = "SELECT is_blocked FROM contacts WHERE user_id = ? AND contact_id = ?";
|
||||||
try (Connection connection = getConnection()) {
|
try (Connection connection = getConnection()) {
|
||||||
PreparedStatement stmt = connection.prepareStatement(sql);
|
PreparedStatement stmt = connection.prepareStatement(sql);
|
||||||
|
|||||||
@@ -2283,6 +2283,12 @@ public class ClientHandler implements Runnable {
|
|||||||
String content = requestJson.getString("content");
|
String content = requestJson.getString("content");
|
||||||
String receiverType = requestJson.getString("receiver_type");
|
String receiverType = requestJson.getString("receiver_type");
|
||||||
UUID receiverId = UUID.fromString(requestJson.getString("receiver_id"));
|
UUID receiverId = UUID.fromString(requestJson.getString("receiver_id"));
|
||||||
|
//For block
|
||||||
|
if ("private".equals(receiverType) && !canSendToPrivate(receiverId, senderId)) {
|
||||||
|
response = new ResponseModel("error", "You can't message this user (blocked).");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
UUID replyToId = UUID.fromString(requestJson.getString("reply_to_id"));
|
UUID replyToId = UUID.fromString(requestJson.getString("reply_to_id"));
|
||||||
|
|
||||||
Message message = new Message(
|
Message message = new Message(
|
||||||
@@ -2305,7 +2311,9 @@ public class ClientHandler implements Runnable {
|
|||||||
.put("excerpt", excerpt));
|
.put("excerpt", excerpt));
|
||||||
|
|
||||||
List<UUID> receivers = Receivers.resolveFor(receiverType, receiverId, senderId);
|
List<UUID> receivers = Receivers.resolveFor(receiverType, receiverId, senderId);
|
||||||
RealTimeEventDispatcher.sendNewMessage(message, receivers, "reply", meta);
|
//RealTimeEventDispatcher.sendNewMessage(message, receivers, "reply", meta);
|
||||||
|
RealTimeEventDispatcher.sendNewMessageFiltered(message, receivers, senderId, "reply", meta);
|
||||||
|
|
||||||
|
|
||||||
response = saved ?
|
response = saved ?
|
||||||
new ResponseModel("success", "Reply sent") :
|
new ResponseModel("success", "Reply sent") :
|
||||||
@@ -2314,10 +2322,17 @@ public class ClientHandler implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "forward_message" : {
|
case "forward_message" : {
|
||||||
|
UUID senderId = currentUser.getInternal_uuid();
|
||||||
UUID originalMessageId = UUID.fromString(requestJson.getString("original_message_id"));
|
UUID originalMessageId = UUID.fromString(requestJson.getString("original_message_id"));
|
||||||
UUID targetChatId = UUID.fromString(requestJson.getString("target_chat_id"));
|
UUID targetChatId = UUID.fromString(requestJson.getString("target_chat_id"));
|
||||||
String targetChatType = requestJson.getString("target_chat_type");
|
String targetChatType = requestJson.getString("target_chat_type");
|
||||||
|
|
||||||
|
//For block
|
||||||
|
if ("private".equals(targetChatType) && !canSendToPrivate(targetChatId, senderId)) {
|
||||||
|
response = new ResponseModel("error", "You can't message this user (blocked).");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// original message
|
// original message
|
||||||
Message original = MessageDatabase.findById(originalMessageId);
|
Message original = MessageDatabase.findById(originalMessageId);
|
||||||
if (original == null) {
|
if (original == null) {
|
||||||
@@ -2355,7 +2370,9 @@ public class ClientHandler implements Runnable {
|
|||||||
.put("sender_name", userDatabase.findByInternalUUID(original.getSender_id()).getProfile_name()));
|
.put("sender_name", userDatabase.findByInternalUUID(original.getSender_id()).getProfile_name()));
|
||||||
|
|
||||||
List<UUID> receivers = Receivers.resolveFor(targetChatType, targetChatId, currentUser.getInternal_uuid());
|
List<UUID> receivers = Receivers.resolveFor(targetChatType, targetChatId, currentUser.getInternal_uuid());
|
||||||
RealTimeEventDispatcher.sendNewMessage(forwarded, receivers, "forward", meta);
|
//RealTimeEventDispatcher.sendNewMessage(forwarded, receivers, "forward", meta);
|
||||||
|
RealTimeEventDispatcher.sendNewMessageFiltered(forwarded, receivers, senderId, "forward", meta);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
response = new ResponseModel("error", "Failed to forward message.");
|
response = new ResponseModel("error", "Failed to forward message.");
|
||||||
}
|
}
|
||||||
@@ -2466,6 +2483,13 @@ public class ClientHandler implements Runnable {
|
|||||||
|
|
||||||
if(Objects.equals(receiverType, "private")){
|
if(Objects.equals(receiverType, "private")){
|
||||||
PrivateChatDatabase.clearDeletedFlag(senderId, receiverId);
|
PrivateChatDatabase.clearDeletedFlag(senderId, receiverId);
|
||||||
|
UUID other = PrivateChatDatabase.getOtherParticipant(receiverId, senderId);
|
||||||
|
if (other == null) {
|
||||||
|
return new ResponseModel("error", "Invalid private chat.");
|
||||||
|
}
|
||||||
|
if (ContactDatabase.isBlocked(senderId, other) || ContactDatabase.isBlocked(other, senderId)) {
|
||||||
|
return new ResponseModel("error", "You can't message this user (blocked).");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -2573,6 +2597,12 @@ public class ClientHandler implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// helper
|
||||||
|
private boolean canSendToPrivate(UUID chatId, UUID senderId) {
|
||||||
|
UUID other = PrivateChatDatabase.getOtherParticipant(chatId, senderId);
|
||||||
|
if (other == null) return false;
|
||||||
|
return !(ContactDatabase.isBlocked(senderId, other) || ContactDatabase.isBlocked(other, senderId));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.to.telegramfinalproject.Server;
|
|||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.to.telegramfinalproject.Database.ChannelDatabase;
|
import org.to.telegramfinalproject.Database.ChannelDatabase;
|
||||||
|
import org.to.telegramfinalproject.Database.ContactDatabase;
|
||||||
import org.to.telegramfinalproject.Database.GroupDatabase;
|
import org.to.telegramfinalproject.Database.GroupDatabase;
|
||||||
import org.to.telegramfinalproject.Database.userDatabase;
|
import org.to.telegramfinalproject.Database.userDatabase;
|
||||||
import org.to.telegramfinalproject.Models.Message;
|
import org.to.telegramfinalproject.Models.Message;
|
||||||
@@ -121,6 +122,31 @@ public class RealTimeEventDispatcher {
|
|||||||
for (UUID userId : receivers) sendToUser(userId, payload);
|
for (UUID userId : receivers) sendToUser(userId, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void sendNewMessageFiltered(Message m, List<UUID> receivers, UUID senderId, String kind, JSONObject meta) {
|
||||||
|
JSONObject payload = new JSONObject()
|
||||||
|
.put("action", "new_message")
|
||||||
|
.put("data", new JSONObject()
|
||||||
|
.put("id", m.getMessage_id().toString())
|
||||||
|
.put("message_id", m.getMessage_id().toString())
|
||||||
|
.put("sender_id", m.getSender_id().toString())
|
||||||
|
.put("sender_name", userDatabase.findByInternalUUID(m.getSender_id()).getProfile_name())
|
||||||
|
.put("receiver_id", m.getReceiver_id().toString())
|
||||||
|
.put("receiver_type", m.getReceiver_type())
|
||||||
|
.put("content", m.getContent())
|
||||||
|
.put("message_type", m.getMessage_type())
|
||||||
|
.put("send_at", m.getSend_at().toString())
|
||||||
|
.put("kind", kind)
|
||||||
|
.put("meta", meta != null ? meta : JSONObject.NULL)
|
||||||
|
);
|
||||||
|
|
||||||
|
for (UUID uid : receivers) {
|
||||||
|
//If receiver blocked
|
||||||
|
if ("private".equalsIgnoreCase(m.getReceiver_type()) && ContactDatabase.isBlocked(uid, senderId)) continue;
|
||||||
|
sendToUser(uid, payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void notifyMessageDeletedGlobal(UUID chatId, UUID messageId, List<UUID> receivers) {
|
public static void notifyMessageDeletedGlobal(UUID chatId, UUID messageId, List<UUID> receivers) {
|
||||||
JSONObject data = new JSONObject();
|
JSONObject data = new JSONObject();
|
||||||
data.put("chat_id", chatId.toString());
|
data.put("chat_id", chatId.toString());
|
||||||
@@ -389,6 +415,13 @@ public class RealTimeEventDispatcher {
|
|||||||
for (UUID userId : receivers) {
|
for (UUID userId : receivers) {
|
||||||
sendToUser(userId, payload);
|
sendToUser(userId, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (UUID uid : receivers) {
|
||||||
|
//If receiver blocked
|
||||||
|
if ("private".equalsIgnoreCase(message.getReceiver_type()) && ContactDatabase.isBlocked(uid, message.getSender_id())) continue;
|
||||||
|
sendToUser(uid, payload);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user