Skip to content

Commit 791a596

Browse files
authored
Merge pull request #450 from sublinks/feature/446-comment-post-saves
Feature/446 comment post saves
2 parents 23b5843 + c7444eb commit 791a596

72 files changed

Lines changed: 1591 additions & 936 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/admin/controllers/AdminController.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ AddAdminResponse create(@Valid @RequestBody final AddAdmin addAdminForm, JwtPers
103103

104104
aclService.canPerson(person)
105105
.performTheAction(RolePermissionInstanceTypes.INSTANCE_ADD_ADMIN)
106-
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "not_an_admin"));
106+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "unauthorized"));
107107

108108
final Person personToAdd = personRepository.findById((long) addAdminForm.person_id())
109109
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "person_not_found"));
@@ -151,7 +151,7 @@ GetUnreadRegistrationApplicationCountResponse registrationApplicationCount(
151151
aclService.canPerson(person)
152152
.performTheAction(RolePermissionInstanceTypes.INSTANCE_REMOVE_ADMIN)
153153

154-
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "not_an_admin"));
154+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "unauthorized"));
155155

156156
return GetUnreadRegistrationApplicationCountResponse.builder()
157157
.registration_applications(
@@ -174,7 +174,7 @@ ListRegistrationApplicationsResponse registrationApplicationList(
174174

175175
aclService.canPerson(person)
176176
.performTheAction(RolePermissionRegistrationApplicationTypes.REGISTRATION_APPLICATION_READ)
177-
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "not_an_admin"));
177+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "unauthorized"));
178178

179179
final List<PersonRegistrationApplication> personRegistrationApplications = personRegistrationApplicationRepository.findAllByApplicationStatus(
180180
PersonRegistrationApplicationStatus.pending);
@@ -201,7 +201,7 @@ RegistrationApplicationResponse registrationApplicationApprove(
201201
aclService.canPerson(person)
202202
.performTheAction(
203203
RolePermissionRegistrationApplicationTypes.REGISTRATION_APPLICATION_UPDATE)
204-
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "not_an_admin"));
204+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "unauthorized"));
205205

206206
final PersonRegistrationApplication personRegistrationApplication = personRegistrationApplicationRepository.findById(
207207
(long) approveRegistrationApplicationForm.id())
@@ -237,7 +237,7 @@ PurgeItemResponse purgePerson(@Valid @RequestBody final PurgePerson purgePersonF
237237

238238
aclService.canPerson(person)
239239
.performTheAction(RolePermissionPersonTypes.PURGE_USER)
240-
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "not_an_admin"));
240+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "unauthorized"));
241241

242242
final Person personToPurge = personRepository.findById((long) purgePersonForm.person_id())
243243
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "person_not_found"));
@@ -263,7 +263,7 @@ PurgeItemResponse purgeCommunity(@Valid @RequestBody final PurgeCommunity purgeC
263263

264264
aclService.canPerson(person)
265265
.performTheAction(RolePermissionCommunityTypes.PURGE_COMMUNITY)
266-
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "not_an_admin"));
266+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "unauthorized"));
267267

268268
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED);
269269
}
@@ -281,7 +281,7 @@ PurgeItemResponse purgePost(@Valid @RequestBody final PurgePost purgePostForm,
281281

282282
aclService.canPerson(person)
283283
.performTheAction(RolePermissionPostTypes.PURGE_POST)
284-
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "not_an_admin"));
284+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "unauthorized"));
285285

286286
final Post postToPurge = postRepository.getReferenceById((long) purgePostForm.post_id());
287287
try {
@@ -308,7 +308,7 @@ PurgeItemResponse purgeComment(@Valid @RequestBody final PurgeComment purgeComme
308308

309309
aclService.canPerson(person)
310310
.performTheAction(RolePermissionCommentTypes.PURGE_COMMENT)
311-
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "not_an_admin"));
311+
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "unauthorized"));
312312

313313
final Comment commentToPurge = commentRepository.getReferenceById(
314314
(long) purgeCommentForm.comment_id());

