Search in sources :

Example 1 with WebGuild

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();
}
Also used : JSONObject(org.json.JSONObject) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) Map(java.util.Map) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

Example 2 with WebGuild

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();
}
Also used : Calendar(com.google.api.services.calendar.Calendar) ArrayList(java.util.ArrayList) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) JSONObject(org.json.JSONObject) Events(com.google.api.services.calendar.model.Events) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Event(com.google.api.services.calendar.model.Event) Map(java.util.Map) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

Example 3 with WebGuild

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();
}
Also used : Calendar(com.google.api.services.calendar.model.Calendar) WebCalendar(com.cloudcraftgaming.discal.api.object.web.WebCalendar) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) HashMap(java.util.HashMap) Map(java.util.Map) JSONException(org.json.JSONException)

Example 4 with WebGuild

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();
}
Also used : IUser(sx.blah.discord.handle.obj.IUser) JSONException(org.json.JSONException) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) IGuild(sx.blah.discord.handle.obj.IGuild) HashMap(java.util.HashMap) Map(java.util.Map) JSONException(org.json.JSONException)

Example 5 with WebGuild

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();
}
Also used : Calendar(com.google.api.services.calendar.model.Calendar) WebCalendar(com.cloudcraftgaming.discal.api.object.web.WebCalendar) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) WebCalendar(com.cloudcraftgaming.discal.api.object.web.WebCalendar) JSONException(org.json.JSONException) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) AclRule(com.google.api.services.calendar.model.AclRule) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)14 Map (java.util.Map)13 HashMap (java.util.HashMap)8 JSONException (org.json.JSONException)8 GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)6 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)6 JSONObject (org.json.JSONObject)5 DateTime (com.google.api.client.util.DateTime)4 Calendar (com.google.api.services.calendar.Calendar)4 Event (com.google.api.services.calendar.model.Event)4 EventDateTime (com.google.api.services.calendar.model.EventDateTime)4 EventData (com.cloudcraftgaming.discal.api.object.event.EventData)3 Recurrence (com.cloudcraftgaming.discal.api.object.event.Recurrence)3 WebCalendar (com.cloudcraftgaming.discal.api.object.web.WebCalendar)3 ArrayList (java.util.ArrayList)3 IGuild (sx.blah.discord.handle.obj.IGuild)3 Announcement (com.cloudcraftgaming.discal.api.object.announcement.Announcement)2 Calendar (com.google.api.services.calendar.model.Calendar)2 Events (com.google.api.services.calendar.model.Events)2 IUser (sx.blah.discord.handle.obj.IUser)2