Skip to content

Commit

Permalink
Merge pull request #63 from Team-Lecue/feat/#59-badword_filtering_set…
Browse files Browse the repository at this point in the history
…ting

[FEAT] 비속어 필터링 설정
  • Loading branch information
eeddiinn authored Jan 16, 2024
2 parents 884fc8a + 3d3c3a5 commit 8fd5e51
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 21 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ dependencies {

// Discord
implementation('com.github.napstr:logback-discord-appender:1.0.0')

// BadWordFiltering
implementation 'io.github.vaneproject:badwordfiltering:1.0.0'
}

tasks.named('bootBuildImage') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.sopt.lequuServer.domain.book.facade;

import static org.sopt.lequuServer.global.s3.enums.ImageFolderName.BOOK_FAVORITE_IMAGE_FOLDER_NAME;

import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.sopt.lequuServer.domain.book.dto.request.BookCreateRequestDto;
import org.sopt.lequuServer.domain.book.dto.response.BookCreateResponseDto;
Expand All @@ -18,10 +14,16 @@
import org.sopt.lequuServer.domain.sticker.model.PostedSticker;
import org.sopt.lequuServer.domain.sticker.repository.PostedStickerRepository;
import org.sopt.lequuServer.domain.sticker.repository.StickerRepository;
import org.sopt.lequuServer.global.BadWordFilterService;
import org.sopt.lequuServer.global.s3.service.S3Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

