Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented mod actions for community #39

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.sublinks.sublinksapi.comment.dto.Comment;
import com.sublinks.sublinksapi.community.dto.Community;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.sublinks.sublinksapi.person.dto.Person;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface CommentRepository extends JpaRepository<Comment, Long>, CommentRepositorySearch {

List<Comment> allCommentsByCommunityAndPerson(Community community, Person person);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import com.sublinks.sublinksapi.comment.dto.CommentAggregate;
import com.sublinks.sublinksapi.comment.events.CommentCreatedPublisher;
import com.sublinks.sublinksapi.comment.events.CommentUpdatedPublisher;
import com.sublinks.sublinksapi.comment.models.CommentSearchCriteria;
import com.sublinks.sublinksapi.comment.repositories.CommentAggregateRepository;
import com.sublinks.sublinksapi.comment.repositories.CommentRepository;
import com.sublinks.sublinksapi.community.dto.Community;
import com.sublinks.sublinksapi.instance.models.LocalInstanceContext;
import java.util.Optional;
import com.sublinks.sublinksapi.person.dto.Person;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -82,4 +85,14 @@ public void updateComment(final Comment comment) {
commentRepository.save(comment);
commentUpdatedPublisher.publish(comment);
}

@Transactional
public void removeAllCommentsFromUser(final Community community, final Person person,
final boolean removed) {

commentRepository.allCommentsByCommunityAndPerson(community, person).forEach(comment -> {
comment.setRemoved(removed);
commentRepository.save(comment);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public enum LinkPersonCommunityType {
moderator,
follower,
pending_follow,
blocked
blocked,
banned
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sublinks.sublinksapi.person.dto.Person;
import com.sublinks.sublinksapi.person.enums.LinkPersonCommunityType;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -13,9 +14,15 @@ public interface LinkPersonCommunityRepository extends JpaRepository<LinkPersonC
Optional<LinkPersonCommunity> getLinkPersonCommunityByCommunityAndPersonAndLinkType(
Community community, Person person, LinkPersonCommunityType type);

List<LinkPersonCommunity> getLinkPersonCommunityByCommunityAndPersonAndLinkTypeIsIn(
Community community, Person person, List<LinkPersonCommunityType> types);

Optional<LinkPersonCommunity> getLinkPersonCommunityByPersonAndLinkType(Person person,
LinkPersonCommunityType type);

Collection<LinkPersonCommunity> getLinkPersonCommunitiesByPersonAndLinkType(Person person,
LinkPersonCommunityType type);

Collection<LinkPersonCommunity> getLinkPersonCommunitiesByCommunityAndLinkTypeIsIn(
Community community, List<LinkPersonCommunityType> types);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.sublinks.sublinksapi.person.repositories.LinkPersonCommunityRepository;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,23 +26,24 @@ public class LinkPersonCommunityService {

public boolean hasLink(Person person, Community community, LinkPersonCommunityType type) {

final Optional<LinkPersonCommunity> linkPersonCommunity =
linkPersonCommunityRepository.getLinkPersonCommunityByCommunityAndPersonAndLinkType(
community,
person,
type
);
final Optional<LinkPersonCommunity> linkPersonCommunity = linkPersonCommunityRepository.getLinkPersonCommunityByCommunityAndPersonAndLinkType(
community, person, type);
return linkPersonCommunity.isPresent();
}

public boolean hasAnyLink(Person person, Community community,
List<LinkPersonCommunityType> types) {

final List<LinkPersonCommunity> linkPersonCommunity = linkPersonCommunityRepository.getLinkPersonCommunityByCommunityAndPersonAndLinkTypeIsIn(
community, person, types);
return linkPersonCommunity.isEmpty();
}

@Transactional
public void addLink(Person person, Community community, LinkPersonCommunityType type) {

final LinkPersonCommunity newLink = LinkPersonCommunity.builder()
.community(community)
.person(person)
.linkType(type)
.build();
final LinkPersonCommunity newLink = LinkPersonCommunity.builder().community(community)
.person(person).linkType(type).build();
person.getLinkPersonCommunity().add(newLink);
community.getLinkPersonCommunity().add(newLink);
linkPersonCommunityRepository.save(newLink);
Expand All @@ -51,12 +53,8 @@ public void addLink(Person person, Community community, LinkPersonCommunityType
@Transactional
public void removeLink(Person person, Community community, LinkPersonCommunityType type) {

final Optional<LinkPersonCommunity> linkPersonCommunity =
linkPersonCommunityRepository.getLinkPersonCommunityByCommunityAndPersonAndLinkType(
community,
person,
type
);
final Optional<LinkPersonCommunity> linkPersonCommunity = linkPersonCommunityRepository.getLinkPersonCommunityByCommunityAndPersonAndLinkType(
community, person, type);
if (linkPersonCommunity.isEmpty()) {
return;
}
Expand All @@ -70,13 +68,23 @@ public void removeLink(Person person, Community community, LinkPersonCommunityTy

public Collection<Community> getPersonLinkByType(Person person, LinkPersonCommunityType type) {

Collection<LinkPersonCommunity> linkPersonCommunities = linkPersonCommunityRepository
.getLinkPersonCommunitiesByPersonAndLinkType(person, type);
Collection<LinkPersonCommunity> linkPersonCommunities = linkPersonCommunityRepository.getLinkPersonCommunitiesByPersonAndLinkType(
person, type);

Collection<Community> communities = new ArrayList<>();
for (LinkPersonCommunity linkPersonCommunity : linkPersonCommunities) {
communities.add(linkPersonCommunity.getCommunity());
}
return communities;
}

public Collection<Person> getPersonsFromCommunityAndListTypes(Community community,
List<LinkPersonCommunityType> types) {

Collection<LinkPersonCommunity> linkPersonCommunities = linkPersonCommunityRepository.getLinkPersonCommunitiesByCommunityAndLinkTypeIsIn(
community, types);

return linkPersonCommunities.stream().map(LinkPersonCommunity::getPerson).toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.sublinks.sublinksapi.community.dto.Community;
import com.sublinks.sublinksapi.post.dto.PostReport;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface PostReportRepository extends JpaRepository<PostReport, Long>,
PostReportRepositorySearch {


}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.sublinks.sublinksapi.post.repositories;

import com.sublinks.sublinksapi.community.dto.Community;
import com.sublinks.sublinksapi.person.dto.Person;
import com.sublinks.sublinksapi.post.dto.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface PostRepository extends JpaRepository<Post, Long>, PostRepositorySearch {

List<Post> allPostsByCommunityAndPerson(Community community, Person person);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sublinks.sublinksapi.post.services;

import com.sublinks.sublinksapi.community.dto.Community;
import com.sublinks.sublinksapi.person.dto.LinkPersonPost;
import com.sublinks.sublinksapi.person.dto.Person;
import com.sublinks.sublinksapi.person.enums.LinkPersonPostType;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class PostService {
private final UrlUtil urlUtil;

public String getPostMd5Hash(final Post post) {

return getStringMd5Hash(post.getLinkUrl());
}

Expand Down Expand Up @@ -81,10 +83,8 @@ public void createPost(final Post post, final Person creator) {
post.setPrivateKey(keys.privateKey());

post.setLocal(true);
final PostAggregate postAggregate = PostAggregate.builder()
.post(post)
.community(post.getCommunity())
.build();
final PostAggregate postAggregate = PostAggregate.builder().post(post)
.community(post.getCommunity()).build();
post.setPostAggregate(postAggregate);
post.setActivityPubId("");
postRepository.save(post); // @todo fix second save making post look edited right away
Expand All @@ -102,4 +102,14 @@ public void softDeletePost(final Post post) {
postRepository.save(post);
postDeletedPublisher.publish(post);
}

@Transactional
public void removeAllPostsFromUser(final Community community, final Person person,
final boolean removed) {

postRepository.allPostsByCommunityAndPerson(community, person).forEach(post -> {
post.setRemoved(removed);
postRepository.save(post);
});
}
}