Skip to content
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
Empty file added .codex
Empty file.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ APPLE_CLIENT_SECRET=your-apple-client-secret-jwt-here


CONTENT_MODERATION_WEBHOOK_URL=https://inscriptions.cdacb.in/n8n/webhook/content-moderation
# CONTENT_MODERATION_INSECURE_SSL=true
CONTENT_MODERATION_INSECURE_SSL=true

CONTENT_MODERATION_SAFE_THRESHOLD=0.7
CONTENT_MODERATION_CONNECT_TIMEOUT_MS=5000
CONTENT_MODERATION_READ_TIMEOUT_MS=10000
CONTENT_MODERATION_INSECURE_SSL=false
# CONTENT_MODERATION_INSECURE_SSL=false
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class ArchiveComment {
@JsonSerialize(using = ToStringSerializer.class)
private ObjectId id;

@Field("originalCommentId")
@JsonProperty("originalCommentId")
@JsonSerialize(using = ToStringSerializer.class)
private ObjectId originalCommentId;

@Field("postId")
@JsonProperty("postId")
@JsonSerialize(using = ToStringSerializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public class ArchivePost {
@JsonSerialize(using = ToStringSerializer.class)
private ObjectId id;

@Field("originalPostId")
@JsonProperty("originalPostId")
@JsonSerialize(using = ToStringSerializer.class)
private ObjectId originalPostId;

@Field("user_id")
@JsonProperty("user_id")
@JsonSerialize(using = ToStringSerializer.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cadac.stone_inscription.admin.repository;

import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.cadac.stone_inscription.admin.entity.ArchiveComment;

@Repository
public interface ArchiveCommentRepository extends MongoRepository<ArchiveComment, ObjectId> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cadac.stone_inscription.admin.repository;

import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.cadac.stone_inscription.admin.entity.ArchivePost;

@Repository
public interface ArchivePostRepository extends MongoRepository<ArchivePost, ObjectId> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.cadac.stone_inscription.content.delete;

import org.springframework.stereotype.Component;

import com.cadac.stone_inscription.admin.entity.ArchiveComment;
import com.cadac.stone_inscription.admin.entity.ArchivePost;
import com.cadac.stone_inscription.entity.InscriptionPost;
import com.cadac.stone_inscription.entity.PublicPostDescription;

@Component
public class ArchiveContentMapper {

public ArchivePost toArchivePost(InscriptionPost post) {
return ArchivePost.builder()
.originalPostId(post.getId())
.userId(post.getUserId())
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())
.images(post.getImages() != null ? ArchivePost.Images.builder()
.thumbnailImage(post.getImages().getThumbnailImage())
.image(post.getImages().getImage())
.build() : null)
.description(post.getDescription() != null ? ArchivePost.Description.builder()
.title(post.getDescription().getTitle())
.subject(post.getDescription().getSubject())
.description(post.getDescription().getDescription())
.scriptLanguage(post.getDescription().getScriptLanguage())
.language(post.getDescription().getLanguage())
.englishTranslation(post.getDescription().getEnglishTranslation())
.moderation(post.getDescription().getModeration())
.upvote(post.getDescription().getUpvote())
.geolocation(post.getDescription().getGeolocation() != null ? ArchivePost.GeoLocation.builder()
.lat(post.getDescription().getGeolocation().getLat())
.lon(post.getDescription().getGeolocation().getLon())
.state(post.getDescription().getGeolocation().getState())
.city(post.getDescription().getGeolocation().getCity())
.country(post.getDescription().getGeolocation().getCountry())
.build() : null)
.createdAt(post.getDescription().getCreatedAt())
.updatedAt(post.getDescription().getUpdatedAt())
.build() : null)
.topic(post.getTopic())
.script(post.getScript())
.type(post.getType())
.status(post.getStatus())
.report(post.getReport())
.build();
}

public ArchiveComment toArchiveComment(PublicPostDescription comment) {
return ArchiveComment.builder()
.originalCommentId(comment.getId())
.postId(comment.getPostId())
.userId(comment.getUserId())
.username(comment.getUsername())
.userImageUrl(comment.getUserImageUrl())
.description(comment.getDescription())
.moderation(comment.getModeration())
.upvote(comment.getUpvote())
.createdAt(comment.getCreatedAt())
.updatedAt(comment.getUpdatedAt())
.status(comment.getStatus())
.report(comment.getReport())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cadac.stone_inscription.content.delete;

import lombok.Builder;
import lombok.Value;

@Value
@Builder
public class ContentDeleteResult {
int archivedPosts;
int archivedComments;
int deletedImages;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.cadac.stone_inscription.content.delete;

import java.util.Collections;
import java.util.List;

import org.bson.types.ObjectId;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.cadac.stone_inscription.admin.repository.ArchiveCommentRepository;
import com.cadac.stone_inscription.admin.repository.ArchivePostRepository;
import com.cadac.stone_inscription.entity.InscriptionPost;
import com.cadac.stone_inscription.entity.PublicPostDescription;
import com.cadac.stone_inscription.entity.User;
import com.cadac.stone_inscription.exception.StoneInscriptionException;
import com.cadac.stone_inscription.repository.ImagesDataRepo;
import com.cadac.stone_inscription.repository.InscriptionPostRepo;
import com.cadac.stone_inscription.repository.PublicPostDescriptionRepo;
import com.cadac.stone_inscription.repository.UserRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ContentDeleteService {

private final InscriptionPostRepo inscriptionPostRepo;
private final PublicPostDescriptionRepo publicPostDescriptionRepo;
private final ArchivePostRepository archivePostRepository;
private final ArchiveCommentRepository archiveCommentRepository;
private final ArchiveContentMapper archiveContentMapper;
private final ImagesDataRepo imagesDataRepo;
private final UserRepository userRepository;

public ContentDeleteResult deletePost(ObjectId postId) {
InscriptionPost post = inscriptionPostRepo.findById(postId)
.orElseThrow(() -> new StoneInscriptionException("Unprocesable request", HttpStatus.BAD_REQUEST));

List<PublicPostDescription> comments = publicPostDescriptionRepo.findByPostId(postId);
List<String> imageIds = getImageIds(post);

archivePostRepository.save(archiveContentMapper.toArchivePost(post));
comments.stream()
.map(archiveContentMapper::toArchiveComment)
.forEach(archiveCommentRepository::save);

imageIds.forEach(imagesDataRepo::deleteById);
publicPostDescriptionRepo.deleteAll(comments);
inscriptionPostRepo.delete(post);
decrementUserImagesUploaded(post.getUserId(), imageIds.size());

return ContentDeleteResult.builder()
.archivedPosts(1)
.archivedComments(comments.size())
.deletedImages(imageIds.size())
.build();
}

public ContentDeleteResult deleteComment(ObjectId commentId) {
PublicPostDescription comment = publicPostDescriptionRepo.findById(commentId)
.orElseThrow(() -> new StoneInscriptionException("Unprocesable request", HttpStatus.BAD_REQUEST));

archiveCommentRepository.save(archiveContentMapper.toArchiveComment(comment));
publicPostDescriptionRepo.delete(comment);

return ContentDeleteResult.builder()
.archivedPosts(0)
.archivedComments(1)
.deletedImages(0)
.build();
}

private List<String> getImageIds(InscriptionPost post) {
if (post.getImages() == null || post.getImages().getImage() == null) {
return Collections.emptyList();
}

return post.getImages().getImage();
}

private void decrementUserImagesUploaded(ObjectId userId, int imageCount) {
if (imageCount <= 0) {
return;
}

User user = userRepository.findById(userId)
.orElseThrow(() -> new StoneInscriptionException("User not found", HttpStatus.NOT_FOUND));

int currentCount = user.getImagesUploaded() == null ? 0 : user.getImagesUploaded();
user.setImagesUploaded(Math.max(0, currentCount - imageCount));
userRepository.save(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public ResponseEntity<?> deleteImagesFromPost(HttpServletRequest request,
// TODO: Remove these methods when testing is done to restore normal behavior.
// ============================

// // @PostMapping("/test/updatePost/{email}")
// @PostMapping("/test/updatePost/{email}")
// public ResponseEntity<?> updatePostForTest(
// @PathVariable String email,
// @RequestPart(value = "post", required = false) InscriptionPostDto InscriptionPostDto,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cadac.stone_inscription.content.delete.ContentDeleteService;
import com.cadac.stone_inscription.entity.ImagesData;
import com.cadac.stone_inscription.entity.InscriptionPost;
import com.cadac.stone_inscription.entity.PublicPostDescription;
Expand Down Expand Up @@ -65,6 +66,9 @@ public class PostServiceImp implements PostService {
@Autowired
private ContentModerationService contentModerationService;

@Autowired
private ContentDeleteService contentDeleteService;

@Value("${app.backend.url}")
private String backendUrl;

Expand Down Expand Up @@ -356,13 +360,8 @@ public ResponseEntity<?> postDelete(String usernameFromToken, String postId) {
if (!user.getId().toString().equals(postDelete.get().getUserId().toString())) {
throw new StoneInscriptionException("Unprocesable request", HttpStatus.BAD_REQUEST);
}
int deletedImageCount = getExistingImageIds(postDelete.get()).size();
getExistingImageIds(postDelete.get()).forEach(imagesDataRepo::deleteById);
adjustUserImagesUploaded(user, -deletedImageCount);
userRepository.save(user);

publicPostDescriptionRepo.deleteAllByPostId(postId);
inscriptionPostRepo.deleteById(new ObjectId(postId));
contentDeleteService.deletePost(postDelete.get().getId());
return UserResponse.responseHandler("post deleted", HttpStatus.OK, true);
}

Expand All @@ -380,7 +379,7 @@ public ResponseEntity<?> descriptionDelete(String usernameFromToken, String desc
throw new StoneInscriptionException("Unprocesable request Unauthorized", HttpStatus.UNAUTHORIZED);
}

publicPostDescriptionRepo.deleteById(new ObjectId(descriptionId));
contentDeleteService.deleteComment(postDiscription.get().getId());

return UserResponse.responseHandler("description deleted", HttpStatus.OK, true);
}
Expand Down
Loading