Skip to content

Commit

Permalink
Merge pull request #110 from KUIT-Space/develop
Browse files Browse the repository at this point in the history
24.08.16 ๋ฐฐํฌ
  • Loading branch information
drbug2000 committed Aug 16, 2024
2 parents 868cb9b + 44e459a commit 1c0a7a8
Show file tree
Hide file tree
Showing 19 changed files with 321 additions and 79 deletions.
9 changes: 5 additions & 4 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,8 +61,8 @@ 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")
"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
@@ -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
44 changes: 43 additions & 1 deletion src/main/java/space/space_spring/controller/SpaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argumentResolver.userSpace.UserSpaceAuth;
import space.space_spring.argumentResolver.userSpace.UserSpaceId;
import space.space_spring.dto.pay.dto.PayReceiveInfoDto;
import space.space_spring.dto.pay.dto.PayRequestInfoDto;
import space.space_spring.dto.space.GetSpaceHomeDto;
import space.space_spring.dto.space.GetSpaceJoinDto;
import space.space_spring.dto.space.PostSpaceJoinDto;
import space.space_spring.dto.space.request.PostSpaceCreateDto;
Expand All @@ -17,11 +22,14 @@
import space.space_spring.entity.Space;
import space.space_spring.exception.CustomException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.PayService;
import space.space_spring.service.PostService;
import space.space_spring.service.S3Uploader;
import space.space_spring.service.SpaceService;
import space.space_spring.util.userSpace.UserSpaceUtils;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

import static space.space_spring.response.status.BaseExceptionResponseStatus.*;
Expand All @@ -38,6 +46,8 @@ public class SpaceController {
private final String spaceImgDirName = "spaceImg";
private final String userProfileImgDirName = "userProfileImg";
private final UserSpaceUtils userSpaceUtils;
private final PayService payService;
private final PostService postService;

@PostMapping("")
public BaseResponse<PostSpaceCreateDto.Response> createSpace(@JwtLoginAuth Long userId, @Validated @ModelAttribute PostSpaceCreateDto.Request postSpaceCreateRequest, BindingResult bindingResult) throws IOException {
Expand Down Expand Up @@ -162,7 +172,7 @@ private String processUserProfileImage(MultipartFile userProfileImg) throws IOEx
* ์œ ์ €์˜ ์ŠคํŽ˜์ด์Šค ๊ฐ€์ž… ์ฒ˜๋ฆฌ
*/
@PostMapping("/{spaceId}/join")
BaseResponse<String> joinUserToSpace(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @Validated @ModelAttribute PostSpaceJoinDto.Request request, BindingResult bindingResult) throws IOException {
public BaseResponse<String> joinUserToSpace(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @Validated @ModelAttribute PostSpaceJoinDto.Request request, BindingResult bindingResult) throws IOException {
if (bindingResult.hasErrors()) {
throw new CustomException(INVALID_SPACE_JOIN_REQUEST, getErrorMessage(bindingResult));
}
Expand All @@ -184,4 +194,36 @@ BaseResponse<String> joinUserToSpace(@JwtLoginAuth Long userId, @PathVariable Lo

return new BaseResponse<>("์œ ์ €์˜ ์ŠคํŽ˜์ด์Šค ๊ฐ€์ž… ์ฒ˜๋ฆฌ ์„ฑ๊ณต");
}

/**
* ์ŠคํŽ˜์ด์Šค ํ™ˆ ํ™”๋ฉด
*/
@GetMapping("/{spaceId}")
public BaseResponse<GetSpaceHomeDto.Response> showSpaceHome(@JwtLoginAuth Long userId, @PathVariable Long spaceId, @UserSpaceId Long userSpaceId, @UserSpaceAuth String userAuth) {
log.info("userId = {}, spaceId = {}, userSpaceID = {}, userAuth = {}", userId, spaceId, userSpaceId, userAuth);

// TODO 1. ์ŠคํŽ˜์ด์Šค ์ •๋ณด get
GetSpaceHomeDto.SpaceInfoForHome spaceInfoForHome = spaceService.getSpaceInfoForHome(spaceId);

// TODO 2. ํ•ด๋‹น ์ŠคํŽ˜์ด์Šค์—์„œ์˜ ์œ ์ € ์ •์‚ฐ ์ •๋ณด get
// ์œ ์ €๊ฐ€ ์š”์ฒญํ•œ ์ •์‚ฐ ์ค‘ ํ˜„์žฌ ์ง„ํ–‰์ค‘์ธ ์ •์‚ฐ ๋ฆฌ์ŠคํŠธ
// AND
// ์œ ์ €๊ฐ€ ์š”์ฒญ๋ฐ›์€ ์ •์‚ฐ ์ค‘ ํ˜„์žฌ ์ง„ํ–‰์ค‘์ธ ์ •์‚ฐ ๋ฆฌ์ŠคํŠธ
List<PayRequestInfoDto> payRequestInfoForUser = payService.getPayRequestInfoForUser(userId, spaceId, false);
List<PayReceiveInfoDto> payReceiveInfoForUser = payService.getPayReceiveInfoForUser(userId, spaceId, false);

// TODO 3. ํ•ด๋‹น ์ŠคํŽ˜์ด์Šค์˜ ๊ณต์ง€์‚ฌํ•ญ get
List<GetSpaceHomeDto.SpaceHomeNotice> noticeInfoForHome = postService.getNoticeInfoForHome(spaceId);

// TODO 4. return
return new BaseResponse<>(new GetSpaceHomeDto.Response(
spaceInfoForHome.getSpaceName(),
spaceInfoForHome.getSpaceProfileImg(),
payRequestInfoForUser,
payReceiveInfoForUser,
noticeInfoForHome,
spaceInfoForHome.getMemberNum(),
userAuth
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private boolean validateVoiceRoomInSpace(long spaceId,long voiceRoomId){
private boolean validateManagerPermission(String userSpaceAuth){
//ํ•ด๋‹น ์œ ์ €๊ฐ€ ํ˜„์žฌ space์— ๋Œ€ํ•ด ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ
if(!userSpaceAuth.equals(MANAGER.getAuth())){
throw new CustomException(VOICEROOM_DO_NOT_HAVE_PERMISSION);
throw new CustomException(UNAUTHORIZED_USER);
}
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/space/space_spring/dao/PostDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package space.space_spring.dao;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -20,4 +21,7 @@ public interface PostDao extends JpaRepository<Post, Long> {
"FROM PostLike l WHERE l.post.postId = :postId AND l.user.userId = :userId")
boolean isUserLikedPost(@Param("postId") Long postId, @Param("userId") Long userId);

@Query("SELECT p FROM Post p WHERE p.space = :space AND p.type = :type AND p.status = 'ACTIVE' ORDER BY p.createdAt DESC")
List<Post> findBySpaceAndTypeSortedByNewest(@Param("space") Space space, @Param("type") String type, Pageable pageable);

}
Loading

0 comments on commit 1c0a7a8

Please sign in to comment.