Merge branch 'main' of github.com:nagisa77/OpenIsle

This commit is contained in:
Tim
2025-08-04 21:16:59 +08:00
67 changed files with 1215 additions and 1154 deletions

View File

@@ -1,11 +1,12 @@
package com.openisle.controller;
import com.openisle.dto.MilkTeaInfoDto;
import com.openisle.dto.MilkTeaRedeemRequest;
import com.openisle.model.Activity;
import com.openisle.model.ActivityType;
import com.openisle.model.User;
import com.openisle.service.ActivityService;
import com.openisle.service.UserService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
@@ -25,20 +26,20 @@ public class ActivityController {
}
@GetMapping("/milk-tea")
public MilkTeaInfo milkTea() {
public MilkTeaInfoDto milkTea() {
Activity a = activityService.getByType(ActivityType.MILK_TEA);
long count = activityService.countParticipants(a);
if (!a.isEnded() && count >= 50) {
activityService.end(a);
}
MilkTeaInfo info = new MilkTeaInfo();
MilkTeaInfoDto info = new MilkTeaInfoDto();
info.setRedeemCount(count);
info.setEnded(a.isEnded());
return info;
}
@PostMapping("/milk-tea/redeem")
public java.util.Map<String, String> redeemMilkTea(@RequestBody RedeemRequest req, Authentication auth) {
public java.util.Map<String, String> redeemMilkTea(@RequestBody MilkTeaRedeemRequest req, Authentication auth) {
User user = userService.findByIdentifier(auth.getName()).orElseThrow();
Activity a = activityService.getByType(ActivityType.MILK_TEA);
boolean first = activityService.redeem(a, user, req.getContact());
@@ -47,15 +48,4 @@ public class ActivityController {
}
return java.util.Map.of("message", "updated");
}
@Data
private static class MilkTeaInfo {
private long redeemCount;
private boolean ended;
}
@Data
private static class RedeemRequest {
private String contact;
}
}

View File

@@ -1,13 +1,10 @@
package com.openisle.controller;
import com.openisle.model.PasswordStrength;
import com.openisle.model.PublishMode;
import com.openisle.dto.ConfigDto;
import com.openisle.service.AiUsageService;
import com.openisle.service.PasswordValidator;
import com.openisle.service.PostService;
import com.openisle.service.AiUsageService;
import com.openisle.service.RegisterModeService;
import com.openisle.model.RegisterMode;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
@@ -47,11 +44,4 @@ public class AdminConfigController {
return getConfig();
}
@Data
public static class ConfigDto {
private PublishMode publishMode;
private PasswordStrength passwordStrength;
private Integer aiFormatLimit;
private RegisterMode registerMode;
}
}

View File

