Access restrictions, errors & properties
This commit is contained in:
@@ -89,6 +89,54 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
Database.getInstance().disconnectUser(user);
|
||||
}
|
||||
|
||||
private void replyError(Packet source, String reason) {
|
||||
ErrorPacket packet = new ErrorPacket();
|
||||
packet.setErrorType(source.getType());
|
||||
packet.setReason(reason);
|
||||
|
||||
try {
|
||||
Globals.getPacketManager().replyPacket(source, packet);
|
||||
} catch (IOException e) {
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean errorNullTopic(Packet source, Topic topic) {
|
||||
if (topic == null) {
|
||||
replyError(source, "Topic does not exist");
|
||||
return true;
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean errorNullUser(Packet source, User user) {
|
||||
if (user == null) {
|
||||
replyError(source, "User does not exist");
|
||||
return true;
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean errorNullMessage(Packet source, Message message) {
|
||||
if (message == null) {
|
||||
replyError(source, "Message does not exist");
|
||||
return true;
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean errorUserNotInTopic(Packet source, Topic topic, User user) {
|
||||
if (!topic.hasUser(user)) {
|
||||
replyError(source, "User does not have access to Topic");
|
||||
return true;
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void onRegisterUser(RegisterRequestPacket packet) {
|
||||
SecretUser user = Database.getInstance().registerUser(packet.getUsername(), packet.getAvatarID(), packet.getSource());
|
||||
|
||||
@@ -108,7 +156,7 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
private void onCreateTopic(TopicPushPacket packet) {
|
||||
Topic topic = Database.getInstance().createTopic(packet.getAuth(), packet.getTopicName(), packet.getTopicDescription());
|
||||
|
||||
if (topic == null) return;
|
||||
if (errorNullTopic(packet, topic)) return;
|
||||
|
||||
info("\"{}\" created topic \"{}\" (#{})", topic.getHost().getUsername().toUpperCase(), topic.getName().toUpperCase(), topic.getId());
|
||||
|
||||
@@ -136,12 +184,12 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
Topic topic = Database.getInstance().getTopic(packet.getTopicID());
|
||||
User requester = Database.getInstance().getUserBySecret(packet.getAuth());
|
||||
|
||||
if (requester == null) return;
|
||||
if (topic == null) return;
|
||||
if (errorNullUser(packet, requester)) return;
|
||||
if (errorNullTopic(packet, topic)) return;
|
||||
|
||||
if (topic.hasUser(requester)) return;
|
||||
|
||||
info("\"{}\" joined topic \"{}\" (#{})", topic.getHost().getUsername().toUpperCase(), topic.getName().toUpperCase(), topic.getId());
|
||||
info("\"{}\" joined topic \"{}\" (#{})", requester.getUsername().toUpperCase(), topic.getName().toUpperCase(), topic.getId());
|
||||
topic.addUser(requester);
|
||||
|
||||
SystemMessage message = new SystemMessage(topic.getNextMessageID(), topic, SystemMessage.SystemType.USER_JOIN, requester, "");
|
||||
@@ -161,7 +209,9 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
updatePacket.getUsersJoined().add(new LongData().setValue(requester.getUserID()));
|
||||
|
||||
MessageSystemPacket systemPacket = new MessageSystemPacket();
|
||||
systemPacket.setMessageID(message.getId());
|
||||
systemPacket.setTopicID(topic.getId());
|
||||
systemPacket.setCreatedTimestamp(message.getPostTime());
|
||||
systemPacket.setSystemType(message.getType());
|
||||
systemPacket.setUserID(message.getUser().getUserID());
|
||||
|
||||
@@ -197,7 +247,7 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
private void onFetchTopic(TopicFetchPacket packet) {
|
||||
Topic topic = Database.getInstance().getTopic(packet.getTopicID());
|
||||
|
||||
if (topic == null) return;
|
||||
if (errorNullTopic(packet, topic)) return;
|
||||
|
||||
info("Requested topic #{}", topic.getId());
|
||||
|
||||
@@ -221,7 +271,7 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
private void onFetchUser(UserFetchPacket packet) {
|
||||
User user = Database.getInstance().getUserByID(packet.getUserID());
|
||||
|
||||
if (user == null) return;
|
||||
if (errorNullUser(packet, user)) return;
|
||||
|
||||
info("Requested user #{}", user.getUserID());
|
||||
|
||||
@@ -241,8 +291,8 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
Topic topic = Database.getInstance().getTopic(packet.getTopicID());
|
||||
User user = Database.getInstance().getUserBySecret(packet.getAuth());
|
||||
|
||||
if (user == null) return;
|
||||
if (topic == null) return;
|
||||
if (errorNullUser(packet, user)) return;
|
||||
if (errorNullTopic(packet, topic)) return;
|
||||
|
||||
UserMessage userMessage = new UserMessage(topic.getNextMessageID(), user, topic, packet.getMessage());
|
||||
|
||||
@@ -260,8 +310,10 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
}
|
||||
|
||||
MessageCreatedPacket messageCreated = new MessageCreatedPacket();
|
||||
messageCreated.setMessageID(userMessage.getId());
|
||||
messageCreated.setAuthorID(user.getUserID());
|
||||
messageCreated.setTopicID(topic.getId());
|
||||
messageCreated.setCreatedTimestamp(userMessage.getPostTime());
|
||||
messageCreated.setContents(userMessage.getContents());
|
||||
|
||||
for (User babbler : topic.getUsersReadOnly().values()) {
|
||||
@@ -279,8 +331,12 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
|
||||
private void onMessageListFetch(MessageListFetchPacket packet) {
|
||||
Topic topic = Database.getInstance().getTopic(packet.getTopicID());
|
||||
User requester = Database.getInstance().getUserBySecret(packet.getAuth());
|
||||
|
||||
if (topic == null) return;
|
||||
if (errorNullTopic(packet, topic)) return;
|
||||
if (errorNullUser(packet, requester)) return;
|
||||
|
||||
if (errorUserNotInTopic(packet, topic, requester)) return;
|
||||
|
||||
info("Requested Message list on #{}", topic.getId());
|
||||
|
||||
@@ -299,18 +355,23 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
|
||||
private void onFetchMessage(MessageFetchPacket packet) {
|
||||
Topic topic = Database.getInstance().getTopic(packet.getTopicID());
|
||||
User requester = Database.getInstance().getUserBySecret(packet.getAuth());
|
||||
|
||||
if (topic == null) return;
|
||||
if (errorNullTopic(packet, topic)) return;
|
||||
if (errorNullUser(packet, requester)) return;
|
||||
|
||||
if (errorUserNotInTopic(packet, topic, requester)) return;
|
||||
|
||||
Message message = topic.getMessageByID(packet.getMessageID());
|
||||
|
||||
if (message == null) return;
|
||||
|
||||
if (errorNullMessage(packet, message)) return;
|
||||
|
||||
info("Requested message #{} on #{}", message.getId(), topic.getId());
|
||||
|
||||
MessageDataPacket resp = new MessageDataPacket();
|
||||
resp.setMessageID(message.getId());
|
||||
resp.setTopicID(message.getTopic().getId());
|
||||
resp.setCreatedTimestamp(message.getPostTime());
|
||||
|
||||
if (message instanceof UserMessage userMessage) {
|
||||
resp.setMessageType(MessageDataPacket.MessageType.USER);
|
||||
@@ -335,11 +396,13 @@ public record UserSocket(Socket socket) implements Runnable {
|
||||
private void onTypingUser(TypingPingPacket packet) {
|
||||
User user = Database.getInstance().getUserBySecret(packet.getAuth());
|
||||
|
||||
if (user == null) return;
|
||||
if (errorNullUser(packet, user)) return;
|
||||
|
||||
TypingTopic topic = Database.getInstance().getTopic(packet.getTopicID());
|
||||
|
||||
if (topic == null || !topic.hasUser(user)) return;
|
||||
if (errorNullTopic(packet, topic)) return;
|
||||
|
||||
if (!topic.hasUser(user)) return;
|
||||
|
||||
if (packet.isTyping()) {
|
||||
topic.addTypingUser(user);
|
||||
|
||||
Reference in New Issue
Block a user