Skip to content

Commit

Permalink
fix: let content link return the relative position of fact list
Browse files Browse the repository at this point in the history
  • Loading branch information
YukinaMochizuki committed Dec 3, 2024
1 parent 09e8d02 commit 510cd4e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import tw.commonground.backend.service.fact.entity.FactEntity;
import tw.commonground.backend.service.issue.entity.IssueEntity;
import tw.commonground.backend.service.issue.entity.SimpleIssueEntity;
import tw.commonground.backend.shared.content.ContentContainFact;
import tw.commonground.backend.shared.content.ContentContainFactParser;

import java.util.List;

Expand All @@ -13,13 +15,17 @@ private IssueMapper() {
}

public static IssueResponse toResponse(IssueEntity entity, List<FactEntity> factEntities) {

ContentContainFact insight = ContentContainFactParser.separateContentAndFacts(entity.getInsight(),
factEntities.stream().map(FactEntity::getId).toList());

return IssueResponse.builder()
.id(entity.getId().toString())
.createAt(entity.getCreateAt())
.updatedAt(entity.getUpdatedAt())
.title(entity.getTitle())
.description(entity.getDescription())
.insight(entity.getInsight())
.insight(insight.getText())
.authorId(entity.getAuthorId())
.authorName(entity.getAuthorName())
.authorAvatar(entity.getAuthorAvatar())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import tw.commonground.backend.service.viewpoint.entity.Reaction;
import tw.commonground.backend.service.viewpoint.entity.ViewpointEntity;
import tw.commonground.backend.service.viewpoint.entity.ViewpointReactionEntity;
import tw.commonground.backend.shared.content.ContentContainFact;
import tw.commonground.backend.shared.content.ContentContainFactParser;

import java.util.List;

Expand All @@ -26,12 +28,15 @@ public static ViewpointReactionResponse toReactionResponse(Reaction reaction) {
public static ViewpointResponse toResponse(ViewpointEntity viewpointEntity, Reaction reaction,
List<FactEntity> factEntities) {

ContentContainFact content = ContentContainFactParser.separateContentAndFacts(viewpointEntity.getContent(),
factEntities.stream().map(FactEntity::getId).toList());

return ViewpointResponse.builder()
.id(viewpointEntity.getId())
.createdAt(viewpointEntity.getCreatedAt())
.updatedAt(viewpointEntity.getUpdatedAt())
.title(viewpointEntity.getTitle())
.content(viewpointEntity.getContent())
.content(content.getText())
.authorId(viewpointEntity.getAuthorId())
.authorName(viewpointEntity.getAuthorName())
.authorAvatar(viewpointEntity.getAuthorAvatar())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,35 @@ public static ContentContainFact separateContentAndFacts(String text) {

return contentContainFact;
}

public static ContentContainFact separateContentAndFacts(String text, List<UUID> existingFacts) {
StringBuilder replacedText = new StringBuilder();

Matcher matcher = CONTENT_LINK_PATTERN.matcher(text);
List<UUID> uuids = new ArrayList<>(existingFacts);
while (matcher.find()) {
String linkText = matcher.group(1);
String linkPositions = matcher.group(2);

List<Integer> positions = new ArrayList<>();
for (String uid : linkPositions.split(",")) {
uid = uid.trim();
UUID uuid = UUID.fromString(uid);
positions.add(uuids.indexOf(uuid));
}

String newLink = "[" + linkText + "]("
+ String.join(",", positions.stream().map(Object::toString).toList()) + ")";
matcher.appendReplacement(replacedText, newLink);
}

matcher.appendTail(replacedText);

ContentContainFact contentContainFact = new ContentContainFact();
contentContainFact.setText(replacedText.toString());
contentContainFact.setFacts(uuids);

return contentContainFact;
}
}

0 comments on commit 510cd4e

Please sign in to comment.