Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 1 | package eu.mulk.mulkcms2.benki.bookmarks; |
| 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.users.User; |
| 7 | import io.quarkus.panache.common.Sort; |
| 8 | import io.quarkus.qute.Template; |
| 9 | import io.quarkus.qute.TemplateExtension; |
| 10 | import io.quarkus.qute.TemplateInstance; |
| 11 | import io.quarkus.qute.api.ResourcePath; |
| 12 | import io.quarkus.security.identity.SecurityIdentity; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame^] | 13 | import java.net.URI; |
| 14 | import java.net.URISyntaxException; |
| 15 | import java.time.OffsetDateTime; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 16 | import java.time.format.DateTimeFormatter; |
| 17 | import java.time.format.FormatStyle; |
| 18 | import java.time.temporal.TemporalAccessor; |
| 19 | import java.util.List; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame^] | 20 | import java.util.Set; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 21 | import javax.inject.Inject; |
| 22 | import javax.json.spi.JsonProvider; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame^] | 23 | import javax.transaction.Transactional; |
| 24 | import javax.validation.constraints.NotEmpty; |
| 25 | import javax.validation.constraints.NotNull; |
| 26 | import javax.validation.constraints.Pattern; |
| 27 | import javax.ws.rs.BadRequestException; |
| 28 | import javax.ws.rs.FormParam; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 29 | import javax.ws.rs.GET; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame^] | 30 | import javax.ws.rs.POST; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.Produces; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame^] | 33 | import javax.ws.rs.core.Response; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 34 | import org.jboss.logging.Logger; |
| 35 | |
| 36 | @Path("/bookmarks") |
| 37 | public class BookmarkResource { |
| 38 | |
| 39 | private static Logger log = Logger.getLogger(BookmarkResource.class); |
| 40 | |
| 41 | private static DateTimeFormatter htmlDateFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; |
| 42 | |
| 43 | private static DateTimeFormatter humanDateFormatter = |
| 44 | DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT); |
| 45 | |
| 46 | private static JsonProvider jsonProvider = JsonProvider.provider(); |
| 47 | |
| 48 | @ResourcePath("benki/bookmarks/bookmarkList.html") |
| 49 | @Inject |
| 50 | Template bookmarkList; |
| 51 | |
| 52 | @Inject SecurityIdentity identity; |
| 53 | |
| 54 | @GET |
| 55 | @Produces(TEXT_HTML) |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame^] | 56 | public TemplateInstance getIndex() { |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 57 | List<Bookmark> bookmarks; |
| 58 | if (identity.isAnonymous()) { |
| 59 | Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult(); |
| 60 | bookmarks = |
| 61 | Bookmark.find( |
| 62 | "select bm from Bookmark bm join bm.targets target left join fetch bm.owner where target = ?1", |
| 63 | Sort.by("date").descending(), |
| 64 | world) |
| 65 | .list(); |
| 66 | } else { |
| 67 | var userName = identity.getPrincipal().getName(); |
| 68 | User user = |
| 69 | User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult(); |
| 70 | bookmarks = |
| 71 | Bookmark.find( |
| 72 | "select bm from BenkiUser u inner join u.visibleBookmarks bm left join fetch bm.owner where u.id = ?1", |
| 73 | Sort.by("date").descending(), |
| 74 | user.id) |
| 75 | .list(); |
| 76 | } |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame^] | 77 | return bookmarkList |
| 78 | .data("bookmarks", bookmarks) |
| 79 | .data("authenticated", !identity.isAnonymous()); |
| 80 | } |
| 81 | |
| 82 | @POST |
| 83 | @Transactional |
| 84 | public Response postBookmark( |
| 85 | @FormParam("uri") URI uri, |
| 86 | @FormParam("title") @NotEmpty String title, |
| 87 | @FormParam("description") String description, |
| 88 | @FormParam("visibility") @NotNull @Pattern(regexp = "public|semiprivate|private") String visibility) |
| 89 | throws URISyntaxException { |
| 90 | |
| 91 | var userName = identity.getPrincipal().getName(); |
| 92 | User user = |
| 93 | User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult(); |
| 94 | |
| 95 | var bookmark = new Bookmark(); |
| 96 | bookmark.uri = uri.toString(); |
| 97 | bookmark.title = title; |
| 98 | bookmark.tags = Set.of(); |
| 99 | bookmark.description = description != null ? description : ""; |
| 100 | bookmark.owner = user; |
| 101 | bookmark.date = OffsetDateTime.now(); |
| 102 | |
| 103 | if (visibility.equals("public")) { |
| 104 | Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult(); |
| 105 | bookmark.targets = Set.of(world); |
| 106 | } else if (visibility.equals("semiprivate")) { |
| 107 | bookmark.targets = Set.copyOf(user.defaultTargets); |
| 108 | } else if (!visibility.equals("private")) { |
| 109 | throw new BadRequestException(String.format("invalid visibility ā%sā", visibility)); |
| 110 | } |
| 111 | |
| 112 | bookmark.persistAndFlush(); |
| 113 | |
| 114 | return Response.seeOther(new URI("/bookmarks")).build(); |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @TemplateExtension |
| 118 | static String humanDateTime(TemporalAccessor x) { |
| 119 | return humanDateFormatter.format(x); |
| 120 | } |
| 121 | |
| 122 | @TemplateExtension |
| 123 | static String htmlDateTime(TemporalAccessor x) { |
| 124 | return htmlDateFormatter.format(x); |
| 125 | } |
| 126 | } |