From 573f806676a7ebc2a62aa39d742127fbf74fbb3c Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 22 Mar 2020 09:23:07 +0100 Subject: Lazychat: Add paging. Change-Id: I1db0b0733397d5e7a77b925ba185a39a72041f3e --- .../mulkcms2/benki/bookmarks/BookmarkResource.java | 4 +- .../mulkcms2/benki/lazychat/LazychatResource.java | 79 +++++++++++++++------- 2 files changed, 55 insertions(+), 28 deletions(-) (limited to 'src/main/java/eu') diff --git a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java index 2272db2..1f57c74 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java @@ -105,7 +105,7 @@ public class BookmarkResource { var q = Bookmark.findViewable(session, identity, null, cursor, maxResults); return bookmarkList - .data("bookmarks", q.posts) + .data("posts", q.posts) .data("feedUri", "/bookmarks/feed") .data("authenticated", !identity.isAnonymous()) .data("hasPreviousPage", q.prevCursor != null) @@ -130,7 +130,7 @@ public class BookmarkResource { var q = Bookmark.findViewable(session, identity, owner, cursor, maxResults); return bookmarkList - .data("bookmarks", q.posts) + .data("posts", q.posts) .data("feedUri", String.format("/bookmarks/~%s/feed", ownerName)) .data("authenticated", !identity.isAnonymous()) .data("hasPreviousPage", q.prevCursor != null) diff --git a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatResource.java b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatResource.java index fb46217..4284d96 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatResource.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/lazychat/LazychatResource.java @@ -2,10 +2,7 @@ package eu.mulk.mulkcms2.benki.lazychat; import static javax.ws.rs.core.MediaType.TEXT_HTML; -import eu.mulk.mulkcms2.benki.accesscontrol.Role; -import eu.mulk.mulkcms2.benki.bookmarks.Bookmark; import eu.mulk.mulkcms2.benki.users.User; -import io.quarkus.panache.common.Sort; import io.quarkus.qute.Template; import io.quarkus.qute.TemplateExtension; import io.quarkus.qute.TemplateInstance; @@ -14,12 +11,18 @@ import io.quarkus.security.identity.SecurityIdentity; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.time.temporal.TemporalAccessor; -import java.util.List; +import javax.annotation.CheckForNull; import javax.inject.Inject; import javax.json.spi.JsonProvider; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.hibernate.Session; import org.jboss.logging.Logger; @Path("/lazychat") @@ -34,36 +37,60 @@ public class LazychatResource { private static JsonProvider jsonProvider = JsonProvider.provider(); + @ConfigProperty(name = "mulkcms.lazychat.default-max-results") + int defaultMaxResults; + @ResourcePath("benki/lazychat/lazychatList.html") @Inject Template lazychatList; @Inject SecurityIdentity identity; + @PersistenceContext EntityManager entityManager; + + @GET + @Produces(TEXT_HTML) + public TemplateInstance getIndex( + @QueryParam("i") @CheckForNull Integer cursor, + @QueryParam("n") @CheckForNull Integer maxResults) { + + maxResults = maxResults == null ? defaultMaxResults : maxResults; + + var session = entityManager.unwrap(Session.class); + var q = LazychatMessage.findViewable(session, identity, null, cursor, maxResults); + + return lazychatList + .data("posts", q.posts) + .data("authenticated", !identity.isAnonymous()) + .data("hasPreviousPage", q.prevCursor != null) + .data("hasNextPage", q.nextCursor != null) + .data("previousCursor", q.prevCursor) + .data("nextCursor", q.nextCursor) + .data("pageSize", maxResults); + } + @GET + @Path("~{ownerName}") @Produces(TEXT_HTML) - public TemplateInstance getPage() { - List lazychatMessages; - if (identity.isAnonymous()) { - Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult(); - lazychatMessages = - Bookmark.find( - "select lm from LazychatMessage lm join lm.targets target left join fetch lm.owner where target = ?1", - Sort.by("date").descending(), - world) - .list(); - } else { - var userName = identity.getPrincipal().getName(); - User user = - User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult(); - lazychatMessages = - Bookmark.find( - "select lm from BenkiUser u inner join u.visibleLazychatMessages lm left join fetch lm.owner where u.id = ?1", - Sort.by("date").descending(), - user.id) - .list(); - } - return lazychatList.data("lazychatMessages", lazychatMessages); + public TemplateInstance getUserIndex( + @PathParam("ownerName") String ownerName, + @QueryParam("i") @CheckForNull Integer cursor, + @QueryParam("n") @CheckForNull Integer maxResults) { + + maxResults = maxResults == null ? defaultMaxResults : maxResults; + + var owner = User.findByNickname(ownerName); + var session = entityManager.unwrap(Session.class); + var q = LazychatMessage.findViewable(session, identity, owner, cursor, maxResults); + + return lazychatList + .data("posts", q.posts) + .data("authenticated", !identity.isAnonymous()) + .data("hasPreviousPage", q.prevCursor != null) + .data("hasNextPage", q.nextCursor != null) + .data("previousCursor", q.prevCursor) + .data("nextCursor", q.nextCursor) + .data("pageSize", maxResults); } @TemplateExtension -- cgit v1.2.3