| 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 and showChat is true, or user 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 | ||
| 60 | 
1
1. getChatMessages : negated conditional → KILLED | 
            if (!userCommonsLookup.isPresent()) { | 
| 61 | 
1
1. getChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getChatMessages → KILLED | 
                return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | 
| 62 |             } | |
| 63 | ||
| 64 |             UserCommons userCommons = userCommonsLookup.get(); | |
| 65 | 
1
1. getChatMessages : negated conditional → KILLED | 
            if(!userCommons.getCommons().isShowChat()){ | 
| 66 | 
1
1. getChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getChatMessages → KILLED | 
                return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | 
| 67 |             } | |
| 68 |         } | |
| 69 | ||
| 70 |         // Return the list of non-hidden chat messages | |
| 71 |         Page<ChatMessage> messages = chatMessageRepository.findByCommonsId(commonsId, PageRequest.of(page, size, Sort.by("timestamp").descending())); | |
| 72 | 
1
1. getChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getChatMessages → KILLED | 
        return ResponseEntity.ok(messages); | 
| 73 |     } | |
| 74 | ||
| 75 |     @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") | |
| 76 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 77 |     @GetMapping("/admin/get") | |
| 78 |     public ResponseEntity<Object> getAllChatMessages(@Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 79 |                                                     @Parameter(name="page") @RequestParam int page, | |
| 80 |                                                     @Parameter(name="size") @RequestParam int size) { | |
| 81 |          | |
| 82 |         // Return the list of chat messages | |
| 83 |         Page<ChatMessage> messages = chatMessageRepository.findAllByCommonsId(commonsId, PageRequest.of(page, size, Sort.by("timestamp").descending())); | |
| 84 | 
1
1. getAllChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getAllChatMessages → KILLED | 
        return ResponseEntity.ok(messages); | 
| 85 |     } | |
| 86 | ||
| 87 |      | |
| 88 |     @Operation(summary = "Get hidden chat messages", description = "Get all hidden chat messages associated with a specific commons. Used only by admins") | |
| 89 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 90 |     @GetMapping("/admin/hidden") | |
| 91 |     public ResponseEntity<Object> getHiddenChatMessages(@Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 92 |                                                     @Parameter(name="page") @RequestParam int page, | |
| 93 |                                                     @Parameter(name="size") @RequestParam int size) { | |
| 94 |          | |
| 95 |         // Return the list of hidden chat messages | |
| 96 |         Page<ChatMessage> messages = chatMessageRepository.findByCommonsIdAndHidden(commonsId, PageRequest.of(page, size, Sort.by("timestamp").descending())); | |
| 97 | 
1
1. getHiddenChatMessages : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::getHiddenChatMessages → KILLED | 
        return ResponseEntity.ok(messages); | 
| 98 |     } | |
| 99 |      | |
| 100 |      | |
| 101 |     @Operation(summary = "Create a chat message", description = "Create a chat message associated with a specific commons") | |
| 102 |     @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 103 |     @PostMapping("/post") | |
| 104 |     public ResponseEntity<Object> createChatMessage(@Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 105 |                                                     @Parameter(description = "The message to be sent") @RequestParam String content) { | |
| 106 |          | |
| 107 |         User user = getCurrentUser().getUser(); | |
| 108 |         Long userId = user.getId(); | |
| 109 | ||
| 110 |         // Make sure the user is part of the commons and showChat is true, or user is an admin | |
| 111 |         Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 112 | 
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"))){ | 
| 113 |             log.info("User is not an admin"); | |
| 114 |             Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
| 115 | ||
| 116 | 
1
1. createChatMessage : negated conditional → KILLED | 
            if (!userCommonsLookup.isPresent()) { | 
| 117 | 
1
1. createChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::createChatMessage → KILLED | 
                return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | 
| 118 |             } | |
| 119 | ||
| 120 |             UserCommons userCommons = userCommonsLookup.get(); | |
| 121 | 
1
1. createChatMessage : negated conditional → KILLED | 
            if(!userCommons.getCommons().isShowChat()){ | 
| 122 | 
1
1. createChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::createChatMessage → KILLED | 
                return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | 
| 123 |             } | |
| 124 |         } | |
| 125 | ||
| 126 |         // Create the chat message | |
| 127 |         ChatMessage chatMessage = ChatMessage.builder() | |
| 128 |         .commonsId(commonsId) | |
| 129 |         .userId(userId) | |
| 130 |         .message(content) | |
| 131 |         .hidden(false) | |
| 132 |         .dm(false) | |
| 133 |         .toUserId(0) | |
| 134 |         .build(); | |
| 135 | ||
| 136 |         // Save the message | |
| 137 |         chatMessageRepository.save(chatMessage); | |
| 138 | ||
| 139 | 
1
1. createChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::createChatMessage → KILLED | 
        return ResponseEntity.ok(chatMessage); | 
