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 Oct 16, 2019
1 parent 5e36c2a commit 0781f14
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
16 changes: 8 additions & 8 deletions src/main/java/net/cryptic_game/microservice/chat/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

public class App extends MicroService {

private final ChannelHandler channelHandler;
private static ChannelHandler channelHandler;

public App() {
private App() {
super("chat");

this.channelHandler = new ChannelHandler();

this.channelHandler.addChanel("global");
}

public static void main(String[] args) {
BasicConfigurator.configure();

channelHandler = new ChannelHandler();
channelHandler.addChanel("global");

new App();
}

public ChannelHandler getChannelHandler() {
return this.channelHandler;
public static ChannelHandler getChannelHandler() {
return channelHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public class Channel {

private final UUID uuid;
private String name;
private final ArrayList<User> user;
private final ArrayList<User> users;

public Channel(final String name) {
Channel(final String name) {
this.uuid = UUID.randomUUID();
this.name = name;
this.user = new ArrayList<>();
this.users = new ArrayList<>();
}

public UUID getUuid() {
Expand All @@ -29,15 +29,27 @@ public void setName(final String name) {
this.name = name;
}

public ArrayList<User> getUser() {
return this.user;
public ArrayList<User> getUsers() {
return this.users;
}

public void addUser(final User user) {
this.user.add(user);
public boolean addUser(final User user) {
for(User u : users) {
if(u.getUUID().equals(user.getUUID())) {
return false;
}
}
this.users.add(user);
return true;
}

public void removeUser(final User user) {
this.user.remove(user);
public boolean removeUser(final User user) {
for(User u : users) {
if(u.getUUID().equals(user.getUUID())) {
this.users.remove(user);
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public void notifyUsers(final ChatAction action, final Channel channel, final UU
.add("data", anJSON()
.add("action", action.getValue())
.add("channel", channel.getUuid().toString())
.add("user", channel.toString()).build()
.add("user", target.toString()).build()
).build();

channel.getUser().forEach(user -> MicroService.getInstance().sendToUser(target, json));
channel.getUsers().forEach(user -> MicroService.getInstance().sendToUser(user.getUUID(), json));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ChannelEndpoints {

@UserEndpoint(path = {"channel", "list"}, keys = {}, types = {})
public static JSONObject getChannel(final JSON data, final UUID uuid) {
final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler();
final ChannelHandler channelHandler = App.getChannelHandler();

final List<JSONObject> channelsJson = new ArrayList<>();

Expand All @@ -32,31 +32,31 @@ public static JSONObject getChannel(final JSON data, final UUID uuid) {
.build());
}

return simple("channel", channelsJson);
return simple("channels", channelsJson);
}

@UserEndpoint(path = {"channel", "members"}, keys = {"channel"}, types = {UUID.class})
public static JSONObject getChannelMembers(final JSON data, final UUID uuid) {
final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler();
final ChannelHandler channelHandler = App.getChannelHandler();

final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel"));

if (channel == null) {
return simple("error", "Channel with the UUID \"" + data.getUUID("channel").toString() + "\" can't be found.");
return simple("error", "channel_not_found");
}

final List<JSONObject> userJson = new ArrayList<>();

for (final User user : channel.getUser()) {
for (final User user : channel.getUsers()) {
userJson.add(simple("name", user.getName()));
}

return simple("user", userJson);
return simple("users", userJson);
}

@UserEndpoint(path = {"channel", "join"}, keys = {"channel"}, types = {UUID.class})
public static JSONObject joinChannel(final JSON data, final UUID uuid) {
final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler();
final ChannelHandler channelHandler = App.getChannelHandler();

final User user = MicroService.getInstance().getUser(uuid);
final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel"));
Expand All @@ -68,15 +68,17 @@ public static JSONObject joinChannel(final JSON data, final UUID uuid) {
return simple("error", "channel_not_found");
}

channelHandler.getChannelByUUID(data.getUUID("channel")).addUser(user);
channelHandler.notifyUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID());
if(channel.addUser(user)) {
channelHandler.notifyUsers(ChatAction.MEMBER_JOIN, channel, user.getUUID());
return simple("success", true);
}

return simple("success", true);
return simple("success", false);
}

@UserEndpoint(path = {"channel", "leave"}, keys = {"channel"}, types = {UUID.class})
public static JSONObject leaveChannel(final JSON data, final UUID uuid) {
final ChannelHandler channelHandler = ((App) App.getInstance()).getChannelHandler();
final ChannelHandler channelHandler = App.getChannelHandler();

final User user = MicroService.getInstance().getUser(uuid);
final Channel channel = channelHandler.getChannelByUUID(data.getUUID("channel"));
Expand All @@ -88,9 +90,11 @@ public static JSONObject leaveChannel(final JSON data, final UUID uuid) {
return simple("error", "channel_not_found");
}

channelHandler.getChannelByUUID(data.getUUID("channel")).removeUser(user);
channelHandler.notifyUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID());
if(channel.removeUser(user)) {
channelHandler.notifyUsers(ChatAction.MEMBER_LEAVE, channel, user.getUUID());
return simple("success", true);
}

return simple("success", true);
return simple("success", false);
}
}

0 comments on commit 0781f14

Please sign in to comment.