Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
Fixed some issues (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikEmmerich committed Nov 7, 2019
1 parent 0f363ba commit ebba44b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.cryptic_game.microservice.chat.channel;

import net.cryptic_game.microservice.chat.App;
import net.cryptic_game.microservice.wrapper.User;

import java.util.ArrayList;
Expand Down Expand Up @@ -40,6 +41,7 @@ public boolean addUser(final User user) {
return false;
}
}
App.getChannelHandler().notifyAllChannelUsers(ChatAction.MEMBER_JOIN, this, user.getName());
this.users.add(user);
return true;
}
Expand All @@ -48,6 +50,7 @@ public boolean removeUser(final User user) {
for (final User u : this.users) {
if (u.getUUID().equals(user.getUUID())) {
this.users.remove(user);
App.getChannelHandler().notifyAllChannelUsers(ChatAction.MEMBER_LEAVE, this, user.getName());
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public Channel addChanel(final String name) {
return channel;
}

public void removeChannel(final UUID channelUuid, final UUID userUuid) {
public void removeChannel(final UUID channelUuid, final String userUuid) {
this.removeChannel(channelUuid, userUuid, true);
}

public void removeChannel(final UUID channelUuid, final UUID userUuid, final boolean notifyUsers) {
public void removeChannel(final UUID channelUuid, final String userUuid, final boolean notifyUsers) {
final Channel channel = this.getChannelByUUID(channelUuid);
if (notifyUsers) this.notifyAllChannelUsers(ChatAction.CHANNEL_DELETE, channel, userUuid);
this.channels.remove(channel);
Expand All @@ -45,31 +45,31 @@ public List<Channel> getChannels() {
return this.channels;
}

public void notifyUser(final User user, final ChatAction action, final Channel channel, final UUID target) {
public void notifyUser(final User user, final ChatAction action, final Channel channel, final String target) {
this.notifyUsers(new ArrayList<>(Collections.singletonList(user)), action, channel, target);
}

public void notifyUser(final User user, final ChatAction action, final Channel channel, final UUID target, final JSONObject content) {
public void notifyUser(final User user, final ChatAction action, final Channel channel, final String target, final JSONObject content) {
this.notifyUsers(new ArrayList<>(Collections.singletonList(user)), action, channel, target, content);
}

public void notifyUsers(final List<User> users, final ChatAction action, final Channel channel, final UUID target) {
public void notifyUsers(final List<User> users, final ChatAction action, final Channel channel, final String target) {
this.notifyUsers(users, action, channel, target, null);
}

public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final UUID target) {
public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final String target) {
this.notifyUsers(channel.getUsers(), action, channel, target, null);
}

public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final UUID target, final JSONObject content) {
public void notifyAllChannelUsers(final ChatAction action, final Channel channel, final String target, final JSONObject content) {
this.notifyUsers(channel.getUsers(), action, channel, target, content);
}

public void notifyUsers(final List<User> users, ChatAction action, final Channel channel, final UUID target, final JSONObject content) {
public void notifyUsers(final List<User> users, ChatAction action, final Channel channel, final String target, final JSONObject content) {
final JSONBuilder data = anJSON()
.add("action", action.getValue())
.add("channel", channel.getUuid().toString())
.add("user", target.toString());
.add("user", target);

if (content != null) {
data.add("content", content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public static JSONObject joinChannel(final JSON data, final UUID uuid) {
}

if (channel.addUser(user)) {
channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID());
return SUCCESS.getJson();
}

Expand All @@ -95,7 +94,6 @@ public static JSONObject leaveChannel(final JSON data, final UUID uuid) {
}

if (channel.removeUser(user)) {
channelHandler.notifyAllChannelUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID());
return SUCCESS.getJson();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.cryptic_game.microservice.chat.endpoint;

import net.cryptic_game.microservice.MicroService;
import net.cryptic_game.microservice.chat.App;
import net.cryptic_game.microservice.chat.channel.Channel;
import net.cryptic_game.microservice.chat.channel.ChatAction;
Expand All @@ -11,8 +12,7 @@
import java.util.Date;
import java.util.UUID;

import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.CHANNEL_NOT_FOUND;
import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.USER_NOT_IN_CHANNEL;
import static net.cryptic_game.microservice.chat.endpoint.EndpointResponse.*;
import static net.cryptic_game.microservice.utils.JSONBuilder.anJSON;
import static net.cryptic_game.microservice.utils.JSONBuilder.simple;

Expand All @@ -24,7 +24,12 @@ private MessageEndpoints() {
@UserEndpoint(path = {"message", "send"}, keys = {"channel", "message"}, types = {String.class, String.class})
public static JSONObject sendMessage(final JSON data, final UUID userUuid) {
final UUID channelUuid = data.getUUID("channel");
final String messageContent = data.get("content");
final String messageContent = data.get("message");
final User user = MicroService.getInstance().getUser(userUuid);

if (user == null) {
return USER_NOT_FOUND.getJson();
}

final Channel channel = App.getChannelHandler().getChannelByUUID(channelUuid);
if (channel == null) {
Expand All @@ -33,19 +38,24 @@ public static JSONObject sendMessage(final JSON data, final UUID userUuid) {

final JSONObject content = anJSON()
.add("message", messageContent)
.add("send-date", new Date())
.add("send-date", new Date().getTime())
.build();

App.getChannelHandler().notifyAllChannelUsers(ChatAction.SEND_MESSAGE, channel, userUuid, content);
App.getChannelHandler().notifyAllChannelUsers(ChatAction.SEND_MESSAGE, channel, user.getName(), content);

return simple("success", true);
}

@UserEndpoint(path = {"message", "whisper"}, keys = {"channel", "message", "target"}, types = {String.class, String.class, String.class})
public static JSONObject whisperMessage(final JSON data, final UUID userUuid) {
final UUID channelUuid = data.getUUID("channel");
final String messageContent = data.get("content");
final String messageContent = data.get("message");
final String targetName = data.get("target");
final User user = MicroService.getInstance().getUser(userUuid);

if (user == null) {
return USER_NOT_FOUND.getJson();
}

final Channel channel = App.getChannelHandler().getChannelByUUID(channelUuid);
if (channel == null) {
Expand All @@ -59,10 +69,10 @@ public static JSONObject whisperMessage(final JSON data, final UUID userUuid) {

final JSONObject content = anJSON()
.add("message", messageContent)
.add("send-date", new Date())
.add("send-date", new Date().getTime())
.build();

App.getChannelHandler().notifyUser(target, ChatAction.WHISPER_MESSAGE, channel, userUuid, content);
App.getChannelHandler().notifyUser(target, ChatAction.WHISPER_MESSAGE, channel, user.getName(), content);

return simple("success", true);
}
Expand Down

0 comments on commit ebba44b

Please sign in to comment.