| 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.UserCommons; | |
| 23 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 24 | ||
| 25 | import org.springframework.security.core.Authentication; | |
| 26 | ||
| 27 | ||
| 28 | import java.util.Optional; | |
| 29 | ||
| 30 | @Tag(name = "Chat Message") | |
| 31 | @RequestMapping("/api/chat") | |
| 32 | @RestController | |
| 33 | @Slf4j | |
| 34 | public class ChatMessageController extends ApiController{ | |
| 35 | ||
| 36 | @Autowired | |
| 37 | private ChatMessageRepository chatMessageRepository; | |
| 38 | ||
| 39 | @Autowired | |
| 40 | private UserCommonsRepository userCommonsRepository; | |
| 41 | ||
| 42 | @Autowired | |
| 43 | ObjectMapper mapper; | |
| 44 | ||
| 45 | @Operation(summary = "Get all chat messages", description = "Get all chat messages associated with a specific commons.") | |
| 46 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 47 | @GetMapping("/get") | |
| 48 | public ResponseEntity<Object> getChatMessages(@Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 49 | @Parameter(name="page") @RequestParam int page, | |
| 50 | @Parameter(name="size") @RequestParam int size) { | |
| 51 | | |
| 52 | // Make sure the user is part of the commons or is an admin | |
| 53 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 54 |
3
1. getChatMessages : negated conditional → KILLED 2. lambda$getChatMessages$0 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$getChatMessages$0 → KILLED 3. lambda$getChatMessages$0 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$getChatMessages$0 → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
| 55 | log.info("User is not an admin"); | |
| 56 | User user = getCurrentUser().getUser(); | |
| 57 | Long userId = user.getId(); | |
| 58 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
| 59 |
3
1. getChatMessages : negated conditional → KILLED 2. getChatMessages : negated conditional → KILLED 3. getChatMessages : negated conditional → KILLED |
boolean flag = (userCommonsLookup.isPresent()) ? ((userCommonsLookup.get().getCommons() != null)?(!userCommonsLookup.get().getCommons().isShowChat()):(true)) : (true); |
| 60 | ||
| 61 |
1
1. getChatMessages : negated conditional → KILLED |
if (flag) { |
| 62 |
1
1. getChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getChatMessages → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | // Return the list of non-hidden chat messages | |
| 67 | Page<ChatMessage> messages = chatMessageRepository.findByCommonsId(commonsId, PageRequest.of(page, size, Sort.by("timestamp").descending())); | |
| 68 |
1
1. getChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getChatMessages → KILLED |
return ResponseEntity.ok(messages); |
| 69 | } | |
| 70 | ||
| 71 | @Operation(summary = "Get all chat messages (Admins)", description = "Get all chat messages associated with a specific commons, even the hidden ones. Used only by admins") | |
| 72 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 73 | @GetMapping("/admin/get") | |
| 74 | public ResponseEntity<Object> getAllChatMessages(@Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 75 | @Parameter(name="page") @RequestParam int page, | |
| 76 | @Parameter(name="size") @RequestParam int size) { | |
| 77 | | |
| 78 | // Return the list of chat messages | |
| 79 | Page<ChatMessage> messages = chatMessageRepository.findAllByCommonsId(commonsId, PageRequest.of(page, size, Sort.by("timestamp").descending())); | |
| 80 |
1
1. getAllChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getAllChatMessages → KILLED |
return ResponseEntity.ok(messages); |
| 81 | } | |
| 82 | ||
| 83 | | |
| 84 | @Operation(summary = "Get hidden chat messages", description = "Get all hidden chat messages associated with a specific commons. Used only by admins") | |
| 85 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 86 | @GetMapping("/admin/hidden") | |
| 87 | public ResponseEntity<Object> getHiddenChatMessages(@Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 88 | @Parameter(name="page") @RequestParam int page, | |
| 89 | @Parameter(name="size") @RequestParam int size) { | |
| 90 | | |
| 91 | // Return the list of hidden chat messages | |
| 92 | Page<ChatMessage> messages = chatMessageRepository.findByCommonsIdAndHidden(commonsId, PageRequest.of(page, size, Sort.by("timestamp").descending())); | |
| 93 |
1
1. getHiddenChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getHiddenChatMessages → KILLED |
return ResponseEntity.ok(messages); |
| 94 | } | |
| 95 | | |
| 96 | | |
| 97 | @Operation(summary = "Create a chat message", description = "Create a chat message associated with a specific commons") | |
| 98 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 99 | @PostMapping("/post") | |
| 100 | public ResponseEntity<Object> createChatMessage(@Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 101 | @Parameter(description = "The message to be sent") @RequestParam String content) { | |
| 102 | | |
| 103 | User user = getCurrentUser().getUser(); | |
| 104 | Long userId = user.getId(); | |
| 105 | ||
| 106 | // Make sure the user is part of the commons or is an admin | |
| 107 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 108 |
3
1. createChatMessage : negated conditional → KILLED 2. lambda$createChatMessage$1 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$createChatMessage$1 → KILLED 3. lambda$createChatMessage$1 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$createChatMessage$1 → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
| 109 | log.info("User is not an admin"); | |
| 110 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
| 111 |
3
1. createChatMessage : negated conditional → KILLED 2. createChatMessage : negated conditional → KILLED 3. createChatMessage : negated conditional → KILLED |
boolean flag = (userCommonsLookup.isPresent()) ? ((userCommonsLookup.get().getCommons() != null)?(!userCommonsLookup.get().getCommons().isShowChat()):(false)) : (true); |
| 112 | ||
| 113 |
1
1. createChatMessage : negated conditional → KILLED |
if (flag) { |
| 114 |
1
1. createChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::createChatMessage → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | // Create the chat message | |
| 119 | ChatMessage chatMessage = ChatMessage.builder() | |
| 120 | .commonsId(commonsId) | |
| 121 | .userId(userId) | |
| 122 | .message(content) | |
| 123 | .hidden(false) | |
| 124 | .dm(false) | |
| 125 | .toUserId(0) | |
| 126 | .build(); | |
| 127 | ||
| 128 | // Save the message | |
| 129 | chatMessageRepository.save(chatMessage); | |
| 130 | ||
| 131 |
1
1. createChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::createChatMessage → KILLED |
return ResponseEntity.ok(chatMessage); |
| 132 | } | |
| 133 | ||
| 134 | @Operation(summary = "Delete a chat message", description = "Delete a chat message associated with a specific commons") | |
| 135 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 136 | @PutMapping("/hide") | |
| 137 | public ResponseEntity<Object> hideChatMessage(@Parameter(description = "The id of the chat message") @RequestParam Long chatMessageId) { | |
| 138 | ||
| 139 | // Try to get the chat message | |
| 140 | Optional<ChatMessage> chatMessageLookup = chatMessageRepository.findById(chatMessageId); | |
| 141 | | |
| 142 |
1
1. hideChatMessage : negated conditional → KILLED |
if (!chatMessageLookup.isPresent()) { |
| 143 |
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED |
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); |
| 144 | } | |
| 145 | ||
| 146 | ChatMessage chatMessage = chatMessageLookup.get(); | |
| 147 | ||
| 148 | User user = getCurrentUser().getUser(); | |
| 149 | Long userId = user.getId(); | |
| 150 | ||
| 151 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(chatMessage.getCommonsId(), userId); | |
| 152 |
3
1. hideChatMessage : negated conditional → KILLED 2. hideChatMessage : negated conditional → KILLED 3. hideChatMessage : negated conditional → KILLED |
boolean flag = (userCommonsLookup.isPresent()) ? ((userCommonsLookup.get().getCommons() != null)?(!userCommonsLookup.get().getCommons().isShowChat()):(true)) : (true); |
| 153 | ||
| 154 | // Check if the user is the author of the message or that chat is disabled | |
| 155 |
2
1. hideChatMessage : negated conditional → KILLED 2. hideChatMessage : negated conditional → KILLED |
if (chatMessage.getUserId() != userId || flag) { |
| 156 | // Check if the user is an admin | |
| 157 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 158 |
3
1. hideChatMessage : negated conditional → KILLED 2. lambda$hideChatMessage$2 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$hideChatMessage$2 → KILLED 3. lambda$hideChatMessage$2 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$hideChatMessage$2 → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
| 159 |
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); |
| 160 | } | |
| 161 | } | |
| 162 | ||
| 163 | // Hide the message | |
| 164 |
1
1. hideChatMessage : removed call to edu/ucsb/cs156/happiercows/entities/ChatMessage::setHidden → KILLED |
chatMessage.setHidden(true); |
| 165 | chatMessageRepository.save(chatMessage); | |
| 166 | ||
| 167 |
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED |
return ResponseEntity.ok(chatMessage); |
| 168 | } | |
| 169 | ||
| 170 | | |
| 171 | ||
| 172 | } | |
Mutations | ||
| 54 |
1.1 2.2 3.3 |
|
| 59 |
1.1 2.2 3.3 |
|
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 68 |
1.1 |
|
| 80 |
1.1 |
|
| 93 |
1.1 |
|
| 108 |
1.1 2.2 3.3 |
|
| 111 |
1.1 2.2 3.3 |
|
| 113 |
1.1 |
|
| 114 |
1.1 |
|
| 131 |
1.1 |
|
| 142 |
1.1 |
|
| 143 |
1.1 |
|
| 152 |
1.1 2.2 3.3 |
|
| 155 |
1.1 2.2 |
|
| 158 |
1.1 2.2 3.3 |
|
| 159 |
1.1 |
|
| 164 |
1.1 |
|
| 167 |
1.1 |