use of org.jsolf.JSONObject in project Emolga by TecToast.
the class HttpHandler method unban.
@Route(route = "/unban", method = "POST")
public static void unban(String cookie, HttpServletRequest req, HttpServletResponse res) throws SQLException, IOException {
ResultSet session = DISCORD_AUTH.getSessionByCookie(cookie);
if (session.next()) {
JSONObject body = getBody(req);
long gid = Long.parseLong(body.getString("guild"));
Guild g = emolgajda.getGuildById(gid);
if (g == null) {
replyError(res, "No matching guild");
return;
}
if (!g.retrieveMemberById(session.getLong("userid")).complete().hasPermission(Permission.MANAGE_SERVER)) {
replyError(res, "No permission to unban users for guild %s", g.getName());
return;
}
res.getWriter().println(DBManagers.BAN.unban(g, Long.parseLong(body.getString("member"))));
} else {
res.getWriter().println(new JSONObject().put("error", "invalidcookie").toString());
}
}
use of org.jsolf.JSONObject in project Emolga by TecToast.
the class HttpHandler method exchangeCodeRoute.
@Route(route = "/discordauth", needsCookie = false)
public static void exchangeCodeRoute(String cookie, HttpServletRequest req, HttpServletResponse res) throws IOException {
Map<String, String> map = getQueryMap(req.getQueryString());
if (!map.containsKey("code")) {
res.sendRedirect("http://localhost:4200");
}
String code = map.get("code");
JSONObject tokens = exchangeCode(code);
logger.info("tokens = " + tokens);
JSONObject data = getUserInfo(tokens.getString("access_token"));
logger.info("data = " + data);
res.sendRedirect("https://emolga.epizy.com/?c=" + DISCORD_AUTH.generateCookie(tokens, Long.parseLong(data.getString("id"))));
// .send(new JSONObject().put("key", "token").put("token", DISCORD_AUTH.generateCookie(tokens, Long.parseLong(data.getString("id")))).toString());
}
use of org.jsolf.JSONObject in project Emolga by TecToast.
the class HttpHandler method warnedUsers.
@Route(route = "/guilddata/(\\d+)/warned")
public static void warnedUsers(String cookie, HttpServletRequest req, HttpServletResponse res, List<String> args) throws SQLException, IOException {
ResultSet session = DISCORD_AUTH.getSessionByCookie(cookie);
if (session.next()) {
long gid = Long.parseLong(args.get(0));
Guild g = emolgajda.getGuildById(gid);
if (g == null) {
replyError(res, "No matching guild");
return;
}
if (!g.retrieveMemberById(session.getLong("userid")).complete().hasPermission(Permission.MANAGE_SERVER)) {
replyError(res, "No permission to view warned users for guild %s", g.getName());
return;
}
res.getWriter().println(DBManagers.WARNS.getWarns(g));
} else {
res.getWriter().println(new JSONObject().put("error", "invalidcookie").toString());
}
}
use of org.jsolf.JSONObject in project Emolga by TecToast.
the class HttpHandler method guilds.
@Route(route = "/guilds")
public static void guilds(String cookie, HttpServletRequest req, HttpServletResponse res) throws SQLException, IOException {
if (guildCache.containsKey(cookie)) {
logger.info("FROM CACHE");
res.getWriter().println(guildCache.get(cookie));
return;
}
ResultSet session = DISCORD_AUTH.getSessionByCookie(cookie);
if (session.next()) {
String token = getAccessToken(session);
JSONArray guilds = getGuilds(token);
logger.info("token = " + token);
List<String> gl = emolgajda.getGuilds().stream().map(Guild::getId).toList();
JSONArray arr = new JSONArray();
List<JSONObject> joined = new LinkedList<>();
List<JSONObject> notJoined = new LinkedList<>();
for (JSONObject json : guilds.toJSONList()) {
JSONObject o = new JSONObject();
if ((json.getLong("permissions") & Permission.MANAGE_SERVER.getRawValue()) > 0) {
o.put("id", json.getString("id"));
o.put("name", json.getString("name"));
o.put("url", json.isNull("icon") ? "assets/images/defaultservericon.png" : "https://cdn.discordapp.com/icons/" + json.getLong("id") + "/" + json.getString("icon") + ".png");
boolean j = gl.contains(json.getString("id"));
o.put("joined", j);
if (j)
joined.add(o);
else
notJoined.add(o);
}
}
joined.forEach(arr::put);
notJoined.forEach(arr::put);
guildCache.put(cookie, arr);
res.getWriter().println(arr);
} else {
res.getWriter().println(new JSONObject().put("error", "invalidcookie").toString());
}
}
use of org.jsolf.JSONObject in project Emolga by TecToast.
the class HttpHandler method bannedUsers.
@Route(route = "/guilddata/(\\d+)/banned")
public static void bannedUsers(String cookie, HttpServletRequest req, HttpServletResponse res, List<String> args) throws SQLException, IOException {
ResultSet session = DISCORD_AUTH.getSessionByCookie(cookie);
if (session.next()) {
long gid = Long.parseLong(args.get(0));
Guild g = emolgajda.getGuildById(gid);
if (g == null) {
replyError(res, "No matching guild");
return;
}
if (!g.retrieveMemberById(session.getLong("userid")).complete().hasPermission(Permission.MANAGE_SERVER)) {
replyError(res, "No permission to view banned users for guild %s", g.getName());
return;
}
res.getWriter().println(DBManagers.BAN.getBans(g));
} else {
res.getWriter().println(new JSONObject().put("error", "invalidcookie").toString());
}
}
Aggregations