Skip to content

Commit ae2711d

Browse files
multiple fixes and clean
1 parent a010e68 commit ae2711d

File tree

59 files changed

+922
-749
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+922
-749
lines changed

backend/src/main/java/com/mercure/config/HandShakeInterceptor.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
package com.mercure.config;
22

33
import com.mercure.service.UserService;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
46
import org.springframework.beans.factory.annotation.Autowired;
57
import org.springframework.context.annotation.Configuration;
68
import org.springframework.http.server.ServerHttpRequest;
79
import org.springframework.http.server.ServerHttpResponse;
810
import org.springframework.http.server.ServletServerHttpRequest;
911
import org.springframework.util.StringUtils;
12+
import org.springframework.web.socket.CloseStatus;
1013
import org.springframework.web.socket.WebSocketHandler;
14+
import org.springframework.web.socket.WebSocketMessage;
15+
import org.springframework.web.socket.WebSocketSession;
1116
import org.springframework.web.socket.server.HandshakeInterceptor;
1217

1318
import javax.servlet.http.HttpSession;
1419
import java.util.Map;
1520

1621
@Configuration
17-
public class HandShakeInterceptor implements HandshakeInterceptor {
22+
public class HandShakeInterceptor implements HandshakeInterceptor, WebSocketHandler {
1823

1924
@Autowired
2025
private UserService userService;
2126

27+
private Logger log = LoggerFactory.getLogger(HandShakeInterceptor.class);
28+
2229
@Override
2330
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) {
2431
String jwtToken = request.getURI().getQuery().substring(6);
@@ -31,13 +38,38 @@ public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse res
3138
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
3239
HttpSession session = servletRequest.getServletRequest().getSession();
3340
attributes.put("sessionId", session.getId());
34-
userService.getMyMap().put(userId, session.getId());
41+
userService.getWsSessions().put(userId, session.getId());
3542
}
3643
return !StringUtils.isEmpty(name);
3744
}
3845

3946
@Override
4047
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
48+
// log.info("handshake finished");
49+
}
50+
51+
@Override
52+
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
53+
54+
}
55+
56+
@Override
57+
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
58+
4159
}
4260

61+
@Override
62+
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
63+
64+
}
65+
66+
@Override
67+
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
68+
log.info("connection closed : {}", session);
69+
}
70+
71+
@Override
72+
public boolean supportsPartialMessages() {
73+
return false;
74+
}
4375
}

