Add endpoint for ongoing events, fix language in ongoing command

This commit is contained in:
NovaFox161
2020-12-02 21:50:49 -06:00
parent 6414915ad4
commit a7b75bcf56
2 changed files with 98 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
package org.dreamexposure.discal.client.module.command;
import com.google.api.services.calendar.model.Event;
import discord4j.core.event.domain.message.MessageCreateEvent;
import org.dreamexposure.discal.client.message.EventMessageFormatter;
import org.dreamexposure.discal.client.message.Messages;
import org.dreamexposure.discal.core.database.DatabaseManager;
@@ -9,13 +9,15 @@ import org.dreamexposure.discal.core.object.GuildSettings;
import org.dreamexposure.discal.core.object.command.CommandInfo;
import org.dreamexposure.discal.core.utils.GlobalConst;
import org.dreamexposure.discal.core.wrapper.google.EventWrapper;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import discord4j.core.event.domain.message.MessageCreateEvent;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* Created by Nova Fox on 1/3/2017.
* Website: www.cloudcraftgaming.com
@@ -189,17 +191,18 @@ public class EventListCommand implements Command {
}
@SuppressWarnings("DuplicatedCode")
//TODO: Add translations for new text values
private Mono<Void> moduleOngoing(String[] args, MessageCreateEvent event, GuildSettings settings) {
return Mono.defer(() -> {
if (args.length == 1) {
return DatabaseManager.getMainCalendar(settings.getGuildID()) //TODO: Support multi-cal
.flatMap(data -> {
final long start = System.currentTimeMillis() - (GlobalConst.oneDayMs * 14); // 2 weeks ago
final long now = System.currentTimeMillis() + GlobalConst.oneDayMs; // one day from now
final long end = System.currentTimeMillis() + GlobalConst.oneDayMs; // one day from now
return EventWrapper.getEvents(data, settings, start, now).flatMap(events -> {
return EventWrapper.getEvents(data, settings, start, end).flatMap(events -> {
if (events.isEmpty()) {
return Messages.sendMessage(Messages.getMessage("Event.List.Found.None", settings), event);
return Messages.sendMessage("No ongoing events found!", event);
} else {
//Filter through and check if they are currently on-going.
List<Event> ongoing = events.stream()
@@ -209,17 +212,17 @@ public class EventListCommand implements Command {
//Filtered...
if (ongoing.isEmpty()) {
return Messages.sendMessage(Messages.getMessage("Event.List.Found.None", settings), event);
return Messages.sendMessage("No ongoing events found!", event);
} else if (ongoing.size() == 1) {
return EventMessageFormatter
.getEventEmbed(ongoing.get(0), data.getCalendarNumber(), settings).flatMap(embed ->
Messages.sendMessage(
Messages.getMessage("Event.List.Found.One", settings), embed, event));
Messages.sendMessage("One ongoing event found", embed, event));
} else {
return Messages.sendMessage(Messages.getMessage("Event.List.Found.Many", "%amount%",
ongoing.size() + "", settings), event).then(Flux.fromIterable(ongoing)
.concatMap(e -> EventMessageFormatter.getCondensedEventEmbed(e, data.getCalendarNumber(), settings)
.flatMap(embed -> Messages.sendMessage(embed, event))).then())
return Messages.sendMessage(ongoing.size() + " ongoing events found." +
" It make take some time to list them all...", event)
.then(Flux.fromIterable(ongoing)
.concatMap(e -> EventMessageFormatter.getCondensedEventEmbed(e, data.getCalendarNumber(), settings)
.flatMap(embed -> Messages.sendMessage(embed, event))).then())
.thenReturn(GlobalConst.NOT_EMPTY);
}
}

View File

@@ -0,0 +1,82 @@
package org.dreamexposure.discal.server.api.endpoints.v2.event.list;
import org.dreamexposure.discal.core.database.DatabaseManager;
import org.dreamexposure.discal.core.logger.LogFeed;
import org.dreamexposure.discal.core.logger.object.LogObject;
import org.dreamexposure.discal.core.object.GuildSettings;
import org.dreamexposure.discal.core.object.web.AuthenticationState;
import org.dreamexposure.discal.core.utils.GlobalConst;
import org.dreamexposure.discal.core.utils.JsonUtils;
import org.dreamexposure.discal.core.wrapper.google.EventWrapper;
import org.dreamexposure.discal.server.utils.Authentication;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import discord4j.common.util.Snowflake;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/v2/events/list")
public class ListEventOngoingEndpoint {
@PostMapping(value = "/ongoing", produces = "application/json")
public String getEventsOngoing(HttpServletRequest request, HttpServletResponse response, @RequestBody String rBody) {
//Authenticate...
final AuthenticationState authState = Authentication.authenticate(request);
if (!authState.isSuccess()) {
response.setStatus(authState.getStatus());
response.setContentType("application/json");
return authState.toJson();
}
//Okay, now handle actual request.
try {
final JSONObject requestBody = new JSONObject(rBody);
final Snowflake guildId = Snowflake.of(requestBody.getString("guild_id"));
final int calNumber = requestBody.getInt("calendar_number");
final long start = System.currentTimeMillis() - (GlobalConst.oneDayMs * 14); // 2 weeks ago
final long end = System.currentTimeMillis() + GlobalConst.oneDayMs; // one day from now
final GuildSettings settings = DatabaseManager.getSettings(guildId).block();
//okay, lets actually get the date's events.
final List<JSONObject> events = DatabaseManager.getCalendar(settings.getGuildID(), calNumber)
.flatMap(calData -> EventWrapper.getEvents(calData, settings, start, end))
.flatMapMany(Flux::fromIterable)
.filter(e -> e.getStart().getDateTime().getValue() < System.currentTimeMillis())
.filter(e -> e.getEnd().getDateTime().getValue() > System.currentTimeMillis())
.map(e -> JsonUtils.convertEventToJson(e, settings))
.collectList()
.block();
final JSONObject body = new JSONObject();
body.put("events", events);
body.put("message", "Ongoing events successfully listed.");
response.setContentType("application/json");
response.setStatus(GlobalConst.STATUS_SUCCESS);
return body.toString();
} catch (final JSONException e) {
e.printStackTrace();
response.setContentType("application/json");
response.setStatus(GlobalConst.STATUS_BAD_REQUEST);
return JsonUtils.getJsonResponseMessage("Bad Request");
} catch (final Exception e) {
LogFeed.log(LogObject
.forException("[API-v2]", "get ongoing events err", e, this.getClass()));
response.setContentType("application/json");
response.setStatus(GlobalConst.STATUS_INTERNAL_ERROR);
return JsonUtils.getJsonResponseMessage("Internal Server Error");
}
}
}