blob: 8be3d4acdb9bad4e2e3a63d4955ad43b0f19f65d [file] [log] [blame]
Matthias Andreas Benkard94b5e7b2020-02-09 20:02:20 +01001package eu.mulk.mulkcms2.benki.lazychat;
2
3import static javax.ws.rs.core.MediaType.TEXT_HTML;
4
5import eu.mulk.mulkcms2.benki.accesscontrol.Role;
6import eu.mulk.mulkcms2.benki.bookmarks.Bookmark;
7import eu.mulk.mulkcms2.benki.users.User;
8import io.quarkus.panache.common.Sort;
9import io.quarkus.qute.Template;
10import io.quarkus.qute.TemplateExtension;
11import io.quarkus.qute.TemplateInstance;
12import io.quarkus.qute.api.ResourcePath;
13import io.quarkus.security.identity.SecurityIdentity;
14import java.time.format.DateTimeFormatter;
15import java.time.format.FormatStyle;
16import java.time.temporal.TemporalAccessor;
17import java.util.List;
18import javax.inject.Inject;
19import javax.json.spi.JsonProvider;
20import javax.ws.rs.GET;
21import javax.ws.rs.Path;
22import javax.ws.rs.Produces;
23import org.jboss.logging.Logger;
24
25@Path("/lazychat")
26public class LazychatResource {
27
28 private static Logger log = Logger.getLogger(LazychatResource.class);
29
30 private static DateTimeFormatter htmlDateFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
31
32 private static DateTimeFormatter humanDateFormatter =
33 DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT);
34
35 private static JsonProvider jsonProvider = JsonProvider.provider();
36
37 @ResourcePath("benki/lazychat/lazychatList.html")
38 @Inject
39 Template lazychatList;
40
41 @Inject SecurityIdentity identity;
42
43 @GET
44 @Produces(TEXT_HTML)
45 public TemplateInstance getPage() {
46 List<LazychatMessage> lazychatMessages;
47 if (identity.isAnonymous()) {
48 Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult();
49 lazychatMessages =
50 Bookmark.find(
51 "select bm from LazychatMessage lm join lm.targets target left join fetch lm.owner where target = ?1",
52 Sort.by("date").descending(),
53 world)
54 .list();
55 } else {
56 var userName = identity.getPrincipal().getName();
57 User user =
58 User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult();
59 lazychatMessages =
60 Bookmark.find(
61 "select lm from BenkiUser u inner join u.visibleLazychatMessages lm left join fetch lm.owner where u.id = ?1",
62 Sort.by("date").descending(),
63 user.id)
64 .list();
65 }
66 return lazychatList.data("lazychatMessages", lazychatMessages);
67 }
68
69 @TemplateExtension
70 static String humanDateTime(TemporalAccessor x) {
71 return humanDateFormatter.format(x);
72 }
73
74 @TemplateExtension
75 static String htmlDateTime(TemporalAccessor x) {
76 return htmlDateFormatter.format(x);
77 }
78}