Skip to content

Commit

Permalink
Merge branch 'master' into upgrade-boot
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Gorzala committed Dec 21, 2023
2 parents 0db2e50 + 43aafb1 commit 4fdba63
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
26 changes: 19 additions & 7 deletions src/main/java/net/dancier/dancer/chat/ChatController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.dancier.dancer.chat;

import lombok.RequiredArgsConstructor;
import net.dancier.dancer.chat.client.SetReadFlagRequestDto;
import net.dancier.dancer.chat.dto.*;
import net.dancier.dancer.contact.ContactController;
import net.dancier.dancer.core.exception.BusinessException;
Expand All @@ -24,14 +25,13 @@
import static net.dancier.dancer.authentication.Constants.ROLE_USER;

@RestController
@RequestMapping("/chats")
@RequiredArgsConstructor
public class ChatController {
private final static Logger log = LoggerFactory.getLogger(ChatController.class);

private final ChatService chatService;

@GetMapping("")
@GetMapping("/chats")
@Secured(ROLE_USER)
public ResponseEntity<List<ChatDto>> getChats(@CurrentUser AuthenticatedUser authenticatedUser) {
log.info("Fetching chats for user {}.", authenticatedUser.getUserId());
Expand All @@ -40,9 +40,9 @@ public ResponseEntity<List<ChatDto>> getChats(@CurrentUser AuthenticatedUser aut
);
}

@PostMapping("")
@PostMapping("/chats")
@Secured(ROLE_USER)
public ResponseEntity postChat(
public ResponseEntity<CreatedChatDto> postChat(
@CurrentUser AuthenticatedUser authenticatedUser,
@RequestBody CreateChatDto createChatDto) {
log.info("Creating a new chat for User {}.", authenticatedUser.getUserId());
Expand All @@ -59,12 +59,13 @@ public ResponseEntity postChat(
headers.set("Location", location.toString());

return new ResponseEntity(
createdChatDto,
headers,
HttpStatus.CREATED
);
}

@GetMapping("/{chatId}")
@GetMapping("/chats/{chatId}")
@Secured(ROLE_USER)
public ResponseEntity<ChatDto> getChat(
@CurrentUser AuthenticatedUser authenticatedUser,
Expand All @@ -75,7 +76,7 @@ public ResponseEntity<ChatDto> getChat(
);
}

@GetMapping("/{chatId}/messages")
@GetMapping("/chats/{chatId}/messages")
@Secured(ROLE_USER)
public ResponseEntity<MessageDto[]> getMessages(
@CurrentUser AuthenticatedUser authenticatedUser,
Expand All @@ -87,7 +88,7 @@ public ResponseEntity<MessageDto[]> getMessages(
);
}

@PostMapping("/{chatId}/messages")
@PostMapping("/chats/{chatId}/messages")
@Secured(ROLE_USER)
public ResponseEntity postMessage(
@CurrentUser AuthenticatedUser authenticatedUser,
Expand All @@ -98,6 +99,17 @@ public ResponseEntity postMessage(
return new ResponseEntity(HttpStatus.CREATED);
}

@PutMapping("/messages/{messageId}")
@Secured(ROLE_USER)
public ResponseEntity putReadFlag(
@CurrentUser AuthenticatedUser authenticatedUser,
@PathVariable UUID messageId,
@RequestBody SetReadFlagRequestDto setReadFlagRequestDto
) {
chatService.setReadFlag(messageId, authenticatedUser.getDancerIdOrThrow(), setReadFlagRequestDto.getRead());
return new ResponseEntity(HttpStatus.NO_CONTENT);
}

@ExceptionHandler({BusinessException.class})
public ResponseEntity handle(Throwable throwable) {
log.info("got this: " + throwable);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/dancier/dancer/chat/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public Void createMessage(UUID chatId, UUID dancerId, CreateMessageDto createMes
return chatServiceClient.createMessage(chatId, remoteCreateMessageDto);
}

public Void setReadFlag(UUID messageId, UUID participantId, Boolean read) {
return chatServiceClient.setReadFlag(messageId, participantId, read);
}
private void throwIfDancerIsNotInChat(List<UUID> participantIds, UUID currentDancerId) {
log.info("Before Check");
log.info("Part: " + participantIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public Void createMessage(UUID chatId, RemoteCreateMessageDto remoteCreateMessag
.block();
}

public Void setReadFlag(UUID messageId, UUID participantId, Boolean read) {
SetReadFlagRequestDto setReadFlagRequestDto = new SetReadFlagRequestDto();
setReadFlagRequestDto.setRead(read);

return webClient.put()
.uri(uriBuilder -> uriBuilder
.path("/messages/{messagesId}/read-by/{participantId}").build(messageId, participantId)
).body(Mono.just(setReadFlagRequestDto), SetReadFlagRequestDto.class)
.retrieve()
.bodyToMono(Void.class)
.block();

}

private ExchangeFilterFunction buildRetryExchangeFilterFunction() {
return (request, next) -> next.exchange(request)
.flatMap(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.dancier.dancer.chat.client;

import lombok.Data;

@Data
public class SetReadFlagRequestDto {
Boolean read;
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void postChatShouldReturnTheChat() throws Exception {
.andExpect(status().isCreated())
.andExpect(header().exists("Location"));

result.andExpect(jsonPath("$").doesNotExist());
result.andExpect(jsonPath("$.id").isNotEmpty());
}

@Test
Expand Down
51 changes: 28 additions & 23 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -412,27 +412,26 @@ paths:
description: when the mail is on it's way
/chats:
get:
summary: returns a list of all chats of a dancer. This endpoint does not return dancers or messages, for this, the resource endpoint needs to be called
summary: returns a list of all chats of a dancer. This endpoint will also include the timestamp of the last message on the chat and the last message. To get a list of all messages, use the endpoints for this purpose
tags:
- Chats
responses:
'200':
description: "list of chats for a dancer"
description: "list of chats for the logged in dancer"
content:
application/json:
schema:
$ref: '#/components/schemas/chatListResponse'
example:
chats:
- chatId: 543
dancerIds: [123, 433]
participantIds: [123, 433]
lastActivity: "2022-10-10T18:29:48.843Z"
type: "direct"
lastMessage:
text: Lorem ipsum
authorId: 123
id: 1234
readByDancers: [123]
readByParticipants: [123]
createdAt: "2022-10-10T18:29:48.843Z"
default:
description: Unexpected error
Expand Down Expand Up @@ -529,6 +528,30 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/error'
/messages/{messageId}:
put:
summary: set the read flag for a message for the logged in user
tags:
- Chats
parameters:
- in: path
name: messageId
description: messageId
required: true
schema:
type: string
requestBody:
content:
application/json:
schema:
type: object
properties:
read:
type: boolean
responses:
'204':
description: ""

/dancers:
post:
summary: Used to retrieve a list of dancers
Expand Down Expand Up @@ -872,8 +895,6 @@ components:
items:
type: string
example: [ 123, 321 ]
type:
$ref: '#/components/schemas/chatType'
chatResponse:
$ref: '#/components/schemas/detailedChat'
chatListResponse:
Expand Down Expand Up @@ -928,21 +949,10 @@ components:
chatId:
type: string
example: 543
imageHash:
type: string
description: |
hash for the image shown for this chat. For a personal chat it is the image hash of the profile picture of the chat partner
dancers:
type: array
description: Dancers participating in the chat
items:
$ref: '#/components/schemas/chatProfile'
lastActivity:
type: string
format: date-time
description: Datetime of the last activity from this chat
type:
$ref: '#/components/schemas/chatType'
messageListResponse:
type: array
items:
Expand Down Expand Up @@ -979,11 +989,6 @@ components:
type: string
description: The content of the message
example: Lorem ipsum
chatType:
type: string
enum:
- GROUP
- DIRECT
error:
type: object
properties:
Expand Down

0 comments on commit 4fdba63

Please sign in to comment.