feat: add tag timeline and summary sections

This commit is contained in:
Tim
2025-07-14 13:41:33 +08:00
parent a043e6fab9
commit c93452f1e3
7 changed files with 174 additions and 8 deletions

View File

@@ -30,7 +30,13 @@ public class TagController {
approved = false;
}
}
Tag tag = tagService.createTag(req.getName(), req.getDescription(), req.getIcon(), req.getSmallIcon(), approved);
Tag tag = tagService.createTag(
req.getName(),
req.getDescription(),
req.getIcon(),
req.getSmallIcon(),
approved,
auth != null ? auth.getName() : null);
long count = postService.countPostsByTag(tag.getId());
return toDto(tag, count);
}

View File

@@ -22,6 +22,7 @@ public class UserController {
private final PostService postService;
private final CommentService commentService;
private final ReactionService reactionService;
private final TagService tagService;
private final SubscriptionService subscriptionService;
private final PostReadService postReadService;
private final UserVisitService userVisitService;
@@ -38,6 +39,9 @@ public class UserController {
@Value("${app.user.replies-limit:50}")
private int defaultRepliesLimit;
@Value("${app.user.tags-limit:50}")
private int defaultTagsLimit;
@Value("${app.snippet-length:50}")
private int snippetLength;
@@ -122,6 +126,48 @@ public class UserController {
.collect(java.util.stream.Collectors.toList());
}
@GetMapping("/{identifier}/hot-tags")
public java.util.List<TagInfoDto> hotTags(@PathVariable("identifier") String identifier,
@RequestParam(value = "limit", required = false) Integer limit) {
int l = limit != null ? limit : 10;
User user = userService.findByIdentifier(identifier).orElseThrow();
return tagService.getTagsByUser(user.getUsername()).stream()
.map(t -> {
TagInfoDto dto = new TagInfoDto();
dto.setId(t.getId());
dto.setName(t.getName());
dto.setDescription(t.getDescription());
dto.setIcon(t.getIcon());
dto.setSmallIcon(t.getSmallIcon());
dto.setCreatedAt(t.getCreatedAt());
dto.setCount(postService.countPostsByTag(t.getId()));
return dto;
})
.sorted((a, b) -> Long.compare(b.getCount(), a.getCount()))
.limit(l)
.collect(java.util.stream.Collectors.toList());
}
@GetMapping("/{identifier}/tags")
public java.util.List<TagInfoDto> userTags(@PathVariable("identifier") String identifier,
@RequestParam(value = "limit", required = false) Integer limit) {
int l = limit != null ? limit : defaultTagsLimit;
User user = userService.findByIdentifier(identifier).orElseThrow();
return tagService.getRecentTagsByUser(user.getUsername(), l).stream()
.map(t -> {
TagInfoDto dto = new TagInfoDto();
dto.setId(t.getId());
dto.setName(t.getName());
dto.setDescription(t.getDescription());
dto.setIcon(t.getIcon());
dto.setSmallIcon(t.getSmallIcon());
dto.setCreatedAt(t.getCreatedAt());
dto.setCount(postService.countPostsByTag(t.getId()));
return dto;
})
.collect(java.util.stream.Collectors.toList());
}
@GetMapping("/{identifier}/following")
public java.util.List<UserDto> following(@PathVariable("identifier") String identifier) {
User user = userService.findByIdentifier(identifier).orElseThrow();
@@ -262,6 +308,17 @@ public class UserController {
private ParentCommentDto parentComment;
}
@Data
private static class TagInfoDto {
private Long id;
private String name;
private String description;
private String icon;
private String smallIcon;
private java.time.LocalDateTime createdAt;
private Long count;
}
@Data
private static class ParentCommentDto {
private Long id;

View File

@@ -4,6 +4,10 @@ import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import java.time.LocalDateTime;
@Entity
@Getter
@@ -29,4 +33,13 @@ public class Tag {
@Column(nullable = false)
private boolean approved = true;
@CreationTimestamp
@Column(nullable = false, updatable = false,
columnDefinition = "DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6)")
private LocalDateTime createdAt;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "creator_id")
private User creator;
}

View File

@@ -1,7 +1,9 @@
package com.openisle.repository;
import com.openisle.model.Tag;
import com.openisle.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.domain.Pageable;
import java.util.List;
@@ -10,4 +12,7 @@ public interface TagRepository extends JpaRepository<Tag, Long> {
List<Tag> findByApproved(boolean approved);
List<Tag> findByApprovedTrue();
List<Tag> findByNameContainingIgnoreCaseAndApprovedTrue(String keyword);
List<Tag> findByCreatorOrderByCreatedAtDesc(User creator, Pageable pageable);
List<Tag> findByCreator(User creator);
}

View File

@@ -1,7 +1,11 @@
package com.openisle.service;
import com.openisle.model.Tag;
import com.openisle.model.User;
import com.openisle.repository.TagRepository;
import com.openisle.repository.UserRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -12,8 +16,9 @@ import java.util.List;
public class TagService {
private final TagRepository tagRepository;
private final TagValidator tagValidator;
private final UserRepository userRepository;
public Tag createTag(String name, String description, String icon, String smallIcon, boolean approved) {
public Tag createTag(String name, String description, String icon, String smallIcon, boolean approved, String creatorUsername) {
tagValidator.validate(name);
Tag tag = new Tag();
tag.setName(name);
@@ -21,11 +26,20 @@ public class TagService {
tag.setIcon(icon);
tag.setSmallIcon(smallIcon);
tag.setApproved(approved);
if (creatorUsername != null) {
User creator = userRepository.findByUsername(creatorUsername)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
tag.setCreator(creator);
}
return tagRepository.save(tag);
}
public Tag createTag(String name, String description, String icon, String smallIcon, boolean approved) {
return createTag(name, description, icon, smallIcon, approved, null);
}
public Tag createTag(String name, String description, String icon, String smallIcon) {
return createTag(name, description, icon, smallIcon, true);
return createTag(name, description, icon, smallIcon, true, null);
}
public Tag updateTag(Long id, String name, String description, String icon, String smallIcon) {
@@ -77,4 +91,17 @@ public class TagService {
}
return tagRepository.findByNameContainingIgnoreCaseAndApprovedTrue(keyword);
}
public List<Tag> getRecentTagsByUser(String username, int limit) {
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
Pageable pageable = PageRequest.of(0, limit);
return tagRepository.findByCreatorOrderByCreatedAtDesc(user, pageable);
}
public List<Tag> getTagsByUser(String username) {
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
return tagRepository.findByCreator(user);
}
}