mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-21 14:30:59 +08:00
Merge pull request #128 from nagisa77/codex/update-usercontroller-to-include-snippets-and-comments
Add snippet preview settings and enrich comment info
This commit is contained in:
@@ -36,6 +36,9 @@ public class UserController {
|
||||
@Value("${app.user.replies-limit:50}")
|
||||
private int defaultRepliesLimit;
|
||||
|
||||
@Value("${app.snippet-length:50}")
|
||||
private int snippetLength;
|
||||
|
||||
@GetMapping("/me")
|
||||
public ResponseEntity<UserDto> me(Authentication auth) {
|
||||
User user = userService.findByUsername(auth.getName()).orElseThrow();
|
||||
@@ -172,6 +175,12 @@ public class UserController {
|
||||
PostMetaDto dto = new PostMetaDto();
|
||||
dto.setId(post.getId());
|
||||
dto.setTitle(post.getTitle());
|
||||
if (snippetLength >= 0) {
|
||||
String c = post.getContent();
|
||||
dto.setSnippet(c.length() > snippetLength ? c.substring(0, snippetLength) : c);
|
||||
} else {
|
||||
dto.setSnippet(post.getContent());
|
||||
}
|
||||
dto.setCreatedAt(post.getCreatedAt());
|
||||
dto.setCategory(post.getCategory().getName());
|
||||
dto.setViews(post.getViews());
|
||||
@@ -183,7 +192,14 @@ public class UserController {
|
||||
dto.setId(comment.getId());
|
||||
dto.setContent(comment.getContent());
|
||||
dto.setCreatedAt(comment.getCreatedAt());
|
||||
dto.setPostId(comment.getPost().getId());
|
||||
dto.setPost(toMetaDto(comment.getPost()));
|
||||
if (comment.getParent() != null) {
|
||||
ParentCommentDto pc = new ParentCommentDto();
|
||||
pc.setId(comment.getParent().getId());
|
||||
pc.setAuthor(comment.getParent().getAuthor().getUsername());
|
||||
pc.setContent(comment.getParent().getContent());
|
||||
dto.setParentComment(pc);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
@@ -206,6 +222,7 @@ public class UserController {
|
||||
private static class PostMetaDto {
|
||||
private Long id;
|
||||
private String title;
|
||||
private String snippet;
|
||||
private java.time.LocalDateTime createdAt;
|
||||
private String category;
|
||||
private long views;
|
||||
@@ -216,7 +233,15 @@ public class UserController {
|
||||
private Long id;
|
||||
private String content;
|
||||
private java.time.LocalDateTime createdAt;
|
||||
private Long postId;
|
||||
private PostMetaDto post;
|
||||
private ParentCommentDto parentComment;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class ParentCommentDto {
|
||||
private Long id;
|
||||
private String author;
|
||||
private String content;
|
||||
}
|
||||
|
||||
@Data
|
||||
|
||||
@@ -22,6 +22,9 @@ public class SearchService {
|
||||
private final PostRepository postRepository;
|
||||
private final CommentRepository commentRepository;
|
||||
|
||||
@org.springframework.beans.factory.annotation.Value("${app.snippet-length:50}")
|
||||
private int snippetLength;
|
||||
|
||||
public List<User> searchUsers(String keyword) {
|
||||
return userRepository.findByUsernameContainingIgnoreCase(keyword);
|
||||
}
|
||||
@@ -104,18 +107,29 @@ public class SearchService {
|
||||
|
||||
private String extractSnippet(String content, String keyword, boolean fromStart) {
|
||||
if (content == null) return "";
|
||||
int limit = snippetLength;
|
||||
if (fromStart) {
|
||||
return content.length() > 50 ? content.substring(0, 50) : content;
|
||||
if (limit < 0) {
|
||||
return content;
|
||||
}
|
||||
return content.length() > limit ? content.substring(0, limit) : content;
|
||||
}
|
||||
String lower = content.toLowerCase();
|
||||
String kw = keyword.toLowerCase();
|
||||
int idx = lower.indexOf(kw);
|
||||
if (idx == -1) {
|
||||
return content.length() > 50 ? content.substring(0, 50) : content;
|
||||
if (limit < 0) {
|
||||
return content;
|
||||
}
|
||||
return content.length() > limit ? content.substring(0, limit) : content;
|
||||
}
|
||||
int start = Math.max(0, idx - 20);
|
||||
int end = Math.min(content.length(), idx + kw.length() + 20);
|
||||
return content.substring(start, end);
|
||||
String snippet = content.substring(start, end);
|
||||
if (limit >= 0 && snippet.length() > limit) {
|
||||
snippet = snippet.substring(0, limit);
|
||||
}
|
||||
return snippet;
|
||||
}
|
||||
|
||||
public record SearchResult(String type, Long id, String text, String subText, String extra, Long postId) {}
|
||||
|
||||
@@ -20,6 +20,8 @@ app.upload.max-size=${UPLOAD_MAX_SIZE:5242880}
|
||||
# Default list size for user posts and replies
|
||||
app.user.posts-limit=${USER_POSTS_LIMIT:10}
|
||||
app.user.replies-limit=${USER_REPLIES_LIMIT:50}
|
||||
# Length of extracted snippets for posts and search (-1 to disable truncation)
|
||||
app.snippet-length=${SNIPPET_LENGTH:50}
|
||||
|
||||
# Captcha configuration
|
||||
app.captcha.enabled=${CAPTCHA_ENABLED:false}
|
||||
|
||||
Reference in New Issue
Block a user