Matthias Andreas Benkard | 94b5e7b | 2020-02-09 20:02:20 +0100 | [diff] [blame] | 1 | package eu.mulk.mulkcms2.benki.lazychat; |
| 2 | |
Matthias Andreas Benkard | e3bc3ee | 2023-08-06 16:21:11 +0200 | [diff] [blame] | 3 | import static jakarta.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED; |
| 4 | import static jakarta.ws.rs.core.MediaType.MULTIPART_FORM_DATA; |
| 5 | import static jakarta.ws.rs.core.MediaType.WILDCARD; |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame] | 6 | |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 7 | import eu.mulk.mulkcms2.benki.posts.Post; |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 8 | import eu.mulk.mulkcms2.benki.posts.PostFilter; |
| 9 | import eu.mulk.mulkcms2.benki.posts.PostResource; |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 10 | import io.quarkus.security.Authenticated; |
Matthias Andreas Benkard | 37dce40 | 2023-09-06 13:51:43 +0200 | [diff] [blame^] | 11 | import io.smallrye.common.annotation.Blocking; |
Matthias Andreas Benkard | e3bc3ee | 2023-08-06 16:21:11 +0200 | [diff] [blame] | 12 | import jakarta.transaction.Transactional; |
| 13 | import jakarta.validation.constraints.NotNull; |
| 14 | import jakarta.ws.rs.Consumes; |
| 15 | import jakarta.ws.rs.ForbiddenException; |
| 16 | import jakarta.ws.rs.FormParam; |
| 17 | import jakarta.ws.rs.NotFoundException; |
| 18 | import jakarta.ws.rs.POST; |
| 19 | import jakarta.ws.rs.Path; |
| 20 | import jakarta.ws.rs.PathParam; |
| 21 | import jakarta.ws.rs.Produces; |
| 22 | import jakarta.ws.rs.core.Response; |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 23 | import java.net.URI; |
| 24 | import java.net.URISyntaxException; |
Matthias Andreas Benkard | cf0fe88 | 2020-04-19 18:33:37 +0200 | [diff] [blame] | 25 | import java.security.NoSuchAlgorithmException; |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 26 | import java.time.OffsetDateTime; |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 27 | import java.util.Objects; |
Matthias Andreas Benkard | 94b5e7b | 2020-02-09 20:02:20 +0100 | [diff] [blame] | 28 | |
| 29 | @Path("/lazychat") |
Matthias Andreas Benkard | 37dce40 | 2023-09-06 13:51:43 +0200 | [diff] [blame^] | 30 | @Blocking |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 31 | public class LazychatResource extends PostResource { |
Matthias Andreas Benkard | 94b5e7b | 2020-02-09 20:02:20 +0100 | [diff] [blame] | 32 | |
Matthias Andreas Benkard | cf0fe88 | 2020-04-19 18:33:37 +0200 | [diff] [blame] | 33 | public LazychatResource() throws NoSuchAlgorithmException { |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 34 | super(PostFilter.LAZYCHAT_MESSAGES_ONLY, "Lazy Chat"); |
Matthias Andreas Benkard | 94b5e7b | 2020-02-09 20:02:20 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 37 | @POST |
| 38 | @Transactional |
| 39 | @Authenticated |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame] | 40 | @Produces(WILDCARD) |
| 41 | @Consumes({APPLICATION_FORM_URLENCODED, MULTIPART_FORM_DATA}) |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 42 | public Response postMessage( |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 43 | @FormParam("text") @NotNull String text, |
| 44 | @FormParam("visibility") @NotNull Post.Visibility visibility) |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 45 | throws URISyntaxException { |
| 46 | |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 47 | var user = Objects.requireNonNull(getCurrentUser()); |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 48 | |
| 49 | var message = new LazychatMessage(); |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 50 | message.owner = user; |
| 51 | message.date = OffsetDateTime.now(); |
| 52 | |
Matthias Andreas Benkard | b4a5bfd | 2023-08-06 21:57:45 +0200 | [diff] [blame] | 53 | message.persist(); |
| 54 | message.setContent(text); |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 55 | |
Matthias Andreas Benkard | b4a5bfd | 2023-08-06 21:57:45 +0200 | [diff] [blame] | 56 | assignPostTargets(visibility, user, message); |
Matthias Andreas Benkard | 12c7cac | 2020-03-26 07:19:45 +0100 | [diff] [blame] | 57 | |
| 58 | return Response.seeOther(new URI("/lazychat")).build(); |
| 59 | } |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 60 | |
| 61 | @POST |
| 62 | @Transactional |
| 63 | @Authenticated |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame] | 64 | @Produces(WILDCARD) |
| 65 | @Consumes({APPLICATION_FORM_URLENCODED, MULTIPART_FORM_DATA}) |
Matthias Andreas Benkard | b608127 | 2020-04-18 15:48:04 +0200 | [diff] [blame] | 66 | @Path("{id}/edit") |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 67 | public Response patchMessage( |
| 68 | @PathParam("id") int id, |
| 69 | @FormParam("text") String text, |
| 70 | @FormParam("visibility") Post.Visibility visibility) |
| 71 | throws URISyntaxException { |
| 72 | |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 73 | var user = Objects.requireNonNull(getCurrentUser()); |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 74 | |
| 75 | var message = getSession().byId(LazychatMessage.class).load(id); |
| 76 | |
| 77 | if (message == null) { |
| 78 | throw new NotFoundException(); |
| 79 | } |
| 80 | |
Matthias Andreas Benkard | cf0fe88 | 2020-04-19 18:33:37 +0200 | [diff] [blame] | 81 | if (message.owner == null || !Objects.equals(message.owner.id, user.id)) { |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 82 | throw new ForbiddenException(); |
| 83 | } |
| 84 | |
Matthias Andreas Benkard | d5498fc | 2020-08-23 21:51:00 +0200 | [diff] [blame] | 85 | message.setContent(text); |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 86 | |
| 87 | assignPostTargets(visibility, user, message); |
| 88 | |
| 89 | return Response.seeOther(new URI("/lazychat")).build(); |
| 90 | } |
Matthias Andreas Benkard | 94b5e7b | 2020-02-09 20:02:20 +0100 | [diff] [blame] | 91 | } |