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.

51 lines
1.6 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
5 years ago
5 years ago
5 years ago
5 years ago
  1. package org.ros.chatto.controller;
  2. import java.security.Principal;
  3. import java.util.Optional;
  4. import org.ros.chatto.dto.ChatMessageDTO;
  5. import org.ros.chatto.service.UserService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.security.core.Authentication;
  8. import org.springframework.security.core.context.SecurityContextHolder;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.ui.Model;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import lombok.RequiredArgsConstructor;
  14. import lombok.extern.slf4j.Slf4j;
  15. @Controller
  16. @RequiredArgsConstructor
  17. @Slf4j
  18. public class Home {
  19. @Autowired
  20. private final UserService userService;
  21. @RequestMapping("/")
  22. public String showPage(Model model, Principal principal) {
  23. final String welcomeMessage = Optional.ofNullable(principal).map(p -> {
  24. return String.format("Welcome back %s!", p.getName());
  25. }).orElse("Welcome to chatto");
  26. model.addAttribute("message", welcomeMessage);
  27. return "home";
  28. }
  29. @GetMapping("/chat")
  30. public String showChat(Model model, Principal principal) {
  31. model.addAttribute(new ChatMessageDTO());
  32. Authentication authentication = SecurityContextHolder.getContext()
  33. .getAuthentication();
  34. boolean isAdmin = authentication.getAuthorities().stream()
  35. .anyMatch(r -> r.getAuthority().equals("ROLE_ADMIN")
  36. || r.getAuthority().equals("ROLE_SUPER_USER"));
  37. log.trace("Is admin? " + isAdmin);
  38. model.addAttribute("activeUsers",
  39. userService.getOtherActiveUsers(principal.getName()));
  40. return "chat";
  41. }
  42. }