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;
import eu.m724.talkpages.theme.ThemeService;
import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateInstance;
import io.vertx.core.http.Cookie;
@ -16,7 +15,6 @@ import jakarta.ws.rs.core.Response;
import org.jboss.resteasy.reactive.RestResponse.Status;
import java.net.URI;
import java.util.List;
@Path("/")
public class IndexResource {
@ -26,32 +24,36 @@ public class IndexResource {
@Inject
HttpServerRequest request;
@Inject
ThemeService themeService;
@CheckedTemplate
public static class Templates {
public static native TemplateInstance index(List<String> themes, String currentTheme);
public static native TemplateInstance index();
}
@GET
@Produces(MediaType.TEXT_HTML)
public TemplateInstance index() {
Cookie cookie = request.getCookie("theme");
String currentTheme = cookie != null ? cookie.getValue() : null;
return Templates.index(themeService.getThemes(), currentTheme);
return Templates.index();
}
@GET
@Path("/theme")
public Response toggleTheme(@QueryParam("theme") String theme) {
NewCookie newCookie = new NewCookie.Builder("theme")
public Response toggleDark() {
Cookie cookie = request.getCookie("dark");
NewCookie newCookie;
if (cookie == null) {
newCookie = new NewCookie.Builder("dark")
.path("/")
.value(theme)
.value("1")
.maxAge(31560000)
.build();
} else {
newCookie = new NewCookie.Builder("dark")
.path("/")
.value("0")
.maxAge(0)
.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.content.Page;
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.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
@ -17,24 +17,19 @@ import org.eclipse.microprofile.config.inject.ConfigProperty;
@Singleton
public class Startup {
@Inject
private ThemeService themeService;
LaunchMode launchMode;
@ConfigProperty(name = "talkpages.systemUser.name")
private String username;
void installStaticRoute(@Observes StartupEvent ignoredEvent, Router router) {
void installStaticRoute(@Observes StartupEvent startupEvent, Router router) {
router.route()
.path("/static/*")
.handler(StaticHandler.create("static/"));
}
public void examplePage(@Observes StartupEvent ignoredEvent) {
initDatabase();
themeService.indexThemes();
}
@Transactional
public void initDatabase() {
public void examplePage(@Observes StartupEvent ignoredEvent) {
if (Account.findByName(username) != null) {
// system user exists so assuming this is not the first run
return;

View file

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

View file

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

View file

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

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.systemUser.name=System
talkpages.theme.default=light
quarkus.http.auth.basic=true

View file

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

View file

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

View file

@ -15,24 +15,15 @@
Login or register
{/if}
</a></li>
<li>
<form action="/theme">
<label for="themePicker">Theme</label>
<select id="themePicker" name="theme">
{#for theme in themes}
{#if theme.equals(currentTheme)}
<option value="{theme}" selected>{theme}</option>
{#else}
<option value="{theme}">{theme}</option>
{/if}
{/for}
</select>
<input type="submit" value="Apply">
</form>
</li>
<li><a href="/theme">
{#if theme:darkTheme}
Light mode
{#else}
Dark mode
{/if}
</a></li>
<li><a href="/page/TalkPages/Terms of Service">Terms of Service</a></li>
</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>
{/include}

View file

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