mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 06:50:53 +08:00
feat: add follow unsubscribe integration
This commit is contained in:
@@ -42,7 +42,7 @@ public class UserController {
|
||||
@GetMapping("/me")
|
||||
public ResponseEntity<UserDto> me(Authentication auth) {
|
||||
User user = userService.findByUsername(auth.getName()).orElseThrow();
|
||||
return ResponseEntity.ok(toDto(user));
|
||||
return ResponseEntity.ok(toDto(user, auth));
|
||||
}
|
||||
|
||||
@PostMapping("/me/avatar")
|
||||
@@ -68,13 +68,14 @@ public class UserController {
|
||||
public ResponseEntity<UserDto> updateProfile(@RequestBody UpdateProfileDto dto,
|
||||
Authentication auth) {
|
||||
User user = userService.updateProfile(auth.getName(), dto.getUsername(), dto.getIntroduction());
|
||||
return ResponseEntity.ok(toDto(user));
|
||||
return ResponseEntity.ok(toDto(user, auth));
|
||||
}
|
||||
|
||||
@GetMapping("/{identifier}")
|
||||
public ResponseEntity<UserDto> getUser(@PathVariable("identifier") String identifier) {
|
||||
public ResponseEntity<UserDto> getUser(@PathVariable("identifier") String identifier,
|
||||
Authentication auth) {
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
return ResponseEntity.ok(toDto(user));
|
||||
return ResponseEntity.ok(toDto(user, auth));
|
||||
}
|
||||
|
||||
@GetMapping("/{identifier}/posts")
|
||||
@@ -138,7 +139,8 @@ public class UserController {
|
||||
@GetMapping("/{identifier}/all")
|
||||
public ResponseEntity<UserAggregateDto> userAggregate(@PathVariable("identifier") String identifier,
|
||||
@RequestParam(value = "postsLimit", required = false) Integer postsLimit,
|
||||
@RequestParam(value = "repliesLimit", required = false) Integer repliesLimit) {
|
||||
@RequestParam(value = "repliesLimit", required = false) Integer repliesLimit,
|
||||
Authentication auth) {
|
||||
User user = userService.findByIdentifier(identifier).orElseThrow();
|
||||
int pLimit = postsLimit != null ? postsLimit : defaultPostsLimit;
|
||||
int rLimit = repliesLimit != null ? repliesLimit : defaultRepliesLimit;
|
||||
@@ -149,13 +151,13 @@ public class UserController {
|
||||
.map(this::toCommentInfoDto)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
UserAggregateDto dto = new UserAggregateDto();
|
||||
dto.setUser(toDto(user));
|
||||
dto.setUser(toDto(user, auth));
|
||||
dto.setPosts(posts);
|
||||
dto.setReplies(replies);
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
private UserDto toDto(User user) {
|
||||
private UserDto toDto(User user, Authentication viewer) {
|
||||
UserDto dto = new UserDto();
|
||||
dto.setId(user.getId());
|
||||
dto.setUsername(user.getUsername());
|
||||
@@ -168,9 +170,18 @@ public class UserController {
|
||||
dto.setCreatedAt(user.getCreatedAt());
|
||||
dto.setLastPostTime(postService.getLastPostTime(user.getUsername()));
|
||||
dto.setTotalViews(postService.getTotalViews(user.getUsername()));
|
||||
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());
|
||||
@@ -219,6 +230,7 @@ public class UserController {
|
||||
private java.time.LocalDateTime createdAt;
|
||||
private java.time.LocalDateTime lastPostTime;
|
||||
private long totalViews;
|
||||
private boolean subscribed;
|
||||
}
|
||||
|
||||
@Data
|
||||
|
||||
@@ -99,4 +99,13 @@ public class SubscriptionService {
|
||||
User user = userRepo.findByUsername(username).orElseThrow();
|
||||
return userSubRepo.countBySubscriber(user);
|
||||
}
|
||||
|
||||
public boolean isSubscribed(String subscriberName, String targetName) {
|
||||
if (subscriberName == null || targetName == null || subscriberName.equals(targetName)) {
|
||||
return false;
|
||||
}
|
||||
User subscriber = userRepo.findByUsername(subscriberName).orElseThrow();
|
||||
User target = userRepo.findByUsername(targetName).orElseThrow();
|
||||
return userSubRepo.findBySubscriberAndTarget(subscriber, target).isPresent();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user