‎src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/comment/controllers/CommentController.java‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,27 @@
2222
import com.sublinks.sublinksapi.api.lemmy.v3.common.controllers.AbstractLemmyApiController;
2323
import com.sublinks.sublinksapi.authorization.enums.RolePermissionCommentTypes;
2424
import com.sublinks.sublinksapi.authorization.services.AclService;
25-
import com.sublinks.sublinksapi.authorization.services.RolePermissionService;
2625
import com.sublinks.sublinksapi.comment.entities.Comment;
2726
import com.sublinks.sublinksapi.comment.entities.CommentLike;
2827
import com.sublinks.sublinksapi.comment.entities.CommentReply;
2928
import com.sublinks.sublinksapi.comment.entities.CommentReport;
30-
import com.sublinks.sublinksapi.comment.entities.CommentSave;
29+
import com.sublinks.sublinksapi.comment.entities.LinkPersonComment;
3130
import com.sublinks.sublinksapi.comment.enums.CommentSortType;
31+
import com.sublinks.sublinksapi.comment.enums.LinkPersonCommentType;
3232
import com.sublinks.sublinksapi.comment.models.CommentSearchCriteria;
3333
import com.sublinks.sublinksapi.comment.models.CommentSearchCriteria.CommentSearchCriteriaBuilder;
34-
import com.sublinks.sublinksapi.comment.repositories.ComentSaveRepository;
3534
import com.sublinks.sublinksapi.comment.repositories.CommentReplyRepository;
3635
import com.sublinks.sublinksapi.comment.repositories.CommentRepository;
3736
import com.sublinks.sublinksapi.comment.services.CommentLikeService;
3837
import com.sublinks.sublinksapi.comment.services.CommentReadService;
3938
import com.sublinks.sublinksapi.comment.services.CommentReplyService;
4039
import com.sublinks.sublinksapi.comment.services.CommentReportService;
41-
import com.sublinks.sublinksapi.comment.services.CommentSaveService;
4240
import com.sublinks.sublinksapi.comment.services.CommentService;
4341
import com.sublinks.sublinksapi.language.entities.Language;
4442
import com.sublinks.sublinksapi.language.repositories.LanguageRepository;
4543
import com.sublinks.sublinksapi.person.entities.Person;
4644
import com.sublinks.sublinksapi.person.enums.ListingType;
45+
import com.sublinks.sublinksapi.person.services.LinkPersonCommentService;
4746
import com.sublinks.sublinksapi.person.services.PersonService;
4847
import com.sublinks.sublinksapi.post.entities.Post;
4948
import com.sublinks.sublinksapi.post.repositories.PostRepository;
@@ -97,9 +96,7 @@ public class CommentController extends AbstractLemmyApiController {
9796
private final CommentReplyRepository commentReplyRepository;
9897
private final CommentReplyService commentReplyService;
9998
private final SlurFilterService slurFilterService;
100-
private final RolePermissionService rolePermissionService;
101-
private final CommentSaveService commentSaveForLaterService;
102-
private final ComentSaveRepository commentSaveForLaterRepository;
99+
private final LinkPersonCommentService linkPersonCommentService;
103100
private final AclService aclService;
104101

105102
/**
@@ -468,29 +465,26 @@ CommentResponse saveForLater(@Valid @RequestBody final SaveComment saveCommentFo
468465
.onCommunity(comment.getCommunity())
469466
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "unauthorized"));
470467

471-
Optional<CommentSave> commentSaveForLater = commentSaveForLaterRepository.findFirstByPersonAndComment(
472-
person, comment);
468+
final Optional<LinkPersonComment> linkPersonComment = linkPersonCommentService.getLink(comment,
469+
person, LinkPersonCommentType.saved);
473470

474471
if (saveCommentForm.save()) {
475-
if (commentSaveForLater.isPresent()) {
472+
if (linkPersonComment.isPresent()) {
476473
return CommentResponse.builder()
477474
.comment_view(lemmyCommentService.createCommentView(comment, person))
478475
.recipient_ids(new ArrayList<>())
479476
.build();
480477
}
481-
commentSaveForLaterService.createCommentSave(CommentSave.builder()
482-
.comment(comment)
483-
.person(person)
484-
.build());
478+
linkPersonCommentService.createCommentLink(comment, person, LinkPersonCommentType.saved);
485479
} else {
486-
if (commentSaveForLater.isEmpty()) {
480+
if (linkPersonComment.isEmpty()) {
487481
return CommentResponse.builder()
488482
.comment_view(lemmyCommentService.createCommentView(comment, person))
489483
.recipient_ids(new ArrayList<>())
490484
.build();
491485
}
492486

493-
commentSaveForLaterService.deleteCommentSave(commentSaveForLater.get());
487+
linkPersonCommentService.deleteLink(linkPersonComment.get());
494488
}
495489

496490
return CommentResponse.builder()

‎src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/comment/controllers/CommentModActionsController.java‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.sublinks.sublinksapi.community.entities.Community;
3030
import com.sublinks.sublinksapi.community.repositories.CommunityRepository;
3131
import com.sublinks.sublinksapi.moderation.entities.ModerationLog;
32+
import com.sublinks.sublinksapi.person.entities.LinkPersonCommunity;
3233
import com.sublinks.sublinksapi.person.entities.Person;
3334
import com.sublinks.sublinksapi.person.enums.LinkPersonCommunityType;
3435
import com.sublinks.sublinksapi.person.services.LinkPersonCommunityService;
@@ -204,11 +205,10 @@ CommentReportResponse reportResolve(
204205
RolePermissionInstanceTypes.REPORT_INSTANCE_RESOLVE);
205206

206207
if (!isAdmin) {
207-
final boolean isModerator = linkPersonCommunityService.hasLink(person,
208-
commentReport.getComment()
209-
.getCommunity(), LinkPersonCommunityType.moderator)
210-
|| linkPersonCommunityService.hasLink(person, commentReport.getComment()
211-
.getCommunity(), LinkPersonCommunityType.owner);
208+
final boolean isModerator = linkPersonCommunityService.hasLink(commentReport.getComment()
209+
.getCommunity(), person, LinkPersonCommunityType.moderator)
210+
|| linkPersonCommunityService.hasLink(commentReport.getComment()
211+
.getCommunity(), person, LinkPersonCommunityType.owner);
212212
if (!isModerator) {
213213
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
214214
}
@@ -260,17 +260,23 @@ ListCommentReportsResponse reportList(@Valid ListCommentReports listCommentRepor
260260

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

263-
moderatingCommunities.addAll(
264-
linkPersonCommunityService.getPersonLinkByType(person, LinkPersonCommunityType.owner));
265-
moderatingCommunities.addAll(linkPersonCommunityService.getPersonLinkByType(person,
266-
LinkPersonCommunityType.moderator));
263+
moderatingCommunities.addAll(linkPersonCommunityService.getLinks(person,
264+
LinkPersonCommunityType.owner)
265+
.stream()
266+
.map(LinkPersonCommunity::getCommunity)
267+
.toList());
268+
moderatingCommunities.addAll(linkPersonCommunityService.getLinks(person,
269+
LinkPersonCommunityType.moderator)
270+
.stream()
271+
.map(LinkPersonCommunity::getCommunity)
272+
.toList());
267273
} else {
268274
Community community = communityRepository.findById(
269275
(long) listCommentReportsForm.community_id())
270276
.orElseThrow(
271277
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "community_not_found"));
272-
if (!linkPersonCommunityService.hasLink(person, community, LinkPersonCommunityType.owner)
273-
&& !linkPersonCommunityService.hasLink(person, community,
278+
if (!linkPersonCommunityService.hasLink(community, person, LinkPersonCommunityType.owner)
279+
&& !linkPersonCommunityService.hasLink(community, person,
274280
LinkPersonCommunityType.moderator)) {
275281
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
276282
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.sublinks.sublinksapi.api.lemmy.v3.comment.mappers;
2+
3+
import com.sublinks.sublinksapi.api.lemmy.v3.enums.SortType;
4+
import org.mapstruct.Mapper;
5+
import org.mapstruct.MappingConstants;
6+
import org.mapstruct.ValueMapping;
7+
import org.springframework.core.convert.converter.Converter;
8+
import org.springframework.lang.Nullable;
9+
10+
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
11+
public interface CommentSortTypeSortMapper extends
12+
Converter<SortType, com.sublinks.sublinksapi.comment.enums.CommentSortType> {
13+
14+
@Override
15+
@ValueMapping(source = "Hot", target = "Hot")
16+
@ValueMapping(source = "Active", target = "Hot")
17+
@ValueMapping(source = "MostComments", target = "Hot")
18+
@ValueMapping(source = "NewComments", target = "Hot")
19+
@ValueMapping(source = "TopHour", target = "Top")
20+
@ValueMapping(source = "TopSixHour", target = "Top")
21+
@ValueMapping(source = "TopTwelveHour", target = "Top")
22+
@ValueMapping(source = "TopThreeMonths", target = "Top")
23+
@ValueMapping(source = "TopSixMonths", target = "Top")
24+
@ValueMapping(source = "TopNineMonths", target = "Top")
25+
@ValueMapping(source = "Controversial", target = "Top")
26+
@ValueMapping(source = "Scaled", target = "Top")
27+
@ValueMapping(source = "TopDay", target = "Top")
28+
@ValueMapping(source = "TopWeek", target = "Top")
29+
@ValueMapping(source = "TopMonth", target = "Top")
30+
@ValueMapping(source = "TopYear", target = "Top")
31+
@ValueMapping(source = "TopAll", target = "Top")
32+
@ValueMapping(source = "New", target = "New")
33+
@ValueMapping(source = "Old", target = "Old")
34+
com.sublinks.sublinksapi.comment.enums.CommentSortType convert(@Nullable SortType sortType);
35+
}

‎src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/comment/services/LemmyCommentReplyService.java‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import com.sublinks.sublinksapi.api.lemmy.v3.post.models.Post;
99
import com.sublinks.sublinksapi.api.lemmy.v3.user.models.Person;
1010
import com.sublinks.sublinksapi.authorization.services.RolePermissionService;
11+
import com.sublinks.sublinksapi.comment.enums.LinkPersonCommentType;
1112
import com.sublinks.sublinksapi.comment.services.CommentLikeService;
1213
import com.sublinks.sublinksapi.person.enums.LinkPersonCommunityType;
1314
import com.sublinks.sublinksapi.person.enums.LinkPersonPersonType;
14-
import com.sublinks.sublinksapi.person.repositories.LinkPersonPersonRepository;
15-
import com.sublinks.sublinksapi.person.repositories.LinkPersonPostRepository;
15+
import com.sublinks.sublinksapi.person.services.LinkPersonCommentService;
1616
import com.sublinks.sublinksapi.person.services.LinkPersonCommunityService;
1717
import com.sublinks.sublinksapi.person.services.LinkPersonPersonService;
1818
import lombok.AllArgsConstructor;
@@ -27,10 +27,8 @@ public class LemmyCommentReplyService {
2727
private final ConversionService conversionService;
2828
private final CommentLikeService commentLikeService;
2929
private final LinkPersonCommunityService linkPersonCommunityService;
30-
private final LinkPersonPostRepository linkPersonPostRepository;
31-
private final LinkPersonPersonRepository linkPersonPersonRepository;
32-
private final RolePermissionService rolePermissionService;
3330
private final LinkPersonPersonService linkPersonPersonService;
31+
private final LinkPersonCommentService linkPersonCommentService;
3432

3533
@NonNull
3634
public CommentReplyView createCommentReplyView(
@@ -74,14 +72,13 @@ private CommentReplyView.CommentReplyViewBuilder commentReplyViewBuilder(
7472
if (!creatorIsAdmin) {
7573
creatorIsBlocked = linkPersonPersonService.hasLink(creator, person,
7674
LinkPersonPersonType.blocked);
77-
creatorBannedFromCommunity = linkPersonCommunityService.hasLink(creator,
78-
commentReply.getComment()
79-
.getCommunity(), LinkPersonCommunityType.banned);
75+
creatorBannedFromCommunity = linkPersonCommunityService.hasLink(commentReply.getComment()
76+
.getCommunity(), creator, LinkPersonCommunityType.banned);
8077

8178
}
8279
if (!creatorBannedFromCommunity) {
83-
creatorIsModerator = linkPersonCommunityService.hasLink(creator, commentReply.getComment()
84-
.getCommunity(), LinkPersonCommunityType.moderator);
80+
creatorIsModerator = linkPersonCommunityService.hasLink(commentReply.getComment()
81+
.getCommunity(), creator, LinkPersonCommunityType.moderator);
8582
}
8683
final CommentReply lemmyCommentReply = conversionService.convert(commentReply,
8784
CommentReply.class);
@@ -95,13 +92,14 @@ private CommentReplyView.CommentReplyViewBuilder commentReplyViewBuilder(
9592
.community(lemmyCommunity)
9693
.my_vote(personVote)
9794
.recipient(conversionService.convert(commentReply.getRecipient(), Person.class))
98-
.subscribed(linkPersonCommunityService.hasLink(person, commentReply.getComment()
99-
.getCommunity(), LinkPersonCommunityType.follower))
95+
.subscribed(linkPersonCommunityService.hasLink(commentReply.getComment()
96+
.getCommunity(), person, LinkPersonCommunityType.follower))
10097
.creator_banned_from_community(creatorBannedFromCommunity)
10198
.creator_is_moderator(creatorIsModerator)
10299
.creator_is_admin(creatorIsAdmin)
103100
.creator_blocked(creatorIsBlocked)
104-
.saved(false);
101+
.saved(linkPersonCommentService.hasLink(commentReply.getComment(), person,
102+
LinkPersonCommentType.saved));
105103

106104
}
107105
}

‎src/main/java/com/sublinks/sublinksapi/api/lemmy/v3/comment/services/LemmyCommentReportService.java‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ private CommentReportView.CommentReportViewBuilder commentViewBuilder(
3939
final CommentReport lemmyCommentReport = conversionService.convert(commentReport,
4040
CommentReport.class);
4141

42-
final Person lemmyCommentCreator = conversionService.convert(
43-
commentReport.getComment().getPerson(), Person.class);
42+
final Person lemmyCommentCreator = conversionService.convert(commentReport.getComment()
43+
.getPerson(), Person.class);
4444
final Person lemmyResolver = conversionService.convert(commentReport.getResolver(),
4545
Person.class);
4646

@@ -50,20 +50,21 @@ private CommentReportView.CommentReportViewBuilder commentViewBuilder(
5050
final Comment lemmyComment = conversionService.convert(commentReport.getComment(),
5151
Comment.class);
5252

53-
final CommentAggregates commentAggregates = conversionService.convert(
54-
commentReport.getComment().getCommentAggregate(), CommentAggregates.class);
53+
final CommentAggregates commentAggregates = conversionService.convert(commentReport.getComment()
54+
.getCommentAggregate(), CommentAggregates.class);
5555

56-
final Post lemmyPost = conversionService.convert(commentReport.getComment().getPost(),
57-
Post.class);
56+
final Post lemmyPost = conversionService.convert(commentReport.getComment()
57+
.getPost(), Post.class);
5858

59-
final Community lemmyCommunity = conversionService.convert(
60-
commentReport.getComment().getCommunity(), Community.class);
59+
final Community lemmyCommunity = conversionService.convert(commentReport.getComment()
60+
.getCommunity(), Community.class);
6161

6262
final int personVote = commentLikeService.getPersonCommentVote(Person,
6363
commentReport.getComment());
6464

65-
final boolean creatorBannedFromCommunity = linkPersonCommunityService.hasLink(creator,
66-
commentReport.getComment().getCommunity(), LinkPersonCommunityType.banned);
65+
final boolean creatorBannedFromCommunity = linkPersonCommunityService.hasLink(
66+
commentReport.getComment()
67+
.getCommunity(), creator, LinkPersonCommunityType.banned);
6768

6869
return CommentReportView.builder()
6970
.creator(lemmyCreator)
@@ -72,7 +73,8 @@ private CommentReportView.CommentReportViewBuilder commentViewBuilder(
7273
.comment_report(lemmyCommentReport)
7374
.comment_creator(lemmyCommentCreator)
7475
.community(lemmyCommunity)
75-
.my_vote(personVote).resolver(lemmyResolver)
76+
.my_vote(personVote)
77+
.resolver(lemmyResolver)
7678
.counts(commentAggregates)
7779
.creator_banned_from_community(creatorBannedFromCommunity);
7880
}

0 commit comments

Comments
 (0)