| 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.users.User; |
| import io.quarkus.qute.Template; |
| import io.quarkus.qute.TemplateExtension; |
| import io.quarkus.qute.TemplateInstance; |
| import io.quarkus.qute.api.ResourcePath; |
| import io.quarkus.security.Authenticated; |
| import io.quarkus.security.identity.SecurityIdentity; |
| import java.net.URI; |
| import java.net.URISyntaxException; |
| import java.time.OffsetDateTime; |
| import java.time.format.DateTimeFormatter; |
| import java.time.format.FormatStyle; |
| import java.time.temporal.TemporalAccessor; |
| import java.util.Set; |
| import javax.annotation.CheckForNull; |
| import javax.inject.Inject; |
| import javax.json.spi.JsonProvider; |
| import javax.persistence.EntityManager; |
| import javax.persistence.PersistenceContext; |
| import javax.transaction.Transactional; |
| import javax.validation.constraints.NotNull; |
| import javax.validation.constraints.Pattern; |
| import javax.ws.rs.BadRequestException; |
| import javax.ws.rs.FormParam; |
| import javax.ws.rs.GET; |
| import javax.ws.rs.POST; |
| import javax.ws.rs.Path; |
| import javax.ws.rs.PathParam; |
| import javax.ws.rs.Produces; |
| import javax.ws.rs.QueryParam; |
| import javax.ws.rs.core.Response; |
| import org.eclipse.microprofile.config.inject.ConfigProperty; |
| import org.hibernate.Session; |
| import org.jboss.logging.Logger; |
| |
| @Path("/lazychat") |
| public class LazychatResource { |
| |
| private static final Logger log = Logger.getLogger(LazychatResource.class); |
| |
| private static final DateTimeFormatter htmlDateFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; |
| |
| private static final DateTimeFormatter humanDateFormatter = |
| DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT); |
| |
| private static final JsonProvider jsonProvider = JsonProvider.provider(); |
| |
| @ConfigProperty(name = "mulkcms.lazychat.default-max-results") |
| int defaultMaxResults; |
| |
| @ResourcePath("benki/posts/postList.html") |
| @Inject |
| Template postList; |
| |
| @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 postList |
| .data("posts", q.posts) |
| .data("pageTitle", "Lazy Chat") |
| .data("showBookmarkForm", false) |
| .data("showLazychatForm", !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 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 postList |
| .data("posts", q.posts) |
| .data("pageTitle", "Lazy Chat") |
| .data("showBookmarkForm", false) |
| .data("showLazychatForm", !identity.isAnonymous()) |
| .data("hasPreviousPage", q.prevCursor != null) |
| .data("hasNextPage", q.nextCursor != null) |
| .data("previousCursor", q.prevCursor) |
| .data("nextCursor", q.nextCursor) |
| .data("pageSize", maxResults); |
| } |
| |
| @POST |
| @Transactional |
| @Authenticated |
| public Response postMessage( |
| @FormParam("text") String text, |
| @FormParam("visibility") @NotNull @Pattern(regexp = "public|semiprivate|private") |
| String visibility) |
| throws URISyntaxException { |
| |
| var userName = identity.getPrincipal().getName(); |
| var user = User.findByNickname(userName); |
| |
| var message = new LazychatMessage(); |
| message.content = text; |
| message.format = "markdown"; |
| message.owner = user; |
| message.date = OffsetDateTime.now(); |
| |
| if (visibility.equals("public")) { |
| Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult(); |
| message.targets = Set.of(world); |
| } else if (visibility.equals("semiprivate")) { |
| message.targets = Set.copyOf(user.defaultTargets); |
| } else if (!visibility.equals("private")) { |
| throw new BadRequestException(String.format("invalid visibility ā%sā", visibility)); |
| } |
| |
| message.persistAndFlush(); |
| |
| return Response.seeOther(new URI("/lazychat")).build(); |
| } |
| |
| @TemplateExtension |
| static String humanDateTime(TemporalAccessor x) { |
| return humanDateFormatter.format(x); |
| } |
| |
| @TemplateExtension |
| static String htmlDateTime(TemporalAccessor x) { |
| return htmlDateFormatter.format(x); |
| } |
| } |