|
| 1 | +package net.b07z.sepia.websockets.endpoints; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.json.simple.JSONArray; |
| 7 | +import org.json.simple.JSONObject; |
| 8 | + |
| 9 | +import net.b07z.sepia.server.core.data.Role; |
| 10 | +import net.b07z.sepia.server.core.server.RequestParameters; |
| 11 | +import net.b07z.sepia.server.core.server.RequestPostParameters; |
| 12 | +import net.b07z.sepia.server.core.server.SparkJavaFw; |
| 13 | +import net.b07z.sepia.server.core.tools.JSON; |
| 14 | +import net.b07z.sepia.server.core.users.Account; |
| 15 | +import net.b07z.sepia.websockets.common.SocketChannel; |
| 16 | +import net.b07z.sepia.websockets.common.SocketChannelPool; |
| 17 | +import spark.Request; |
| 18 | +import spark.Response; |
| 19 | + |
| 20 | +/** |
| 21 | + * Create, join and remove channels. |
| 22 | + * |
| 23 | + * @author Florian Quirin |
| 24 | + */ |
| 25 | +public class ChannelManager { |
| 26 | + |
| 27 | + /** |
| 28 | + * Create a new channel. |
| 29 | + */ |
| 30 | + public static String createChannel(Request request, Response response){ |
| 31 | + //get parameters (or throw error) |
| 32 | + RequestParameters params = new RequestPostParameters(request); |
| 33 | + |
| 34 | + //authenticate |
| 35 | + Account userAccount = new Account(); |
| 36 | + if (userAccount.authenticate(params)){ |
| 37 | + //log.info("Authenticated: " + userAccount.getUserID() + ", roles: " + userAccount.getUserRoles()); //debug |
| 38 | + if (userAccount.hasRole(Role.developer.name())){ |
| 39 | + //get parameters |
| 40 | + String channelId = params.getString("channelId"); |
| 41 | + JSONArray initialMembers = params.getJsonArray("members"); |
| 42 | + String owner = userAccount.getUserID(); |
| 43 | + boolean isPublic = params.getBoolOrDefault("isPublic", false); |
| 44 | + boolean addAssistant = params.getBoolOrDefault("addAssistant", true); |
| 45 | + |
| 46 | + if (channelId == null || channelId.isEmpty()){ |
| 47 | + JSONObject msgJSON = JSON.make("result", "fail", "error", "missing channelId"); |
| 48 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 200); |
| 49 | + }else{ |
| 50 | + //filter system channels (user can't create them) |
| 51 | + if (SocketChannel.systemChannels.contains(channelId)){ |
| 52 | + JSONObject msgJSON = JSON.make("result", "fail", "error", "invalid channelId"); |
| 53 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 200); |
| 54 | + } |
| 55 | + //filter valid channel names |
| 56 | + String cleanChannelId = channelId.replaceAll("[^\\w\\s]","").replaceAll("\\s+", " "); |
| 57 | + if (!cleanChannelId.equals(channelId)){ |
| 58 | + JSONObject msgJSON = JSON.make("result", "fail", "error", "invalid channelId, please use only letters, numbers and '_'."); |
| 59 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 200); |
| 60 | + } |
| 61 | + } |
| 62 | + try{ |
| 63 | + //create channel |
| 64 | + String channelKey = SocketChannelPool.createChannel(channelId, owner, isPublic); |
| 65 | + if (channelKey != null){ |
| 66 | + List<String> members = new ArrayList<>(); |
| 67 | + members.add(owner); |
| 68 | + if (initialMembers != null && !initialMembers.isEmpty()){ |
| 69 | + for (Object o : initialMembers){ |
| 70 | + members.add((String) o); |
| 71 | + } |
| 72 | + } |
| 73 | + SocketChannel sc = SocketChannelPool.getChannel(channelId); |
| 74 | + for (String s : members){ |
| 75 | + sc.addUser(s, channelKey); |
| 76 | + //System.out.println("Member: " + s); //DEBUG |
| 77 | + } |
| 78 | + if (addAssistant){ |
| 79 | + sc.addSystemDefaultAssistant(); //Add SEPIA too |
| 80 | + //System.out.println("Member: " + SocketConfig.systemAssistantId); //DEBUG |
| 81 | + } |
| 82 | + JSONObject msgJSON = JSON.make("result", "success", "key", channelKey, "channelId", channelId); |
| 83 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 200); |
| 84 | + |
| 85 | + }else{ |
| 86 | + //error |
| 87 | + JSONObject msgJSON = JSON.make("result", "fail", "error", "creation failed! Maybe already exists?"); |
| 88 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 200); |
| 89 | + } |
| 90 | + }catch (Exception e){ |
| 91 | + //error |
| 92 | + JSONObject msgJSON = JSON.make("result", "fail", "error", e.getMessage()); |
| 93 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 200); |
| 94 | + } |
| 95 | + }else{ |
| 96 | + //refuse |
| 97 | + JSONObject msgJSON = JSON.make("result", "fail", "error", "not authorized, missing required role"); |
| 98 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 404); |
| 99 | + } |
| 100 | + }else{ |
| 101 | + //refuse |
| 102 | + JSONObject msgJSON = JSON.make("result", "fail", "error", "not authorized"); |
| 103 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 404); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Join a channel. |
| 109 | + */ |
| 110 | + public static String joinChannel(Request request, Response response){ |
| 111 | + //TODO: |
| 112 | + // - authenticate |
| 113 | + // - get parameters (channelId, key, user) |
| 114 | + // - return success |
| 115 | + // ... |
| 116 | + |
| 117 | + //get parameters (or throw error) |
| 118 | + RequestParameters params = new RequestPostParameters(request); |
| 119 | + |
| 120 | + //authenticate |
| 121 | + Account userAccount = new Account(); |
| 122 | + if (userAccount.authenticate(params)){ |
| 123 | + |
| 124 | + JSONObject msgJSON = JSON.make("result", "fail", "error", "not yet implemented"); |
| 125 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 200); |
| 126 | + |
| 127 | + }else{ |
| 128 | + //refuse |
| 129 | + JSONObject msgJSON = JSON.make("result", "fail", "error", "not authorized"); |
| 130 | + return SparkJavaFw.returnResult(request, response, msgJSON.toJSONString(), 404); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments