From 66ead173489549b95cb693de11322701a0a012a5 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Thu, 19 Sep 2024 18:06:18 +0200 Subject: [PATCH] fix stuff by breaking it --- src/main/java/eu/m724/talkpages/Startup.java | 14 ++++++++++++-- .../java/eu/m724/talkpages/auth/AuthService.java | 2 +- .../m724/talkpages/orm/entity/auth/Account.java | 14 +++++++++----- .../m724/talkpages/orm/entity/content/Page.java | 2 ++ .../orm/entity/content/PageRevision.java | 12 +++++++++--- .../talkpages/page/action/ActionService.java | 16 +++++++++------- .../m724/talkpages/template/AuthExtension.java | 8 +++++++- .../resources/templates/AuthResource/delete.html | 6 +++--- 8 files changed, 52 insertions(+), 22 deletions(-) diff --git a/src/main/java/eu/m724/talkpages/Startup.java b/src/main/java/eu/m724/talkpages/Startup.java index a0fefcb..3e9688c 100644 --- a/src/main/java/eu/m724/talkpages/Startup.java +++ b/src/main/java/eu/m724/talkpages/Startup.java @@ -1,5 +1,6 @@ package eu.m724.talkpages; +import eu.m724.talkpages.auth.AuthService; import eu.m724.talkpages.orm.entity.auth.Account; import eu.m724.talkpages.orm.entity.content.Page; import eu.m724.talkpages.orm.entity.content.PageRevision; @@ -19,6 +20,9 @@ public class Startup { @Inject private ThemeService themeService; + @Inject + private AuthService authService; + @ConfigProperty(name = "talkpages.systemUser.name") private String username; @@ -42,6 +46,12 @@ public class Startup { System.out.println("Performing first run setup"); + try { + authService.register("user", "1234"); + } catch (AuthService.UsernameExistsException e) { + throw new RuntimeException(e); + } + Account account = new Account(username); account.persistAndFlush(); @@ -58,12 +68,12 @@ public class Startup { @Transactional public Page addPage(Account account, String title, String content) { Page page = new Page(title); - PageRevision revision = new PageRevision(page, account, content); + PageRevision revision = new PageRevision(page, content); + revision.setAuthor(account); page.setLatestRevision(revision); page.persistAndFlush(); - account.getRevisions().add(revision); return page; } } diff --git a/src/main/java/eu/m724/talkpages/auth/AuthService.java b/src/main/java/eu/m724/talkpages/auth/AuthService.java index 96c9f31..d95ac77 100644 --- a/src/main/java/eu/m724/talkpages/auth/AuthService.java +++ b/src/main/java/eu/m724/talkpages/auth/AuthService.java @@ -24,7 +24,7 @@ public class AuthService { * @throws UsernameExistsException if user with same username already exists */ @Transactional - Session register(String username, String password) throws UsernameExistsException { + public Session register(String username, String password) throws UsernameExistsException { Account account = Account.findByName(username); if (account != null) { diff --git a/src/main/java/eu/m724/talkpages/orm/entity/auth/Account.java b/src/main/java/eu/m724/talkpages/orm/entity/auth/Account.java index fc1ee22..7c0a6fa 100644 --- a/src/main/java/eu/m724/talkpages/orm/entity/auth/Account.java +++ b/src/main/java/eu/m724/talkpages/orm/entity/auth/Account.java @@ -59,12 +59,9 @@ public class Account extends PanacheEntity { private Set roles = new HashSet<>(); - @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL) + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) private Set sessions = new HashSet<>(); - @OneToMany(cascade = CascadeType.ALL, orphanRemoval = false, fetch = FetchType.EAGER) - private List revisions = new ArrayList<>(); - // Getters @@ -73,11 +70,18 @@ public class Account extends PanacheEntity { public String getPassword() { return password; } public Set getRoles() { return roles; } public Set getSessions() { return sessions; } - public List getRevisions() { return revisions; } public boolean isSystemAccount() { return password == null; } + public List getRevisions() { + return PageRevision.find("author", this).list(); + } + + public long getRevisionsCount() { + return PageRevision.count("author", this); + } + // Operations public static Account findByName(String name) { diff --git a/src/main/java/eu/m724/talkpages/orm/entity/content/Page.java b/src/main/java/eu/m724/talkpages/orm/entity/content/Page.java index 3910686..d4adac6 100644 --- a/src/main/java/eu/m724/talkpages/orm/entity/content/Page.java +++ b/src/main/java/eu/m724/talkpages/orm/entity/content/Page.java @@ -3,6 +3,7 @@ package eu.m724.talkpages.orm.entity.content; import eu.m724.talkpages.page.action.NoSuchNamespaceException; import io.quarkus.hibernate.orm.panache.PanacheEntity; import jakarta.persistence.*; +import jakarta.transaction.Transactional; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -56,6 +57,7 @@ public class Page extends PanacheEntity { public List getSubpages() { return subpages; } public Page getParentPage() { return parentPage; } + @Transactional public void setLatestRevision(PageRevision pageRevision) { revisions.add(pageRevision); latestRevision = pageRevision; diff --git a/src/main/java/eu/m724/talkpages/orm/entity/content/PageRevision.java b/src/main/java/eu/m724/talkpages/orm/entity/content/PageRevision.java index 37f8ebd..0c00b1f 100644 --- a/src/main/java/eu/m724/talkpages/orm/entity/content/PageRevision.java +++ b/src/main/java/eu/m724/talkpages/orm/entity/content/PageRevision.java @@ -3,6 +3,7 @@ package eu.m724.talkpages.orm.entity.content; import eu.m724.talkpages.orm.entity.auth.Account; import io.quarkus.hibernate.orm.panache.PanacheEntity; import jakarta.persistence.*; +import jakarta.transaction.Transactional; import org.hibernate.annotations.CreationTimestamp; import java.time.LocalDateTime; @@ -15,7 +16,7 @@ import java.time.LocalDateTime; public class PageRevision extends PanacheEntity { private PageRevision() {} - public PageRevision(Page page, Account author, String content) { + public PageRevision(Page page, String content) { long index = 1; int delta = content.length(); @@ -29,7 +30,6 @@ public class PageRevision extends PanacheEntity { this.page = page; this.index = index; - this.author = author; this.content = content; this.delta = delta; } @@ -45,7 +45,8 @@ public class PageRevision extends PanacheEntity { @CreationTimestamp private LocalDateTime timestamp; - @ManyToOne(cascade = CascadeType.DETACH) + // not a relationship because complicated + @ManyToOne private Account author; // TODO wondering about a table only for content and meta and title perhaps @@ -64,6 +65,11 @@ public class PageRevision extends PanacheEntity { public String getContent() { return content; } public int getDelta() { return delta; } + @Transactional + public void setAuthor(Account account) { + this.author = account; + } + // Operations diff --git a/src/main/java/eu/m724/talkpages/page/action/ActionService.java b/src/main/java/eu/m724/talkpages/page/action/ActionService.java index ec37806..06486c9 100644 --- a/src/main/java/eu/m724/talkpages/page/action/ActionService.java +++ b/src/main/java/eu/m724/talkpages/page/action/ActionService.java @@ -6,6 +6,10 @@ import eu.m724.talkpages.orm.entity.content.PageRevision; import jakarta.enterprise.context.ApplicationScoped; import jakarta.transaction.Transactional; +/** + * This service handles creating and editing pages.
+ * It creates pages and revisions and everything + */ @ApplicationScoped public class ActionService { @Transactional @@ -20,13 +24,12 @@ public class ActionService { } Page page = new Page(title); - PageRevision revision = new PageRevision(page, account, content); + PageRevision revision = new PageRevision(page, content); // TODO + revision.setAuthor(account); page.setLatestRevision(revision); page.persistAndFlush(); - account.getRevisions().add(revision); - return page; } @@ -34,12 +37,11 @@ public class ActionService { Page editPage(Page page, String newContent, Account account) { // constraints are not checked - PageRevision revision = new PageRevision(page, account, newContent); + PageRevision revision = new PageRevision(page, newContent); + revision.setAuthor(account); + revision.persistAndFlush(); page.setLatestRevision(revision); - account.getRevisions().add(revision); - - revision.persist(); return page; } diff --git a/src/main/java/eu/m724/talkpages/template/AuthExtension.java b/src/main/java/eu/m724/talkpages/template/AuthExtension.java index 2bba941..f1dec36 100644 --- a/src/main/java/eu/m724/talkpages/template/AuthExtension.java +++ b/src/main/java/eu/m724/talkpages/template/AuthExtension.java @@ -1,5 +1,7 @@ package eu.m724.talkpages.template; +import eu.m724.talkpages.orm.entity.auth.Account; +import eu.m724.talkpages.orm.entity.auth.Session; import io.quarkus.arc.Arc; import io.quarkus.arc.InstanceHandle; import io.quarkus.qute.TemplateExtension; @@ -13,12 +15,16 @@ public class AuthExtension { return !identity.isAnonymous(); } + public static Session session() { + SecurityIdentity identity = getIdentity(); + return identity.getAttribute("session"); + } + public static String name() { SecurityIdentity identity = getIdentity(); return identity.isAnonymous() ? identity.getAttribute("address") : identity.getPrincipal().getName(); } - private static SecurityIdentity getIdentity() { try (InstanceHandle handle = Arc.container().instance(CurrentIdentityAssociation.class)) { return handle.get().getIdentity(); diff --git a/src/main/resources/templates/AuthResource/delete.html b/src/main/resources/templates/AuthResource/delete.html index 288a58c..2299173 100644 --- a/src/main/resources/templates/AuthResource/delete.html +++ b/src/main/resources/templates/AuthResource/delete.html @@ -8,7 +8,7 @@
  • Your account {user:name} will be deleted.
  • Your user page and talk page, along with their subpages, and all revisions from all users, will be deleted.
  • -
  • Your authored revisions will remain, but your name will be replaced with a shared "Deleted User"
  • +
  • Your {user:session.account.getRevisionsCount} authored revisions will remain, but your name will be replaced with a shared "Deleted User"
  • Your username will become available.
{#else} @@ -23,11 +23,11 @@
  • Your account {user:name} will be deleted.
  • Your user page and talk page, along with their subpages, and all revisions from all users, will be deleted.
  • Your authored revisions will remain, but your name will be replaced with a shared "Deleted User"
  • -
  • If you continue your activity on this website, your actions will still be posted as that IP address.
  • +
  • If you continue your activity on this website, your account will be re-created.
  • {/if} -

    While data is deleted on our side, it probably has been saved on third party archiving websites, legally or not. This is beyond our control, and we take no responsibility for it.

    +

    While data is deleted on our side, it probably has been saved on third party archiving websites. This is beyond our control, and we take no responsibility for it.