use of com.cloudcraftgaming.discal.api.object.calendar.CalendarData in project DisCal-Discord-Bot by NovaFox161.
the class CalendarEndpoint method getCalendar.
public static String getCalendar(Request request, Response response) {
try {
JSONObject jsonMain = new JSONObject(request.body());
Long guildId = jsonMain.getLong("guild_id");
Integer calNumber = jsonMain.getInt("number");
CalendarData calendar = DatabaseManager.getManager().getCalendar(guildId, calNumber);
if (!calendar.getCalendarAddress().equalsIgnoreCase("primary")) {
JSONObject body = new JSONObject();
body.put("external", calendar.isExternal());
body.put("id", calendar.getCalendarId());
body.put("address", calendar.getCalendarAddress());
response.type("application/json");
response.status(200);
response.body(body.toString());
} else {
response.type("application/json");
response.status(404);
response.body(ResponseUtils.getJsonResponseMessage("Calendar not found"));
}
} catch (JSONException e) {
e.printStackTrace();
halt(400, "Bad Request");
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB-API] Internal get calendar error", e, CalendarEndpoint.class, true);
halt(500, "Internal Server Error");
}
return response.body();
}
use of com.cloudcraftgaming.discal.api.object.calendar.CalendarData 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.calendar.CalendarData 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();
}
use of com.cloudcraftgaming.discal.api.object.calendar.CalendarData in project DisCal-Discord-Bot by NovaFox161.
the class TimeUtils method inPast.
public static boolean inPast(String eventId, GuildSettings settings) {
// TODO: Support multiple calendars
if (EventUtils.eventExists(settings, eventId)) {
if (settings.useExternalCalendar()) {
try {
Calendar service = CalendarAuth.getCalendarService(settings);
CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
Event e = service.events().get(calendarData.getCalendarId(), eventId).execute();
return inPast(e);
} catch (Exception e) {
Logger.getLogger().exception(null, "Failed to get external calendar auth", e, TimeUtils.class, true);
// Return false and allow RSVP so user is not adversely affected.
return false;
}
} else {
try {
Calendar service = CalendarAuth.getCalendarService();
CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
Event e = service.events().get(calendarData.getCalendarId(), eventId).execute();
return inPast(e);
} catch (Exception e) {
Logger.getLogger().exception(null, "Failed to get calendar auth", e, TimeUtils.class, true);
// Return false and allow RSVP so user is not adversely affected.
return false;
}
}
}
return false;
}
use of com.cloudcraftgaming.discal.api.object.calendar.CalendarData in project DisCal-Discord-Bot by NovaFox161.
the class CalendarCreator method edit.
@SuppressWarnings("SameParameterValue")
public PreCalendar edit(MessageReceivedEvent event, GuildSettings settings, boolean handleCreatorMessage) {
long guildId = event.getMessage().getGuild().getLongID();
if (!hasPreCalendar(guildId)) {
// TODO: Support multiple calendars
CalendarData data = DatabaseManager.getManager().getMainCalendar(guildId);
try {
com.google.api.services.calendar.Calendar service;
if (settings.useExternalCalendar()) {
service = CalendarAuth.getCalendarService(settings);
} else {
service = CalendarAuth.getCalendarService();
}
Calendar calendar = service.calendars().get(data.getCalendarAddress()).execute();
PreCalendar preCalendar = new PreCalendar(guildId, calendar);
preCalendar.setEditing(true);
preCalendar.setCalendarId(data.getCalendarAddress());
if (handleCreatorMessage) {
if (PermissionChecker.botHasMessageManagePerms(event)) {
IMessage msg = Message.sendMessage(CalendarMessageFormatter.getPreCalendarEmbed(preCalendar, settings), MessageManager.getMessage("Creator.Calendar.Edit.Init", settings), event);
preCalendar.setCreatorMessage(msg);
} else {
Message.sendMessage(MessageManager.getMessage("Creator.Notif.MANAGE_MESSAGES", settings), event);
}
}
calendars.add(preCalendar);
return preCalendar;
} catch (Exception e) {
Logger.getLogger().exception(event.getMessage().getAuthor(), "Failed to init calendar editor", e, this.getClass(), true);
return null;
}
} else {
return getPreCalendar(guildId);
}
}
Aggregations