backend/src/main/java/com/mercure/controller/WsController.java

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import com.mercure.dto.*;
44
import com.mercure.entity.MessageEntity;
5-
import com.mercure.entity.UserEntity;
6-
import com.mercure.service.GroupService;
7-
import com.mercure.service.GroupUserJoinService;
8-
import com.mercure.service.MessageService;
9-
import com.mercure.service.UserService;
5+
import com.mercure.entity.MessageUserEntity;
6+
import com.mercure.service.*;
107
import com.mercure.utils.*;
118
import org.json.simple.JSONObject;
129
import org.json.simple.parser.JSONParser;
@@ -52,6 +49,9 @@ public class WsController {
5249
@Autowired
5350
private GroupUserJoinService groupUserJoinService;
5451

52+
@Autowired
53+
private UserSeenMessageService seenMessageService;
54+
5555
@GetMapping
5656
public String testRoute(HttpServletRequest request) {
5757
String requestTokenHeader = request.getHeader("authorization");
@@ -63,7 +63,7 @@ public String testRoute(HttpServletRequest request) {
6363

6464
@MessageMapping("/message")
6565
public void mainChannel(InputTransportDTO dto, @Header("simpSessionId") String sessionId) {
66-
Map<Integer, String> sessions = userService.getMyMap();
66+
Map<Integer, String> sessions = userService.getWsSessions();
6767
TransportActionEnum action = dto.getAction();
6868
switch (action) {
6969
case INIT_USER_DATA:
@@ -82,22 +82,26 @@ public void mainChannel(InputTransportDTO dto, @Header("simpSessionId") String s
8282
if (dto.getGroupUrl().equals("") || groupUserJoinService.checkIfUserIsAuthorizedInGroup(dto.getUserId(), groupId)) {
8383
break;
8484
}
85-
List<MessageDTO> messages = this.getConversationMessage(dto.getGroupUrl());
85+
WrapperMessageDTO messages = this.getConversationMessage(dto.getGroupUrl(), dto.getMessageId());
8686
OutputTransportDTO resMessages = new OutputTransportDTO();
87-
resMessages.setAction(TransportActionEnum.FETCH_GROUP_MESSAGES);
87+
if (dto.getMessageId() == -1) {
88+
resMessages.setAction(TransportActionEnum.FETCH_GROUP_MESSAGES);
89+
} else {
90+
resMessages.setAction(TransportActionEnum.ADD_CHAT_HISTORY);
91+
}
8892
resMessages.setObject(messages);
8993
this.messagingTemplate.convertAndSend("/topic/user/" + dto.getUserId(), resMessages);
9094
}
9195
break;
92-
// case GRANT_USER_ADMIN:
93-
// String grantResponse = doUserAction(dto.getWsToken(), dto.getUserId(), dto.getGroupUrl(), dto.getAction());
94-
// OutputTransportDTO res = new OutputTransportDTO();
95-
// res.setAction(TransportActionEnum.GRANT_USER_ADMIN);
96-
// MessageDTO m = new MessageDTO();
97-
// m.setSender();
98-
// m.setMessage(grantResponse);
99-
// res.setObject(grantResponse);
100-
// break;
96+
case MARK_MESSAGE_AS_SEEN:
97+
if (!dto.getGroupUrl().equals("")) {
98+
int messageId = messageService.findLastMessageIdByGroupId(groupService.findGroupByUrl(dto.getGroupUrl()));
99+
MessageUserEntity messageUserEntity = seenMessageService.findByMessageId(messageId, dto.getUserId());
100+
if (messageUserEntity == null) break;
101+
messageUserEntity.setSeen(true);
102+
seenMessageService.saveMessageUserEntity(messageUserEntity);
103+
}
104+
break;
101105
default:
102106
break;
103107
}
@@ -138,14 +142,18 @@ public void getAndSaveMessage(int userId, String groupUrl, String message) {
138142
}
139143
MessageEntity messageEntity = new MessageEntity(userId, groupId, MessageTypeEnum.TEXT.toString(), message);
140144
MessageEntity msg = messageService.save(messageEntity);
141-
MessageDTO messageDTO = messageService.createNotificationMessageDTO(msg);
142145
List<Integer> toSend = messageService.createNotificationList(userId, groupUrl);
146+
147+
// Save seen message
148+
seenMessageService.saveMessageNotSeen(msg, groupId);
149+
143150
OutputTransportDTO dto = new OutputTransportDTO();
144151
dto.setAction(TransportActionEnum.NOTIFICATION_MESSAGE);
145-
dto.setObject(messageDTO);
146-
toSend.forEach(toUserId -> messagingTemplate.convertAndSend("/topic/user/" + toUserId, dto));
147-
messageService.createMessageDTO(msg.getId(), msg.getType(), msg.getUser_id(), msg.getCreatedAt().toString(), msg.getGroup_id(), msg.getMessage());
148-
152+
toSend.forEach(toUserId -> {
153+
MessageDTO messageDTO = messageService.createNotificationMessageDTO(msg, toUserId);
154+
dto.setObject(messageDTO);
155+
messagingTemplate.convertAndSend("/topic/user/" + toUserId, dto);
156+
});
149157
}
150158

151159
@MessageMapping("/message/call/{userId}/group/{groupUrl}")
@@ -181,44 +189,24 @@ public void wsCreateConversation(String req) throws ParseException {
181189
* @param url The group url to map
182190
* @return List of message
183191
*/
184-
public List<MessageDTO> getConversationMessage(String url) {
192+
public WrapperMessageDTO getConversationMessage(String url, int messageId) {
193+
WrapperMessageDTO wrapper = new WrapperMessageDTO();
185194
if (url != null) {
186195
List<MessageDTO> messageDTOS = new ArrayList<>();
187196
int groupId = groupService.findGroupByUrl(url);
188-
messageService.findByGroupId(groupId).forEach(msg -> {
189-
messageDTOS.add(messageService.createMessageDTO(msg.getId(), msg.getType(), msg.getUser_id(), msg.getCreatedAt().toString(), msg.getGroup_id(), msg.getMessage()));
190-
});
191-
return messageDTOS;
197+
List<MessageEntity> newMessages = messageService.findByGroupId(groupId, messageId);
198+
int lastMessageId = newMessages != null && newMessages.size() != 0 ? newMessages.get(0).getId() : 0;
199+
List<MessageEntity> afterMessages = messageService.findByGroupId(groupId, lastMessageId);
200+
if (newMessages != null) {
201+
wrapper.setLastMessage(afterMessages != null && afterMessages.size() == 0);
202+
newMessages.forEach(msg ->
203+
messageDTOS.add(messageService
204+
.createMessageDTO(msg.getId(), msg.getType(), msg.getUser_id(), msg.getCreatedAt().toString(), msg.getGroup_id(), msg.getMessage()))
205+
);
206+
}
207+
wrapper.setMessages(messageDTOS);
208+
return wrapper;
192209
}
193210
return null;
194211
}
195-
196-
// private String doUserAction(String wsToken, Integer userIdToChange, String groupUrl, TransportActionEnum action) {
197-
// int groupId = groupService.findGroupByUrl(groupUrl);
198-
// int userAdminRequestId = userService.findUserIdWithToken(wsToken);
199-
// UserEntity userEntity = userService.findById(userIdToChange);
200-
// if (userEntity != null) {
201-
// int adminUserId = userEntity.getId();
202-
// if (action.equals("removeUser")) {
203-
// groupUserJoinService.removeUserFromConversation(userAdminRequestId, groupId);
204-
// }
205-
// if (userService.checkIfUserIsAdmin(adminUserId, groupId)) {
206-
// try {
207-
// if (action.equals(TransportActionEnum.GRANT_USER_ADMIN)) {
208-
// groupUserJoinService.grantUserAdminInConversation(userIdToChange, groupId);
209-
// return userEntity.getFirstName() + " has been granted administrator to " + groupService.getGroupName(groupUrl);
210-
// }
211-
// if (action.equals("delete")) {
212-
// groupUserJoinService.removeUserFromConversation(userIdToChange, groupId);
213-
// }
214-
// if (action.equals("removeAdmin")) {
215-
// groupUserJoinService.removeUserAdminFromConversation(userIdToChange, groupId);
216-
// }
217-
// } catch (Exception e) {
218-
// log.warn("Error during performing {} : {}", action, e.getMessage());
219-
// }
220-
// }
221-
// }
222-
// return "";
223-
// }
224212
}

backend/src/main/java/com/mercure/controller/WsFileController.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import com.mercure.dto.MessageDTO;
44
import com.mercure.dto.NotificationDTO;
5+
import com.mercure.dto.OutputTransportDTO;
56
import com.mercure.entity.FileEntity;
67
import com.mercure.entity.MessageEntity;
78
import com.mercure.service.GroupService;
89
import com.mercure.service.MessageService;
910
import com.mercure.service.StorageService;
11+
import com.mercure.service.UserSeenMessageService;
1012
import com.mercure.utils.MessageTypeEnum;
13+
import com.mercure.utils.TransportActionEnum;
1114
import org.json.simple.JSONObject;
1215
import org.slf4j.Logger;
1316
import org.slf4j.LoggerFactory;
@@ -42,6 +45,9 @@ public class WsFileController {
4245
@Autowired
4346
private StorageService storageService;
4447

48+
@Autowired
49+
private UserSeenMessageService seenMessageService;
50+
4551
/**
4652
* Receive file to put in DB and send it back to the group conversation
4753
*
@@ -52,18 +58,17 @@ public class WsFileController {
5258
*/
5359
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
5460
public ResponseEntity<?> uploadFile(@RequestParam(name = "file") MultipartFile file, @RequestParam(name = "userId") int userId, @RequestParam(name = "groupUrl") String groupUrl) {
55-
FileEntity fileEntity = new FileEntity();
5661
int groupId = groupService.findGroupByUrl(groupUrl);
5762
try {
5863
MessageEntity messageEntity = messageService.createAndSaveMessage(userId, groupId, MessageTypeEnum.FILE.toString(), "has send a file");
5964
storageService.store(file, messageEntity.getId());
65+
OutputTransportDTO res = new OutputTransportDTO();
6066
NotificationDTO notificationDTO = messageService.createNotificationDTO(messageEntity);
67+
res.setAction(TransportActionEnum.NOTIFICATION_MESSAGE);
68+
res.setObject(notificationDTO);
69+
seenMessageService.saveMessageNotSeen(messageEntity, groupId);
6170
List<Integer> toSend = messageService.createNotificationList(userId, groupUrl);
62-
toSend.forEach(toUserId -> messagingTemplate.convertAndSend("/topic/user/" + toUserId, notificationDTO));
63-
// JSONObject jsonObject = new JSONObject();
64-
// jsonObject.put("url", fileEntity.getUrl());
65-
// jsonObject.put("date", fileEntity.getCreatedAt());
66-
// jsonObject.put("name", fileEntity.getFilename());
71+
toSend.forEach(toUserId -> messagingTemplate.convertAndSend("/topic/user/" + toUserId, res));
6772
} catch (Exception e) {
6873
log.error("Cannot save file, caused by {}", e.getMessage());
6974
return ResponseEntity.status(500).build();

backend/src/main/java/com/mercure/dto/GroupDTO.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public GroupDTO(int id, String url, String name, String lastMessage, String last
2828

2929
private String groupType;
3030

31+
private String lastMessageSender;
32+
3133
private String lastMessage;
3234

3335
private String lastMessageDate;
@@ -66,6 +68,14 @@ public void setGroupType(String groupType) {
6668
this.groupType = groupType;
6769
}
6870

71+
public String getLastMessageSender() {
72+
return lastMessageSender;
73+
}
74+
75+
public void setLastMessageSender(String lastMessageSender) {
76+
this.lastMessageSender = lastMessageSender;
77+
}
78+
6979
public String getLastMessage() {
7080
return lastMessage;
7181
}

backend/src/main/java/com/mercure/dto/InputTransportDTO.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class InputTransportDTO {
1414

1515
private String message;
1616

17+
private int messageId;
18+
1719
public int getUserId() {
1820
return userId;
1921
}
@@ -33,4 +35,8 @@ public String getGroupUrl() {
3335
public String getMessage() {
3436
return message;
3537
}
38+
39+
public int getMessageId() {
40+
return messageId;
41+
}
3642
}

backend/src/main/java/com/mercure/dto/MessageDTO.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class MessageDTO {
55
public MessageDTO() {
66
}
77

8-
public MessageDTO(int id, String type, String message, int userId, int groupId, String groupUrl, String sender, String time, String initials, String color, String fileUrl) {
8+
public MessageDTO(int id, String type, String message, int userId, int groupId, String groupUrl, String sender, String time, String initials, String color, String fileUrl, boolean isMessageSeen) {
99
this.id = id;
1010
this.type = type;
1111
this.message = message;
@@ -17,6 +17,7 @@ public MessageDTO(int id, String type, String message, int userId, int groupId,
1717
this.initials = initials;
1818
this.color = color;
1919
this.fileUrl = fileUrl;
20+
this.isMessageSeen = isMessageSeen;
2021
}
2122

2223
private int id;
@@ -41,6 +42,8 @@ public MessageDTO(int id, String type, String message, int userId, int groupId,
4142

4243
private String fileUrl;
4344

45+
private boolean isMessageSeen;
46+
4447
public int getId() {
4548
return id;
4649
}
@@ -128,4 +131,12 @@ public String getFileUrl() {
128131
public void setFileUrl(String fileUrl) {
129132
this.fileUrl = fileUrl;
130133
}
134+
135+
public boolean isMessageSeen() {
136+
return isMessageSeen;
137+
}
138+
139+
public void setMessageSeen(boolean messageSeen) {
140+
isMessageSeen = messageSeen;
141+
}
131142
}

0 commit comments

Comments
 (0)