Handle some additional settings.

This commit is contained in:
NovaFox161
2019-06-12 11:36:34 -05:00
parent 66d29b904c
commit 6900ae7a8d
12 changed files with 59 additions and 24 deletions

View File

@@ -90,7 +90,7 @@ public class DisCalClient {
//Start Redis pub/sub listeners
PubSubManager.get().init(BotSettings.REDIS_HOSTNAME.get(), Integer.valueOf(BotSettings.REDIS_PORT.get()), "N/a", BotSettings.REDIS_PASSWORD.get());
//We must register each channel we want to use. This is super important.
PubSubManager.get().register(clientId(), "DisCal/ToClient/All");
PubSubManager.get().register(clientId(), BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All");
//Login
client.login().block();

View File

@@ -5,8 +5,12 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableAutoConfiguration
@@ -17,4 +21,15 @@ public class ServletConfig implements
factory.setPort(Integer.valueOf(BotSettings.PORT.get()));
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/"));
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
CorsRegistration reg = registry.addMapping("/api/**");
reg.allowedOrigins("*");
}
};
}
}

View File

@@ -9,6 +9,7 @@ import org.dreamexposure.discal.core.database.DatabaseManager;
import org.dreamexposure.discal.core.enums.network.DisCalRealm;
import org.dreamexposure.discal.core.enums.network.PubSubReason;
import org.dreamexposure.discal.core.logger.Logger;
import org.dreamexposure.discal.core.object.BotSettings;
import org.dreamexposure.discal.core.object.GuildSettings;
import org.dreamexposure.novautils.events.network.pubsub.PubSubReceiveEvent;
@@ -26,17 +27,17 @@ import java.util.Optional;
public class PubSubListener {
@Subscribe
public void handle(PubSubReceiveEvent event) {
Optional<Guild> g = Optional.empty();
//Check if this even applies to us!
if (event.getData().has("Guild-Id")) {
g = GuildFinder.findGuild(Snowflake.of(event.getData().getString("Guild-Id")));
if (!g.isPresent()) return; //Guild not connected to this client, correct client will handle this.
}
if (event.getChannelName().equalsIgnoreCase(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All")) {
Optional<Guild> g = Optional.empty();
//Check if this even applies to us!
if (event.getData().has("Guild-Id")) {
g = GuildFinder.findGuild(Snowflake.of(event.getData().getString("Guild-Id")));
if (!g.isPresent()) return; //Guild not connected to this client, correct client will handle this.
}
PubSubReason reason = PubSubReason.valueOf(event.getData().getString("Reason"));
DisCalRealm realm = DisCalRealm.valueOf(event.getData().getString("Realm"));
PubSubReason reason = PubSubReason.valueOf(event.getData().getString("Reason"));
DisCalRealm realm = DisCalRealm.valueOf(event.getData().getString("Realm"));
if (event.getChannelName().equalsIgnoreCase("DisCal/ToClient/All")) {
switch (reason) {
case UPDATE:
if (realm.equals(DisCalRealm.BOT_SETTINGS)) {

View File

@@ -14,6 +14,7 @@ import org.dreamexposure.discal.core.crypto.KeyGenerator;
import org.dreamexposure.discal.core.database.DatabaseManager;
import org.dreamexposure.discal.core.enums.network.DisCalRealm;
import org.dreamexposure.discal.core.enums.network.PubSubReason;
import org.dreamexposure.discal.core.object.BotSettings;
import org.dreamexposure.discal.core.object.GuildSettings;
import org.dreamexposure.discal.core.object.command.CommandInfo;
import org.dreamexposure.discal.core.object.web.UserAPIAccount;
@@ -159,7 +160,7 @@ public class DevCommand implements ICommand {
request.put("Realm", DisCalRealm.GUILD_IS_PATRON);
request.put("Guild-Id", args[1]);
PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request);
PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request);
MessageManager.sendMessageAsync("DisCal will update the isPatron status of the guild (if connected). Please allow some time for this to propagate across the network!", event);
} else {
@@ -236,7 +237,7 @@ public class DevCommand implements ICommand {
request.put("Realm", DisCalRealm.GUILD_IS_DEV);
request.put("Guild-Id", args[1]);
PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request);
PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request);
MessageManager.sendMessageAsync("DisCal will update the isDevGuild status of the guild (if connected). Please allow some time for this to propagate across the network!", event);
} else {
@@ -275,7 +276,7 @@ public class DevCommand implements ICommand {
request.put("Guild-Id", args[1]);
request.put("Max-Calendars", mc);
PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request);
PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request);
MessageManager.sendMessageAsync("DisCal will update the max calendar limit of the specified guild (if connected). Please allow some time for this to propagate across the network!", event);
} catch (NumberFormatException e) {
@@ -311,7 +312,7 @@ public class DevCommand implements ICommand {
request.put("Realm", DisCalRealm.GUILD_LEAVE);
request.put("Guild-Id", args[1]);
PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request);
PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request);
MessageManager.sendMessageAsync("DisCal will leave the specified guild (if connected). Please allow some time for this to propagate across the network!", event);
} else {
@@ -328,7 +329,7 @@ public class DevCommand implements ICommand {
request.put("Reason", PubSubReason.HANDLE.name());
request.put("Realm", DisCalRealm.BOT_LANGS);
PubSubManager.get().publish("DisCal/ToClient/All", DisCalClient.clientId(), request);
PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", DisCalClient.clientId(), request);
MessageManager.sendMessageAsync("Reloading lang files! Please give this time to propagate across the network.", event);
}

View File

@@ -2,6 +2,7 @@ package org.dreamexposure.discal.client.service;
import org.dreamexposure.discal.client.DisCalClient;
import org.dreamexposure.discal.core.logger.Logger;
import org.dreamexposure.discal.core.object.BotSettings;
import org.dreamexposure.novautils.network.pubsub.PubSubManager;
import org.joda.time.Interval;
import org.joda.time.Period;
@@ -33,7 +34,7 @@ public class KeepAliveHandler {
data.put("Uptime", humanReadableUptime());
//TODO: Add announcement count!!!
PubSubManager.get().publish("DisCal/ToServer/KeepAlive", DisCalClient.clientId(), data);
PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToServer/KeepAlive", DisCalClient.clientId(), data);
Logger.getLogger().debug("Sent keep alive to server.", false);
}

View File

@@ -17,8 +17,9 @@ public enum BotSettings {
SQL_DB, SQL_PREFIX,
REDIS_PASSWORD, REDIS_HOSTNAME, REDIS_PORT,
PUBSUB_PREFIX,
COM_USER, COM_PASS,
COM_USER, COM_PASS, COM_SUB_DOMAIN,
TOKEN, SECRET, ID,

View File

@@ -59,7 +59,7 @@ public class DisCalServer {
//Start Redis PubSub Listeners
PubSubManager.get().init(BotSettings.REDIS_HOSTNAME.get(), Integer.valueOf(BotSettings.REDIS_PORT.get()), "N/a", BotSettings.REDIS_PASSWORD.get());
//We must register each channel we want to use. This is super important.
PubSubManager.get().register(-1, "DisCal/ToServer/KeepAlive");
PubSubManager.get().register(-1, BotSettings.PUBSUB_PREFIX.get() + "/ToServer/KeepAlive");
//Handle the rest of the bullshit
UpdateDisBotData.init();

View File

@@ -5,8 +5,12 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableAutoConfiguration
@@ -17,4 +21,15 @@ public class ServletConfig implements
factory.setPort(Integer.valueOf(BotSettings.PORT.get()));
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/"));
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
CorsRegistration reg = registry.addMapping("/api/**");
reg.allowedOrigins("*");
}
};
}
}

View File

@@ -70,7 +70,7 @@ public class DashboardHandler {
try {
Request httpRequest = new Request.Builder()
.url("https://client-" + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/dashboard/guild")
.url("https://" + BotSettings.COM_SUB_DOMAIN.get() + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/dashboard/guild")
.post(httpRequestBody)
.header("Content-Type", "application/json")
.header("Authorization", Credentials.basic(BotSettings.COM_USER.get(), BotSettings.COM_PASS.get()))
@@ -193,7 +193,7 @@ public class DashboardHandler {
data.put("Guild-Id", g.getId());
data.put("Bot-Nick", g.getBotNick());
PubSubManager.get().publish("DisCal/ToClient/All", -1, data);
PubSubManager.get().publish(BotSettings.PUBSUB_PREFIX.get() + "/ToClient/All", -1, data);
}
} else if (queryParams.containsKey("prefix")) {
//Update prefix...

View File

@@ -125,7 +125,7 @@ public class DiscordAccountHandler {
try {
Request httpRequest = new Request.Builder()
.url("https://client-" + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/embed/calendar")
.url("https://" + BotSettings.COM_SUB_DOMAIN.get() + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/embed/calendar")
.post(httpRequestBody)
.header("Content-Type", "application/json")
.header("Authorization", Credentials.basic(BotSettings.COM_USER.get(), BotSettings.COM_PASS.get()))
@@ -185,7 +185,7 @@ public class DiscordAccountHandler {
try {
Request httpRequest = new Request.Builder()
.url("https://client-" + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/embed/calendar")
.url("https://" + BotSettings.COM_SUB_DOMAIN.get() + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/embed/calendar")
.post(httpRequestBody)
.header("Content-Type", "application/json")
.header("Authorization", Credentials.basic(BotSettings.COM_USER.get(), BotSettings.COM_PASS.get()))

View File

@@ -2,6 +2,7 @@ package org.dreamexposure.discal.server.listeners;
import com.google.common.eventbus.Subscribe;
import org.dreamexposure.discal.core.logger.Logger;
import org.dreamexposure.discal.core.object.BotSettings;
import org.dreamexposure.discal.core.object.network.discal.ConnectedClient;
import org.dreamexposure.discal.server.DisCalServer;
import org.dreamexposure.novautils.events.network.pubsub.PubSubReceiveEvent;
@@ -19,7 +20,7 @@ public class PubSubListener {
@Subscribe
public static void handle(PubSubReceiveEvent event) {
//Handle keep alive...
if (event.getChannelName().equalsIgnoreCase("DisCal/ToServer/KeepAlive")) {
if (event.getChannelName().equalsIgnoreCase(BotSettings.PUBSUB_PREFIX.get() + "/ToServer/KeepAlive")) {
if (DisCalServer.getNetworkInfo().clientExists(event.getClient())) {
//In network, update info...
ConnectedClient cc = DisCalServer.getNetworkInfo().getClient(event.getClient());

View File

@@ -108,7 +108,7 @@ public class DiscordLoginHandler {
try {
Request requestNew = new Request.Builder()
.url("https://client-" + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/dashboard/defaults")
.url("https://" + BotSettings.COM_SUB_DOMAIN.get() + cc.getClientIndex() + ".discalbot.com/api/v1/com/website/dashboard/defaults")
.post(httpRequestBody)
.header("Content-Type", "application/json")
.header("Authorization", Credentials.basic(BotSettings.COM_USER.get(), BotSettings.COM_PASS.get()))