Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/xdnw/locutus
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Oct 25, 2024
2 parents 0d8da15 + 4c091fc commit 1c0a839
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public Set<SelectorInfo> getSelectorInfo() {
new SelectorInfo("USER_ID", "123456789012345678", "A discord user id"),
new SelectorInfo("https://politicsandwar.com/index.php?id=15&tax_id=TAX_ID", "https://politicsandwar.com/index.php?id=15&tax_id=1234", "A full tax url"),
new SelectorInfo("TAX_ID", "tax_id=1234", "A tax bracket id or url"),
new SelectorInfo("*", null, "All nations")
new SelectorInfo("*", null, "All nations"),
new SelectorInfo("nation(<timestamp>,<includeVM:bool>):SELECTOR", "nation(5d,true):*", "As a sheet tab name; a snapshot selector, with optional timestamp and includeVM")
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8664,6 +8664,22 @@ public VM_ALERT_CHANNEL channel(String value) {
}
}
public static class settings_recruit{
@AutoRegister(clazz=link.locutus.discord.db.guild.GuildKey.class,method="MAIL_NEW_APPLICANTS", field="MAIL_NEW_APPLICANTS")
public static class MAIL_NEW_APPLICANTS extends CommandRef {
public static final MAIL_NEW_APPLICANTS cmd = new MAIL_NEW_APPLICANTS();
public MAIL_NEW_APPLICANTS enabled(String value) {
return set("enabled", value);
}

}
@AutoRegister(clazz=link.locutus.discord.db.guild.GuildKey.class,method="MAIL_NEW_APPLICANTS_TEXT", field="MAIL_NEW_APPLICANTS_TEXT")
public static class MAIL_NEW_APPLICANTS_TEXT extends CommandRef {
public static final MAIL_NEW_APPLICANTS_TEXT cmd = new MAIL_NEW_APPLICANTS_TEXT();
public MAIL_NEW_APPLICANTS_TEXT message(String value) {
return set("message", value);
}

}
@AutoRegister(clazz=link.locutus.discord.db.guild.GuildKey.class,method="RECRUIT_MESSAGE_CONTENT", field="RECRUIT_MESSAGE_CONTENT")
public static class RECRUIT_MESSAGE_CONTENT extends CommandRef {
public static final RECRUIT_MESSAGE_CONTENT cmd = new RECRUIT_MESSAGE_CONTENT();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ public static Guild guild(Context context, @Default @Me DBNation nation, @Defaul
public static final String TOKEN_URL = "https://discord.com/api/oauth2/token";
public static final String API_URL = "https://discord.com/api/users/@me";

public static void logout(Context context, @Nullable DBAuthRecord auth, boolean redirect) {
public static void logout(WebStore ws, Context context, @Nullable DBAuthRecord auth, boolean redirect) {
for (PageHandler.CookieType type : PageHandler.CookieType.values()) {
context.removeCookie(type.getCookieId());
}
if (auth != null) {
WebRoot.db().removeToken(auth.getUUID(), auth.getNationIdRaw(), auth.getUserIdRaw());
}
if (redirect) {
PageHelper.redirect(context, WebRoot.REDIRECT);
PageHelper.redirect(ws, context, WebRoot.REDIRECT);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public String login(WebStore ws, Context context, @Default Integer nation, @Defa
public String logout(WebStore ws, Context context) throws IOException {
DBAuthRecord auth = AuthBindings.getAuth(ws, context, false, false, false);
Guild guild = auth == null ? null : AuthBindings.guild(context, auth.getNation(true), auth.getUser(true), false);
AuthBindings.logout(context, auth, false);
AuthBindings.logout(ws, context, auth, false);
if (auth != null) {
if (guild == null) guild = auth.getDefaultGuild();
Map<String, Object> data = auth.toMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import link.locutus.discord.commands.manager.v2.binding.WebStore;
import link.locutus.discord.commands.manager.v2.binding.annotation.Command;
import link.locutus.discord.commands.manager.v2.binding.annotation.Me;
import link.locutus.discord.commands.manager.v2.binding.annotation.NoForm;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.IsAlliance;
import link.locutus.discord.commands.manager.v2.impl.discord.permission.RolePermission;
import link.locutus.discord.db.GuildDB;
Expand Down Expand Up @@ -51,6 +52,7 @@ public class IAPages {
@Command()
@RolePermission(Roles.INTERNAL_AFFAIRS_STAFF)
@IsAlliance
@NoForm
public String iaChannels(WebStore ws, @Me GuildDB db, @Me DBNation me, @Me User author) throws ExecutionException, InterruptedException {
IACategory iaCat = db.getIACategory();
iaCat.load();
Expand Down Expand Up @@ -154,6 +156,7 @@ public String iaChannels(WebStore ws, @Me GuildDB db, @Me DBNation me, @Me User
@Command()
@RolePermission(Roles.INTERNAL_AFFAIRS_STAFF)
@IsAlliance
@NoForm
public Object memberAuditIndex(WebStore ws, @Me GuildDB db) throws IOException {
AllianceList alliance = db.getAllianceList();
IACheckup checkup = new IACheckup(db, alliance, true);
Expand Down Expand Up @@ -193,6 +196,7 @@ public Object memberAuditIndex(WebStore ws, @Me GuildDB db) throws IOException {
@Command()
@IsAlliance
@RolePermission(Roles.INTERNAL_AFFAIRS_STAFF)
@NoForm
public Object mentorIndex(WebStore ws, IACategory iaCat, @Me Guild guild, @Me GuildDB db, @Me DBNation me) throws IOException {
List<DBNation> mentors = new ArrayList<>();
Roles[] mentorRoles = new Roles[]{Roles.INTERVIEWER, Roles.MENTOR};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ private void recursive(CommandCallable root, Consumer<CommandCallable> onEach) {
}

@Command()
@NoForm
public Object guildindex(WebStore ws, @Me GuildDB db, @Me User user, ArgumentStack stack) {
CommandGroup commands = Locutus.imp().getCommandManager().getV2().getCommands();
CommandGroup pages = WebRoot.getInstance().getPageHandler().getCommands();
Expand All @@ -230,9 +231,10 @@ public Object guildindex(WebStore ws, @Me GuildDB db, @Me User user, ArgumentSta
}

@Command()
@NoForm
public Object register(WebStore ws, Context context, @Default @Me User user) throws IOException {
if (user == null) {
return PageHelper.redirect(context, AuthBindings.getDiscordAuthUrl());
return PageHelper.redirect(ws, context, AuthBindings.getDiscordAuthUrl());
}
DBAuthRecord auth = AuthBindings.getAuth(ws, context, true, true, true);
return WebStore.render(f -> JteunregisterGenerated.render(f, null, ws));
Expand All @@ -249,7 +251,7 @@ public Object login(WebStore ws, Context context, @Default @Me GuildDB current,
// return and redirect
String url = AuthBindings.getRedirect(context, true);
if (url != null) {
return PageHelper.redirect(context, url);
return PageHelper.redirect(ws, context, url);
}
Map<String, Object> result = new HashMap<>();
if (nation != null) {
Expand All @@ -271,9 +273,9 @@ public Object login(WebStore ws, Context context, @Default @Me GuildDB current,
}

@Command()
public Object setguild(Context context, Guild guild) {
public Object setguild(WebStore ws, Context context, Guild guild) {
AuthBindings.setGuild(context, guild);
return PageHelper.redirect(context, AuthBindings.getRedirect(context, true));
return PageHelper.redirect(ws, context, AuthBindings.getRedirect(context, true));
}

@Command()
Expand Down Expand Up @@ -329,12 +331,13 @@ public Object logout(WebStore ws, Context context, @Me @Default DBAuthRecord aut
if (context.method() != HandlerType.POST) {
return WebStore.render(f -> JtelogoutGenerated.render(f, null, ws, auth));
} else {
AuthBindings.logout(context, auth, true);
AuthBindings.logout(ws, context, auth, true);
return null;
}
}

@Command
@NoForm
public Object unregister(WebStore ws, Context context, @Me @Default DBAuthRecord auth) throws IOException {
if (auth == null) {
return "You are not logged in";
Expand All @@ -349,7 +352,7 @@ public Object unregister(WebStore ws, Context context, @Me @Default DBAuthRecord
}
Locutus.imp().getDiscordDB().unregister(nationId, userId);
Locutus.imp().getDiscordDB().deleteApiKeyPairByNation(nationId);
AuthBindings.logout(context, auth, true);
AuthBindings.logout(ws, context, auth, true);
return "Unregistering. If you are not redirected, please visit <a href=\"" + WebRoot.REDIRECT + "\">" + WebRoot.REDIRECT + "</a>";
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package link.locutus.discord.web.commands.page;

import io.javalin.http.Context;
import link.locutus.discord.commands.manager.v2.binding.WebStore;
import link.locutus.discord.util.MarkupUtil;
import gg.jte.generated.precompiled.auth.JteredirectGenerated;
import java.net.MalformedURLException;
import java.net.URL;

public class PageHelper {
public static String redirect(Context context, String url) {
public static String redirect(WebStore ws, Context context, String url) {
context.redirect(url);
context.status(304);
context.status(302);
context.header("cache-control", "no-store");
return "Redirecting to " + MarkupUtil.htmlUrl(url, url) + ". If you are not redirected, click the link.";
URL urlObj;
try {
urlObj = new URL(url);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
return WebStore.render(f -> JteredirectGenerated.render(f, null, ws, urlObj));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import link.locutus.discord.Locutus;
import link.locutus.discord.commands.manager.v2.binding.WebStore;
import link.locutus.discord.commands.manager.v2.binding.annotation.NoForm;
import link.locutus.discord.commands.manager.v2.impl.pw.refs.CM;
import link.locutus.discord.commands.WarCategory;
import link.locutus.discord.commands.manager.v2.binding.annotation.Command;
Expand Down Expand Up @@ -52,6 +53,7 @@ public class WarPages {
// }

@Command(desc = "Wars which are currently uncountered")
@NoForm
@RolePermission(Roles.MILCOM)
public String counter(WebStore ws, @Me GuildDB db) {
Set<Integer> offshore = db.getCoalition(Coalition.OFFSHORE);
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/link/locutus/discord/web/jooby/PageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,10 @@ private ArgumentStack createStack(Context ctx, List<String> args) {
}

private void handleCommand(Context ctx) {
WebStore ws = null;
try {
ArgumentStack stack = createStack(ctx);
ws = stack.getStore().getProvided(WebStore.class);
ctx.header("Content-Type", "text/html;charset=UTF-8");
String path = stack.getCurrent();
boolean isPost = ctx.method() == HandlerType.POST;
Expand All @@ -434,8 +436,7 @@ private void handleCommand(Context ctx) {
String prefix = cmd instanceof ParametricCallable ? "sse" : "command";
String endpoint = WebRoot.REDIRECT + "/" + prefix + "/" + cmd.getFullPath("/");
if (!endpoint.endsWith("/")) endpoint += "/";
ctx.result(WebUtil.minify(cmd.toHtml(stack.getStore().getProvided(WebStore.class), stack.getPermissionHandler(), endpoint, true)));
ctx.status(200);
ctx.result(WebUtil.minify(cmd.toHtml(ws, stack.getPermissionHandler(), endpoint, true)));
break;
}
// page
Expand Down Expand Up @@ -463,13 +464,12 @@ private void handleCommand(Context ctx) {
}
Object[] parsed = parametric.parseArgumentMap(queryMap, stack.getStore(), validators, permisser);
Object cmdResult = parametric.call(parametric.getObject(), stack.getStore(), parsed);
result = wrap(stack.getStore().getProvided(WebStore.class), cmdResult, ctx);
result = wrap(ws, cmdResult, ctx);
} else {
result = cmd.toHtml(stack.getStore().getProvided(WebStore.class), stack.getPermissionHandler(), false);
result = cmd.toHtml(ws, stack.getPermissionHandler(), false);
}
if (result != null && (!(result instanceof String) || !result.toString().isEmpty())) {
ctx.result(WebUtil.minify(result.toString()));
ctx.status(200);
} else if (result != null) {
throw new IllegalArgumentException("Illegal result: " + result + " for " + path);
} else {
Expand All @@ -479,11 +479,11 @@ private void handleCommand(Context ctx) {
}
} catch (Throwable e) {
System.out.println("Throwable " + e.getMessage());
handleErrors(e, ctx);
handleErrors(e, ws, ctx);
}
}

private void handleErrors(Throwable e, Context ctx) {
private void handleErrors(Throwable e, WebStore ws, Context ctx) {
while (e.getCause() != null) {
Throwable tmp = e.getCause();
if (tmp == e) break;
Expand All @@ -497,7 +497,7 @@ private void handleErrors(Throwable e, Context ctx) {
ctx.result(WebUtil.minify(msg));
return;
}
PageHelper.redirect(ctx, redirectResponse.getMessage());
PageHelper.redirect(ws, ctx, redirectResponse.getMessage());
return;
}
e.printStackTrace();
Expand Down Expand Up @@ -581,13 +581,15 @@ private LocalValueStore setupLocals(LocalValueStore<Object> locals, Context ctx,
}

public void sseCmdPage(SseClient2 sse) throws IOException {
WebStore ws = null;
try {
Context ctx = sse.ctx;
String pathStr = ctx.path();
if (pathStr.startsWith("/")) pathStr = pathStr.substring(1);
List<String> path = new ArrayList<>(Arrays.asList(pathStr.split("/")));
path.remove("command");
LocalValueStore locals = setupLocals(null, ctx, path);
ws = (WebStore) locals.getProvided(WebStore.class);
CommandCallable cmd = commands.getCallable(path);
if (cmd == null) {
sseMessage(sse, "Command not found: " + path, false);
Expand Down Expand Up @@ -618,7 +620,7 @@ public void sseCmdPage(SseClient2 sse) throws IOException {
response.addProperty("value", redirect);
sse.sendEvent(response);
} catch (Throwable e) {
handleErrors(e, sse.ctx);
handleErrors(e, ws, sse.ctx);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public String generateMarkdown() {
commandMarkdownSpoiler(CM.settings_recruit.RECRUIT_MESSAGE_DELAY.cmd),
"# Send a test message",
commandMarkdownSpoiler(CM.mail.recruit.cmd),
"# Configure a message to send new applicants",
commandMarkdownSpoiler(CM.settings_recruit.MAIL_NEW_APPLICANTS.cmd),
commandMarkdownSpoiler(CM.settings_recruit.MAIL_NEW_APPLICANTS_TEXT.cmd),
"""
# Other recruitment strategies
- Members inviting their friends to the game
Expand Down
7 changes: 7 additions & 0 deletions src/main/jte/auth/redirect.jte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@param link.locutus.discord.commands.manager.v2.binding.WebStore ws
@param java.net.URL url

@template.main(ws = ws, content = @`
Redirecting to <a href="${url.toString()}">${url.toString()}</a>
If you are not redirected, click the link.
`, title = "Redirect", navbar = null)

0 comments on commit 1c0a839

Please sign in to comment.