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