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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,20 @@ public void cleanupUnusedImages() {
}
}

// private helper methods
@Transactional
public void deleteTtsFile(String ttsUrl) {
if (ttsUrl == null || ttsUrl.isBlank()) {
return;
}

String key = extractKeyFromS3Url(ttsUrl);

try {
amazonS3.deleteObject(new DeleteObjectRequest(bucket, key));
} catch (Exception e) {
}
}

private Image findByUrl(String imageUrl) {
String filename = extractFilename(imageUrl);

Expand All @@ -70,9 +83,7 @@ private void deleteFromS3(Image img) {

try {
amazonS3.deleteObject(new DeleteObjectRequest(bucket, key));
log.info("S3 파일 삭제 완료: {}", key);
} catch (Exception e) {
log.error("S3 삭제 실패: {}", key, e);
throw new RuntimeException("S3 삭제 실패: " + key, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public interface ShorlogRepository extends JpaRepository<Shorlog, Long> {
Page<Shorlog> findAllByOrderByCreatedAtDesc(Pageable pageable);

// 팔로잉 피드 조회
// TODO: Follow 기능 구현 후 수정 (1번 주권영)
@Query(value = "SELECT DISTINCT s FROM Shorlog s " +
"JOIN FETCH s.user " +
"LEFT JOIN FETCH s.images si " +
Expand Down Expand Up @@ -145,4 +144,11 @@ WHERE s.user_id IN (:userIds)

@Query("SELECT s.viewCount FROM Shorlog s WHERE s.id = :id")
long findViewCount(@Param("id") Long id);

@Query("SELECT s.id FROM Shorlog s WHERE s.user.id = :userId")
List<Long> findAllIdsByUserId(@Param("userId") Long userId);

@Query("SELECT s.ttsUrl FROM Shorlog s WHERE s.user.id = :userId AND s.ttsUrl IS NOT NULL")
List<String> findTtsUrlsByUserId(@Param("userId") Long userId);
}

39 changes: 39 additions & 0 deletions src/main/java/com/back/domain/user/auth/service/AuthService.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package com.back.domain.user.auth.service;

import com.back.domain.shared.image.service.ImageLifecycleService;
import com.back.domain.shorlog.shorlog.event.ShorlogDeletedEvent;
import com.back.domain.shorlog.shorlog.repository.ShorlogRepository;
import com.back.domain.user.mail.service.VerificationTokenService;
import com.back.domain.user.refreshToken.service.RefreshTokenService;
import com.back.domain.user.auth.dto.OAuth2CompleteJoinRequestDto;
import com.back.domain.user.auth.dto.PasswordResetRequestDto;
import com.back.domain.user.auth.dto.UserJoinRequestDto;
import com.back.domain.user.auth.dto.UserLoginRequestDto;
import com.back.domain.user.user.entity.User;
import com.back.domain.user.user.file.ProfileImageService;
import com.back.domain.user.user.repository.UserDeletionJdbcRepository;
import com.back.domain.user.user.repository.UserRepository;
import com.back.global.config.security.jwt.JwtTokenProvider;
import com.back.global.exception.AuthException;
import com.back.global.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AuthService {
Expand All @@ -26,6 +36,10 @@ public class AuthService {
private final VerificationTokenService verificationTokenService;
private final JwtTokenProvider jwtTokenProvider;
private final UserDeletionJdbcRepository userDeletionJdbcRepository;
private final ProfileImageService profileImageService;
private final ShorlogRepository shorlogRepository;
private final ImageLifecycleService imageLifecycleService;
private final ApplicationEventPublisher eventPublisher;

@Transactional
public User join(UserJoinRequestDto dto) {
Expand Down Expand Up @@ -140,8 +154,33 @@ public String getEmailByUsername(String username) {

@Transactional
public void withdrawUserHardDelete(Long userId) {
User user = getUserById(userId);

// RefreshToken 삭제 (Redis)
refreshTokenService.deleteRefreshTokenByUserId(userId);

// 프로필 이미지 삭제 (S3)
try {
profileImageService.updateFile(user.getProfileImgUrl(), null, true);
} catch (IOException e) {
throw new AuthException("500-1", "프로필 이미지 삭제 중 오류가 발생했습니다.");
}

// TTS 파일 삭제 (S3)
List<String> ttsUrls = shorlogRepository.findTtsUrlsByUserId(userId);
for (String ttsUrl : ttsUrls) {
if (ttsUrl != null && !ttsUrl.isBlank()) {
imageLifecycleService.deleteTtsFile(ttsUrl);
}
}

// Elasticsearch 숏로그 인덱스 삭제 (이벤트 발행)
List<Long> shorlogIds = shorlogRepository.findAllIdsByUserId(userId);
for (Long shorlogId : shorlogIds) {
eventPublisher.publishEvent(new ShorlogDeletedEvent(shorlogId));
}

// MySQL 데이터 완전 삭제 (JDBC 일괄 처리)
userDeletionJdbcRepository.deleteUserCompletely(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -13,6 +14,7 @@ public class UserDeletionJdbcRepository {

private final JdbcTemplate jdbcTemplate;

@Transactional
public void deleteUserCompletely(Long userId) {
// 0) 메시지까지 전부 삭제: 탈퇴 유저가 속한 thread 통째로 삭제
List<Long> threadIds = jdbcTemplate.queryForList(
Expand All @@ -33,7 +35,7 @@ public void deleteUserCompletely(Long userId) {

// 1) 팔로우
jdbcTemplate.update("DELETE FROM follow WHERE from_user_id = ? OR to_user_id = ?", userId, userId);

jdbcTemplate.update("DELETE FROM notification WHERE receiver_id = ? OR sender_id = ?", userId, userId);
// 2) 유저가 누른 좋아요/북마크/반응
jdbcTemplate.update("DELETE FROM blog_like WHERE user_id = ?", userId);
jdbcTemplate.update("DELETE FROM blog_bookmarks WHERE user_id = ?", userId);
Expand Down
Loading