A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
  1. package org.ros.chatto.controller;
  2. import java.security.Principal;
  3. import java.time.Instant;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.stream.Collectors;
  7. import javax.validation.Valid;
  8. import org.ros.chatto.dto.ActiveUserDTO;
  9. import org.ros.chatto.dto.ChatMessageDTO;
  10. import org.ros.chatto.dto.MessageCipherDTO;
  11. import org.ros.chatto.error.ErrorModel;
  12. import org.ros.chatto.error.ErrorResponse;
  13. import org.ros.chatto.service.ChatService;
  14. import org.ros.chatto.service.UserService;
  15. import org.springframework.http.HttpStatus;
  16. import org.springframework.http.ResponseEntity;
  17. import org.springframework.validation.BindingResult;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.PathVariable;
  20. import org.springframework.web.bind.annotation.PostMapping;
  21. import org.springframework.web.bind.annotation.RequestBody;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RequestParam;
  24. import org.springframework.web.bind.annotation.RestController;
  25. import lombok.RequiredArgsConstructor;
  26. @RestController
  27. @RequestMapping("/api/chat")
  28. @RequiredArgsConstructor
  29. public class ChatMessageController {
  30. private final ChatService chatService;
  31. private final UserService userService;
  32. @PostMapping(value = "/post/message", consumes = { "application/json" })
  33. public ResponseEntity<?> newMessage(
  34. @RequestBody @Valid final ChatMessageDTO chatMessageDTO,
  35. final BindingResult bindingResult, final Principal principal) {
  36. if (bindingResult.hasErrors()) {
  37. return new ResponseEntity<ErrorResponse>(
  38. handleValidationErrors(bindingResult),
  39. HttpStatus.BAD_REQUEST);
  40. }
  41. final MessageCipherDTO messageCipher = chatMessageDTO
  42. .getMessageCipher();
  43. final String fromUser = principal.getName();
  44. final String toUser = chatMessageDTO.getToUser();
  45. System.out.println("Message cipher = " + messageCipher);
  46. final ChatMessageDTO chatMessageDTOSaved = chatService
  47. .createMessage(fromUser, toUser, messageCipher);
  48. return new ResponseEntity<ChatMessageDTO>(chatMessageDTOSaved,
  49. HttpStatus.CREATED);
  50. }
  51. private ErrorResponse handleValidationErrors(
  52. final BindingResult bindingResult) {
  53. final List<ErrorModel> errorMessages = bindingResult.getFieldErrors()
  54. .stream()
  55. .map(err -> new ErrorModel(err.getField(),
  56. err.getRejectedValue(), err.getDefaultMessage()))
  57. .distinct().collect(Collectors.toList());
  58. return ErrorResponse.builder().errorMessage(errorMessages).build();
  59. }
  60. @GetMapping(value = "/get/messages/{userName}")
  61. public List<ChatMessageDTO> getAllMessages(
  62. @PathVariable final String userName, final Principal principal) {
  63. final List<ChatMessageDTO> chatMessageDTOs = chatService
  64. .getAllMessages(principal.getName(), userName);
  65. return chatMessageDTOs;
  66. }
  67. @GetMapping(value = "/get/messages/{userName}", params = { "page", "size" })
  68. public List<ChatMessageDTO> findPaginated(
  69. @RequestParam("page") final int page,
  70. @RequestParam("size") final int size,
  71. @PathVariable final String userName, final Principal principal) {
  72. final List<ChatMessageDTO> chatMessageDTOs = chatService
  73. .getMessagePage(principal.getName(), userName, page, size);
  74. return chatMessageDTOs;
  75. }
  76. @GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
  77. public List<ChatMessageDTO> sendNewMessages(
  78. @PathVariable final String userName,
  79. @PathVariable final Instant lastMessageTime,
  80. final Principal principal) {
  81. final List<ChatMessageDTO> chatMessageDTOs = chatService.getNewMessages(
  82. principal.getName(), userName, Date.from(lastMessageTime));
  83. return chatMessageDTOs;
  84. }
  85. @GetMapping("/get/users")
  86. public List<String> getAllOtherUsers(final Principal principal) {
  87. return userService.findAllOtherUsers(principal.getName());
  88. }
  89. @GetMapping("/get/active-users")
  90. public List<ActiveUserDTO> getAllOtherActiveUsers(
  91. final Principal principal) {
  92. return userService.getOtherActiveUsers(principal.getName());
  93. }
  94. @GetMapping("/get/token")
  95. public ResponseEntity<?> getToken() {
  96. return new ResponseEntity<String>(HttpStatus.OK);
  97. }
  98. }