| 140 |     } | |
| 141 | ||
| 142 |     @Operation(summary = "Delete a chat message", description = "Delete a chat message associated with a specific commons") | |
| 143 |     @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 144 |     @PutMapping("/hide") | |
| 145 |     public ResponseEntity<Object> hideChatMessage(@Parameter(description = "The id of the chat message") @RequestParam Long chatMessageId) { | |
| 146 | ||
| 147 |         // Try to get the chat message | |
| 148 |         Optional<ChatMessage> chatMessageLookup = chatMessageRepository.findById(chatMessageId); | |
| 149 | 
1
1. hideChatMessage : negated conditional → KILLED | 
        if (!chatMessageLookup.isPresent()) { | 
| 150 | 
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED | 
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); | 
| 151 |         } | |
| 152 |         ChatMessage chatMessage = chatMessageLookup.get(); | |
| 153 | ||
| 154 |         User user = getCurrentUser().getUser(); | |
| 155 |         Long userId = user.getId(); | |
| 156 | ||
| 157 |         // Check if the user is the author of the message | |
| 158 | 
1
1. hideChatMessage : negated conditional → KILLED | 
        if (chatMessage.getUserId() != userId) { | 
| 159 |             // Check if the user is an admin | |
| 160 |             Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 161 | 
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"))){ | 
| 162 | 
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED | 
                return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | 
| 163 |             } | |
| 164 |         } | |
| 165 | ||
| 166 |         // Check if showChat is true | |
| 167 |         Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(chatMessage.getCommonsId(), userId); | |
| 168 |         UserCommons userCommons = userCommonsLookup.get(); | |
| 169 | 
1
1. hideChatMessage : negated conditional → KILLED | 
        if (!userCommons.getCommons().isShowChat()){ | 
| 170 |             // Check if the user is an admin | |
| 171 |             Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 172 | 
3
1. hideChatMessage : negated conditional → KILLED 2. lambda$hideChatMessage$3 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$hideChatMessage$3 → KILLED 3. lambda$hideChatMessage$3 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::lambda$hideChatMessage$3 → KILLED  | 
            if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ | 
| 173 | 
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED | 
                return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | 
| 174 |             } | |
| 175 |         } | |
| 176 | ||
| 177 |         // Hide the message | |
| 178 | 
1
1. hideChatMessage : removed call to edu/ucsb/cs156/happiercows/entities/ChatMessage::setHidden → KILLED | 
        chatMessage.setHidden(true); | 
| 179 |         chatMessageRepository.save(chatMessage); | |
| 180 | ||
| 181 | 
1
1. hideChatMessage : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ChatMessageController::hideChatMessage → KILLED | 
        return ResponseEntity.ok(chatMessage); | 
| 182 |     } | |
| 183 | ||
| 184 |      | |
| 185 | ||
| 186 | } | |
Mutations | ||
| 54 | 
 
 1.1 2.2 3.3  | 
|
| 60 | 
 
 1.1  | 
|
| 61 | 
 
 1.1  | 
|
| 65 | 
 
 1.1  | 
|
| 66 | 
 
 1.1  | 
|
| 72 | 
 
 1.1  | 
|
| 84 | 
 
 1.1  | 
|
| 97 | 
 
 1.1  | 
|
| 112 | 
 
 1.1 2.2 3.3  | 
|
| 116 | 
 
 1.1  | 
|
| 117 | 
 
 1.1  | 
|
| 121 | 
 
 1.1  | 
|
| 122 | 
 
 1.1  | 
|
| 139 | 
 
 1.1  | 
|
| 149 | 
 
 1.1  | 
|
| 150 | 
 
 1.1  | 
|
| 158 | 
 
 1.1  | 
|
| 161 | 
 
 1.1 2.2 3.3  | 
|
| 162 | 
 
 1.1  | 
|
| 169 | 
 
 1.1  | 
|
| 172 | 
 
 1.1 2.2 3.3  | 
|
| 173 | 
 
 1.1  | 
|
| 178 | 
 
 1.1  | 
|
| 181 | 
 
 1.1  |