Skip to content

Commit

Permalink
Merge branch 'develop' into refactoring/#100/voice-room-data-save
Browse files Browse the repository at this point in the history
  • Loading branch information
drbug2000 committed Aug 16, 2024
2 parents 6004272 + 9080b49 commit 81f0f4c
Show file tree
Hide file tree
Showing 33 changed files with 587 additions and 85 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // JPA ์Šคํƒ€ํ„ฐ ์ถ”๊ฐ€
runtimeOnly 'com.h2database:h2' // test์šฉ db์ธ h2Database ์ถ”๊ฐ€
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0' // p6spy ์„ค์ • ์ถ”๊ฐ€
// implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0' // p6spy ์„ค์ • ์ถ”๊ฐ€
implementation 'org.springframework.boot:spring-boot-starter-validation' // validation ์ถ”๊ฐ€
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-websocket' // webSocket ์ถ”๊ฐ€
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/space/space_spring/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuthHandlerArgumentResolver;
import space.space_spring.argumentResolver.userSpace.UserSpaceAuth;
import space.space_spring.argumentResolver.userSpace.UserSpaceAuthHandlerArgumentResolver;
import space.space_spring.argumentResolver.userSpace.UserSpaceIdHandlerArgumentResolver;
import space.space_spring.config.interceptorURL.JwtLoginInterceptorURL;
import space.space_spring.config.interceptorURL.UserSpaceValidationInterceptorURL;
import space.space_spring.interceptor.UserSpaceValidationInterceptor;
import space.space_spring.interceptor.jwtLogin.JwtLoginAuthInterceptor;

Expand All @@ -34,7 +35,7 @@ public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtLoginAuthInterceptor)
.order(1);

for (InterceptorURL interceptorURL : InterceptorURL.values()) {
for (JwtLoginInterceptorURL interceptorURL : JwtLoginInterceptorURL.values()) {
registration.addPathPatterns(interceptorURL.getUrlPattern());
}

Expand All @@ -60,9 +61,9 @@ public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("http://localhost:3000/", "http://localhost:5173/", "https://localhost:5173/",
"http://localhost:5173/KUIT-Space-Front/", "https://localhost:5173/KUIT-Space-Front/",
"https://kuit-space.github.io/KUIT-Space-front/")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.exposedHeaders("location")
"https://kuit-space.github.io/", "https://kuit-space-front.vercel.app/")
.allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH")
.exposedHeaders("location", "Authorization")
.allowedHeaders("*")
.allowCredentials(true);
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/space/space_spring/config/WebSocketConfig.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package space.space_spring.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import space.space_spring.interceptor.jwtSocket.JwtChannelInterceptor;

@Configuration
Expand All @@ -24,6 +27,7 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {
.withSockJS();
}


@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
// ๋ฉ”์„ธ์ง€ ๊ตฌ๋…(์ˆ˜์‹ ) ์š”์ฒญ์˜ ์—”๋“œํฌ์ธํŠธ
Expand All @@ -37,4 +41,21 @@ public void configureMessageBroker(MessageBrokerRegistry registry) {
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(jwtChannelInterceptor);
}

@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
registration.setMessageSizeLimit(50 * 1024 * 1024); // 50MB
registration.setSendBufferSizeLimit(50 * 1024 * 1024); // 50MB
registration.setSendTimeLimit(20 * 10000); // ๋” ๊ธด ์‹œ๊ฐ„ ์ œํ•œ ์„ค์ •
}

@Bean
public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
ServletServerContainerFactoryBean factoryBean = new ServletServerContainerFactoryBean();
factoryBean.setMaxTextMessageBufferSize(50 * 1028 * 1024);
factoryBean.setMaxBinaryMessageBufferSize(50 * 1028 * 1024);
factoryBean.setMaxSessionIdleTimeout(20 * 10000L);
factoryBean.setAsyncSendTimeout(20 * 10000L);
return factoryBean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package space.space_spring.config.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;

