mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-10 03:57:32 +08:00
feat: add user leveling and experience system
This commit is contained in:
@@ -144,6 +144,9 @@ export default {
|
|||||||
} else {
|
} else {
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
reactions.value.push(data)
|
reactions.value.push(data)
|
||||||
|
if (data.reward && data.reward > 0) {
|
||||||
|
toast.success(`获得 ${data.reward} 经验值`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
emit('update:modelValue', reactions.value)
|
emit('update:modelValue', reactions.value)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -232,7 +232,11 @@ export default {
|
|||||||
})
|
})
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
toast.success('发布成功')
|
if (data.reward && data.reward > 0) {
|
||||||
|
toast.success(`发布成功,获得 ${data.reward} 经验值`)
|
||||||
|
} else {
|
||||||
|
toast.success('发布成功')
|
||||||
|
}
|
||||||
if (data.id) {
|
if (data.id) {
|
||||||
window.location.href = `/posts/${data.id}`
|
window.location.href = `/posts/${data.id}`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -354,7 +354,11 @@ export default {
|
|||||||
comments.value.push(mapComment(data))
|
comments.value.push(mapComment(data))
|
||||||
await nextTick()
|
await nextTick()
|
||||||
gatherPostItems()
|
gatherPostItems()
|
||||||
toast.success('评论成功')
|
if (data.reward && data.reward > 0) {
|
||||||
|
toast.success(`评论成功,获得 ${data.reward} 经验值`)
|
||||||
|
} else {
|
||||||
|
toast.success('评论成功')
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
toast.error('评论失败')
|
toast.error('评论失败')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.openisle.controller;
|
|||||||
import com.openisle.model.Comment;
|
import com.openisle.model.Comment;
|
||||||
import com.openisle.service.CommentService;
|
import com.openisle.service.CommentService;
|
||||||
import com.openisle.service.CaptchaService;
|
import com.openisle.service.CaptchaService;
|
||||||
|
import com.openisle.service.LevelService;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -19,6 +20,7 @@ import java.util.stream.Collectors;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class CommentController {
|
public class CommentController {
|
||||||
private final CommentService commentService;
|
private final CommentService commentService;
|
||||||
|
private final LevelService levelService;
|
||||||
private final CaptchaService captchaService;
|
private final CaptchaService captchaService;
|
||||||
|
|
||||||
@Value("${app.captcha.enabled:false}")
|
@Value("${app.captcha.enabled:false}")
|
||||||
@@ -35,7 +37,9 @@ public class CommentController {
|
|||||||
return ResponseEntity.badRequest().build();
|
return ResponseEntity.badRequest().build();
|
||||||
}
|
}
|
||||||
Comment comment = commentService.addComment(auth.getName(), postId, req.getContent());
|
Comment comment = commentService.addComment(auth.getName(), postId, req.getContent());
|
||||||
return ResponseEntity.ok(toDto(comment));
|
CommentDto dto = toDto(comment);
|
||||||
|
dto.setReward(levelService.awardForComment(auth.getName()));
|
||||||
|
return ResponseEntity.ok(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/comments/{commentId}/replies")
|
@PostMapping("/comments/{commentId}/replies")
|
||||||
@@ -46,7 +50,9 @@ public class CommentController {
|
|||||||
return ResponseEntity.badRequest().build();
|
return ResponseEntity.badRequest().build();
|
||||||
}
|
}
|
||||||
Comment comment = commentService.addReply(auth.getName(), commentId, req.getContent());
|
Comment comment = commentService.addReply(auth.getName(), commentId, req.getContent());
|
||||||
return ResponseEntity.ok(toDto(comment));
|
CommentDto dto = toDto(comment);
|
||||||
|
dto.setReward(levelService.awardForComment(auth.getName()));
|
||||||
|
return ResponseEntity.ok(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/posts/{postId}/comments")
|
@GetMapping("/posts/{postId}/comments")
|
||||||
@@ -76,6 +82,7 @@ public class CommentController {
|
|||||||
dto.setContent(comment.getContent());
|
dto.setContent(comment.getContent());
|
||||||
dto.setCreatedAt(comment.getCreatedAt());
|
dto.setCreatedAt(comment.getCreatedAt());
|
||||||
dto.setAuthor(toAuthorDto(comment.getAuthor()));
|
dto.setAuthor(toAuthorDto(comment.getAuthor()));
|
||||||
|
dto.setReward(0);
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,6 +107,7 @@ public class CommentController {
|
|||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
private AuthorDto author;
|
private AuthorDto author;
|
||||||
private List<CommentDto> replies;
|
private List<CommentDto> replies;
|
||||||
|
private int reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import com.openisle.service.CaptchaService;
|
|||||||
import com.openisle.service.DraftService;
|
import com.openisle.service.DraftService;
|
||||||
import com.openisle.service.SubscriptionService;
|
import com.openisle.service.SubscriptionService;
|
||||||
import com.openisle.service.UserVisitService;
|
import com.openisle.service.UserVisitService;
|
||||||
|
import com.openisle.service.LevelService;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -29,6 +30,7 @@ public class PostController {
|
|||||||
private final CommentService commentService;
|
private final CommentService commentService;
|
||||||
private final ReactionService reactionService;
|
private final ReactionService reactionService;
|
||||||
private final SubscriptionService subscriptionService;
|
private final SubscriptionService subscriptionService;
|
||||||
|
private final LevelService levelService;
|
||||||
private final CaptchaService captchaService;
|
private final CaptchaService captchaService;
|
||||||
private final DraftService draftService;
|
private final DraftService draftService;
|
||||||
private final UserVisitService userVisitService;
|
private final UserVisitService userVisitService;
|
||||||
@@ -47,7 +49,9 @@ public class PostController {
|
|||||||
Post post = postService.createPost(auth.getName(), req.getCategoryId(),
|
Post post = postService.createPost(auth.getName(), req.getCategoryId(),
|
||||||
req.getTitle(), req.getContent(), req.getTagIds());
|
req.getTitle(), req.getContent(), req.getTagIds());
|
||||||
draftService.deleteDraft(auth.getName());
|
draftService.deleteDraft(auth.getName());
|
||||||
return ResponseEntity.ok(toDto(post));
|
PostDto dto = toDto(post);
|
||||||
|
dto.setReward(levelService.awardForPost(auth.getName()));
|
||||||
|
return ResponseEntity.ok(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
@@ -187,6 +191,7 @@ public class PostController {
|
|||||||
|
|
||||||
java.time.LocalDateTime last = commentService.getLastCommentTime(post.getId());
|
java.time.LocalDateTime last = commentService.getLastCommentTime(post.getId());
|
||||||
dto.setLastReplyAt(last != null ? last : post.getCreatedAt());
|
dto.setLastReplyAt(last != null ? last : post.getCreatedAt());
|
||||||
|
dto.setReward(0);
|
||||||
|
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
@@ -223,6 +228,7 @@ public class PostController {
|
|||||||
dto.setContent(comment.getContent());
|
dto.setContent(comment.getContent());
|
||||||
dto.setCreatedAt(comment.getCreatedAt());
|
dto.setCreatedAt(comment.getCreatedAt());
|
||||||
dto.setAuthor(toAuthorDto(comment.getAuthor()));
|
dto.setAuthor(toAuthorDto(comment.getAuthor()));
|
||||||
|
dto.setReward(0);
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,6 +243,7 @@ public class PostController {
|
|||||||
if (reaction.getComment() != null) {
|
if (reaction.getComment() != null) {
|
||||||
dto.setCommentId(reaction.getComment().getId());
|
dto.setCommentId(reaction.getComment().getId());
|
||||||
}
|
}
|
||||||
|
dto.setReward(0);
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,6 +301,7 @@ public class PostController {
|
|||||||
private List<ReactionDto> reactions;
|
private List<ReactionDto> reactions;
|
||||||
private java.util.List<AuthorDto> participants;
|
private java.util.List<AuthorDto> participants;
|
||||||
private boolean subscribed;
|
private boolean subscribed;
|
||||||
|
private int reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -329,6 +337,7 @@ public class PostController {
|
|||||||
private AuthorDto author;
|
private AuthorDto author;
|
||||||
private List<CommentDto> replies;
|
private List<CommentDto> replies;
|
||||||
private List<ReactionDto> reactions;
|
private List<ReactionDto> reactions;
|
||||||
|
private int reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -338,5 +347,6 @@ public class PostController {
|
|||||||
private String user;
|
private String user;
|
||||||
private Long postId;
|
private Long postId;
|
||||||
private Long commentId;
|
private Long commentId;
|
||||||
|
private int reward;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.openisle.controller;
|
|||||||
import com.openisle.model.Reaction;
|
import com.openisle.model.Reaction;
|
||||||
import com.openisle.model.ReactionType;
|
import com.openisle.model.ReactionType;
|
||||||
import com.openisle.service.ReactionService;
|
import com.openisle.service.ReactionService;
|
||||||
|
import com.openisle.service.LevelService;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ReactionController {
|
public class ReactionController {
|
||||||
private final ReactionService reactionService;
|
private final ReactionService reactionService;
|
||||||
|
private final LevelService levelService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all available reaction types.
|
* Get all available reaction types.
|
||||||
@@ -31,7 +33,9 @@ public class ReactionController {
|
|||||||
if (reaction == null) {
|
if (reaction == null) {
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(toDto(reaction));
|
ReactionDto dto = toDto(reaction);
|
||||||
|
dto.setReward(levelService.awardForReaction(auth.getName()));
|
||||||
|
return ResponseEntity.ok(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/comments/{commentId}/reactions")
|
@PostMapping("/comments/{commentId}/reactions")
|
||||||
@@ -42,7 +46,9 @@ public class ReactionController {
|
|||||||
if (reaction == null) {
|
if (reaction == null) {
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(toDto(reaction));
|
ReactionDto dto = toDto(reaction);
|
||||||
|
dto.setReward(levelService.awardForReaction(auth.getName()));
|
||||||
|
return ResponseEntity.ok(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ReactionDto toDto(Reaction reaction) {
|
private ReactionDto toDto(Reaction reaction) {
|
||||||
@@ -56,6 +62,7 @@ public class ReactionController {
|
|||||||
if (reaction.getComment() != null) {
|
if (reaction.getComment() != null) {
|
||||||
dto.setCommentId(reaction.getComment().getId());
|
dto.setCommentId(reaction.getComment().getId());
|
||||||
}
|
}
|
||||||
|
dto.setReward(0);
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,5 +78,6 @@ public class ReactionController {
|
|||||||
private String user;
|
private String user;
|
||||||
private Long postId;
|
private Long postId;
|
||||||
private Long commentId;
|
private Long commentId;
|
||||||
|
private int reward;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ public class UserController {
|
|||||||
private final SubscriptionService subscriptionService;
|
private final SubscriptionService subscriptionService;
|
||||||
private final PostReadService postReadService;
|
private final PostReadService postReadService;
|
||||||
private final UserVisitService userVisitService;
|
private final UserVisitService userVisitService;
|
||||||
|
private final LevelService levelService;
|
||||||
|
|
||||||
@Value("${app.upload.check-type:true}")
|
@Value("${app.upload.check-type:true}")
|
||||||
private boolean checkImageType;
|
private boolean checkImageType;
|
||||||
@@ -78,6 +79,12 @@ public class UserController {
|
|||||||
return ResponseEntity.ok(toDto(user, auth));
|
return ResponseEntity.ok(toDto(user, auth));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/me/signin")
|
||||||
|
public Map<String, Integer> signIn(Authentication auth) {
|
||||||
|
int reward = levelService.awardForSignin(auth.getName());
|
||||||
|
return Map.of("reward", reward);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/{identifier}")
|
@GetMapping("/{identifier}")
|
||||||
public ResponseEntity<UserDto> getUser(@PathVariable("identifier") String identifier,
|
public ResponseEntity<UserDto> getUser(@PathVariable("identifier") String identifier,
|
||||||
Authentication auth) {
|
Authentication auth) {
|
||||||
@@ -223,6 +230,9 @@ public class UserController {
|
|||||||
dto.setReadPosts(postReadService.countReads(user.getUsername()));
|
dto.setReadPosts(postReadService.countReads(user.getUsername()));
|
||||||
dto.setLikesSent(reactionService.countLikesSent(user.getUsername()));
|
dto.setLikesSent(reactionService.countLikesSent(user.getUsername()));
|
||||||
dto.setLikesReceived(reactionService.countLikesReceived(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) {
|
if (viewer != null) {
|
||||||
dto.setSubscribed(subscriptionService.isSubscribed(viewer.getName(), user.getUsername()));
|
dto.setSubscribed(subscriptionService.isSubscribed(viewer.getName(), user.getUsername()));
|
||||||
} else {
|
} else {
|
||||||
@@ -288,6 +298,9 @@ public class UserController {
|
|||||||
private long likesSent;
|
private long likesSent;
|
||||||
private long likesReceived;
|
private long likesReceived;
|
||||||
private boolean subscribed;
|
private boolean subscribed;
|
||||||
|
private int experience;
|
||||||
|
private int currentLevel;
|
||||||
|
private int nextLevelExp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import jakarta.persistence.*;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import org.hibernate.annotations.CreationTimestamp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reaction entity representing a user's reaction to a post or comment.
|
* Reaction entity representing a user's reaction to a post or comment.
|
||||||
@@ -37,4 +38,9 @@ public class Reaction {
|
|||||||
@ManyToOne(fetch = FetchType.LAZY)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "comment_id")
|
@JoinColumn(name = "comment_id")
|
||||||
private Comment comment;
|
private Comment comment;
|
||||||
|
|
||||||
|
@CreationTimestamp
|
||||||
|
@Column(nullable = false, updatable = false,
|
||||||
|
columnDefinition = "DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6)")
|
||||||
|
private java.time.LocalDateTime createdAt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ public class User {
|
|||||||
|
|
||||||
private String avatar;
|
private String avatar;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private int experience = 0;
|
||||||
|
|
||||||
@Column(length = 1000)
|
@Column(length = 1000)
|
||||||
private String introduction;
|
private String introduction;
|
||||||
|
|
||||||
|
|||||||
@@ -19,4 +19,8 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
|
|||||||
|
|
||||||
@org.springframework.data.jpa.repository.Query("SELECT MAX(c.createdAt) FROM Comment c WHERE c.post = :post")
|
@org.springframework.data.jpa.repository.Query("SELECT MAX(c.createdAt) FROM Comment c WHERE c.post = :post")
|
||||||
java.time.LocalDateTime findLastCommentTime(@org.springframework.data.repository.query.Param("post") Post post);
|
java.time.LocalDateTime findLastCommentTime(@org.springframework.data.repository.query.Param("post") Post post);
|
||||||
|
|
||||||
|
@org.springframework.data.jpa.repository.Query("SELECT COUNT(c) FROM Comment c WHERE c.author.username = :username AND c.createdAt >= :start")
|
||||||
|
long countByAuthorAfter(@org.springframework.data.repository.query.Param("username") String username,
|
||||||
|
@org.springframework.data.repository.query.Param("start") java.time.LocalDateTime start);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,6 +87,9 @@ public interface PostRepository extends JpaRepository<Post, Long> {
|
|||||||
@Query("SELECT SUM(p.views) FROM Post p WHERE p.author.username = :username AND p.status = com.openisle.model.PostStatus.PUBLISHED")
|
@Query("SELECT SUM(p.views) FROM Post p WHERE p.author.username = :username AND p.status = com.openisle.model.PostStatus.PUBLISHED")
|
||||||
Long sumViews(@Param("username") String username);
|
Long sumViews(@Param("username") String username);
|
||||||
|
|
||||||
|
@Query("SELECT COUNT(p) FROM Post p WHERE p.author.username = :username AND p.createdAt >= :start")
|
||||||
|
long countByAuthorAfter(@Param("username") String username, @Param("start") java.time.LocalDateTime start);
|
||||||
|
|
||||||
long countByCategory_Id(Long categoryId);
|
long countByCategory_Id(Long categoryId);
|
||||||
|
|
||||||
long countDistinctByTags_Id(Long tagId);
|
long countDistinctByTags_Id(Long tagId);
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ public interface ReactionRepository extends JpaRepository<Reaction, Long> {
|
|||||||
@Query("SELECT COUNT(r) FROM Reaction r WHERE r.user.username = :username AND r.type = com.openisle.model.ReactionType.LIKE")
|
@Query("SELECT COUNT(r) FROM Reaction r WHERE r.user.username = :username AND r.type = com.openisle.model.ReactionType.LIKE")
|
||||||
long countLikesSent(@Param("username") String username);
|
long countLikesSent(@Param("username") String username);
|
||||||
|
|
||||||
|
@Query("SELECT COUNT(r) FROM Reaction r WHERE r.user.username = :username AND r.createdAt >= :start")
|
||||||
|
long countByUserAfter(@Param("username") String username, @Param("start") java.time.LocalDateTime start);
|
||||||
|
|
||||||
@Query("SELECT COUNT(r) FROM Reaction r WHERE r.type = com.openisle.model.ReactionType.LIKE AND ((r.post IS NOT NULL AND r.post.author.username = :username) OR (r.comment IS NOT NULL AND r.comment.author.username = :username))")
|
@Query("SELECT COUNT(r) FROM Reaction r WHERE r.type = com.openisle.model.ReactionType.LIKE AND ((r.post IS NOT NULL AND r.post.author.username = :username) OR (r.comment IS NOT NULL AND r.comment.author.username = :username))")
|
||||||
long countLikesReceived(@Param("username") String username);
|
long countLikesReceived(@Param("username") String username);
|
||||||
}
|
}
|
||||||
|
|||||||
76
src/main/java/com/openisle/service/LevelService.java
Normal file
76
src/main/java/com/openisle/service/LevelService.java
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package com.openisle.service;
|
||||||
|
|
||||||
|
import com.openisle.model.User;
|
||||||
|
import com.openisle.repository.CommentRepository;
|
||||||
|
import com.openisle.repository.PostRepository;
|
||||||
|
import com.openisle.repository.ReactionRepository;
|
||||||
|
import com.openisle.repository.UserRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class LevelService {
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
private final PostRepository postRepository;
|
||||||
|
private final CommentRepository commentRepository;
|
||||||
|
private final ReactionRepository reactionRepository;
|
||||||
|
private final UserVisitService userVisitService;
|
||||||
|
|
||||||
|
private static final int[] LEVEL_EXP = {100,200,300,600,1200,10000};
|
||||||
|
|
||||||
|
private int addExperience(User user, int amount) {
|
||||||
|
user.setExperience(user.getExperience() + amount);
|
||||||
|
userRepository.save(user);
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int awardForPost(String username) {
|
||||||
|
User user = userRepository.findByUsername(username).orElseThrow();
|
||||||
|
LocalDateTime start = LocalDate.now().atStartOfDay();
|
||||||
|
long count = postRepository.countByAuthorAfter(username, start);
|
||||||
|
if (count >= 1) return 0;
|
||||||
|
return addExperience(user,30);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int awardForComment(String username) {
|
||||||
|
User user = userRepository.findByUsername(username).orElseThrow();
|
||||||
|
LocalDateTime start = LocalDate.now().atStartOfDay();
|
||||||
|
long count = commentRepository.countByAuthorAfter(username, start);
|
||||||
|
if (count >= 3) return 0;
|
||||||
|
return addExperience(user,10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int awardForReaction(String username) {
|
||||||
|
User user = userRepository.findByUsername(username).orElseThrow();
|
||||||
|
LocalDateTime start = LocalDate.now().atStartOfDay();
|
||||||
|
long count = reactionRepository.countByUserAfter(username, start);
|
||||||
|
if (count >= 3) return 0;
|
||||||
|
return addExperience(user,5);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int awardForSignin(String username) {
|
||||||
|
boolean first = userVisitService.recordVisit(username);
|
||||||
|
if (!first) return 0;
|
||||||
|
User user = userRepository.findByUsername(username).orElseThrow();
|
||||||
|
return addExperience(user,5);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLevel(int exp) {
|
||||||
|
int level = 0;
|
||||||
|
for (int t : LEVEL_EXP) {
|
||||||
|
if (exp >= t) level++; else break;
|
||||||
|
}
|
||||||
|
return level;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int nextLevelExp(int exp) {
|
||||||
|
for (int t : LEVEL_EXP) {
|
||||||
|
if (exp < t) return t;
|
||||||
|
}
|
||||||
|
return LEVEL_EXP[LEVEL_EXP.length-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,15 +17,16 @@ public class UserVisitService {
|
|||||||
private final UserVisitRepository userVisitRepository;
|
private final UserVisitRepository userVisitRepository;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
public void recordVisit(String username) {
|
public boolean recordVisit(String username) {
|
||||||
User user = userRepository.findByUsername(username)
|
User user = userRepository.findByUsername(username)
|
||||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
||||||
LocalDate today = LocalDate.now();
|
LocalDate today = LocalDate.now();
|
||||||
userVisitRepository.findByUserAndVisitDate(user, today).orElseGet(() -> {
|
return userVisitRepository.findByUserAndVisitDate(user, today).map(v -> false).orElseGet(() -> {
|
||||||
UserVisit visit = new UserVisit();
|
UserVisit visit = new UserVisit();
|
||||||
visit.setUser(user);
|
visit.setUser(user);
|
||||||
visit.setVisitDate(today);
|
visit.setVisitDate(today);
|
||||||
return userVisitRepository.save(visit);
|
userVisitRepository.save(visit);
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.openisle.model.Post;
|
|||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
import com.openisle.service.CommentService;
|
import com.openisle.service.CommentService;
|
||||||
import com.openisle.service.CaptchaService;
|
import com.openisle.service.CaptchaService;
|
||||||
|
import com.openisle.service.LevelService;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -33,6 +34,8 @@ class CommentControllerTest {
|
|||||||
private CommentService commentService;
|
private CommentService commentService;
|
||||||
@MockBean
|
@MockBean
|
||||||
private CaptchaService captchaService;
|
private CaptchaService captchaService;
|
||||||
|
@MockBean
|
||||||
|
private LevelService levelService;
|
||||||
|
|
||||||
private Comment createComment(Long id, String content, String authorName) {
|
private Comment createComment(Long id, String content, String authorName) {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import com.openisle.service.CommentService;
|
|||||||
import com.openisle.service.ReactionService;
|
import com.openisle.service.ReactionService;
|
||||||
import com.openisle.service.CaptchaService;
|
import com.openisle.service.CaptchaService;
|
||||||
import com.openisle.service.DraftService;
|
import com.openisle.service.DraftService;
|
||||||
|
import com.openisle.service.LevelService;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -50,6 +51,8 @@ class PostControllerTest {
|
|||||||
private CaptchaService captchaService;
|
private CaptchaService captchaService;
|
||||||
@MockBean
|
@MockBean
|
||||||
private DraftService draftService;
|
private DraftService draftService;
|
||||||
|
@MockBean
|
||||||
|
private LevelService levelService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createAndGetPost() throws Exception {
|
void createAndGetPost() throws Exception {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.openisle.model.Reaction;
|
|||||||
import com.openisle.model.ReactionType;
|
import com.openisle.model.ReactionType;
|
||||||
import com.openisle.model.User;
|
import com.openisle.model.User;
|
||||||
import com.openisle.service.ReactionService;
|
import com.openisle.service.ReactionService;
|
||||||
|
import com.openisle.service.LevelService;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -29,6 +30,8 @@ class ReactionControllerTest {
|
|||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private ReactionService reactionService;
|
private ReactionService reactionService;
|
||||||
|
@MockBean
|
||||||
|
private LevelService levelService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void reactToPost() throws Exception {
|
void reactToPost() throws Exception {
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ class UserControllerTest {
|
|||||||
private PostService postService;
|
private PostService postService;
|
||||||
@MockBean
|
@MockBean
|
||||||
private CommentService commentService;
|
private CommentService commentService;
|
||||||
|
@MockBean
|
||||||
|
private LevelService levelService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getCurrentUser() throws Exception {
|
void getCurrentUser() throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user