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

package org.ros.chatto.controller;
import java.security.Principal;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import com.spencerwi.either.Result;
import org.ros.chatto.dto.AdminUserDTO;
import org.ros.chatto.dto.ChatMessageDTO;
import org.ros.chatto.dto.ReencryptionDTO;
import org.ros.chatto.service.AdminService;
import org.ros.chatto.service.ChatService;
import org.ros.chatto.service.UserDTOSpec;
import org.ros.chatto.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
@RestController
@RequestMapping("/api/admin")
@RequiredArgsConstructor
public class AdminRESTController {
private final ChatService chatService;
private final UserService userService;
private final AdminService adminService;
@PostMapping(value = "/post/re-encrypt", consumes = { "application/json" })
public ResponseEntity<ReencryptionDTO> reencryptMessages(
@RequestBody @Valid List<ReencryptionDTO> reencryptionDTOs,
Principal principal) {
if (reencryptionDTOs.size() > 0) {
chatService.reencryptMessages(reencryptionDTOs);
}
return new ResponseEntity<ReencryptionDTO>(HttpStatus.OK);
}
@GetMapping(value = "/get/messages/{userName}")
public List<ReencryptionDTO> sendAllMessages(@PathVariable String userName,
Principal principal) {
List<ReencryptionDTO> reencryptionDTOs = chatService
.getAllMessagesForReencryption(principal.getName(), userName);
return reencryptionDTOs;
}
@GetMapping(value = "/get/messages/{userName}/{lastMessageTime}")
public List<ChatMessageDTO> sendNewMessages(@PathVariable String userName,
@PathVariable Instant lastMessageTime, Principal principal) {
List<ChatMessageDTO> chatMessageDTOs = chatService
.getNewMessages(principal.getName(), userName, lastMessageTime);
return chatMessageDTOs;
}
@GetMapping("/get/users")
public List<AdminUserDTO> getUsers(Principal principal,
@RequestParam Optional<String> select) {
if (select.isPresent()) {
switch (select.get()) {
case "all":
return adminService.getUsers("", UserDTOSpec.ALL_USERS);
case "regular":
return adminService.getUsers("", UserDTOSpec.REGULAR_USERS);
}
}
return adminService.getUsers(principal.getName(),
UserDTOSpec.OTHER_USERS);
}
@DeleteMapping("/delete/users/{userName}")
public ResponseEntity<?> deleteUser(@PathVariable String userName) {
Result<Void> res = adminService.deleteUser(userName);
if (res.isOk()) {
return ResponseEntity.ok()
.body("Deleted User with name - " + userName);
} else {
return ResponseEntity.badRequest().body(
"An error occured while trying to delete user with name - "
+ userName);
}
}
}