summaryrefslogtreecommitdiff
path: root/src/main/java/eu/mulk/mulkcms2/benki/bookmarks/BookmarkResource.java
blob: 4d08298d3a77a7f6cc2d2810a9d18782d387dc70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package eu.mulk.mulkcms2.benki.bookmarks;

import static jakarta.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static jakarta.ws.rs.core.MediaType.MULTIPART_FORM_DATA;
import static jakarta.ws.rs.core.MediaType.TEXT_HTML;
import static jakarta.ws.rs.core.MediaType.WILDCARD;

import eu.mulk.mulkcms2.benki.posts.Post;
import eu.mulk.mulkcms2.benki.posts.PostFilter;
import eu.mulk.mulkcms2.benki.posts.PostResource;
import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateInstance;
import io.quarkus.security.Authenticated;
import jakarta.json.JsonObject;
import jakarta.transaction.Transactional;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.ForbiddenException;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.NoSuchAlgorithmException;
import java.time.OffsetDateTime;
import java.util.Objects;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.jsoup.Jsoup;

@Path("/bookmarks")
public class BookmarkResource extends PostResource {

  @CheckedTemplate(basePath = "benki/bookmarks")
  static class Templates {

    public static native TemplateInstance newBookmark(
        @CheckForNull String uri, @CheckForNull String title, @CheckForNull String description);
  }

  public BookmarkResource() throws NoSuchAlgorithmException {
    super(PostFilter.BOOKMARKS_ONLY, "Bookmarks");
  }

  @POST
  @Transactional
  @Authenticated
  @Produces(WILDCARD)
  @Consumes({APPLICATION_FORM_URLENCODED, MULTIPART_FORM_DATA})
  public Response postBookmark(
      @FormParam("uri") @NotNull URI uri,
      @FormParam("title") @NotEmpty String title,
      @FormParam("description") @CheckForNull String description,
      @FormParam("visibility") @NotNull Post.Visibility visibility)
      throws URISyntaxException {

    var user = Objects.requireNonNull(getCurrentUser());

    var bookmark = new Bookmark();
    bookmark.uri = uri.toString();
    bookmark.tags = Set.of();
    bookmark.owner = user;
    bookmark.date = OffsetDateTime.now();

    bookmark.persist();

    bookmark.setTitle(title);
    bookmark.setDescription(description);

    assignPostTargets(visibility, user, bookmark);

    return Response.seeOther(new URI("/bookmarks")).build();
  }

  @POST
  @Transactional
  @Authenticated
  @Produces(WILDCARD)
  @Consumes({APPLICATION_FORM_URLENCODED, MULTIPART_FORM_DATA})
  @Path("{id}/edit")
  public Response patchMessage(
      @PathParam("id") int id,
      @FormParam("uri") @NotNull URI uri,
      @FormParam("title") @NotEmpty String title,
      @FormParam("description") @CheckForNull String description,
      @FormParam("visibility") Post.Visibility visibility)
      throws URISyntaxException {

    var user = Objects.requireNonNull(getCurrentUser());

    var bookmark = getSession().byId(Bookmark.class).load(id);

    if (bookmark == null) {
      throw new NotFoundException();
    }

    if (bookmark.owner == null || !Objects.equals(bookmark.owner.id, user.id)) {
      throw new ForbiddenException();
    }

    bookmark.uri = uri.toString();
    bookmark.tags = Set.of();
    bookmark.setTitle(title);
    bookmark.setDescription(description);
    bookmark.owner = user;

    assignPostTargets(visibility, user, bookmark);

    return Response.seeOther(new URI("/bookmarks")).build();
  }

  @GET
  @Authenticated
  @Path("new")
  @Produces(TEXT_HTML)
  public TemplateInstance getNewBookmarkForm(
      @QueryParam("uri") @CheckForNull String uri,
      @QueryParam("title") @CheckForNull String title,
      @QueryParam("description") @CheckForNull String description) {
    return Templates.newBookmark(uri, title, description);
  }

  @GET
  @Path("page-info")
  @Authenticated
  @Produces(APPLICATION_JSON)
  public JsonObject getPageInfo(@QueryParam("uri") URI uri) throws IOException {
    var document = Jsoup.connect(uri.toString()).get();
    return jsonProvider.createObjectBuilder().add("title", document.title()).build();
  }
}