Compare commits

..

No commits in common. "dc29ccbcd688c24885c77ce4ca5d3464d509e62c" and "6197b4ec9ae22ba48ce28b4676608c8c2837e2b3" have entirely different histories.

11 changed files with 43 additions and 105 deletions

View file

@ -1,6 +1,5 @@
package eu.m724.talkpages; package eu.m724.talkpages;
import eu.m724.talkpages.theme.ThemeService;
import io.quarkus.qute.CheckedTemplate; import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateInstance; import io.quarkus.qute.TemplateInstance;
import io.vertx.core.http.Cookie; import io.vertx.core.http.Cookie;
@ -16,7 +15,6 @@ import jakarta.ws.rs.core.Response;
import org.jboss.resteasy.reactive.RestResponse.Status; import org.jboss.resteasy.reactive.RestResponse.Status;
import java.net.URI; import java.net.URI;
import java.util.List;
@Path("/") @Path("/")
public class IndexResource { public class IndexResource {
@ -26,32 +24,36 @@ public class IndexResource {
@Inject @Inject
HttpServerRequest request; HttpServerRequest request;
@Inject
ThemeService themeService;
@CheckedTemplate @CheckedTemplate
public static class Templates { public static class Templates {
public static native TemplateInstance index(List<String> themes, String currentTheme); public static native TemplateInstance index();
} }
@GET @GET
@Produces(MediaType.TEXT_HTML) @Produces(MediaType.TEXT_HTML)
public TemplateInstance index() { public TemplateInstance index() {
return Templates.index();
Cookie cookie = request.getCookie("theme");
String currentTheme = cookie != null ? cookie.getValue() : null;
return Templates.index(themeService.getThemes(), currentTheme);
} }
@GET @GET
@Path("/theme") @Path("/theme")
public Response toggleTheme(@QueryParam("theme") String theme) { public Response toggleDark() {
NewCookie newCookie = new NewCookie.Builder("theme") Cookie cookie = request.getCookie("dark");
NewCookie newCookie;
if (cookie == null) {
newCookie = new NewCookie.Builder("dark")
.path("/") .path("/")
.value(theme) .value("1")
.maxAge(31560000) .maxAge(31560000)
.build(); .build();
} else {
newCookie = new NewCookie.Builder("dark")
.path("/")
.value("0")
.maxAge(0)
.build();
}
return Response.temporaryRedirect(URI.create("/")).cookie(newCookie).build(); return Response.temporaryRedirect(URI.create("/")).cookie(newCookie).build();
} }

View file

@ -3,7 +3,7 @@ package eu.m724.talkpages;
import eu.m724.talkpages.orm.entity.auth.Account; import eu.m724.talkpages.orm.entity.auth.Account;
import eu.m724.talkpages.orm.entity.content.Page; import eu.m724.talkpages.orm.entity.content.Page;
import eu.m724.talkpages.orm.entity.content.PageRevision; import eu.m724.talkpages.orm.entity.content.PageRevision;
import eu.m724.talkpages.theme.ThemeService; import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.StartupEvent; import io.quarkus.runtime.StartupEvent;
import io.vertx.ext.web.Router; import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler; import io.vertx.ext.web.handler.StaticHandler;
@ -17,24 +17,19 @@ import org.eclipse.microprofile.config.inject.ConfigProperty;
@Singleton @Singleton
public class Startup { public class Startup {
@Inject @Inject
private ThemeService themeService; LaunchMode launchMode;
@ConfigProperty(name = "talkpages.systemUser.name") @ConfigProperty(name = "talkpages.systemUser.name")
private String username; private String username;
void installStaticRoute(@Observes StartupEvent ignoredEvent, Router router) { void installStaticRoute(@Observes StartupEvent startupEvent, Router router) {
router.route() router.route()
.path("/static/*") .path("/static/*")
.handler(StaticHandler.create("static/")); .handler(StaticHandler.create("static/"));
} }
public void examplePage(@Observes StartupEvent ignoredEvent) {
initDatabase();
themeService.indexThemes();
}
@Transactional @Transactional
public void initDatabase() { public void examplePage(@Observes StartupEvent ignoredEvent) {
if (Account.findByName(username) != null) { if (Account.findByName(username) != null) {
// system user exists so assuming this is not the first run // system user exists so assuming this is not the first run
return; return;

View file

@ -10,7 +10,7 @@ public class AccountService {
// TODO I think it would be better to accept InetAddress // TODO I think it would be better to accept InetAddress
@Transactional @Transactional
public Account addressAccount(String address) { public Account addressAccount(String address) {
Account account = Account.findByName(address); Account account = Account.findById(address);
if (account != null) if (account != null)
return account; return account;

View file

@ -1,7 +1,10 @@
package eu.m724.talkpages.template; package eu.m724.talkpages.template;
import io.quarkus.qute.TemplateExtension; import io.quarkus.qute.TemplateExtension;
import io.vertx.core.http.Cookie;
import io.vertx.core.http.HttpServerRequest;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.safety.Safelist; import org.jsoup.safety.Safelist;

View file

@ -1,27 +1,21 @@
package eu.m724.talkpages.template; package eu.m724.talkpages.template;
import eu.m724.talkpages.theme.ThemeService; import eu.m724.talkpages.RedirectService;
import io.quarkus.arc.Arc; import io.quarkus.arc.Arc;
import io.quarkus.qute.TemplateExtension; import io.quarkus.qute.TemplateExtension;
import io.vertx.core.Vertx;
import io.vertx.core.http.Cookie; import io.vertx.core.http.Cookie;
import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.HttpServerRequest;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Request;
@TemplateExtension(namespace = "theme") @TemplateExtension(namespace = "theme")
public class ThemeExtension { public class ThemeExtension {
public static String getTheme() { public static boolean darkTheme() {
// I don't like those arc containers // this is bad
HttpServerRequest request = Arc.container().instance(HttpServerRequest.class).get(); HttpServerRequest request = Arc.container().instance(HttpServerRequest.class).get();
ThemeService themeService = Arc.container().instance(ThemeService.class).get(); Cookie cookie = request.getCookie("dark");
Cookie cookie = request.getCookie("theme"); return cookie == null || cookie.getValue().equals("0");
if (cookie != null) {
String theme = cookie.getValue();
if (themeService.getThemes().contains(theme))
return theme;
}
return themeService.getDefaultTheme();
} }
} }

View file

@ -1,45 +0,0 @@
package eu.m724.talkpages.theme;
import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ApplicationScoped
public class ThemeService {
@ConfigProperty(name = "talkpages.theme.default")
private String defaultTheme;
private List<String> themes = new ArrayList<>();
public List<String> getThemes() {
return themes;
}
public String getDefaultTheme() {
return defaultTheme;
}
public void indexThemes() {
URL url = getClass().getClassLoader().getResource("static" + File.separator + "theme");
if (url == null) return;
File directory = new File(url.getPath());
if (!directory.isDirectory()) return;
File[] contents = directory.listFiles();
if (contents == null) return;
themes = Arrays.stream(contents)
.map(File::getName)
.filter(fn -> fn.endsWith(".css"))
.map(fn -> fn.substring(0, fn.length() - 4))
.toList();
System.out.printf("Found %d themes: %s\n", themes.size(), String.join(", ", themes));
}
}

View file

@ -1,6 +1,5 @@
talkpages.homePage=/ talkpages.homePage=/
talkpages.systemUser.name=System talkpages.systemUser.name=System
talkpages.theme.default=light
quarkus.http.auth.basic=true quarkus.http.auth.basic=true

View file

@ -1,5 +1,5 @@
body { body {
background: #222; background: #202020;
color: white; color: white;
} }

View file

@ -1 +0,0 @@
/* stub */

View file

@ -15,24 +15,15 @@
Login or register Login or register
{/if} {/if}
</a></li> </a></li>
<li> <li><a href="/theme">
<form action="/theme"> {#if theme:darkTheme}
<label for="themePicker">Theme</label> Light mode
<select id="themePicker" name="theme">
{#for theme in themes}
{#if theme.equals(currentTheme)}
<option value="{theme}" selected>{theme}</option>
{#else} {#else}
<option value="{theme}">{theme}</option> Dark mode
{/if} {/if}
{/for} </a></li>
</select> <li><a href="/page/TalkPages/Terms of Service">Terms of Service</a></li>
<input type="submit" value="Apply">
</form>
</li>
</ul> </ul>
<small><a href="/page/TalkPages/Terms of Service">Terms of Service</a></small>
<br>
<small>Running <a href="/page/TalkPages">TalkPages</a> version {config:["quarkus.application.version"]}</small> <small>Running <a href="/page/TalkPages">TalkPages</a> version {config:["quarkus.application.version"]}</small>
{/include} {/include}

View file

@ -5,8 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- TODO opengraph tags and maybe some nice css. also add errors and stuff here --> <!-- TODO opengraph tags and maybe some nice css. also add errors and stuff here -->
<link rel="stylesheet" href="/static/style.css"> <link rel="stylesheet" href="/static/style.css">
{#if theme:getTheme??} {#if theme:darkTheme}
<link rel="stylesheet" href="/static/theme/{theme:getTheme}.css"> <link rel="stylesheet" href="/static/dark.css">
{/if} {/if}
<title>{#insert pageTitle /}{#if !customTitle??} - TalkPages{/if}</title> <!-- I give up --> <title>{#insert pageTitle /}{#if !customTitle??} - TalkPages{/if}</title> <!-- I give up -->
</head> </head>