Matthias Andreas Benkard | 94b5e7b | 2020-02-09 20:02:20 +0100 | [diff] [blame^] | 1 | package eu.mulk.mulkcms2.benki.lazychat; |
| 2 | |
| 3 | import static javax.ws.rs.core.MediaType.TEXT_HTML; |
| 4 | |
| 5 | import eu.mulk.mulkcms2.benki.accesscontrol.Role; |
| 6 | import eu.mulk.mulkcms2.benki.bookmarks.Bookmark; |
| 7 | import eu.mulk.mulkcms2.benki.users.User; |
| 8 | import io.quarkus.panache.common.Sort; |
| 9 | import io.quarkus.qute.Template; |
| 10 | import io.quarkus.qute.TemplateExtension; |
| 11 | import io.quarkus.qute.TemplateInstance; |
| 12 | import io.quarkus.qute.api.ResourcePath; |
| 13 | import io.quarkus.security.identity.SecurityIdentity; |
| 14 | import java.time.format.DateTimeFormatter; |
| 15 | import java.time.format.FormatStyle; |
| 16 | import java.time.temporal.TemporalAccessor; |
| 17 | import java.util.List; |
| 18 | import javax.inject.Inject; |
| 19 | import javax.json.spi.JsonProvider; |
| 20 | import javax.ws.rs.GET; |
| 21 | import javax.ws.rs.Path; |
| 22 | import javax.ws.rs.Produces; |
| 23 | import org.jboss.logging.Logger; |
| 24 | |
| 25 | @Path("/lazychat") |
| 26 | public 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 | } |