diff --git a/src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/user/controllers/UserController.java b/src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/user/controllers/UserController.java index 46f2836c..42d9074e 100644 --- a/src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/user/controllers/UserController.java +++ b/src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/user/controllers/UserController.java @@ -11,6 +11,7 @@ import com.sublinks.sublinksapi.api.lemmy.v3.user.models.GetPersonMentions; import com.sublinks.sublinksapi.api.lemmy.v3.user.models.GetPersonMentionsResponse; import com.sublinks.sublinksapi.api.lemmy.v3.user.models.GetRepliesResponse; +import com.sublinks.sublinksapi.api.lemmy.v3.user.models.GetUnreadCount; import com.sublinks.sublinksapi.api.lemmy.v3.user.models.GetUnreadCountResponse; import com.sublinks.sublinksapi.api.lemmy.v3.user.models.MarkPersonMentionAsRead; import com.sublinks.sublinksapi.api.lemmy.v3.user.models.PersonMentionResponse; @@ -31,6 +32,7 @@ import com.sublinks.sublinksapi.person.repositories.PersonMentionRepository; import com.sublinks.sublinksapi.person.repositories.PersonRepository; import com.sublinks.sublinksapi.person.services.PersonService; +import com.sublinks.sublinksapi.privatemessages.repositories.PrivateMessageRepository; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; @@ -71,6 +73,7 @@ public class UserController { private final LemmyPersonMentionService lemmyPersonMentionService; private final CommentReplyRepository commentReplyRepository; private final LemmyCommentReplyService lemmyCommentReplyService; + private final PrivateMessageRepository privateMessageRepository; @Operation(summary = "Get the details for a person.") @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK", content = { @@ -149,8 +152,8 @@ GetRepliesResponse replies(@Valid final GetReplies getReplies, JwtPerson princip final List commentReplies = commentReplyRepository.allCommentReplysBySearchCriteria( com.sublinks.sublinksapi.comment.models.CommentReplySearchCriteria.builder() - .sortType(getReplies.sort()).unreadOnly(getReplies.unread_only() != null - && getReplies.unread_only()) + .sortType(getReplies.sort()) + .unreadOnly(getReplies.unread_only() != null && getReplies.unread_only()) .page(getReplies.page()).perPage(getReplies.limit()).build()); final List commentReplyViews = new ArrayList<>(); @@ -246,8 +249,18 @@ public LoginResponse saveUserSettings(@Valid @RequestBody SaveUserSettings saveU @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK", content = { @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = GetUnreadCountResponse.class))})}) @GetMapping("unread_count") - GetUnreadCountResponse unreadCount() { + GetUnreadCountResponse unreadCount(@Valid final GetUnreadCount getUnreadCountForm, + JwtPerson principal) { + + final Person person = Optional.ofNullable((Person) principal.getPrincipal()) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED)); + + GetUnreadCountResponse.GetUnreadCountResponseBuilder builder = GetUnreadCountResponse.builder(); + + builder.mentions((int) personMentionRepository.countByRecipientAndIsReadFalse(person)); + builder.replies((int) commentReplyRepository.countByRecipientAndIsReadFalse(person)); + builder.private_messages((int) privateMessageRepository.countByRecipientAndIsReadFalse(person)); - return GetUnreadCountResponse.builder().build(); + return builder.build(); } } diff --git a/src/main/java/com/sublinks/sublinksapi/comment/repositories/CommentReplyRepository.java b/src/main/java/com/sublinks/sublinksapi/comment/repositories/CommentReplyRepository.java index b50ba9c1..68bbae27 100644 --- a/src/main/java/com/sublinks/sublinksapi/comment/repositories/CommentReplyRepository.java +++ b/src/main/java/com/sublinks/sublinksapi/comment/repositories/CommentReplyRepository.java @@ -1,10 +1,13 @@ package com.sublinks.sublinksapi.comment.repositories; import com.sublinks.sublinksapi.comment.dto.CommentReply; +import com.sublinks.sublinksapi.person.dto.Person; import org.springframework.data.jpa.repository.JpaRepository; import java.util.Optional; -public interface CommentReplyRepository extends JpaRepository, CommentReplyRepositorySearch { +public interface CommentReplyRepository extends JpaRepository, + CommentReplyRepositorySearch { + long countByRecipientAndIsReadFalse(Person recipient); } diff --git a/src/main/java/com/sublinks/sublinksapi/person/repositories/PersonMentionRepository.java b/src/main/java/com/sublinks/sublinksapi/person/repositories/PersonMentionRepository.java index 4ede5d5e..b4450dfc 100644 --- a/src/main/java/com/sublinks/sublinksapi/person/repositories/PersonMentionRepository.java +++ b/src/main/java/com/sublinks/sublinksapi/person/repositories/PersonMentionRepository.java @@ -1,9 +1,11 @@ package com.sublinks.sublinksapi.person.repositories; +import com.sublinks.sublinksapi.person.dto.Person; import com.sublinks.sublinksapi.person.dto.PersonMention; import org.springframework.data.jpa.repository.JpaRepository; public interface PersonMentionRepository extends JpaRepository, PersonMentionRepositorySearch { + long countByRecipientAndIsReadFalse(Person recipient); } diff --git a/src/main/java/com/sublinks/sublinksapi/privatemessages/repositories/PrivateMessageRepository.java b/src/main/java/com/sublinks/sublinksapi/privatemessages/repositories/PrivateMessageRepository.java index 600fd0b9..5a85b7af 100644 --- a/src/main/java/com/sublinks/sublinksapi/privatemessages/repositories/PrivateMessageRepository.java +++ b/src/main/java/com/sublinks/sublinksapi/privatemessages/repositories/PrivateMessageRepository.java @@ -1,9 +1,12 @@ package com.sublinks.sublinksapi.privatemessages.repositories; +import com.sublinks.sublinksapi.person.dto.Person; import com.sublinks.sublinksapi.privatemessages.dto.PrivateMessage; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; public interface PrivateMessageRepository extends JpaRepository, PrivateMessageRepositorySearch { + long countByRecipientAndIsReadFalse(Person recipient); }