import static org.sopt.lequuServer.global.s3.enums.ImageFolderName.BOOK_FAVORITE_IMAGE_FOLDER_NAME;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -33,6 +35,7 @@ public class BookFacade {
private final NoteRepository noteRepository;
private final StickerRepository stickerRepository;
private final PostedStickerRepository postedStickerRepository;
private final BadWordFilterService badWordFilterService;
private final S3Service s3Service;

@Transactional
Expand All @@ -53,17 +56,17 @@ public BookCreateResponseDto createBook(BookCreateRequestDto request, Long membe
// Presigned URL 이미지 업로드하기 위한 이미지 파일명 가져오기
// String imageUrl = s3Service.getURL(BOOK_FAVORITE_IMAGE_FOLDER_NAME.getValue() + request.favoriteImage());
String imageUrl = s3Service.getCloudFrontURL(
BOOK_FAVORITE_IMAGE_FOLDER_NAME.getValue() + request.favoriteImage());
BOOK_FAVORITE_IMAGE_FOLDER_NAME.getValue() + request.favoriteImage());

Book book = Book.builder()
.uuid(bookUuid)
.favoriteName(request.favoriteName())
.favoriteImage(imageUrl)
.title(request.title())
.description(request.description())
.backgroundColor(request.backgroundColor())
.member(member)
.build();
.uuid(bookUuid)
.favoriteName(badWordFilterService.badWordChange(request.favoriteName()))
.favoriteImage(imageUrl)
.title(badWordFilterService.badWordChange(request.title()))
.description(badWordFilterService.badWordChange(request.description()))
.backgroundColor(request.backgroundColor())
.member(member)
.build();

return bookService.createBook(book, member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.sopt.lequuServer.domain.member.model.SocialPlatform;
import org.sopt.lequuServer.domain.member.repository.MemberRepository;
import org.sopt.lequuServer.domain.note.model.Note;
import org.sopt.lequuServer.global.BadWordFilterService;
import org.sopt.lequuServer.global.auth.fegin.kakao.KakaoLoginService;
import org.sopt.lequuServer.global.auth.jwt.JwtProvider;
import org.sopt.lequuServer.global.auth.jwt.TokenDto;
Expand All @@ -38,6 +39,7 @@ public class MemberService {

private final JwtProvider jwtProvider;
private final KakaoLoginService kakaoLoginService;
private final BadWordFilterService badWordFilterService;

@Transactional
public MemberLoginResponseDto login(String socialAccessToken, SocialLoginRequestDto request) {
Expand All @@ -50,8 +52,8 @@ public MemberLoginResponseDto login(String socialAccessToken, SocialLoginRequest
boolean isRegistered = isUserBySocialAndSocialId(socialPlatform, socialId);
if (!isRegistered) {
Member member = Member.builder()
.socialPlatform(socialPlatform)
.socialId(socialId).build();
.socialPlatform(socialPlatform)
.socialId(socialId).build();

memberRepository.save(member);
}
Expand Down Expand Up @@ -96,7 +98,7 @@ private void validateMemberId(Long memberId) {

private Member getUserBySocialAndSocialId(SocialPlatform socialPlatform, String socialId) {
return memberRepository.findBySocialPlatformAndSocialId(socialPlatform, socialId)
.orElseThrow(() -> new CustomException(NOT_FOUND_MEMBER_ERROR));
.orElseThrow(() -> new CustomException(NOT_FOUND_MEMBER_ERROR));
}

private boolean isUserBySocialAndSocialId(SocialPlatform socialPlatform, String socialId) {
Expand Down Expand Up @@ -125,8 +127,7 @@ public MemberNicknameResponseDto setMemberNickname(Long memberId, MemberNickname
}

Member member = memberRepository.findByIdOrThrow(memberId);

member.updateNickname(request.nickname().strip());
member.updateNickname(badWordFilterService.badWordChange(request.nickname().strip()));
return MemberNicknameResponseDto.of(memberId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.sopt.lequuServer.domain.note.facade;

import static org.sopt.lequuServer.global.s3.enums.ImageFolderName.NOTE_BACKGROUND_IMAGE_FOLDER_NAME;

import lombok.RequiredArgsConstructor;
import org.sopt.lequuServer.domain.book.model.Book;
import org.sopt.lequuServer.domain.book.repository.BookRepository;
Expand All @@ -11,10 +9,13 @@
import org.sopt.lequuServer.domain.note.dto.response.NoteCreateResponseDto;
import org.sopt.lequuServer.domain.note.model.Note;
import org.sopt.lequuServer.domain.note.service.NoteService;
import org.sopt.lequuServer.global.BadWordFilterService;
import org.sopt.lequuServer.global.s3.service.S3Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static org.sopt.lequuServer.global.s3.enums.ImageFolderName.NOTE_BACKGROUND_IMAGE_FOLDER_NAME;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -24,6 +25,7 @@ public class NoteFacade {
private final BookRepository bookRepository;
private final NoteService noteService;
private final S3Service s3Service;
private final BadWordFilterService badWordFilterService;

@Transactional
public NoteCreateResponseDto createNote(Long userId, NoteCreateDto noteCreateDto) {
Expand All @@ -36,6 +38,6 @@ public NoteCreateResponseDto createNote(Long userId, NoteCreateDto noteCreateDto
background = s3Service.getCloudFrontURL(NOTE_BACKGROUND_IMAGE_FOLDER_NAME.getValue() + noteCreateDto.background());
}

return noteService.saveNote(Note.of(noteCreateDto.content(), background, noteCreateDto.textColor(), member, book), member, book);
return noteService.saveNote(Note.of(badWordFilterService.badWordChange(noteCreateDto.content()), background, noteCreateDto.textColor(), member, book), member, book);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.sopt.lequuServer.global;

import com.vane.badwordfiltering.BadWordFiltering;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Collections;
import java.util.HashSet;


@Service
public class BadWordFilterService {

private final BadWordFiltering badWordFiltering;
private final HashSet<String> badWords;
private final String[] symbols;

public BadWordFilterService() throws IOException {
badWordFiltering = new BadWordFiltering();
symbols = new String[]{"!", "@", "#", "$", "%", "^", "&", "*", "_"};

// 비속어 필터링을 제외할 단어 설정 가능
String[] enableList = new String[]{"똥"};
for (String s : enableList) {
badWordFiltering.remove(s);
}

// ArrayList 대신 HashSet 사용하면 중복된 단어는 제외할 수 있음
badWords = new HashSet<>();

readBadWords("BadWord.txt");
badWordFiltering.addAll(badWords);
}

public String badWordChange(String string) {
return badWordFiltering.change(string, symbols);
}

private void readBadWords(String fileName) throws IOException {
// ClassLoader를 통해 resource 폴더의 경로를 가져옴 (ClassLoader를 사용해야 resource폴더에 있는 badword.txt가져올 수 있음)
ClassLoader classLoader = getClass().getClassLoader();
URL resourceUrl = classLoader.getResource(fileName);

try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceUrl.openStream()))) {
String line;

// , 을 기준으로 분리
while ((line = reader.readLine()) != null) {
String[] splitWords = line.split(", ");
Collections.addAll(badWords, splitWords);
}
}
}
}
Loading

0 comments on commit 8fd5e51

Please sign in to comment.