Search in sources :

Example 6 with CalendarData

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();
}
Also used : JSONObject(org.json.JSONObject) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

Example 7 with CalendarData

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();
}
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 8 with CalendarData

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();
}
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)

Example 9 with CalendarData

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;
}
Also used : CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.Calendar) Event(com.google.api.services.calendar.model.Event) PreEvent(com.cloudcraftgaming.discal.api.object.event.PreEvent) ParseException(java.text.ParseException)

Example 10 with CalendarData

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);
    }
}
Also used : PreCalendar(com.cloudcraftgaming.discal.api.object.calendar.PreCalendar) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) PreCalendar(com.cloudcraftgaming.discal.api.object.calendar.PreCalendar) Calendar(com.google.api.services.calendar.model.Calendar) IMessage(sx.blah.discord.handle.obj.IMessage)

Aggregations

CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)29 Calendar (com.google.api.services.calendar.Calendar)11 Event (com.google.api.services.calendar.model.Event)9 GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)6 WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)6 DateTime (com.google.api.client.util.DateTime)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 JSONObject (org.json.JSONObject)6 EventData (com.cloudcraftgaming.discal.api.object.event.EventData)5 Calendar (com.google.api.services.calendar.model.Calendar)5 EventDateTime (com.google.api.services.calendar.model.EventDateTime)4 Events (com.google.api.services.calendar.model.Events)4 LocalDateTime (java.time.LocalDateTime)4 DateTimeFormatter (java.time.format.DateTimeFormatter)4 JSONException (org.json.JSONException)4 Recurrence (com.cloudcraftgaming.discal.api.object.event.Recurrence)3 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)3 EventColor (com.cloudcraftgaming.discal.api.enums.event.EventColor)2 PreCalendar (com.cloudcraftgaming.discal.api.object.calendar.PreCalendar)2