use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.
the class EventEndpoint method deleteEvent.
public static String deleteEvent(Request request, Response response) {
JSONObject requestBody = new JSONObject(request.body());
String eventId = requestBody.getString("id");
GuildSettings settings;
// Check if logged in, else get guild ID from body.
if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
WebGuild g = (WebGuild) m.get("selected");
g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
settings = g.getSettings();
} else {
Long guildId = requestBody.getLong("guild_id");
settings = DatabaseManager.getManager().getSettings(guildId);
}
// okay, time to properly delete the event
if (EventUtils.deleteEvent(settings, eventId)) {
// Deleted!
response.body(ResponseUtils.getJsonResponseMessage("Successfully deleted event!"));
} else {
// Oh nos! we failed >.<
response.status(500);
response.body(ResponseUtils.getJsonResponseMessage("Failed to delete event!"));
}
return response.body();
}
use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.
the class EventEndpoint method getEventsForMonth.
public static String getEventsForMonth(Request request, Response response) {
JSONObject requestBody = new JSONObject(request.body());
Integer daysInMonth = Integer.valueOf(requestBody.getString("DaysInMonth"));
Long startEpoch = Long.valueOf(requestBody.getString("StartEpoch"));
Long endEpoch = startEpoch + (86400000L * daysInMonth);
GuildSettings settings;
if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
WebGuild g = (WebGuild) m.get("selected");
g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
settings = g.getSettings();
} else {
long guildId = requestBody.getLong("guild_id");
settings = DatabaseManager.getManager().getSettings(guildId);
}
// okay, lets actually get the month's events.
try {
Calendar service;
if (settings.useExternalCalendar()) {
service = CalendarAuth.getCalendarService(settings);
} else {
service = CalendarAuth.getCalendarService();
}
CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
Events events = service.events().list(calendarData.getCalendarAddress()).setTimeMin(new DateTime(startEpoch)).setTimeMax(new DateTime(endEpoch)).setOrderBy("startTime").setSingleEvents(true).setShowDeleted(false).execute();
List<Event> items = events.getItems();
List<JSONObject> eventsJson = new ArrayList<>();
for (Event e : items) {
JSONObject jo = new JSONObject();
jo.put("id", e.getId());
jo.put("epochStart", e.getStart().getDateTime().getValue());
jo.put("epochEnd", e.getEnd().getDateTime().getValue());
eventsJson.add(jo);
}
JSONObject body = new JSONObject();
body.put("events", eventsJson);
body.put("count", eventsJson.size() + "");
response.body(body.toString());
} catch (Exception e) {
response.body("Internal server error!");
Logger.getLogger().exception(null, "[WEB] Failed to retrieve events for a month.", e, EventEndpoint.class, true);
return response.body();
}
return response.body();
}
use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.
the class DashboardHandler method handleCalendarUpdate.
public static String handleCalendarUpdate(Request request, Response response) {
try {
if (request.queryParams().contains("cal-name")) {
// Update calendar name/summary...
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
WebGuild g = (WebGuild) m.get("selected");
if (g.isDiscalRole()) {
try {
if (g.getCalendar().isExternal()) {
Calendar cal = CalendarAuth.getCalendarService(g.getSettings()).calendars().get(g.getCalendar().getId()).execute();
cal.setSummary(request.queryParams("cal-name"));
CalendarAuth.getCalendarService(g.getSettings()).calendars().update(g.getCalendar().getId(), cal).execute();
} else {
Calendar cal = CalendarAuth.getCalendarService().calendars().get(g.getCalendar().getId()).execute();
cal.setSummary(request.queryParams("cal-name"));
CalendarAuth.getCalendarService().calendars().update(g.getCalendar().getId(), cal).execute();
}
g.getCalendar().setName(request.queryParams("cal-name"));
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB] Failed to update calendar name", e, DashboardHandler.class, true);
}
}
} else if (request.queryParams().contains("cal-desc")) {
// Update calendar description...
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
WebGuild g = (WebGuild) m.get("selected");
if (g.isDiscalRole()) {
try {
if (g.getCalendar().isExternal()) {
Calendar cal = CalendarAuth.getCalendarService(g.getSettings()).calendars().get(g.getCalendar().getId()).execute();
cal.setDescription(request.queryParams("cal-desc"));
CalendarAuth.getCalendarService(g.getSettings()).calendars().update(g.getCalendar().getId(), cal).execute();
} else {
Calendar cal = CalendarAuth.getCalendarService().calendars().get(g.getCalendar().getId()).execute();
cal.setDescription(request.queryParams("cal-desc"));
CalendarAuth.getCalendarService().calendars().update(g.getCalendar().getId(), cal).execute();
}
g.getCalendar().setDescription(request.queryParams("cal-desc"));
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB] Failed to update calendar description", e, DashboardHandler.class, true);
}
}
} else if (request.queryParams().contains("cal-tz")) {
// Update calendar timezone
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
WebGuild g = (WebGuild) m.get("selected");
if (g.isDiscalRole()) {
try {
if (g.getCalendar().isExternal()) {
Calendar cal = CalendarAuth.getCalendarService(g.getSettings()).calendars().get(g.getCalendar().getId()).execute();
cal.setTimeZone(request.queryParams("cal-tz").replace("___", "/"));
CalendarAuth.getCalendarService(g.getSettings()).calendars().update(g.getCalendar().getId(), cal).execute();
} else {
Calendar cal = CalendarAuth.getCalendarService().calendars().get(g.getCalendar().getId()).execute();
cal.setTimeZone(request.queryParams("cal-tz").replace("___", "/"));
CalendarAuth.getCalendarService().calendars().update(g.getCalendar().getId(), cal).execute();
}
g.getCalendar().setTimezone(request.queryParams("cal-tz"));
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB] Failed to update calendar timezone", e, DashboardHandler.class, true);
}
}
}
// Finally redirect back to the dashboard
response.redirect("/dashboard/guild/calendar", 301);
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB] Calendar update failed!", e, DashboardHandler.class, true);
halt(500, "Internal Server Exception");
}
return response.body();
}
use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.
the class DashboardHandler method handleGuildSelect.
public static String handleGuildSelect(Request request, Response response) {
try {
String guildId = request.queryParams("guild");
IGuild g = Main.client.getGuildByID(Long.valueOf(guildId));
WebGuild wg = new WebGuild().fromGuild(g);
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
IUser u = g.getUserByID(Long.valueOf((String) m.get("id")));
wg.setDiscalRole(PermissionChecker.hasSufficientRole(g, u));
wg.setManageServer(PermissionChecker.hasManageServerRole(g, u));
m.remove("selected");
m.put("selected", wg);
m.remove("settings");
m.remove("admin");
DiscordAccountHandler.getHandler().appendAccount(m, request.session().id());
response.redirect("/dashboard/guild", 301);
} catch (JSONException e) {
Logger.getLogger().exception(null, "[WEB] JSON || Guild Select failed!", e, DashboardHandler.class, true);
response.redirect("/dashboard", 301);
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB] Guild Select failed!", e, DashboardHandler.class, true);
halt(500, "Internal Server Exception");
}
return response.body();
}
use of com.cloudcraftgaming.discal.api.object.web.WebGuild in project DisCal-Discord-Bot by NovaFox161.
the class DashboardHandler method handleCalendarCreate.
public static String handleCalendarCreate(Request request, Response response) {
try {
String name = request.queryParams("cal-name");
String desc = request.queryParams("cal-desc");
String tz = request.queryParams("cal-tz");
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
WebGuild g = (WebGuild) m.get("selected");
if (g.isDiscalRole()) {
Calendar calendar = new Calendar();
calendar.setSummary(name);
calendar.setDescription(desc);
calendar.setTimeZone(tz.replace("___", "/"));
try {
com.google.api.services.calendar.Calendar service;
if (g.getSettings().useExternalCalendar()) {
service = CalendarAuth.getCalendarService(g.getSettings());
} else {
service = CalendarAuth.getCalendarService();
}
Calendar confirmed = service.calendars().insert(calendar).execute();
AclRule rule = new AclRule();
AclRule.Scope scope = new AclRule.Scope();
scope.setType("default");
rule.setScope(scope).setRole("reader");
service.acl().insert(confirmed.getId(), rule).execute();
CalendarData calendarData = new CalendarData(Long.valueOf(g.getId()), 1);
calendarData.setCalendarId(confirmed.getId());
calendarData.setCalendarAddress(confirmed.getId());
DatabaseManager.getManager().updateCalendar(calendarData);
// Refresh to display correct info...
g.setCalendar(new WebCalendar().fromCalendar(calendarData, g.getSettings()));
} catch (Exception ex) {
Logger.getLogger().exception(null, "[WEB] Failed to confirm calendar.", ex, DashboardHandler.class, true);
}
}
// Finally redirect back to the dashboard
response.redirect("/dashboard/guild/calendar", 301);
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB] Calendar create failed!", e, DashboardHandler.class, true);
halt(500, "Internal Server Exception");
}
return response.body();
}
Aggregations