-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatMessageController.java
More file actions
147 lines (127 loc) · 5.33 KB
/
ChatMessageController.java
File metadata and controls
147 lines (127 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package app.dearobjet.backend.domain.chat.controller;
import app.dearobjet.backend.domain.chat.dto.ChatMessageResponse;
import app.dearobjet.backend.domain.chat.dto.ChatMessageCursorResponse;
import app.dearobjet.backend.domain.chat.dto.SendMessageRequest;
import app.dearobjet.backend.domain.chat.service.ChatMessageService;
import app.dearobjet.backend.domain.chat.service.ChatParticipantService;
import app.dearobjet.backend.global.api.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
* 채팅 메시지 REST API 컨트롤러
*/
@RestController
@RequestMapping("/api/chat")
@RequiredArgsConstructor
public class ChatMessageController {
private final ChatMessageService chatMessageService;
private final ChatParticipantService chatParticipantService;
/**
* 메시지 전송 (REST API 방식)
* WebSocket을 사용할 수 없는 환경에서 폴백으로 사용
*
* POST /api/chat/rooms/{roomId}/messages
*/
@PostMapping("/rooms/{roomId}/messages")
public ApiResponse<ChatMessageResponse> sendMessage(
@AuthenticationPrincipal(expression = "userId") Long userId,
@PathVariable String roomId,
@Valid @RequestBody SendMessageRequest request) {
ChatMessageResponse message = chatMessageService.sendMessage(userId, roomId, request);
return ApiResponse.of(message);
}
/**
* 최신 메시지 N개 조회 (커서 초기화)
*
* GET /api/chat/rooms/{roomId}/messages/latest?limit=50
*/
@GetMapping("/rooms/{roomId}/messages/latest")
public ApiResponse<ChatMessageCursorResponse> getLatestMessages(
@AuthenticationPrincipal(expression = "userId") Long userId,
@PathVariable String roomId,
@RequestParam(defaultValue = "50") int limit) {
ChatMessageCursorResponse response = chatMessageService.getLatestMessages(roomId, userId, limit);
return ApiResponse.of(response);
}
/**
* 누락 메시지 동기화 (재접속 복구)
*
* GET /api/chat/rooms/{roomId}/messages/sync?afterMessageId=123&limit=200
*/
@GetMapping("/rooms/{roomId}/messages/sync")
public ApiResponse<ChatMessageCursorResponse> syncMessages(
@AuthenticationPrincipal(expression = "userId") Long userId,
@PathVariable String roomId,
@RequestParam(defaultValue = "0") long afterMessageId,
@RequestParam(defaultValue = "200") int limit) {
ChatMessageCursorResponse response = chatMessageService.syncMessages(roomId, userId, afterMessageId, limit);
return ApiResponse.of(response);
}
/**
* 과거 메시지 더보기
*
* GET /api/chat/rooms/{roomId}/messages/before?beforeMessageId=123&limit=50
*/
@GetMapping("/rooms/{roomId}/messages/before")
public ApiResponse<ChatMessageCursorResponse> getMessagesBefore(
@AuthenticationPrincipal(expression = "userId") Long userId,
@PathVariable String roomId,
@RequestParam long beforeMessageId,
@RequestParam(defaultValue = "50") int limit) {
ChatMessageCursorResponse response = chatMessageService.getMessagesBefore(roomId, userId, beforeMessageId, limit);
return ApiResponse.of(response);
}
/**
* 채팅방 메시지 히스토리 조회
*
* GET /api/chat/rooms/{roomId}/messages?page=0&size=20
*/
@GetMapping("/rooms/{roomId}/messages")
public ApiResponse<List<ChatMessageResponse>> getMessages(
@AuthenticationPrincipal(expression = "userId") Long userId,
@PathVariable String roomId,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
List<ChatMessageResponse> messages = chatMessageService.getMessages(roomId, userId, page, size);
return ApiResponse.of(messages);
}
/**
* 읽음 처리
*
* POST /api/chat/rooms/{roomId}/read
*/
@PostMapping("/rooms/{roomId}/read")
public ApiResponse<Void> markAsRead(
@AuthenticationPrincipal(expression = "userId") Long userId,
@PathVariable String roomId) {
chatMessageService.markAsRead(userId, roomId);
return ApiResponse.of(null);
}
/**
* 사용자의 전체 읽지 않은 메시지 수 조회
*
* GET /api/chat/unread/total
*/
@GetMapping("/unread/total")
public ApiResponse<Map<String, Integer>> getTotalUnreadCount(
@AuthenticationPrincipal(expression = "userId") Long userId) {
int totalUnread = chatParticipantService.getTotalUnreadCount(userId);
return ApiResponse.of(Map.of("totalUnread", totalUnread));
}
/**
* 특정 채팅방의 읽지 않은 메시지 수 조회
*
* GET /api/chat/rooms/{roomId}/unread
*/
@GetMapping("/rooms/{roomId}/unread")
public ApiResponse<Map<String, Integer>> getUnreadCount(
@AuthenticationPrincipal(expression = "userId") Long userId,
@PathVariable String roomId) {
int unreadCount = chatParticipantService.getUnreadCount(userId, roomId);
return ApiResponse.of(Map.of("unreadCount", unreadCount));
}
}