@@ -1,12 +1,11 @@
package com.openisle.controller;
import com.openisle.model.Post;
import com.openisle.dto.PostSummaryDto;
import com.openisle.mapper.PostMapper;
import com.openisle.service.PostService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@@ -18,77 +17,32 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class AdminPostController {
private final PostService postService;
private final PostMapper postMapper;
@GetMapping("/pending")
public List<PostDto> pendingPosts() {
public List<PostSummaryDto> pendingPosts() {
return postService.listPendingPosts().stream()
.map(this::toDto)
.map(postMapper::toSummaryDto)
.collect(Collectors.toList());
}
@PostMapping("/{id}/approve")
public PostDto approve(@PathVariable Long id) {
return toDto(postService.approvePost(id));
public PostSummaryDto approve(@PathVariable Long id) {
return postMapper.toSummaryDto(postService.approvePost(id));
}
@PostMapping("/{id}/reject")
public PostDto reject(@PathVariable Long id) {
return toDto(postService.rejectPost(id));
public PostSummaryDto reject(@PathVariable Long id) {
return postMapper.toSummaryDto(postService.rejectPost(id));
}
@PostMapping("/{id}/pin")
public PostDto pin(@PathVariable Long id) {
return toDto(postService.pinPost(id));
public PostSummaryDto pin(@PathVariable Long id) {
return postMapper.toSummaryDto(postService.pinPost(id));
}
@PostMapping("/{id}/unpin")
public PostDto unpin(@PathVariable Long id) {
return toDto(postService.unpinPost(id));
}
private PostDto toDto(Post post) {
PostDto dto = new PostDto();
dto.setId(post.getId());
dto.setTitle(post.getTitle());
dto.setContent(post.getContent());
dto.setCreatedAt(post.getCreatedAt());
dto.setAuthor(post.getAuthor().getUsername());
dto.setCategory(toCategoryDto(post.getCategory()));
dto.setViews(post.getViews());
dto.setStatus(post.getStatus());
dto.setPinnedAt(post.getPinnedAt());
return dto;
}
private CategoryDto toCategoryDto(com.openisle.model.Category c) {
CategoryDto dto = new CategoryDto();
dto.setId(c.getId());
dto.setName(c.getName());
dto.setDescription(c.getDescription());
dto.setIcon(c.getIcon());
dto.setSmallIcon(c.getSmallIcon());
return dto;
}
@Data
private static class PostDto {
private Long id;
private String title;
private String content;
private LocalDateTime createdAt;
private String author;
private CategoryDto category;
private long views;
private com.openisle.model.PostStatus status;
private LocalDateTime pinnedAt;
}
@Data
private static class CategoryDto {
private Long id;
private String name;
private String description;
private String icon;
private String smallIcon;
public PostSummaryDto unpin(@PathVariable Long id) {
return postMapper.toSummaryDto(postService.unpinPost(id));
}
}

View File

@@ -1,9 +1,10 @@
package com.openisle.controller;
import com.openisle.dto.TagDto;
import com.openisle.mapper.TagMapper;
import com.openisle.model.Tag;
import com.openisle.service.TagService;
import com.openisle.service.PostService;
import lombok.Data;
import com.openisle.service.TagService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
@@ -16,11 +17,12 @@ import java.util.stream.Collectors;
public class AdminTagController {
private final TagService tagService;
private final PostService postService;
private final TagMapper tagMapper;
@GetMapping("/pending")
public List<TagDto> pendingTags() {
return tagService.listPendingTags().stream()
.map(t -> toDto(t, postService.countPostsByTag(t.getId())))
.map(t -> tagMapper.toDto(t, postService.countPostsByTag(t.getId())))
.collect(Collectors.toList());
}
@@ -28,27 +30,6 @@ public class AdminTagController {
public TagDto approve(@PathVariable Long id) {
Tag tag = tagService.approveTag(id);
long count = postService.countPostsByTag(tag.getId());
return toDto(tag, count);
}
private TagDto toDto(Tag tag, long count) {
TagDto dto = new TagDto();
dto.setId(tag.getId());
dto.setName(tag.getName());
dto.setDescription(tag.getDescription());
dto.setIcon(tag.getIcon());
dto.setSmallIcon(tag.getSmallIcon());
dto.setCount(count);
return dto;
}
@Data
private static class TagDto {
private Long id;
private String name;
private String description;
private String icon;
private String smallIcon;
private Long count;
return tagMapper.toDto(tag, count);
}
}

View File

@@ -1,24 +1,16 @@
package com.openisle.controller;
import com.openisle.model.User;
import com.openisle.service.EmailSender;
import com.openisle.service.JwtService;
import com.openisle.service.UserService;
import com.openisle.service.CaptchaService;
import com.openisle.service.GoogleAuthService;
import com.openisle.service.GithubAuthService;
import com.openisle.service.DiscordAuthService;
import com.openisle.service.TwitterAuthService;
import com.openisle.service.RegisterModeService;
import com.openisle.service.NotificationService;
import com.openisle.model.RegisterMode;
import com.openisle.repository.UserRepository;
import com.openisle.dto.*;
import com.openisle.exception.FieldException;
import lombok.Data;
import com.openisle.model.RegisterMode;
import com.openisle.model.User;
import com.openisle.repository.UserRepository;
import com.openisle.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import java.util.Map;
import java.util.Optional;
@@ -154,8 +146,8 @@ public class AuthController {
));
}
if (req.getReason() == null || req.getReason().length() <= 20) {
return ResponseEntity.badRequest().body(Map.of(
if (req.getReason() == null || req.getReason().trim().length() <= 20) {
return ResponseEntity.badRequest().body(Map.of(
"error", "Reason's length must longer than 20",
"reason_code", "INVALID_CREDENTIALS"
));
@@ -306,71 +298,5 @@ public class AuthController {
}
}
@Data
private static class RegisterRequest {
private String username;
private String email;
private String password;
private String captcha;
}
@Data
private static class LoginRequest {
private String username;
private String password;
private String captcha;
}
@Data
private static class GoogleLoginRequest {
private String idToken;
}
@Data
private static class GithubLoginRequest {
private String code;
private String redirectUri;
}
@Data
private static class DiscordLoginRequest {
private String code;
private String redirectUri;
}
@Data
private static class TwitterLoginRequest {
private String code;
private String redirectUri;
private String codeVerifier;
}
@Data
private static class VerifyRequest {
private String username;
private String code;
}
@Data
private static class MakeReasonRequest {
private String token;
private String reason;
}
@Data
private static class ForgotPasswordRequest {
private String email;
}
@Data
private static class VerifyForgotRequest {
private String email;
private String code;
}
@Data
private static class ResetPasswordRequest {
private String token;
private String password;
}
// DTO classes moved to com.openisle.dto package
}

View File

@@ -1,9 +1,13 @@
package com.openisle.controller;
import com.openisle.dto.CategoryDto;
import com.openisle.dto.CategoryRequest;
import com.openisle.dto.PostSummaryDto;
import com.openisle.mapper.CategoryMapper;
import com.openisle.mapper.PostMapper;
import com.openisle.model.Category;
import com.openisle.service.CategoryService;
import com.openisle.service.PostService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
@@ -16,19 +20,21 @@ import java.util.stream.Collectors;
public class CategoryController {
private final CategoryService categoryService;
private final PostService postService;
private final PostMapper postMapper;
private final CategoryMapper categoryMapper;
@PostMapping
public CategoryDto create(@RequestBody CategoryRequest req) {
Category c = categoryService.createCategory(req.getName(), req.getDescription(), req.getIcon(), req.getSmallIcon());
long count = postService.countPostsByCategory(c.getId());
return toDto(c, count);
return categoryMapper.toDto(c, count);
}
@PutMapping("/{id}")
public CategoryDto update(@PathVariable Long id, @RequestBody CategoryRequest req) {
Category c = categoryService.updateCategory(id, req.getName(), req.getDescription(), req.getIcon(), req.getSmallIcon());
long count = postService.countPostsByCategory(c.getId());
return toDto(c, count);
return categoryMapper.toDto(c, count);
}
@DeleteMapping("/{id}")
@@ -39,7 +45,7 @@ public class CategoryController {
@GetMapping
public List<CategoryDto> list() {
return categoryService.listCategories().stream()
.map(c -> toDto(c, postService.countPostsByCategory(c.getId())))
.map(c -> categoryMapper.toDto(c, postService.countPostsByCategory(c.getId())))
.sorted((a, b) -> Long.compare(b.getCount(), a.getCount()))
.collect(Collectors.toList());
}
@@ -48,7 +54,7 @@ public class CategoryController {
public CategoryDto get(@PathVariable Long id) {
Category c = categoryService.getCategory(id);
long count = postService.countPostsByCategory(c.getId());
return toDto(c, count);
return categoryMapper.toDto(c, count);
}
@GetMapping("/{id}/posts")
@@ -57,47 +63,7 @@ public class CategoryController {
@RequestParam(value = "pageSize", required = false) Integer pageSize) {
return postService.listPostsByCategories(java.util.List.of(id), page, pageSize)
.stream()
.map(p -> {
PostSummaryDto dto = new PostSummaryDto();
dto.setId(p.getId());
dto.setTitle(p.getTitle());
return dto;
})
.map(postMapper::toSummaryDto)
.collect(Collectors.toList());
}
private CategoryDto toDto(Category c, long count) {
CategoryDto dto = new CategoryDto();
dto.setId(c.getId());
dto.setName(c.getName());
dto.setIcon(c.getIcon());
dto.setSmallIcon(c.getSmallIcon());
dto.setDescription(c.getDescription());
dto.setCount(count);
return dto;
}
@Data
private static class CategoryRequest {
private String name;
private String description;
private String icon;
private String smallIcon;
}
@Data
private static class CategoryDto {
private Long id;
private String name;
private String description;
private String icon;
private String smallIcon;
private Long count;
}
@Data
private static class PostSummaryDto {
private Long id;
private String title;
}
}

View File

@@ -6,19 +6,14 @@ import com.openisle.dto.CommentRequest;
import com.openisle.mapper.CommentMapper;
import com.openisle.service.CaptchaService;
import com.openisle.service.CommentService;
import com.openisle.service.CaptchaService;
import com.openisle.service.LevelService;
import com.openisle.service.ReactionService;
import com.openisle.model.CommentSort;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@@ -30,7 +25,7 @@ public class CommentController {
private final CommentService commentService;
private final LevelService levelService;
private final CaptchaService captchaService;
private final ReactionService reactionService;
private final CommentMapper commentMapper;
@Value("${app.captcha.enabled:false}")
private boolean captchaEnabled;
@@ -48,7 +43,7 @@ public class CommentController {
return ResponseEntity.badRequest().build();
}
Comment comment = commentService.addComment(auth.getName(), postId, req.getContent());
CommentDto dto = toDto(comment);
CommentDto dto = commentMapper.toDto(comment);
dto.setReward(levelService.awardForComment(auth.getName()));
log.debug("createComment succeeded for comment {}", comment.getId());
return ResponseEntity.ok(dto);
@@ -64,7 +59,7 @@ public class CommentController {
return ResponseEntity.badRequest().build();
}
Comment comment = commentService.addReply(auth.getName(), commentId, req.getContent());
CommentDto dto = toDto(comment);
CommentDto dto = commentMapper.toDto(comment);
dto.setReward(levelService.awardForComment(auth.getName()));
log.debug("replyComment succeeded for comment {}", comment.getId());
return ResponseEntity.ok(dto);
@@ -72,99 +67,19 @@ public class CommentController {
@GetMapping("/posts/{postId}/comments")
public List<CommentDto> listComments(@PathVariable Long postId,
@RequestParam(value = "sort", required = false, defaultValue = "OLDEST") CommentSort sort) {
@RequestParam(value = "sort", required = false, defaultValue = "OLDEST") com.openisle.model.CommentSort sort) {
log.debug("listComments called for post {} with sort {}", postId, sort);
List<CommentDto> list = commentService.getCommentsForPost(postId, sort).stream()
.map(this::toDtoWithReplies)
.map(commentMapper::toDtoWithReplies)
.collect(Collectors.toList());
log.debug("listComments returning {} comments", list.size());
return list;
}
private CommentDto toDtoWithReplies(Comment comment) {
CommentDto dto = toDto(comment);
List<CommentDto> replies = commentService.getReplies(comment.getId()).stream()
.map(this::toDtoWithReplies)
.collect(Collectors.toList());
dto.setReplies(replies);
List<ReactionDto> reactions = reactionService.getReactionsForComment(comment.getId()).stream()
.map(this::toReactionDto)
.collect(Collectors.toList());
dto.setReactions(reactions);
return dto;
}
@DeleteMapping("/comments/{id}")
public void deleteComment(@PathVariable Long id, Authentication auth) {
log.debug("deleteComment called by user {} for comment {}", auth.getName(), id);
commentService.deleteComment(auth.getName(), id);
log.debug("deleteComment completed for comment {}", id);
}
private CommentDto toDto(Comment comment) {
CommentDto dto = new CommentDto();
dto.setId(comment.getId());
dto.setContent(comment.getContent());
dto.setCreatedAt(comment.getCreatedAt());
dto.setAuthor(toAuthorDto(comment.getAuthor()));
dto.setReward(0);
return dto;
}
private AuthorDto toAuthorDto(com.openisle.model.User user) {
AuthorDto dto = new AuthorDto();
dto.setId(user.getId());
dto.setUsername(user.getUsername());
dto.setAvatar(user.getAvatar());
return dto;
}
@Data
private static class CommentRequest {
private String content;
private String captcha;
}
@Data
private static class CommentDto {
private Long id;
private String content;
private LocalDateTime createdAt;
private AuthorDto author;
private List<CommentDto> replies;
private List<ReactionDto> reactions;
private int reward;
}
@Data
private static class AuthorDto {
private Long id;
private String username;
private String avatar;
}
private ReactionDto toReactionDto(com.openisle.model.Reaction reaction) {
ReactionDto dto = new ReactionDto();
dto.setId(reaction.getId());
dto.setType(reaction.getType());
dto.setUser(reaction.getUser().getUsername());
if (reaction.getPost() != null) {
dto.setPostId(reaction.getPost().getId());
}
if (reaction.getComment() != null) {
dto.setCommentId(reaction.getComment().getId());
}
dto.setReward(0);
return dto;
}
@Data
private static class ReactionDto {
private Long id;
private com.openisle.model.ReactionType type;
private String user;
private Long postId;
private Long commentId;
private int reward;
}
}

View File

@@ -1,9 +1,8 @@
package com.openisle.controller;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import com.openisle.dto.SiteConfigDto;
import com.openisle.service.RegisterModeService;
import com.openisle.model.RegisterMode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -34,8 +33,8 @@ public class ConfigController {
private final RegisterModeService registerModeService;
@GetMapping("/config")
public ConfigResponse getConfig() {
ConfigResponse resp = new ConfigResponse();
public SiteConfigDto getConfig() {
SiteConfigDto resp = new SiteConfigDto();
resp.setCaptchaEnabled(captchaEnabled);
resp.setRegisterCaptchaEnabled(registerCaptchaEnabled);
resp.setLoginCaptchaEnabled(loginCaptchaEnabled);
@@ -45,15 +44,4 @@ public class ConfigController {
resp.setRegisterMode(registerModeService.getRegisterMode());
return resp;
}
@Data
private static class ConfigResponse {
private boolean captchaEnabled;
private boolean registerCaptchaEnabled;
private boolean loginCaptchaEnabled;
private boolean postCaptchaEnabled;
private boolean commentCaptchaEnabled;
private int aiFormatLimit;
private RegisterMode registerMode;
}
}

View File

@@ -1,32 +1,32 @@
package com.openisle.controller;
import com.openisle.dto.DraftDto;
import com.openisle.dto.DraftRequest;
import com.openisle.mapper.DraftMapper;
import com.openisle.model.Draft;
import com.openisle.service.DraftService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/api/drafts")
@RequiredArgsConstructor
public class DraftController {
private final DraftService draftService;
private final DraftMapper draftMapper;
@PostMapping
public ResponseEntity<DraftDto> saveDraft(@RequestBody DraftRequest req, Authentication auth) {
Draft draft = draftService.saveDraft(auth.getName(), req.getCategoryId(), req.getTitle(), req.getContent(), req.getTagIds());
return ResponseEntity.ok(toDto(draft));
return ResponseEntity.ok(draftMapper.toDto(draft));
}
@GetMapping("/me")
public ResponseEntity<DraftDto> getMyDraft(Authentication auth) {
return draftService.getDraft(auth.getName())
.map(d -> ResponseEntity.ok(toDto(d)))
.map(d -> ResponseEntity.ok(draftMapper.toDto(d)))
.orElseGet(() -> ResponseEntity.noContent().build());
}
@@ -35,33 +35,4 @@ public class DraftController {
draftService.deleteDraft(auth.getName());
return ResponseEntity.ok().build();
}
private DraftDto toDto(Draft draft) {
DraftDto dto = new DraftDto();
dto.setId(draft.getId());
dto.setTitle(draft.getTitle());
dto.setContent(draft.getContent());
if (draft.getCategory() != null) {
dto.setCategoryId(draft.getCategory().getId());
}
dto.setTagIds(draft.getTags().stream().map(com.openisle.model.Tag::getId).collect(Collectors.toList()));
return dto;
}
@Data
private static class DraftRequest {
private String title;
private String content;
private Long categoryId;
private List<Long> tagIds;
}
@Data
private static class DraftDto {
private Long id;
private String title;
private String content;
private Long categoryId;
private List<Long> tagIds;
}
}

View File

@@ -1,17 +1,14 @@
package com.openisle.controller;
import com.openisle.model.Notification;
import com.openisle.model.NotificationType;
import com.openisle.model.ReactionType;
import com.openisle.model.Comment;
import com.openisle.model.Post;
import com.openisle.dto.NotificationDto;
import com.openisle.dto.NotificationMarkReadRequest;
import com.openisle.dto.NotificationUnreadCountDto;
import com.openisle.mapper.NotificationMapper;
import com.openisle.service.NotificationService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@@ -21,122 +18,26 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class NotificationController {
private final NotificationService notificationService;
private final NotificationMapper notificationMapper;
@GetMapping
public List<NotificationDto> list(@RequestParam(value = "read", required = false) Boolean read,
Authentication auth) {
return notificationService.listNotifications(auth.getName(), read).stream()
.map(this::toDto)
.map(notificationMapper::toDto)
.collect(Collectors.toList());
}
@GetMapping("/unread-count")
public UnreadCount unreadCount(Authentication auth) {
public NotificationUnreadCountDto unreadCount(Authentication auth) {
long count = notificationService.countUnread(auth.getName());
UnreadCount uc = new UnreadCount();
NotificationUnreadCountDto uc = new NotificationUnreadCountDto();
uc.setCount(count);
return uc;
}
@PostMapping("/read")
public void markRead(@RequestBody MarkReadRequest req, Authentication auth) {
public void markRead(@RequestBody NotificationMarkReadRequest req, Authentication auth) {
notificationService.markRead(auth.getName(), req.getIds());
}
private NotificationDto toDto(Notification n) {
NotificationDto dto = new NotificationDto();
dto.setId(n.getId());
dto.setType(n.getType());
if (n.getPost() != null) {
dto.setPost(toPostDto(n.getPost()));
}
if (n.getComment() != null) {
dto.setComment(toCommentDto(n.getComment()));
Comment parent = n.getComment().getParent();
if (parent != null) {
dto.setParentComment(toCommentDto(parent));
}
}
if (n.getFromUser() != null) {
dto.setFromUser(toAuthorDto(n.getFromUser()));
}
if (n.getReactionType() != null) {
dto.setReactionType(n.getReactionType());
}
dto.setApproved(n.getApproved());
dto.setContent(n.getContent());
dto.setRead(n.isRead());
dto.setCreatedAt(n.getCreatedAt());
return dto;
}
private PostDto toPostDto(Post post) {
PostDto dto = new PostDto();
dto.setId(post.getId());
dto.setTitle(post.getTitle());
return dto;
}
private CommentDto toCommentDto(Comment comment) {
CommentDto dto = new CommentDto();
dto.setId(comment.getId());
dto.setContent(comment.getContent());
dto.setCreatedAt(comment.getCreatedAt());
dto.setAuthor(toAuthorDto(comment.getAuthor()));
return dto;
}
private AuthorDto toAuthorDto(com.openisle.model.User user) {
AuthorDto dto = new AuthorDto();
dto.setId(user.getId());
dto.setUsername(user.getUsername());
dto.setAvatar(user.getAvatar());
return dto;
}
@Data
private static class MarkReadRequest {
private List<Long> ids;
}
@Data
private static class NotificationDto {
private Long id;
private NotificationType type;
private PostDto post;
private CommentDto comment;
private CommentDto parentComment;
private AuthorDto fromUser;
private ReactionType reactionType;
private String content;
private Boolean approved;
private boolean read;
private LocalDateTime createdAt;
}
@Data
private static class PostDto {
private Long id;
private String title;
}
@Data
private static class CommentDto {
private Long id;
private String content;
private LocalDateTime createdAt;
private AuthorDto author;
}
@Data
private static class AuthorDto {
private Long id;
private String username;
private String avatar;
}
@Data
private static class UnreadCount {
private long count;
}
}

View File

@@ -1,25 +1,22 @@
package com.openisle.controller;
import com.openisle.model.Comment;
import com.openisle.dto.PostDetailDto;
import com.openisle.dto.PostRequest;
import com.openisle.dto.PostSummaryDto;
import com.openisle.mapper.PostMapper;
import com.openisle.model.Post;
import com.openisle.model.Reaction;
import com.openisle.service.CommentService;
import com.openisle.model.CommentSort;
import com.openisle.service.PostService;
import com.openisle.service.ReactionService;
import com.openisle.service.CaptchaService;
import com.openisle.service.DraftService;
import com.openisle.service.LevelService;
import com.openisle.service.PostService;
import com.openisle.service.SubscriptionService;
import com.openisle.service.UserVisitService;
import com.openisle.service.LevelService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@@ -28,13 +25,12 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class PostController {
private final PostService postService;
private final CommentService commentService;
private final ReactionService reactionService;
private final SubscriptionService subscriptionService;
private final LevelService levelService;
private final CaptchaService captchaService;
private final DraftService draftService;
private final UserVisitService userVisitService;
private final PostMapper postMapper;
@Value("${app.captcha.enabled:false}")
private boolean captchaEnabled;
@@ -43,24 +39,24 @@ public class PostController {
private boolean postCaptchaEnabled;
@PostMapping
public ResponseEntity<PostDto> createPost(@RequestBody PostRequest req, Authentication auth) {
public ResponseEntity<PostDetailDto> createPost(@RequestBody PostRequest req, Authentication auth) {
if (captchaEnabled && postCaptchaEnabled && !captchaService.verify(req.getCaptcha())) {
return ResponseEntity.badRequest().build();
}
Post post = postService.createPost(auth.getName(), req.getCategoryId(),
req.getTitle(), req.getContent(), req.getTagIds());
draftService.deleteDraft(auth.getName());
PostDto dto = toDto(post);
PostDetailDto dto = postMapper.toDetailDto(post, auth.getName());
dto.setReward(levelService.awardForPost(auth.getName()));
return ResponseEntity.ok(dto);
}
@PutMapping("/{id}")
public ResponseEntity<PostDto> updatePost(@PathVariable Long id, @RequestBody PostRequest req,
public ResponseEntity<PostDetailDto> updatePost(@PathVariable Long id, @RequestBody PostRequest req,
Authentication auth) {
Post post = postService.updatePost(id, auth.getName(), req.getCategoryId(),
req.getTitle(), req.getContent(), req.getTagIds());
return ResponseEntity.ok(toDto(post));
return ResponseEntity.ok(postMapper.toDetailDto(post, auth.getName()));
}
@DeleteMapping("/{id}")
@@ -69,14 +65,14 @@ public class PostController {
}
@GetMapping("/{id}")
public ResponseEntity<PostDto> getPost(@PathVariable Long id, Authentication auth) {
public ResponseEntity<PostDetailDto> getPost(@PathVariable Long id, Authentication auth) {
String viewer = auth != null ? auth.getName() : null;
Post post = postService.viewPost(id, viewer);
return ResponseEntity.ok(toDto(post, viewer));
return ResponseEntity.ok(postMapper.toDetailDto(post, viewer));
}
@GetMapping
public List<PostDto> listPosts(@RequestParam(value = "categoryId", required = false) Long categoryId,
public List<PostSummaryDto> listPosts(@RequestParam(value = "categoryId", required = false) Long categoryId,
@RequestParam(value = "categoryIds", required = false) List<Long> categoryIds,
@RequestParam(value = "tagId", required = false) Long tagId,
@RequestParam(value = "tagIds", required = false) List<Long> tagIds,
@@ -101,19 +97,19 @@ public class PostController {
if (hasCategories && hasTags) {
return postService.listPostsByCategoriesAndTags(ids, tids, page, pageSize)
.stream().map(this::toDto).collect(Collectors.toList());
.stream().map(postMapper::toSummaryDto).collect(Collectors.toList());
}
if (hasTags) {
return postService.listPostsByTags(tids, page, pageSize)
.stream().map(this::toDto).collect(Collectors.toList());
.stream().map(postMapper::toSummaryDto).collect(Collectors.toList());
}
return postService.listPostsByCategories(ids, page, pageSize)
.stream().map(this::toDto).collect(Collectors.toList());
.stream().map(postMapper::toSummaryDto).collect(Collectors.toList());
}
@GetMapping("/ranking")
public List<PostDto> rankingPosts(@RequestParam(value = "categoryId", required = false) Long categoryId,
public List<PostSummaryDto> rankingPosts(@RequestParam(value = "categoryId", required = false) Long categoryId,
@RequestParam(value = "categoryIds", required = false) List<Long> categoryIds,
@RequestParam(value = "tagId", required = false) Long tagId,
@RequestParam(value = "tagIds", required = false) List<Long> tagIds,
@@ -134,11 +130,11 @@ public class PostController {
}
return postService.listPostsByViews(ids, tids, page, pageSize)
.stream().map(this::toDto).collect(Collectors.toList());
.stream().map(postMapper::toSummaryDto).collect(Collectors.toList());
}
@GetMapping("/latest-reply")
public List<PostDto> latestReplyPosts(@RequestParam(value = "categoryId", required = false) Long categoryId,
public List<PostSummaryDto> latestReplyPosts(@RequestParam(value = "categoryId", required = false) Long categoryId,
@RequestParam(value = "categoryIds", required = false) List<Long> categoryIds,
@RequestParam(value = "tagId", required = false) Long tagId,
@RequestParam(value = "tagIds", required = false) List<Long> tagIds,
@@ -159,195 +155,6 @@ public class PostController {
}
return postService.listPostsByLatestReply(ids, tids, page, pageSize)
.stream().map(this::toDto).collect(Collectors.toList());
}
private PostDto toDto(Post post) {
PostDto dto = new PostDto();
dto.setId(post.getId());
dto.setTitle(post.getTitle());
dto.setContent(post.getContent());
dto.setCreatedAt(post.getCreatedAt());
dto.setAuthor(toAuthorDto(post.getAuthor()));
dto.setCategory(toCategoryDto(post.getCategory()));
dto.setTags(post.getTags().stream().map(this::toTagDto).collect(Collectors.toList()));
dto.setViews(post.getViews());
dto.setStatus(post.getStatus());
dto.setPinnedAt(post.getPinnedAt());
List<ReactionDto> reactions = reactionService.getReactionsForPost(post.getId())
.stream()
.map(this::toReactionDto)
.collect(Collectors.toList());
dto.setReactions(reactions);
List<CommentDto> comments = commentService.getCommentsForPost(post.getId(), CommentSort.OLDEST)
.stream()
.map(this::toCommentDtoWithReplies)
.collect(Collectors.toList());
dto.setComments(comments);
java.util.List<com.openisle.model.User> participants = commentService.getParticipants(post.getId(), 5);
dto.setParticipants(participants.stream().map(this::toAuthorDto).collect(Collectors.toList()));
java.time.LocalDateTime last = commentService.getLastCommentTime(post.getId());
dto.setLastReplyAt(last != null ? last : post.getCreatedAt());
dto.setReward(0);
return dto;
}
private PostDto toDto(Post post, String viewer) {
PostDto dto = toDto(post);
if (viewer != null) {
dto.setSubscribed(subscriptionService.isPostSubscribed(viewer, post.getId()));
} else {
dto.setSubscribed(false);
}
return dto;
}
private CommentDto toCommentDtoWithReplies(Comment comment) {
CommentDto dto = toCommentDto(comment);
List<CommentDto> replies = commentService.getReplies(comment.getId()).stream()
.map(this::toCommentDtoWithReplies)
.collect(Collectors.toList());
dto.setReplies(replies);
List<ReactionDto> reactions = reactionService.getReactionsForComment(comment.getId())
.stream()
.map(this::toReactionDto)
.collect(Collectors.toList());
dto.setReactions(reactions);
return dto;
}
private CommentDto toCommentDto(Comment comment) {
CommentDto dto = new CommentDto();
dto.setId(comment.getId());
dto.setContent(comment.getContent());
dto.setCreatedAt(comment.getCreatedAt());
dto.setAuthor(toAuthorDto(comment.getAuthor()));
dto.setReward(0);
return dto;
}
private ReactionDto toReactionDto(Reaction reaction) {
ReactionDto dto = new ReactionDto();
dto.setId(reaction.getId());
dto.setType(reaction.getType());
dto.setUser(reaction.getUser().getUsername());
if (reaction.getPost() != null) {
dto.setPostId(reaction.getPost().getId());
}
if (reaction.getComment() != null) {
dto.setCommentId(reaction.getComment().getId());
}
dto.setReward(0);
return dto;
}
private CategoryDto toCategoryDto(com.openisle.model.Category category) {
CategoryDto dto = new CategoryDto();
dto.setId(category.getId());
dto.setName(category.getName());
dto.setDescription(category.getDescription());
dto.setIcon(category.getIcon());
dto.setSmallIcon(category.getSmallIcon());
return dto;
}
private TagDto toTagDto(com.openisle.model.Tag tag) {
TagDto dto = new TagDto();
dto.setId(tag.getId());
dto.setName(tag.getName());
dto.setDescription(tag.getDescription());
dto.setIcon(tag.getIcon());
dto.setSmallIcon(tag.getSmallIcon());
return dto;
}
private AuthorDto toAuthorDto(com.openisle.model.User user) {
AuthorDto dto = new AuthorDto();
dto.setId(user.getId());
dto.setUsername(user.getUsername());
dto.setAvatar(user.getAvatar());
return dto;
}
@Data
private static class PostRequest {
private Long categoryId;
private String title;
private String content;
private java.util.List<Long> tagIds;
private String captcha;
}
@Data
private static class PostDto {
private Long id;
private String title;
private String content;
private LocalDateTime createdAt;
private AuthorDto author;
private CategoryDto category;
private java.util.List<TagDto> tags;
private long views;
private com.openisle.model.PostStatus status;
private LocalDateTime pinnedAt;
private LocalDateTime lastReplyAt;
private List<CommentDto> comments;
private List<ReactionDto> reactions;
private java.util.List<AuthorDto> participants;
private boolean subscribed;
private int reward;
}
@Data
private static class CategoryDto {
private Long id;
private String name;
private String description;
private String icon;
private String smallIcon;
}
@Data
private static class TagDto {
private Long id;
private String name;
private String description;
private String icon;
private String smallIcon;
}
@Data
private static class AuthorDto {
private Long id;
private String username;
private String avatar;
}
@Data
private static class CommentDto {
private Long id;
private String content;
private LocalDateTime createdAt;
private AuthorDto author;
private List<CommentDto> replies;
private List<ReactionDto> reactions;
private int reward;
}
@Data
private static class ReactionDto {
private Long id;
private com.openisle.model.ReactionType type;
private String user;
private Long postId;
private Long commentId;
private int reward;
.stream().map(postMapper::toSummaryDto).collect(Collectors.toList());
}
}

View File

@@ -1,7 +1,8 @@
package com.openisle.controller;
import com.openisle.dto.PushPublicKeyDto;
import com.openisle.dto.PushSubscriptionRequest;
import com.openisle.service.PushSubscriptionService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
@@ -16,26 +17,14 @@ public class PushSubscriptionController {
private String publicKey;
@GetMapping("/public-key")
public PublicKeyResponse getPublicKey() {
PublicKeyResponse r = new PublicKeyResponse();
public PushPublicKeyDto getPublicKey() {
PushPublicKeyDto r = new PushPublicKeyDto();
r.setKey(publicKey);
return r;
}
@PostMapping("/subscribe")
public void subscribe(@RequestBody SubscriptionRequest req, Authentication auth) {
public void subscribe(@RequestBody PushSubscriptionRequest req, Authentication auth) {
pushSubscriptionService.saveSubscription(auth.getName(), req.getEndpoint(), req.getP256dh(), req.getAuth());
}
@Data
private static class PublicKeyResponse {
private String key;
}
@Data
private static class SubscriptionRequest {
private String endpoint;
private String p256dh;
private String auth;
}
}

View File

@@ -1,11 +1,13 @@
package com.openisle.controller;
import com.openisle.dto.ReactionDto;
import com.openisle.dto.ReactionRequest;
import com.openisle.mapper.ReactionMapper;
import com.openisle.model.Reaction;
import com.openisle.model.ReactionType;
import com.openisle.service.ReactionService;
import com.openisle.service.LevelService;
import com.openisle.service.ReactionService;
import jakarta.transaction.Transactional;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
@@ -17,6 +19,7 @@ import org.springframework.web.bind.annotation.*;
public class ReactionController {
private final ReactionService reactionService;
private final LevelService levelService;
private final ReactionMapper reactionMapper;
/**
* Get all available reaction types.
@@ -34,7 +37,7 @@ public class ReactionController {
if (reaction == null) {
return ResponseEntity.noContent().build();
}
ReactionDto dto = toDto(reaction);
ReactionDto dto = reactionMapper.toDto(reaction);
dto.setReward(levelService.awardForReaction(auth.getName()));
return ResponseEntity.ok(dto);
}
@@ -47,38 +50,8 @@ public class ReactionController {
if (reaction == null) {
return ResponseEntity.noContent().build();
}
ReactionDto dto = toDto(reaction);
ReactionDto dto = reactionMapper.toDto(reaction);
dto.setReward(levelService.awardForReaction(auth.getName()));
return ResponseEntity.ok(dto);
}
private ReactionDto toDto(Reaction reaction) {
ReactionDto dto = new ReactionDto();
dto.setId(reaction.getId());
dto.setType(reaction.getType());
dto.setUser(reaction.getUser().getUsername());
if (reaction.getPost() != null) {
dto.setPostId(reaction.getPost().getId());
}
if (reaction.getComment() != null) {
dto.setCommentId(reaction.getComment().getId());
}
dto.setReward(0);
return dto;
}
@Data
private static class ReactionRequest {
private ReactionType type;
}
@Data
private static class ReactionDto {
private Long id;
private ReactionType type;
private String user;
private Long postId;
private Long commentId;
private int reward;
}
}

View File

@@ -1,10 +1,11 @@
package com.openisle.controller;
import com.openisle.model.Post;
import com.openisle.model.Comment;
import com.openisle.model.User;
import com.openisle.dto.PostSummaryDto;
import com.openisle.dto.SearchResultDto;
import com.openisle.dto.UserDto;
import com.openisle.mapper.PostMapper;
import com.openisle.mapper.UserMapper;
import com.openisle.service.SearchService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -19,32 +20,34 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class SearchController {
private final SearchService searchService;
private final UserMapper userMapper;
private final PostMapper postMapper;
@GetMapping("/users")
public List<UserDto> searchUsers(@RequestParam String keyword) {
return searchService.searchUsers(keyword).stream()
.map(this::toUserDto)
.map(userMapper::toDto)
.collect(Collectors.toList());
}
@GetMapping("/posts")
public List<PostDto> searchPosts(@RequestParam String keyword) {
public List<PostSummaryDto> searchPosts(@RequestParam String keyword) {
return searchService.searchPosts(keyword).stream()
.map(this::toPostDto)
.map(postMapper::toSummaryDto)
.collect(Collectors.toList());
}
@GetMapping("/posts/content")
public List<PostDto> searchPostsByContent(@RequestParam String keyword) {
public List<PostSummaryDto> searchPostsByContent(@RequestParam String keyword) {
return searchService.searchPostsByContent(keyword).stream()
.map(this::toPostDto)
.map(postMapper::toSummaryDto)
.collect(Collectors.toList());
}
@GetMapping("/posts/title")
public List<PostDto> searchPostsByTitle(@RequestParam String keyword) {
public List<PostSummaryDto> searchPostsByTitle(@RequestParam String keyword) {
return searchService.searchPostsByTitle(keyword).stream()
.map(this::toPostDto)
.map(postMapper::toSummaryDto)
.collect(Collectors.toList());
}
@@ -63,42 +66,4 @@ public class SearchController {
})
.collect(Collectors.toList());
}
private UserDto toUserDto(User user) {
UserDto dto = new UserDto();
dto.setId(user.getId());
dto.setUsername(user.getUsername());
dto.setAvatar(user.getAvatar());
return dto;
}
private PostDto toPostDto(Post post) {
PostDto dto = new PostDto();
dto.setId(post.getId());
dto.setTitle(post.getTitle());
return dto;
}
@Data
private static class UserDto {
private Long id;
private String username;
private String avatar;
}
@Data
private static class PostDto {
private Long id;
private String title;
}
@Data
private static class SearchResultDto {
private String type;
private Long id;
private String text;
private String subText;
private String extra;
private Long postId;
}
}

View File

@@ -1,12 +1,16 @@
package com.openisle.controller;
import com.openisle.model.Tag;
import com.openisle.service.TagService;
import com.openisle.service.PostService;
import com.openisle.repository.UserRepository;
import com.openisle.dto.PostSummaryDto;
import com.openisle.dto.TagDto;
import com.openisle.dto.TagRequest;
import com.openisle.mapper.PostMapper;
import com.openisle.mapper.TagMapper;
import com.openisle.model.PublishMode;
import com.openisle.model.Role;
import lombok.Data;
import com.openisle.model.Tag;
import com.openisle.repository.UserRepository;
import com.openisle.service.PostService;
import com.openisle.service.TagService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
@@ -20,6 +24,8 @@ public class TagController {
private final TagService tagService;
private final PostService postService;
private final UserRepository userRepository;
private final PostMapper postMapper;
private final TagMapper tagMapper;
@PostMapping
public TagDto create(@RequestBody TagRequest req, org.springframework.security.core.Authentication auth) {
@@ -38,14 +44,14 @@ public class TagController {
approved,
auth != null ? auth.getName() : null);
long count = postService.countPostsByTag(tag.getId());
return toDto(tag, count);
return tagMapper.toDto(tag, count);
}
@PutMapping("/{id}")
public TagDto update(@PathVariable Long id, @RequestBody TagRequest req) {
Tag tag = tagService.updateTag(id, req.getName(), req.getDescription(), req.getIcon(), req.getSmallIcon());
long count = postService.countPostsByTag(tag.getId());
return toDto(tag, count);
return tagMapper.toDto(tag, count);
}
@DeleteMapping("/{id}")
@@ -57,7 +63,7 @@ public class TagController {
public List<TagDto> list(@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "limit", required = false) Integer limit) {
List<TagDto> dtos = tagService.searchTags(keyword).stream()
.map(t -> toDto(t, postService.countPostsByTag(t.getId())))
.map(t -> tagMapper.toDto(t, postService.countPostsByTag(t.getId())))
.sorted((a, b) -> Long.compare(b.getCount(), a.getCount()))
.collect(Collectors.toList());
if (limit != null && limit > 0 && dtos.size() > limit) {
@@ -70,7 +76,7 @@ public class TagController {
public TagDto get(@PathVariable Long id) {
Tag tag = tagService.getTag(id);
long count = postService.countPostsByTag(tag.getId());
return toDto(tag, count);
return tagMapper.toDto(tag, count);
}
@GetMapping("/{id}/posts")
@@ -79,47 +85,7 @@ public class TagController {
@RequestParam(value = "pageSize", required = false) Integer pageSize) {
return postService.listPostsByTags(java.util.List.of(id), page, pageSize)
.stream()
.map(p -> {
PostSummaryDto dto = new PostSummaryDto();
dto.setId(p.getId());
dto.setTitle(p.getTitle());
return dto;
})
.map(postMapper::toSummaryDto)
.collect(Collectors.toList());
}
private TagDto toDto(Tag tag, long count) {
TagDto dto = new TagDto();
dto.setId(tag.getId());
dto.setName(tag.getName());
dto.setIcon(tag.getIcon());
dto.setSmallIcon(tag.getSmallIcon());
dto.setDescription(tag.getDescription());
dto.setCount(count);
return dto;
}
@Data
private static class TagRequest {
private String name;
private String description;
private String icon;
private String smallIcon;
}
@Data
private static class TagDto {
private Long id;
private String name;
private String description;
private String icon;
private String smallIcon;
private Long count;
}
@Data
private static class PostSummaryDto {
private Long id;
private String title;
}
}

View File

@@ -1,11 +1,13 @@
package com.openisle.controller;
import com.openisle.dto.*;
import com.openisle.exception.NotFoundException;
import com.openisle.mapper.TagMapper;
import com.openisle.mapper.UserMapper;
import com.openisle.model.User;
import com.openisle.service.*;
import org.springframework.beans.factory.annotation.Value;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
@@ -25,10 +27,10 @@ public class UserController {
private final ReactionService reactionService;
private final TagService tagService;
private final SubscriptionService subscriptionService;
private final PostReadService postReadService;
private final UserVisitService userVisitService;
private final LevelService levelService;
private final JwtService jwtService;
private final UserMapper userMapper;
private final TagMapper tagMapper;
@Value("${app.upload.check-type:true}")
private boolean checkImageType;
@@ -45,13 +47,10 @@ public class UserController {
@Value("${app.user.tags-limit:50}")
private int defaultTagsLimit;
@Value("${app.snippet-length:50}")
private int snippetLength;
@GetMapping("/me")
public ResponseEntity<UserDto> me(Authentication auth) {
User user = userService.findByUsername(auth.getName()).orElseThrow();
return ResponseEntity.ok(toDto(user, auth));
return ResponseEntity.ok(userMapper.toDto(user, auth));
}
@PostMapping("/me/avatar")
@@ -79,7 +78,7 @@ public class UserController {
User user = userService.updateProfile(auth.getName(), dto.getUsername(), dto.getIntroduction());
return ResponseEntity.ok(Map.of(
"token", jwtService.generateToken(user.getUsername()),
"user", toDto(user, auth)
"user", userMapper.toDto(user, auth)
));
}
@@ -93,7 +92,7 @@ public class UserController {
public ResponseEntity<UserDto> getUser(@PathVariable("identifier") String identifier,
Authentication auth) {
User user = userService.findByIdentifier(identifier).orElseThrow(() -> new NotFoundException("User not found"));
return ResponseEntity.ok(toDto(user, auth));
return ResponseEntity.ok(userMapper.toDto(user, auth));
}
@GetMapping("/{identifier}/posts")
@@ -102,7 +101,7 @@ public class UserController {
int l = limit != null ? limit : defaultPostsLimit;
User user = userService.findByIdentifier(identifier).orElseThrow();
return postService.getRecentPostsByUser(user.getUsername(), l).stream()
.map(this::toMetaDto)
.map(userMapper::toMetaDto)
.collect(java.util.stream.Collectors.toList());
}
@@ -112,7 +111,7 @@ public class UserController {
int l = limit != null ? limit : defaultRepliesLimit;
User user = userService.findByIdentifier(identifier).orElseThrow();
return commentService.getRecentCommentsByUser(user.getUsername(), l).stream()
.map(this::toCommentInfoDto)
.map(userMapper::toCommentInfoDto)
.collect(java.util.stream.Collectors.toList());
}
@@ -123,7 +122,7 @@ public class UserController {
User user = userService.findByIdentifier(identifier).orElseThrow();
java.util.List<Long> ids = reactionService.topPostIds(user.getUsername(), l);
return postService.getPostsByIds(ids).stream()
.map(this::toMetaDto)
.map(userMapper::toMetaDto)
.collect(java.util.stream.Collectors.toList());
}
@@ -134,49 +133,29 @@ public class UserController {
User user = userService.findByIdentifier(identifier).orElseThrow();
java.util.List<Long> ids = reactionService.topCommentIds(user.getUsername(), l);
return commentService.getCommentsByIds(ids).stream()
.map(this::toCommentInfoDto)
.map(userMapper::toCommentInfoDto)
.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) {
public java.util.List<TagDto> 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;
})
.map(t -> tagMapper.toDto(t, postService.countPostsByTag(t.getId())))
.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) {
public java.util.List<TagDto> 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;
})
.map(t -> tagMapper.toDto(t, postService.countPostsByTag(t.getId())))
.collect(java.util.stream.Collectors.toList());
}
@@ -184,7 +163,7 @@ public class UserController {
public java.util.List<UserDto> following(@PathVariable("identifier") String identifier) {
User user = userService.findByIdentifier(identifier).orElseThrow();
return subscriptionService.getSubscribedUsers(user.getUsername()).stream()
.map(this::toDto)
.map(userMapper::toDto)
.collect(java.util.stream.Collectors.toList());
}
@@ -192,17 +171,14 @@ public class UserController {
public java.util.List<UserDto> followers(@PathVariable("identifier") String identifier) {
User user = userService.findByIdentifier(identifier).orElseThrow();
return subscriptionService.getSubscribers(user.getUsername()).stream()
.map(this::toDto)
.map(userMapper::toDto)
.collect(java.util.stream.Collectors.toList());
}
/**
* List all administrator users.
*/
@GetMapping("/admins")
public java.util.List<UserDto> admins() {
return userService.getAdmins().stream()
.map(this::toDto)
.map(userMapper::toDto)
.collect(java.util.stream.Collectors.toList());
}
@@ -215,157 +191,15 @@ public class UserController {
int pLimit = postsLimit != null ? postsLimit : defaultPostsLimit;
int rLimit = repliesLimit != null ? repliesLimit : defaultRepliesLimit;
java.util.List<PostMetaDto> posts = postService.getRecentPostsByUser(user.getUsername(), pLimit).stream()
.map(this::toMetaDto)
.map(userMapper::toMetaDto)
.collect(java.util.stream.Collectors.toList());
java.util.List<CommentInfoDto> replies = commentService.getRecentCommentsByUser(user.getUsername(), rLimit).stream()
.map(this::toCommentInfoDto)
.map(userMapper::toCommentInfoDto)
.collect(java.util.stream.Collectors.toList());
UserAggregateDto dto = new UserAggregateDto();
dto.setUser(toDto(user, auth));
dto.setUser(userMapper.toDto(user, auth));
dto.setPosts(posts);
dto.setReplies(replies);
return ResponseEntity.ok(dto);
}
private UserDto toDto(User user, Authentication viewer) {
UserDto dto = new UserDto();
dto.setId(user.getId());
dto.setUsername(user.getUsername());
dto.setEmail(user.getEmail());
dto.setAvatar(user.getAvatar());
dto.setRole(user.getRole().name());
dto.setIntroduction(user.getIntroduction());
dto.setFollowers(subscriptionService.countSubscribers(user.getUsername()));
dto.setFollowing(subscriptionService.countSubscribed(user.getUsername()));
dto.setCreatedAt(user.getCreatedAt());
dto.setLastPostTime(postService.getLastPostTime(user.getUsername()));
dto.setLastCommentTime(commentService.getLastCommentTimeOfUserByUserId(user.getId()));
dto.setTotalViews(postService.getTotalViews(user.getUsername()));
dto.setVisitedDays(userVisitService.countVisits(user.getUsername()));
dto.setReadPosts(postReadService.countReads(user.getUsername()));
dto.setLikesSent(reactionService.countLikesSent(user.getUsername()));
dto.setLikesReceived(reactionService.countLikesReceived(user.getUsername()));
dto.setExperience(user.getExperience());
dto.setCurrentLevel(levelService.getLevel(user.getExperience()));
dto.setNextLevelExp(levelService.nextLevelExp(user.getExperience()));
if (viewer != null) {
dto.setSubscribed(subscriptionService.isSubscribed(viewer.getName(), user.getUsername()));
} else {
dto.setSubscribed(false);
}
return dto;
}
private UserDto toDto(User user) {
return toDto(user, null);
}
private PostMetaDto toMetaDto(com.openisle.model.Post post) {
PostMetaDto dto = new PostMetaDto();
dto.setId(post.getId());
dto.setTitle(post.getTitle());
String content = post.getContent();
if (content == null) {
content = "";
}
if (snippetLength >= 0) {
dto.setSnippet(content.length() > snippetLength ? content.substring(0, snippetLength) : content);
} else {
dto.setSnippet(content);
}
dto.setCreatedAt(post.getCreatedAt());
dto.setCategory(post.getCategory().getName());
dto.setViews(post.getViews());
return dto;
}
private CommentInfoDto toCommentInfoDto(com.openisle.model.Comment comment) {
CommentInfoDto dto = new CommentInfoDto();
dto.setId(comment.getId());
dto.setContent(comment.getContent());
dto.setCreatedAt(comment.getCreatedAt());
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;
}
@Data
private static class UserDto {
private Long id;
private String username;
private String email;
private String avatar;
private String role;
private String introduction;
private long followers;
private long following;
private java.time.LocalDateTime createdAt;
private java.time.LocalDateTime lastPostTime;
private java.time.LocalDateTime lastCommentTime;
private long totalViews;
private long visitedDays;
private long readPosts;
private long likesSent;
private long likesReceived;
private boolean subscribed;
private int experience;
private int currentLevel;
private int nextLevelExp;
}
@Data
private static class PostMetaDto {
private Long id;
private String title;
private String snippet;
private java.time.LocalDateTime createdAt;
private String category;
private long views;
}
@Data
private static class CommentInfoDto {
private Long id;
private String content;
private java.time.LocalDateTime createdAt;
private PostMetaDto post;
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;
private String author;
private String content;
}
@Data
private static class UpdateProfileDto {
private String username;
private String introduction;
}
@Data
private static class UserAggregateDto {
private UserDto user;
private java.util.List<PostMetaDto> posts;
private java.util.List<CommentInfoDto> replies;
}
}