import java.io.IOException;
@Component
public class RequestCachingFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
filterChain.doFilter(wrappedRequest, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package space.space_spring.config;
package space.space_spring.config.interceptorURL;

import lombok.Getter;

@Getter
public enum InterceptorURL {
public enum JwtLoginInterceptorURL {
SPACE("/space/**"),
TEST("/test/**"),
SPACE_LIST_FOR_USER("/user/space-choice"),
Expand All @@ -12,7 +12,7 @@ public enum InterceptorURL {

private final String urlPattern;

InterceptorURL(String urlPattern) {
JwtLoginInterceptorURL(String urlPattern) {
this.urlPattern = urlPattern;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package space.space_spring.config;
package space.space_spring.config.interceptorURL;

import lombok.Getter;

@Getter
public enum UserSpaceValidationInterceptorURL {
//SPACE("/space/**"),
TEST("/space/{spaceId}/test/**"),
VOICEROOM("/space/{spaceId}/voiceRoom/**")
// VOICEROOM("/space/{spaceId}/voiceRoom/**"),
// CHATROOM("/space/{spaceId}/chat/**")
SPACE("/space/{spaceId}/**")
;

private final String urlPattern;
Expand Down
46 changes: 26 additions & 20 deletions src/main/java/space/space_spring/controller/ChatRoomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argumentResolver.userSpace.UserSpaceAuth;
import space.space_spring.dto.chat.request.CreateChatRoomRequest;
import space.space_spring.dto.chat.request.JoinChatRoomRequest;
import space.space_spring.dto.chat.response.ChatSuccessResponse;
Expand All @@ -19,6 +20,7 @@

import java.io.IOException;

import static space.space_spring.entity.enumStatus.UserSpaceAuth.MANAGER;
import static space.space_spring.response.status.BaseExceptionResponseStatus.*;
import static space.space_spring.util.bindingResult.BindingResultUtils.getErrorMessage;

Expand All @@ -45,17 +47,16 @@ public BaseResponse<ReadChatRoomResponse> readChatRooms(@JwtLoginAuth Long userI
public BaseResponse<CreateChatRoomResponse> createChatRoom(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@UserSpaceAuth String userSpaceAuth,
@Validated @ModelAttribute CreateChatRoomRequest createChatRoomRequest,
BindingResult bindingResult) throws IOException {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new CustomException(UNAUTHORIZED_USER);
}

if (bindingResult.hasErrors()) {
throw new CustomException(INVALID_CHATROOM_CREATE, getErrorMessage(bindingResult));
}

validateManagerPermission(userSpaceAuth);

String chatRoomDirName = "chatRoomImg";
String chatRoomImgUrl = s3Uploader.upload(createChatRoomRequest.getImg(), chatRoomDirName);

Expand All @@ -66,11 +67,13 @@ public BaseResponse<CreateChatRoomResponse> createChatRoom(
* ํŠน์ • ์ฑ„ํŒ…๋ฐฉ์˜ ๋ชจ๋“  ์œ ์ € ์ •๋ณด ์กฐํšŒ
*/
@GetMapping("/{chatRoomId}/member")
public BaseResponse<ReadChatRoomMemberResponse> readChatRoomMembers(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @PathVariable Long chatRoomId) {
public BaseResponse<ReadChatRoomMemberResponse> readChatRoomMembers(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long chatRoomId,
@UserSpaceAuth String userSpaceAuth) {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new CustomException(UNAUTHORIZED_USER);
}
validateManagerPermission(userSpaceAuth);

return new BaseResponse<>(chatRoomService.readChatRoomMembers(spaceId, chatRoomId));
}
Expand All @@ -83,17 +86,16 @@ public BaseResponse<ChatSuccessResponse> joinChatRoom(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long chatRoomId,
@UserSpaceAuth String userSpaceAuth,
@RequestBody JoinChatRoomRequest joinChatRoomRequest,
BindingResult bindingResult) {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new CustomException(UNAUTHORIZED_USER);
}

if(bindingResult.hasErrors()){
throw new CustomException(INVALID_CHATROOM_JOIN,getErrorMessage(bindingResult));
}

validateManagerPermission(userSpaceAuth);

return new BaseResponse<>(chatRoomService.joinChatRoom(chatRoomId, joinChatRoomRequest));
}

Expand All @@ -105,11 +107,10 @@ public BaseResponse<ChatSuccessResponse> modifyChatRoomName(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long chatRoomId,
@RequestParam String name) {
@RequestParam String name,
@UserSpaceAuth String userSpaceAuth) {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new CustomException(UNAUTHORIZED_USER);
}
validateManagerPermission(userSpaceAuth);

return new BaseResponse<>(chatRoomService.modifyChatRoomName(chatRoomId, name));
}
Expand All @@ -132,12 +133,17 @@ public BaseResponse<ChatSuccessResponse> exitChatRoom(
public BaseResponse<ChatSuccessResponse> deleteChatRoom(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long chatRoomId) {
@PathVariable Long chatRoomId,
@UserSpaceAuth String userSpaceAuth) {

if (!userSpaceUtils.isUserManager(userId, spaceId)) {
throw new CustomException(UNAUTHORIZED_USER);
}
validateManagerPermission(userSpaceAuth);

return new BaseResponse<>(chatRoomService.deleteChatRoom(chatRoomId));
}

private void validateManagerPermission(String userSpaceAuth){
if(!userSpaceAuth.equals(MANAGER.getAuth())){
throw new CustomException(UNAUTHORIZED_USER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
import space.space_spring.argumentResolver.userSpace.CheckUserSpace;
import space.space_spring.dto.chat.request.ChatMessageRequest;
import space.space_spring.dto.chat.response.ChatMessageLogResponse;
import space.space_spring.dto.chat.response.ChatMessageResponse;
import space.space_spring.service.ChattingService;
import space.space_spring.service.UserChatRoomService;

import java.io.IOException;
import java.util.Map;

@Slf4j
Expand All @@ -27,15 +29,17 @@ public class ChattingController {

@MessageMapping("/chat/{chatRoomId}") // {chatRoomId} ์ฑ„ํŒ…๋ฐฉ์œผ๋กœ ๋ณด๋‚ธ ๋ฉ”์„ธ์ง€ ๋งคํ•‘
@SendTo("/topic/chat/{chatRoomId}") // {chatRoomId} ์ฑ„ํŒ…๋ฐฉ์„ ๊ตฌ๋…ํ•œ ๊ณณ๋“ค๋กœ ๋ฉ”์„ธ์ง€ ์ „์†ก
@CheckUserSpace(required = false)
public ChatMessageResponse sendChatMessage (@Payload ChatMessageRequest chatMessageRequest, @DestinationVariable Long chatRoomId,
@Header("simpSessionAttributes") Map<String, Object> sessionAttributes) {
@Header("simpSessionAttributes") Map<String, Object> sessionAttributes) throws IOException {
Long senderId = (Long) sessionAttributes.get("userId");
// log.info(senderId + " ๋‹˜์ด " + chatRoomId + " ์ฑ„ํŒ…๋ฐฉ์œผ๋กœ " + chatMessageRequest.getContent() + " ์ „์†ก");

return chattingService.sendChatMessage(senderId, chatMessageRequest, chatRoomId);
}

@SubscribeMapping("/chat/{chatRoomId}") // {chatRoomId} ์ฑ„ํŒ…๋ฐฉ์„ ๊ตฌ๋…
@CheckUserSpace(required = false)
public ChatMessageLogResponse subscribeChatRoom (@DestinationVariable Long chatRoomId, @Header("simpSessionAttributes") Map<String, Object> sessionAttributes) {
// log.info(chatRoomId + " ์ฑ„ํŒ…๋ฐฉ ๊ตฌ๋…");
sessionAttributes.put("chatRoomId", chatRoomId);
Expand All @@ -44,6 +48,7 @@ public ChatMessageLogResponse subscribeChatRoom (@DestinationVariable Long chatR

// socket disconnect ์‹œ ํ˜ธ์ถœ
@EventListener
@CheckUserSpace(required = false)
public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
Map<String, Object> sessionAttributes = headerAccessor.getSessionAttributes();
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/space/space_spring/controller/LikeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package space.space_spring.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.LikeService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/space/{spaceId}/board/post/{postId}")
@Slf4j
public class LikeController {

private final LikeService likeService;

// ๊ฒŒ์‹œ๊ธ€ ์ข‹์•„์š”
@PostMapping("/like")
public BaseResponse<String> likePost(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long postId
) {
// TODO 1: ์œ ์ €๊ฐ€ ์ŠคํŽ˜์ด์Šค์— ์†ํ•˜๋Š”์ง€ ๊ฒ€์ฆ
likeService.validateUserInSpace(userId, spaceId);

// TODO 2: ์œ ์ €๊ฐ€ ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์— ์ด๋ฏธ ์ข‹์•„์š”๋ฅผ ๋ˆŒ๋ €๋Š”์ง€ ๊ฒ€์ฆ
likeService.validateAlreadyLiked(userId, postId);

// TODO 3: ์ข‹์•„์š” ๋กœ์ง ์ˆ˜ํ–‰
likeService.likePost(userId, postId);

return new BaseResponse<>("๊ฒŒ์‹œ๊ธ€์— ์ข‹์•„์š”๋ฅผ ๋ˆŒ๋ €์Šต๋‹ˆ๋‹ค.");
}


// ๊ฒŒ์‹œ๊ธ€ ์ข‹์•„์š” ์ทจ์†Œ
@DeleteMapping("/like")
public BaseResponse<String> unlikePost(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long postId
) {
// TODO 1: ์œ ์ €๊ฐ€ ์ŠคํŽ˜์ด์Šค์— ์†ํ•˜๋Š”์ง€ ๊ฒ€์ฆ
likeService.validateUserInSpace(userId, spaceId);

// TODO 2: ์œ ์ €๊ฐ€ ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์— ์ข‹์•„์š”๋ฅผ ๋ˆŒ๋ €๋Š”์ง€ ๊ฒ€์ฆ
likeService.validateNotLikedYet(userId, postId);

// TODO 3: ์ข‹์•„์š” ์ทจ์†Œ ๋กœ์ง ์ˆ˜ํ–‰
likeService.unlikePost(userId, postId);

return new BaseResponse<>("๊ฒŒ์‹œ๊ธ€์— ์ข‹์•„์š”๋ฅผ ์ทจ์†Œํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
}
}
Loading

0 comments on commit 81f0f4c

Please sign in to comment.