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.

63 lines
2.2 KiB

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