package app.controllers.user; import app.annotations.enums.AuthMethod; import app.annotations.interfaces.CheckWebAccess; import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.WaitAfterNext; import app.entities.messages.Message; import app.entities.messages.MessageSearch; import app.entities.other.SteamID; import app.repositories.GametimeRepository; import app.repositories.MessageRepository; import app.services.ProfileService; import app.services.db.MessageService; import app.utils.SteamIDConverter; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; /** * котроллер для пользователя, чтоб смотреть сообщения */ @RestController @RequestMapping("/api/profile/messages") public class MessagesController { private MessageService messageService; private MessageRepository messageRepository; private ProfileService profileService; @Autowired private GametimeRepository gametimeRepository; @Autowired public MessagesController(MessageService messageService, MessageRepository messageRepository, ProfileService profileService) { this.messageService = messageService; this.messageRepository = messageRepository; this.profileService = profileService; } @PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE}) @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "messages") @CollectStatistic @Deprecated public ResponseEntity getMessages(HttpServletRequest request, @RequestBody(required = false) Payload filter, @RequestParam(required = false) String srv, @RequestParam(required = false, defaultValue = "0") Long offset, @RequestParam(required = false, defaultValue = "20") Integer limit) { if (limit > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); return new ResponseEntity(messageService.getAllMessages(srv, filter==null?null:filter.getMessage(), offset, limit), HttpStatus.OK); } @PostMapping(value = "/pages", consumes = {MediaType.APPLICATION_JSON_VALUE}) @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "messages") @CollectStatistic public Page getMessagesWithFilters(Pageable pageable, @RequestBody(required = false) MessageSearch filter) { if (filter == null) filter = new MessageSearch(); Page messages = messageRepository.getMessages( pageable, filter.getAccounts(profileService), filter.getBegin(), filter.getEnd(), filter.getMessage(), filter.getServerId() ); messages.getContent().forEach((msg) -> { msg.setGametime(gametimeRepository.searchGametimeByAccountId(msg.getAccount_id())); }); return messages; } @PostMapping(value = "/account", consumes = {MediaType.APPLICATION_JSON_VALUE}) @CheckWebAccess(auth_method = AuthMethod.STEAM64) @WaitAfterNext(order = "messages") @CollectStatistic @Deprecated public ResponseEntity getAccountMessages(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String mysteam64, @RequestBody(required = false) Payload filter, @RequestParam(required = false) String steam64, @RequestParam(required = false) String srv, @RequestParam(required = false, defaultValue = "0") Integer offset, @RequestParam(required = false, defaultValue = "20") Integer limit) { SteamID steamID = steam64 == null || steam64.isEmpty() ? SteamIDConverter.getSteamID(mysteam64) : SteamIDConverter.getSteamID(steam64); if (steamID == null) return new ResponseEntity(HttpStatus.NOT_FOUND); if (limit > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); return new ResponseEntity(messageService.getAccountMessages(steamID, srv, filter==null?null: filter.getMessage(), offset, limit), HttpStatus.OK); } }