Skip to content

Commit

Permalink
Merge pull request #34 from participating-online/feature/29-report-count
Browse files Browse the repository at this point in the history
Implemented reports_count
  • Loading branch information
jgrim authored Nov 25, 2023
2 parents 7d6f369 + d5b9ce1 commit 59cbdfd
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,49 @@ ListCommentReportsResponse reportList(@Valid ListCommentReports listCommentRepor

if (!isAdmin) {
final List<Community> moderatingCommunities = new ArrayList<>();
moderatingCommunities.addAll(
linkPersonCommunityService.getPersonLinkByType(person, LinkPersonCommunityType.owner));
moderatingCommunities.addAll(linkPersonCommunityService.getPersonLinkByType(person,
LinkPersonCommunityType.moderator));

if (listCommentReportsForm.community_id() == null) {

moderatingCommunities.addAll(
linkPersonCommunityService.getPersonLinkByType(person, LinkPersonCommunityType.owner));
moderatingCommunities.addAll(linkPersonCommunityService.getPersonLinkByType(person,
LinkPersonCommunityType.moderator));
} else {
Community community = communityRepository.findById(
(long) listCommentReportsForm.community_id()).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "community_not_found"));
if (!linkPersonCommunityService.hasLink(person, community, LinkPersonCommunityType.owner)
&& !linkPersonCommunityService.hasLink(person, community,
LinkPersonCommunityType.moderator)) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
}
moderatingCommunities.add(community);
}
commentReports.addAll(commentReportRepository.allCommentReportsBySearchCriteria(
CommentReportSearchCriteria.builder().unresolvedOnly(
listCommentReportsForm.unresolved_only() == null
|| listCommentReportsForm.unresolved_only())
.perPage(listCommentReportsForm.limit()).page(listCommentReportsForm.page())
.community(moderatingCommunities).build()));
} else {
commentReports.addAll(commentReportRepository.allCommentReportsBySearchCriteria(
CommentReportSearchCriteria.builder().unresolvedOnly(
listCommentReportsForm.unresolved_only() != null
&& listCommentReportsForm.unresolved_only())
.perPage(listCommentReportsForm.limit()).page(listCommentReportsForm.page())
.build()));
if (listCommentReportsForm.community_id() != null) {
Community community = communityRepository.findById(
(long) listCommentReportsForm.community_id()).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "community_not_found"));
commentReports.addAll(commentReportRepository.allCommentReportsBySearchCriteria(
CommentReportSearchCriteria.builder().unresolvedOnly(
listCommentReportsForm.unresolved_only() != null
&& listCommentReportsForm.unresolved_only())
.perPage(listCommentReportsForm.limit()).page(listCommentReportsForm.page())
.community(List.of(community)).build()));
} else {
commentReports.addAll(commentReportRepository.allCommentReportsBySearchCriteria(
CommentReportSearchCriteria.builder().unresolvedOnly(
listCommentReportsForm.unresolved_only() != null
&& listCommentReportsForm.unresolved_only())
.perPage(listCommentReportsForm.limit()).page(listCommentReportsForm.page())
.build()));
}
}

List<CommentReportView> commentReportViews = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import lombok.RequiredArgsConstructor;
import org.springframework.core.convert.ConversionService;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -273,14 +275,11 @@ PostReportResponse report(@Valid @RequestBody final CreatePostReport createPostR
final Post post = postRepository.findById((long) createPostReportForm.post_id())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "post_not_found"));

final PostReport postReport = PostReport.builder()
.post(post)
.creator(person)
final PostReport postReport = PostReport.builder().post(post).creator(person)
.reason(createPostReportForm.reason())
.originalBody(post.getPostBody())
.originalTitle(post.getTitle())
.originalUrl(post.getLinkUrl())
.build();
.originalBody(post.getPostBody() == null ? "" : post.getPostBody())
.originalTitle(post.getTitle() == null ? "" : post.getTitle())
.originalUrl(post.getLinkUrl() == null ? "" : post.getLinkUrl()).build();

postReportService.createPostReport(postReport);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package com.sublinks.sublinksapi.api.lemmy.v3.user.controllers;

