| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 4 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 5 | import lombok.extern.slf4j.Slf4j; | |
| 6 | import io.swagger.v3.oas.annotations.Operation; | |
| 7 | import io.swagger.v3.oas.annotations.Parameter; | |
| 8 | import org.springframework.beans.factory.annotation.Autowired; | |
| 9 | import org.springframework.http.HttpStatus; | |
| 10 | import org.springframework.http.ResponseEntity; | |
| 11 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 12 | import org.springframework.security.core.context.SecurityContextHolder; | |
| 13 | import org.springframework.web.bind.annotation.*; | |
| 14 | import org.springframework.data.domain.Page; | |
| 15 | import org.springframework.data.domain.PageRequest; | |
| 16 | import org.springframework.data.domain.Sort; | |
| 17 | ||
| 18 | import edu.ucsb.cs156.happiercows.entities.ChatMessage; | |
| 19 | import edu.ucsb.cs156.happiercows.repositories.ChatMessageRepository; | |
| 20 | ||
| 21 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 22 | import edu.ucsb.cs156.happiercows.entities.Farmer; | |
| 23 | import edu.ucsb.cs156.happiercows.repositories.FarmerRepository; | |
| 24 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 25 | ||
| 26 | import org.springframework.security.core.Authentication; | |
| 27 | ||
| 28 | ||
| 29 | import java.time.Duration; | |
| 30 | import java.time.Instant; | |
| 31 | import java.util.Map; | |
| 32 | import java.util.Optional; | |
| 33 | ||
| 34 | @Tag(name = "Chat Message") | |
| 35 | @RequestMapping("/api/chat") | |
| 36 | @RestController | |
| 37 | @Slf4j | |
| 38 | public class ChatMessageController extends ApiController{ | |
| 39 | ||
| 40 | @Autowired | |
| 41 | private ChatMessageRepository chatMessageRepository; | |
| 42 | ||
| 43 | @Autowired | |
| 44 | private FarmerRepository farmerRepository; | |
| 45 | ||
| 46 | @Autowired | |
| 47 | private UserRepository userRepository; | |
| 48 | ||
| 49 | @Autowired | |
| 50 | ObjectMapper mapper; | |
| 51 | ||
| 52 | @Operation(summary = "Get all chat messages", description = "Get all chat messages associated with a specific game.") | |
| 53 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 54 | @GetMapping("/get") | |
| 55 | public ResponseEntity<Object> getChatMessages(@Parameter(description = "The id of the common") @RequestParam Long gameId, | |
| 56 | @Parameter(name="page") @RequestParam int page, | |
| 57 | @Parameter(name="size") @RequestParam int size) { | |
| 58 | | |
| 59 | // Make sure the user is part of the game and showChat is true, or user is an admin | |
| 60 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 61 |
3
1. lambda$getChatMessages$0 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$getChatMessages$0 → KILLED 2. lambda$getChatMessages$0 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$getChatMessages$0 → KILLED 3. getChatMessages : negated conditional → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
| 62 | log.info("User is not an admin"); | |
| 63 | User user = getCurrentUser().getUser(); | |
| 64 | Long userId = user.getId(); | |
| 65 | Optional<Farmer> farmerLookup = farmerRepository.findByGameIdAndUserId(gameId, userId); | |
| 66 | ||
| 67 |
1
1. getChatMessages : negated conditional → KILLED |
if (!farmerLookup.isPresent()) { |
| 68 |
1
1. getChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getChatMessages → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 69 | } | |
| 70 | ||
| 71 | Farmer farmer = farmerLookup.get(); | |
| 72 |
1
1. getChatMessages : negated conditional → KILLED |
if(!farmer.getGame().isShowChat()){ |
| 73 |
1
1. getChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getChatMessages → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | // Return the list of non-hidden chat messages | |
| 78 | Page<ChatMessage> messages = chatMessageRepository.findByGameId(gameId, PageRequest.of(page, size, Sort.by("timestamp").descending())); | |
| 79 |
1
1. getChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getChatMessages → KILLED |
return ResponseEntity.ok(messages); |
| 80 | } | |
| 81 | ||
| 82 | @Operation(summary = "Get all chat messages (Admins)", description = "Get all chat messages associated with a specific game, even the hidden ones. Used only by admins") | |
| 83 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 84 | @GetMapping("/admin/get") | |
| 85 | public ResponseEntity<Object> getAllChatMessages(@Parameter(description = "The id of the common") @RequestParam Long gameId, | |
| 86 | @Parameter(name="page") @RequestParam int page, | |
| 87 | @Parameter(name="size") @RequestParam int size) { | |
| 88 | | |
| 89 | // Return the list of chat messages | |
| 90 | Page<ChatMessage> messages = chatMessageRepository.findAllByGameId(gameId, PageRequest.of(page, size, Sort.by("timestamp").descending())); | |
| 91 |
1
1. getAllChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getAllChatMessages → KILLED |
return ResponseEntity.ok(messages); |
| 92 | } | |
| 93 | ||
| 94 | | |
| 95 | @Operation(summary = "Get hidden chat messages", description = "Get all hidden chat messages associated with a specific game. Used only by admins") | |
| 96 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 97 | @GetMapping("/admin/hidden") | |
| 98 | public ResponseEntity<Object> getHiddenChatMessages(@Parameter(description = "The id of the common") @RequestParam Long gameId, | |
| 99 | @Parameter(name="page") @RequestParam int page, | |
| 100 | @Parameter(name="size") @RequestParam int size) { | |
| 101 | | |
| 102 | // Return the list of hidden chat messages | |
| 103 | Page<ChatMessage> messages = chatMessageRepository.findByGameIdAndHidden(gameId, PageRequest.of(page, size, Sort.by("timestamp").descending())); | |
| 104 |
1
1. getHiddenChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getHiddenChatMessages → KILLED |
return ResponseEntity.ok(messages); |
| 105 | } | |
| 106 | | |
| 107 | | |
| 108 | @Operation(summary = "Create a chat message", description = "Create a chat message associated with a specific game") | |
| 109 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 110 | @PostMapping("/post") | |
| 111 | public ResponseEntity<Object> createChatMessage(@Parameter(description = "The id of the common") @RequestParam Long gameId, | |
| 112 | @Parameter(description = "The message to be sent") @RequestParam String content) { | |
| 113 | | |
| 114 | User user = getCurrentUser().getUser(); | |
| 115 | Long userId = user.getId(); | |
| 116 | ||
| 117 | // Make sure the user is part of the game and showChat is true, or user is an admin | |
| 118 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 119 |
3
1. createChatMessage : negated conditional → KILLED 2. lambda$createChatMessage$0 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$createChatMessage$0 → KILLED 3. lambda$createChatMessage$0 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$createChatMessage$0 → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
| 120 | log.info("User is not an admin"); | |
| 121 | Optional<Farmer> farmerLookup = farmerRepository.findByGameIdAndUserId(gameId, userId); | |
| 122 | ||
| 123 |
1
1. createChatMessage : negated conditional → KILLED |
if (!farmerLookup.isPresent()) { |
| 124 |
1
1. createChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::createChatMessage → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 125 | } | |
| 126 | ||
| 127 | Farmer farmer = farmerLookup.get(); | |
| 128 |
1
1. createChatMessage : negated conditional → KILLED |
if(!farmer.getGame().isShowChat()){ |
| 129 |
1
1. createChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::createChatMessage → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | Instant cooldownUntil = userRepository.findById(userId) | |
| 134 | .map(User::getChatCooldownUntil) | |
| 135 | .orElse(null); | |
| 136 | Instant now = Instant.now(); | |
| 137 |
2
1. createChatMessage : negated conditional → KILLED 2. createChatMessage : negated conditional → KILLED |
if (cooldownUntil != null && cooldownUntil.isAfter(now)) { |
| 138 |
2
1. createChatMessage : Replaced long addition with subtraction → KILLED 2. createChatMessage : Replaced long division with multiplication → KILLED |
long remainingSeconds = (Duration.between(now, cooldownUntil).toMillis() + 999) / 1000; |
| 139 |
1
1. createChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::createChatMessage → KILLED |
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body(Map.of( |
| 140 | "message", "You are on chat cooldown for %d more seconds".formatted(remainingSeconds), | |
| 141 | "remainingSeconds", remainingSeconds)); | |
| 142 | } | |
| 143 | ||
| 144 | // Create the chat message | |
| 145 | ChatMessage chatMessage = ChatMessage.builder() | |
| 146 | .gameId(gameId) | |
| 147 | .userId(userId) | |
| 148 | .message(content) | |
| 149 | .hidden(false) | |
| 150 | .dm(false) | |
| 151 | .toUserId(0) | |
| 152 | .build(); | |
| 153 | ||
| 154 | // Save the message | |
| 155 | chatMessageRepository.save(chatMessage); | |
| 156 | ||
| 157 |
1
1. createChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::createChatMessage → KILLED |
return ResponseEntity.ok(chatMessage); |
| 158 | } | |
| 159 | ||
| 160 | @Operation(summary = "Delete a chat message", description = "Delete a chat message associated with a specific game") | |
| 161 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 162 | @PutMapping("/hide") | |
| 163 | public ResponseEntity<Object> hideChatMessage(@Parameter(description = "The id of the chat message") @RequestParam Long chatMessageId) { | |
| 164 | ||
| 165 | // Try to get the chat message | |
| 166 | Optional<ChatMessage> chatMessageLookup = chatMessageRepository.findById(chatMessageId); | |
| 167 |
1
1. hideChatMessage : negated conditional → KILLED |
if (!chatMessageLookup.isPresent()) { |
| 168 |
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED |
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); |
| 169 | } | |
| 170 | ChatMessage chatMessage = chatMessageLookup.get(); | |
| 171 | ||
| 172 | User user = getCurrentUser().getUser(); | |
| 173 | Long userId = user.getId(); | |
| 174 | ||
| 175 | // Check if the user is the author of the message | |
| 176 |
1
1. hideChatMessage : negated conditional → KILLED |
if (chatMessage.getUserId() != userId) { |
| 177 | // Check if the user is an admin | |
| 178 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 179 |
3
1. hideChatMessage : negated conditional → KILLED 2. lambda$hideChatMessage$0 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$hideChatMessage$0 → KILLED 3. lambda$hideChatMessage$0 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$hideChatMessage$0 → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
| 180 |
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 181 | } | |
| 182 | } | |
| 183 | ||
| 184 | // Check if showChat is true | |
| 185 | Optional<Farmer> farmerLookup = farmerRepository.findByGameIdAndUserId(chatMessage.getGameId(), userId); | |
| 186 | Farmer farmer = farmerLookup.get(); | |
| 187 |
1
1. hideChatMessage : negated conditional → KILLED |
if (!farmer.getGame().isShowChat()){ |
| 188 | // Check if the user is an admin | |
| 189 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 190 |
3
1. lambda$hideChatMessage$1 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$hideChatMessage$1 → KILLED 2. lambda$hideChatMessage$1 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$hideChatMessage$1 → KILLED 3. hideChatMessage : negated conditional → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
| 191 |
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 192 | } | |
| 193 | } | |
| 194 | ||
| 195 | // Hide the message | |
| 196 |
1
1. hideChatMessage : removed call to edu/ucsb/cs156/happiercows/entities/ChatMessage::setHidden → KILLED |
chatMessage.setHidden(true); |
| 197 | chatMessageRepository.save(chatMessage); | |
| 198 | ||
| 199 |
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED |
return ResponseEntity.ok(chatMessage); |
| 200 | } | |
| 201 | ||
| 202 | | |
| 203 | ||
| 204 | } | |
Mutations | ||
| 61 |
1.1 2.2 3.3 |
|
| 67 |
1.1 |
|
| 68 |
1.1 |
|
| 72 |
1.1 |
|
| 73 |
1.1 |
|
| 79 |
1.1 |
|
| 91 |
1.1 |
|
| 104 |
1.1 |
|
| 119 |
1.1 2.2 3.3 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |
|
| 128 |
1.1 |
|
| 129 |
1.1 |
|
| 137 |
1.1 2.2 |
|
| 138 |
1.1 2.2 |
|
| 139 |
1.1 |
|
| 157 |
1.1 |
|
| 167 |
1.1 |
|
| 168 |
1.1 |
|
| 176 |
1.1 |
|
| 179 |
1.1 2.2 3.3 |
|
| 180 |
1.1 |
|
| 187 |
1.1 |
|
| 190 |
1.1 2.2 3.3 |
|
| 191 |
1.1 |
|
| 196 |
1.1 |
|
| 199 |
1.1 |