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.

97 lines
3.2 KiB

4 years ago
  1. package org.ros.chatto.controller;
  2. import java.security.Principal;
  3. import java.time.Instant;
  4. import java.util.List;
  5. import java.util.Optional;
  6. import javax.validation.Valid;
  7. import com.spencerwi.either.Result;
  8. import org.ros.chatto.dto.AdminUserDTO;
  9. import org.ros.chatto.dto.ChatMessageDTO;
  10. import org.ros.chatto.dto.ReencryptionDTO;
  11. import org.ros.chatto.service.AdminService;
  12. import org.ros.chatto.service.ChatService;
  13. import org.ros.chatto.service.UserDTOSpec;
  14. import org.ros.chatto.service.UserService;
  15. import org.springframework.http.HttpStatus;
  16. import org.springframework.http.ResponseEntity;
  17. import org.springframework.web.bind.annotation.DeleteMapping;
  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/admin")
  28. @RequiredArgsConstructor
  29. public class AdminRESTController {
  30. private final ChatService chatService;
  31. private final UserService userService;
  32. private final AdminService adminService;
  33. @PostMapping(value = "/post/re-encrypt", consumes = { "application/json" })
  34. public ResponseEntity<ReencryptionDTO> reencryptMessages(
  35. @RequestBody @Valid List<ReencryptionDTO> reencryptionDTOs,
  36. Principal principal) {
  37. if (reencryptionDTOs.size() > 0) {
  38. chatService.reencryptMessages(reencryptionDTOs);
  39. }
  40. return new ResponseEntity<ReencryptionDTO>(HttpStatus.OK);
  41. }
  42. @GetMapping(value = "/get/messages/{userName}")
  43. public List<ReencryptionDTO> sendAllMessages(@PathVariable String userName,
  44. Principal principal) {
  45. List<ReencryptionDTO> reencryptionDTOs = chatService
  46. .getAllMessagesForReencryption(principal.getName(), userName);
  47. return reencryptionDTOs;
  48. }
  49. @GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
  50. public List<ChatMessageDTO> sendNewMessages(@PathVariable String userName,
  51. @PathVariable Instant lastMessageTime, Principal principal) {
  52. List<ChatMessageDTO> chatMessageDTOs = chatService
  53. .getNewMessages(principal.getName(), userName, lastMessageTime);
  54. return chatMessageDTOs;
  55. }
  56. @GetMapping("/get/users")
  57. public List<AdminUserDTO> getUsers(Principal principal,
  58. @RequestParam Optional<String> select) {
  59. if (select.isPresent()) {
  60. switch (select.get()) {
  61. case "all":
  62. return adminService.getUsers("", UserDTOSpec.ALL_USERS);
  63. case "regular":
  64. return adminService.getUsers("", UserDTOSpec.REGULAR_USERS);
  65. }
  66. }
  67. return adminService.getUsers(principal.getName(),
  68. UserDTOSpec.OTHER_USERS);
  69. }
  70. @DeleteMapping("/delete/users/{userName}")
  71. public ResponseEntity<?> deleteUser(@PathVariable String userName) {
  72. Result<Void> res = adminService.deleteUser(userName);
  73. if (res.isOk()) {
  74. return ResponseEntity.ok()
  75. .body("Deleted User with name - " + userName);
  76. } else {
  77. return ResponseEntity.badRequest().body(
  78. "An error occured while trying to delete user with name - "
  79. + userName);
  80. }
  81. }
  82. }