import com.sublinks.sublinksapi.api.lemmy.v3.authentication.JwtPerson;
import com.sublinks.sublinksapi.api.lemmy.v3.common.controllers.AbstractLemmyApiController;
import com.sublinks.sublinksapi.api.lemmy.v3.community.models.GetReportCount;
import com.sublinks.sublinksapi.api.lemmy.v3.site.models.GetSiteResponse;
import com.sublinks.sublinksapi.api.lemmy.v3.user.models.BanPersonResponse;
import com.sublinks.sublinksapi.api.lemmy.v3.user.models.BlockPersonResponse;
import com.sublinks.sublinksapi.api.lemmy.v3.user.models.GetReportCountResponse;
import com.sublinks.sublinksapi.authorization.services.AuthorizationService;
import com.sublinks.sublinksapi.comment.repositories.CommentReportRepository;
import com.sublinks.sublinksapi.community.dto.Community;
import com.sublinks.sublinksapi.community.repositories.CommunityRepository;
import com.sublinks.sublinksapi.person.dto.LinkPersonCommunity;
import com.sublinks.sublinksapi.person.dto.Person;
import com.sublinks.sublinksapi.person.enums.LinkPersonCommunityType;
import com.sublinks.sublinksapi.person.services.LinkPersonCommunityService;
import com.sublinks.sublinksapi.post.repositories.PostReportRepository;
import com.sublinks.sublinksapi.privatemessages.repositories.PrivateMessageReportRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -18,55 +32,108 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping(path = "/api/v3/user")
@Tag(name = "User")
public class UserModActionsController {
public class UserModActionsController extends AbstractLemmyApiController {

private final CommentReportRepository commentReportRepository;
private final PostReportRepository postReportRepository;
private final PrivateMessageReportRepository privateMessageReportRepository;
private final CommunityRepository communityRepository;
private final LinkPersonCommunityService linkPersonCommunityService;
private final AuthorizationService authorizationService;

@Operation(summary = "Ban a person from your site.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK",
content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = BanPersonResponse.class))})
})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = BanPersonResponse.class))})})
@PostMapping("ban")
BanPersonResponse ban() {

return BanPersonResponse.builder().build();
}

@Operation(summary = "Block a person.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK",
content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = BlockPersonResponse.class))})
})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = BlockPersonResponse.class))})})
@PostMapping("block")
BlockPersonResponse block() {

return BlockPersonResponse.builder().build();
}

@Operation(summary = "Get counts for your reports.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK",
content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = GetReportCountResponse.class))})
})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = GetReportCountResponse.class))}),
@ApiResponse(responseCode = "404", description = "Community not found", content = @Content)})
@GetMapping("report_count")
GetReportCountResponse reportCount() {
GetReportCountResponse reportCount(@Valid final GetReportCount getReportCount,
JwtPerson principal) {

final Person person = getPersonOrThrowUnauthorized(principal);

final GetReportCountResponse.GetReportCountResponseBuilder builder = GetReportCountResponse.builder();

if (getReportCount.community_id() != null) {
final Community community = communityRepository.findById((long) getReportCount.community_id())
.orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "coomunity_not_found"));

final boolean isModOfCommunity =
linkPersonCommunityService.hasLink(person, community, LinkPersonCommunityType.moderator)
|| linkPersonCommunityService.hasLink(person, community,
LinkPersonCommunityType.owner);

if (!isModOfCommunity) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
}

builder.comment_reports(
(int) commentReportRepository.countAllCommentReportsByResolvedFalseAndCommunity(
List.of(community)));
builder.post_reports(
(int) postReportRepository.countAllPostReportsByResolvedFalseAndCommunity(
List.of(community)));
builder.private_message_reports(0);
} else {

final boolean isAdmin = authorizationService.isAdmin(person);

if (isAdmin) {
builder.comment_reports(
(int) commentReportRepository.countAllCommentReportsByResolvedFalse());
builder.post_reports(
(int) postReportRepository.countAllPostReportsReportsByResolvedFalse());
builder.private_message_reports(
(int) privateMessageReportRepository.countAllPrivateMessageReportsByResolvedFalse());
} else {
List<Community> communities = new ArrayList<>();

communities.addAll(linkPersonCommunityService.getPersonLinkByType(person,
LinkPersonCommunityType.moderator));
communities.addAll(
linkPersonCommunityService.getPersonLinkByType(person, LinkPersonCommunityType.owner));

builder.comment_reports(
(int) commentReportRepository.countAllCommentReportsByResolvedFalseAndCommunity(
communities));
builder.post_reports(
(int) postReportRepository.countAllPostReportsByResolvedFalseAndCommunity(communities));

builder.private_message_reports(0);
}
}

return GetReportCountResponse.builder().build();
return builder.build();
}

