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.

73 lines
2.5 KiB

4 years ago
4 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 javax.validation.Valid;
  7. import org.ros.chatto.dto.AdminUserDTO;
  8. import org.ros.chatto.dto.ChatMessageDTO;
  9. import org.ros.chatto.dto.ReencryptionDTO;
  10. import org.ros.chatto.service.AdminService;
  11. import org.ros.chatto.service.ChatService;
  12. import org.ros.chatto.service.UserService;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.http.HttpStatus;
  15. import org.springframework.http.ResponseEntity;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.PathVariable;
  18. import org.springframework.web.bind.annotation.PostMapping;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import lombok.RequiredArgsConstructor;
  23. @RestController
  24. @RequestMapping("/api/admin")
  25. @RequiredArgsConstructor
  26. public class AdminRESTController {
  27. private final ChatService chatService;
  28. private final UserService userService;
  29. private final AdminService adminService;
  30. @PostMapping(value = "/post/re-encrypt", consumes = { "application/json" })
  31. public ResponseEntity<ReencryptionDTO> reencryptMessages(
  32. @RequestBody @Valid List<ReencryptionDTO> reencryptionDTOs,
  33. Principal principal) {
  34. if (reencryptionDTOs.size() > 0) {
  35. chatService.reencryptMessages(reencryptionDTOs);
  36. }
  37. return new ResponseEntity<ReencryptionDTO>(HttpStatus.OK);
  38. }
  39. @GetMapping(value = "/get/messages/{userName}")
  40. public List<ReencryptionDTO> sendAllMessages(@PathVariable String userName,
  41. Principal principal) {
  42. List<ReencryptionDTO> reencryptionDTOs = chatService
  43. .getAllMessagesForReencryption(principal.getName(), userName);
  44. return reencryptionDTOs;
  45. }
  46. @GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
  47. public List<ChatMessageDTO> sendNewMessages(@PathVariable String userName,
  48. @PathVariable Instant lastMessageTime, Principal principal) {
  49. List<ChatMessageDTO> chatMessageDTOs = chatService
  50. .getNewMessages(principal.getName(), userName, lastMessageTime);
  51. return chatMessageDTOs;
  52. }
  53. @GetMapping("/regular-users")
  54. public List<String> getAllRegularUsers() {
  55. return userService.getAllRegularUsers();
  56. }
  57. @GetMapping("/get/users")
  58. public List<AdminUserDTO> getAllOtherUsers(Principal principal) {
  59. return adminService.getOtherUsers(principal.getName());
  60. }
  61. }