Skip to content

Commit

Permalink
Make post footnote urls absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Dec 12, 2023
1 parent 71012a9 commit 16e8559
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/teacon/chahoutan/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.teacon.chahoutan.ChahoutanConfig;

import javax.persistence.*;
import java.net.URI;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.HashSet;
Expand Down Expand Up @@ -42,6 +43,11 @@ public Integer getId()
return this.id;
}

public URI getBackendUrl() {
var urlPrefix = URI.create(ChahoutanConfig.BACKEND_URL_PREFIX);
return urlPrefix.resolve("v1/posts/" + this.id);
}

public Revision getRevision()
{
return this.revision;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/teacon/chahoutan/entity/Revision.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public String getText()
return this.text;
}

public URI getBackendUrl() {
var urlPrefix = URI.create(ChahoutanConfig.BACKEND_URL_PREFIX);
return urlPrefix.resolve("v1/posts/" + this.id);
}

public List<String> getAnchors()
{
return this.anchors.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(Map.Entry::getKey).toList();
Expand Down Expand Up @@ -126,8 +131,10 @@ public Map<String, String> getFootnoteUrls()
{
var footnoteCount = this.footnotes.size();
var map = new LinkedHashMap<String, String>(footnoteCount);
var urlPrefix = URI.create(ChahoutanConfig.FRONTEND_URL_PREFIX);
for (int i = 0; i < footnoteCount; ++i) {
map.put(String.format("[%d]", i + 1), "#footnote-" + i);
var url = urlPrefix.resolve(this.post.getId() + "#footnote-" + i);
map.put(String.format("[%d]", i + 1), url.toASCIIString());
}
return Map.copyOf(map);
}
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/org/teacon/chahoutan/network/PostResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ public static PostResponse from(Post post)
{
var revision = post.getRevision();
var revisionName = revision.getTitle();
var urlPrefix = URI.create(ChahoutanConfig.BACKEND_URL_PREFIX);
var url = urlPrefix.resolve("v1/posts/" + post.getId());
var revisionUrl = urlPrefix.resolve("v1/posts/" + revision.getId());
var type = post.getId() <= Post.getLastPublicPostId(null) ? "post" : "draft";
var editors = post.getEditors().stream().sorted().toList();
var images = revision.getImages().stream().map(ImageResponse::from).toList();
return new PostResponse(post.getId(), url, type,
revisionName, revision.getText(), revision.getId(),
revisionUrl, editors, revision.getAnchors(), revision.getAnchorUrls(),
return new PostResponse(post.getId(), post.getBackendUrl(),
type, revisionName, revision.getText(), revision.getId(),
revision.getBackendUrl(), editors, revision.getAnchors(), revision.getAnchorUrls(),
images, revision.getFootnotes(), revision.getFootnoteUrls(), post.getPublishTime());
}

Expand All @@ -49,15 +46,13 @@ public static PostResponse from(Revision revision)
var post = revision.getPost();
var revisionName = revision.getTitle();
var isPost = post.getRevision() != null && post.getRevision().getId().equals(revision.getId());
var urlPrefix = URI.create(ChahoutanConfig.BACKEND_URL_PREFIX);
var url = isPost ? urlPrefix.resolve("v1/posts/" + post.getId()) : null;
var revisionUrl = urlPrefix.resolve("v1/posts/" + revision.getId());
var type = isPost ? post.getId() <= Post.getLastPublicPostId(null) ? "post" : "draft" : "revision";
var editors = isPost ? post.getEditors().stream().sorted().toList() : null;
var images = revision.getImages().stream().map(ImageResponse::from).toList();
return new PostResponse(post.getId(), url, type,
revisionName, revision.getText(), revision.getId(),
revisionUrl, editors, revision.getAnchors(), revision.getAnchorUrls(),
return new PostResponse(post.getId(),
isPost ? post.getBackendUrl() : null,
type, revisionName, revision.getText(), revision.getId(),
revision.getBackendUrl(), editors, revision.getAnchors(), revision.getAnchorUrls(),
images, revision.getFootnotes(), revision.getFootnoteUrls(), post.getPublishTime());
}
}

0 comments on commit 16e8559

Please sign in to comment.