@Operation(summary = "Leave the Site admins.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK",
content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = GetSiteResponse.class))})
})
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = GetSiteResponse.class))})})
@PostMapping("leave_admin")
GetSiteResponse leaveAdmin() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sublinks.sublinksapi.comment.dto.CommentReply;
import com.sublinks.sublinksapi.comment.models.CommentReplySearchCriteria;
import com.sublinks.sublinksapi.community.dto.Community;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.sublinks.sublinksapi.comment.dto.CommentReport;
import com.sublinks.sublinksapi.comment.models.CommentReplySearchCriteria;
import com.sublinks.sublinksapi.comment.models.CommentReportSearchCriteria;
import com.sublinks.sublinksapi.community.dto.Community;
import java.util.List;

public interface CommentReplyRepositorySearch {

List<CommentReply> allCommentReplysBySearchCriteria(
CommentReplySearchCriteria commentReplySearchCriteria);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.sublinks.sublinksapi.comment.dto.CommentReport;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommentReportRepository extends JpaRepository<CommentReport, Long>, CommentReportRepositorySearch {
public interface CommentReportRepository extends JpaRepository<CommentReport, Long>,
CommentReportRepositorySearch {

}

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.sublinks.sublinksapi.comment.repositories;

import com.sublinks.sublinksapi.comment.dto.Comment;
import com.sublinks.sublinksapi.comment.dto.CommentRead;
import com.sublinks.sublinksapi.comment.dto.CommentReport;
import com.sublinks.sublinksapi.comment.models.CommentReportSearchCriteria;
import com.sublinks.sublinksapi.community.dto.Community;
import jakarta.annotation.Nullable;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
Expand All @@ -28,25 +29,33 @@ public List<CommentReport> allCommentReportsBySearchCriteria(
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<CommentReport> cq = cb.createQuery(CommentReport.class);

final Root<CommentReport> commentTable = cq.from(CommentReport.class);
final Root<CommentReport> commentReportTable = cq.from(CommentReport.class);

final List<Predicate> predicates = new ArrayList<>();

if (commentReportSearchCriteria.unresolvedOnly()) {
predicates.add(cb.equal(commentTable.get("resolved"), false));
predicates.add(cb.equal(commentReportTable.get("resolved"), false));
}

if (commentReportSearchCriteria.community() != null) {
// Join Comment and check community id
final Join<CommentReport, Comment> commentJoin = commentTable.join("comment", JoinType.LEFT);
final Join<CommentReport, Comment> commentJoin = commentReportTable.join("comment",
JoinType.LEFT);

List<Predicate> communityPredicates = new ArrayList<>();

commentReportSearchCriteria.community().forEach(community -> {
predicates.add(cb.equal(commentJoin.get("community"), community));
// Add a Or condition for each community

communityPredicates.add(cb.equal(commentJoin.get("community"), community));
});

predicates.add(cb.or(communityPredicates.toArray(new Predicate[0])));
}

cq.where(predicates.toArray(new Predicate[0]));

cq.orderBy(cb.desc(commentTable.get("createdAt")));
cq.orderBy(cb.desc(commentReportTable.get("createdAt")));

int perPage = Math.min(Math.abs(commentReportSearchCriteria.perPage()), 20);
int page = Math.max(commentReportSearchCriteria.page() - 1, 0);
Expand All @@ -57,4 +66,45 @@ public List<CommentReport> allCommentReportsBySearchCriteria(

return query.getResultList();
}

@Override
public long countAllCommentReportsByResolvedFalseAndCommunity(
@Nullable List<Community> communities) {

final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Long> cq = cb.createQuery(Long.class);

final Root<CommentReport> commentReportTable = cq.from(CommentReport.class);

final List<Predicate> predicates = new ArrayList<>();

predicates.add(cb.equal(commentReportTable.get("resolved"), false));

if (communities != null) {
// Join Comment and check community id
final Join<CommentReport, Comment> commentJoin = commentReportTable.join("comment",
JoinType.LEFT);

List<Predicate> communityPredicates = new ArrayList<>();

communities.forEach(community -> {
// Add a Or condition for each community

communityPredicates.add(cb.equal(commentJoin.get("community"), community));
});

predicates.add(cb.or(communityPredicates.toArray(new Predicate[0])));

}

cq.where(predicates.toArray(new Predicate[0]));
cq.select(cb.count(commentReportTable));
return em.createQuery(cq).getSingleResult();
}

@Override
public long countAllCommentReportsByResolvedFalse() {

return countAllCommentReportsByResolvedFalseAndCommunity(null);
}
}
Loading

0 comments on commit 59cbdfd

Please sign in to comment.