diff --git a/src/main/java/org/teacon/chahoutan/entity/Post.java b/src/main/java/org/teacon/chahoutan/entity/Post.java index 2548715..07d1d95 100644 --- a/src/main/java/org/teacon/chahoutan/entity/Post.java +++ b/src/main/java/org/teacon/chahoutan/entity/Post.java @@ -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; @@ -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; diff --git a/src/main/java/org/teacon/chahoutan/entity/Revision.java b/src/main/java/org/teacon/chahoutan/entity/Revision.java index fba981e..44e7213 100644 --- a/src/main/java/org/teacon/chahoutan/entity/Revision.java +++ b/src/main/java/org/teacon/chahoutan/entity/Revision.java @@ -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 getAnchors() { return this.anchors.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(Map.Entry::getKey).toList(); @@ -126,8 +131,10 @@ public Map getFootnoteUrls() { var footnoteCount = this.footnotes.size(); var map = new LinkedHashMap(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); } diff --git a/src/main/java/org/teacon/chahoutan/network/PostResponse.java b/src/main/java/org/teacon/chahoutan/network/PostResponse.java index b4ace94..41a0dfa 100644 --- a/src/main/java/org/teacon/chahoutan/network/PostResponse.java +++ b/src/main/java/org/teacon/chahoutan/network/PostResponse.java @@ -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()); } @@ -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()); } }