diff --git a/api-customer/api-resources/message-endpoint.yaml b/api-customer/api-resources/message-endpoint.yaml new file mode 100644 index 00000000..38ae939a --- /dev/null +++ b/api-customer/api-resources/message-endpoint.yaml @@ -0,0 +1,99 @@ +openapi: "3.1.0" +info: + title: "boardmate_api API" + description: "boardmate_api API" + version: "1.0.0" +servers: + - url: "https://boardmate_api" +paths: + /message/send: + post: + summary: "POST message/send" + operationId: "postMessage" + tags: + - Message + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/MessagePostRequestDto" + required: true + responses: + "200": + description: "OK" + content: + '*/*': + schema: + type: "string" + + /message/history/{clientId}: + get: + summary: "GET message/history/{clientId}" + operationId: "history" + tags: + - Message + parameters: + - name: "clientId" + in: "path" + required: true + schema: + type: "string" + - name: "$top" + in: "query" + required: false + schema: + type: "integer" + format: "int32" + default: "20" + + responses: + "200": + description: "OK" + content: + '*/*': + schema: + type: "array" + items: + $ref: "#/components/schemas/ResponseBodyListMessageDto" + +components: + schemas: + MessagePostRequestDto: + type: "object" + properties: + content: + type: "string" + nullable: true + clientId: + type: "string" + nullable: true + timeStamp: + type: "integer" + format: "int32" + nullable: true + + ResponseBodyListMessageDto: + type: "object" + properties: + data: + type: array + items: + $ref: "#/components/schemas/MessageDto" + nullable: true + message: + type: "string" + nullable: true + success: + type: "boolean" + nullable: false + + MessageDto: + type: "object" + properties: + content: + type: "string" + nullable: true + timestamp: + type: "integer" + format: "int32" + nullable: true \ No newline at end of file diff --git a/api-customer/requirements.txt b/api-customer/requirements.txt index b17bc640..d856f394 100644 --- a/api-customer/requirements.txt +++ b/api-customer/requirements.txt @@ -14,3 +14,4 @@ python-dotenv file:./api-resources/auth-components file:./api-resources/client-components file:./api-resources/game-components +file:./api-resources/message-components diff --git a/api/mosquitto/data/mosquitto.db b/api/mosquitto/data/mosquitto.db index 02caee2c..15c306ca 100644 Binary files a/api/mosquitto/data/mosquitto.db and b/api/mosquitto/data/mosquitto.db differ diff --git a/api/mosquitto/log/mosquitto.log b/api/mosquitto/log/mosquitto.log index 23cc6a09..d35dbd6d 100644 --- a/api/mosquitto/log/mosquitto.log +++ b/api/mosquitto/log/mosquitto.log @@ -1208,3 +1208,179 @@ To fix this, use `chmod 0700 /mosquitto/config/passwords`. 1767308546: Sending PINGRESP to board-mate-api 1767308606: Received PINGREQ from board-mate-api 1767308606: Sending PINGRESP to board-mate-api +1767308666: Received PINGREQ from board-mate-api +1767308666: Sending PINGRESP to board-mate-api +1767308726: Received PINGREQ from board-mate-api +1767308726: Sending PINGRESP to board-mate-api +1767308786: Received PINGREQ from board-mate-api +1767308786: Sending PINGRESP to board-mate-api +1767308846: Received PINGREQ from board-mate-api +1767308846: Sending PINGRESP to board-mate-api +1767308906: Received PINGREQ from board-mate-api +1767308906: Sending PINGRESP to board-mate-api +1767308966: Received PINGREQ from board-mate-api +1767308966: Sending PINGRESP to board-mate-api +1767309026: Received PINGREQ from board-mate-api +1767309026: Sending PINGRESP to board-mate-api +1767309086: Received PINGREQ from board-mate-api +1767309086: Sending PINGRESP to board-mate-api +1767309146: Received PINGREQ from board-mate-api +1767309146: Sending PINGRESP to board-mate-api +1767309206: Received PINGREQ from board-mate-api +1767309206: Sending PINGRESP to board-mate-api +1767309266: Received PINGREQ from board-mate-api +1767309266: Sending PINGRESP to board-mate-api +1767309326: Received PINGREQ from board-mate-api +1767309326: Sending PINGRESP to board-mate-api +1767309386: Received PINGREQ from board-mate-api +1767309386: Sending PINGRESP to board-mate-api +1767309446: Received PINGREQ from board-mate-api +1767309446: Sending PINGRESP to board-mate-api +1767309506: Received PINGREQ from board-mate-api +1767309506: Sending PINGRESP to board-mate-api +1767309566: Received PINGREQ from board-mate-api +1767309566: Sending PINGRESP to board-mate-api +1767309626: Received PINGREQ from board-mate-api +1767309626: Sending PINGRESP to board-mate-api +1767309686: Received PINGREQ from board-mate-api +1767309686: Sending PINGRESP to board-mate-api +1767309746: Received PINGREQ from board-mate-api +1767309746: Sending PINGRESP to board-mate-api +1767309806: Received PINGREQ from board-mate-api +1767309806: Sending PINGRESP to board-mate-api +1767309852: Saving in-memory database to /mosquitto/data//mosquitto.db. +1767309866: Received PINGREQ from board-mate-api +1767309866: Sending PINGRESP to board-mate-api +1767309926: Received PINGREQ from board-mate-api +1767309926: Sending PINGRESP to board-mate-api +1767309986: Received PINGREQ from board-mate-api +1767309986: Sending PINGRESP to board-mate-api +1767310046: Received PINGREQ from board-mate-api +1767310046: Sending PINGRESP to board-mate-api +1767310106: Received PINGREQ from board-mate-api +1767310106: Sending PINGRESP to board-mate-api +1767310166: Received PINGREQ from board-mate-api +1767310166: Sending PINGRESP to board-mate-api +1767310226: Received PINGREQ from board-mate-api +1767310226: Sending PINGRESP to board-mate-api +1767310286: Received PINGREQ from board-mate-api +1767310286: Sending PINGRESP to board-mate-api +1767310346: Received PINGREQ from board-mate-api +1767310346: Sending PINGRESP to board-mate-api +1767310406: Received PINGREQ from board-mate-api +1767310406: Sending PINGRESP to board-mate-api +1767310466: Received PINGREQ from board-mate-api +1767310466: Sending PINGRESP to board-mate-api +1767310526: Received PINGREQ from board-mate-api +1767310526: Sending PINGRESP to board-mate-api +1767310586: Received PINGREQ from board-mate-api +1767310586: Sending PINGRESP to board-mate-api +1767310646: Received PINGREQ from board-mate-api +1767310646: Sending PINGRESP to board-mate-api +1767310706: Received PINGREQ from board-mate-api +1767310706: Sending PINGRESP to board-mate-api +1767310766: Received PINGREQ from board-mate-api +1767310766: Sending PINGRESP to board-mate-api +1767310826: Received PINGREQ from board-mate-api +1767310826: Sending PINGRESP to board-mate-api +1767310886: Received PINGREQ from board-mate-api +1767310886: Sending PINGRESP to board-mate-api +1767310946: Received PINGREQ from board-mate-api +1767310946: Sending PINGRESP to board-mate-api +1767311006: Received PINGREQ from board-mate-api +1767311006: Sending PINGRESP to board-mate-api +1767311066: Received PINGREQ from board-mate-api +1767311066: Sending PINGRESP to board-mate-api +1767311126: Received PINGREQ from board-mate-api +1767311126: Sending PINGRESP to board-mate-api +1767311186: Received PINGREQ from board-mate-api +1767311186: Sending PINGRESP to board-mate-api +1767311246: Received PINGREQ from board-mate-api +1767311246: Sending PINGRESP to board-mate-api +1767311306: Received PINGREQ from board-mate-api +1767311306: Sending PINGRESP to board-mate-api +1767311366: Received PINGREQ from board-mate-api +1767311366: Sending PINGRESP to board-mate-api +1767311426: Received PINGREQ from board-mate-api +1767311426: Sending PINGRESP to board-mate-api +1767311486: Received PINGREQ from board-mate-api +1767311486: Sending PINGRESP to board-mate-api +1767311535: Saving in-memory database to /mosquitto/data//mosquitto.db. +1767311546: Received PINGREQ from board-mate-api +1767311546: Sending PINGRESP to board-mate-api +1767311606: Received PINGREQ from board-mate-api +1767311606: Sending PINGRESP to board-mate-api +1767311666: Received PINGREQ from board-mate-api +1767311666: Sending PINGRESP to board-mate-api +1767311726: Received PINGREQ from board-mate-api +1767311726: Sending PINGRESP to board-mate-api +1767311758: mosquitto version 2.0.22 terminating +1767311758: Saving in-memory database to /mosquitto/data//mosquitto.db. +1767311762: mosquitto version 2.0.22 starting +1767311762: Config loaded from /mosquitto/config/mosquitto.conf. +1767311762: Opening ipv4 listen socket on port 1883. +1767311762: Opening ipv6 listen socket on port 1883. +1767311762: Opening websockets listen socket on port 9001. +1767311762: Opening ipv4 listen socket on port 8883. +1767311762: Opening ipv6 listen socket on port 8883. +1767311762: mosquitto version 2.0.22 running +1767311773: New connection from 172.18.0.7:55688 on port 1883. +1767311774: New client connected from 172.18.0.7:55688 as board-mate-api (p2, c0, k60, u'board-mate-api'). +1767311774: No will message specified. +1767311774: Sending CONNACK to board-mate-api (1, 0) +1767311774: Received SUBSCRIBE from board-mate-api +1767311774: /board-mate/+/telemetry (QoS 1) +1767311774: board-mate-api 1 /board-mate/+/telemetry +1767311774: Sending SUBACK to board-mate-api +1767311834: Received PINGREQ from board-mate-api +1767311834: Sending PINGRESP to board-mate-api +1767311894: Received PINGREQ from board-mate-api +1767311894: Sending PINGRESP to board-mate-api +1767311954: Received PINGREQ from board-mate-api +1767311954: Sending PINGRESP to board-mate-api +1767312014: Received PINGREQ from board-mate-api +1767312014: Sending PINGRESP to board-mate-api +1767312074: Received PINGREQ from board-mate-api +1767312074: Sending PINGRESP to board-mate-api +1767312134: Received PINGREQ from board-mate-api +1767312134: Sending PINGRESP to board-mate-api +1767312194: Received PINGREQ from board-mate-api +1767312194: Sending PINGRESP to board-mate-api +1767312254: Received PINGREQ from board-mate-api +1767312254: Sending PINGRESP to board-mate-api +1767312314: Received PINGREQ from board-mate-api +1767312314: Sending PINGRESP to board-mate-api +1767312374: Received PINGREQ from board-mate-api +1767312374: Sending PINGRESP to board-mate-api +1767312434: Received PINGREQ from board-mate-api +1767312434: Sending PINGRESP to board-mate-api +1767312494: Received PINGREQ from board-mate-api +1767312494: Sending PINGRESP to board-mate-api +1767312554: Received PINGREQ from board-mate-api +1767312554: Sending PINGRESP to board-mate-api +1767312594: mosquitto version 2.0.22 terminating +1767312594: Saving in-memory database to /mosquitto/data//mosquitto.db. +1767312599: mosquitto version 2.0.22 starting +1767312599: Config loaded from /mosquitto/config/mosquitto.conf. +1767312599: Opening ipv4 listen socket on port 1883. +1767312599: Opening ipv6 listen socket on port 1883. +1767312599: Opening websockets listen socket on port 9001. +1767312599: Opening ipv4 listen socket on port 8883. +1767312599: Opening ipv6 listen socket on port 8883. +1767312599: mosquitto version 2.0.22 running +1767312610: New connection from 172.18.0.7:36078 on port 1883. +1767312610: New client connected from 172.18.0.7:36078 as board-mate-api (p2, c0, k60, u'board-mate-api'). +1767312610: No will message specified. +1767312610: Sending CONNACK to board-mate-api (1, 0) +1767312610: Received SUBSCRIBE from board-mate-api +1767312610: /board-mate/+/telemetry (QoS 1) +1767312610: board-mate-api 1 /board-mate/+/telemetry +1767312610: Sending SUBACK to board-mate-api +1767312670: Received PINGREQ from board-mate-api +1767312670: Sending PINGRESP to board-mate-api +1767312730: Received PINGREQ from board-mate-api +1767312730: Sending PINGRESP to board-mate-api +1767312790: Received PINGREQ from board-mate-api +1767312790: Sending PINGRESP to board-mate-api +1767312850: Received PINGREQ from board-mate-api +1767312850: Sending PINGRESP to board-mate-api diff --git a/api/src/main/java/be/naaturel/boardmateapi/controllers/ChatController.java b/api/src/main/java/be/naaturel/boardmateapi/controllers/ChatController.java index 25310be1..4c71e75d 100644 --- a/api/src/main/java/be/naaturel/boardmateapi/controllers/ChatController.java +++ b/api/src/main/java/be/naaturel/boardmateapi/controllers/ChatController.java @@ -1,6 +1,8 @@ package be.naaturel.boardmateapi.controllers; +import be.naaturel.boardmateapi.common.exceptions.ServiceException; import be.naaturel.boardmateapi.common.models.Message; +import be.naaturel.boardmateapi.controllers.dtos.MessageDto; import be.naaturel.boardmateapi.controllers.dtos.MessagePostRequestDto; import be.naaturel.boardmateapi.controllers.dtos.ResponseBody; import be.naaturel.boardmateapi.services.MessageService; @@ -8,10 +10,9 @@ import be.naaturel.boardmateapi.services.WebexService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.List; @RestController public class ChatController { @@ -44,7 +45,7 @@ public class ChatController { return ResponseEntity. status(HttpStatus.OK) .body(result); - } catch (Exception e){ + } catch (ServiceException e){ result.setMessage(e.getMessage()); return ResponseEntity .status(HttpStatus.INTERNAL_SERVER_ERROR) @@ -52,9 +53,32 @@ public class ChatController { } } - @GetMapping("/message/history") - public void history(){ + @GetMapping("/message/history/{clientId}") + public ResponseEntity>> history( + @PathVariable String clientId, + @RequestParam(value = "$top", required = false, defaultValue = "20") int top + ) { + ResponseBody> result = ResponseBody.createEmpty(); + try { + List messages = this.messageService.getHistory(clientId, top); + List dtos = messages.stream() + .map(m -> { + MessageDto dto = new MessageDto(); + dto.setContent(m.getContent()); + dto.setTimestamp(m.getTimeStamp()); + return dto; + }) + .toList(); + + result.setSuccess(true); + result.setData(dtos); + return ResponseEntity.status(HttpStatus.OK).body(result); + + } catch (ServiceException e) { + result.setMessage(e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result); + } } } diff --git a/api/src/main/java/be/naaturel/boardmateapi/controllers/dtos/MessageDto.java b/api/src/main/java/be/naaturel/boardmateapi/controllers/dtos/MessageDto.java new file mode 100644 index 00000000..40916ee2 --- /dev/null +++ b/api/src/main/java/be/naaturel/boardmateapi/controllers/dtos/MessageDto.java @@ -0,0 +1,26 @@ +package be.naaturel.boardmateapi.controllers.dtos; + + +public class MessageDto { + + private String content; + + private int timestamp; + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public int getTimestamp() { + return timestamp; + } + + public void setTimestamp(int timestamp) { + this.timestamp = timestamp; + } + +} diff --git a/api/src/main/java/be/naaturel/boardmateapi/repository/MessageRepo.java b/api/src/main/java/be/naaturel/boardmateapi/repository/MessageRepo.java index 6e246b56..36be1e06 100644 --- a/api/src/main/java/be/naaturel/boardmateapi/repository/MessageRepo.java +++ b/api/src/main/java/be/naaturel/boardmateapi/repository/MessageRepo.java @@ -1,7 +1,14 @@ package be.naaturel.boardmateapi.repository; import be.naaturel.boardmateapi.repository.dtos.MessageDto; +import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; + +import java.util.List; public interface MessageRepo extends MongoRepository { + + List findByClientId(String clientId, Pageable pageable); + } diff --git a/api/src/main/java/be/naaturel/boardmateapi/repository/mappings/MessageMapper.java b/api/src/main/java/be/naaturel/boardmateapi/repository/mappings/MessageMapper.java index e047d9e6..5f4f99e0 100644 --- a/api/src/main/java/be/naaturel/boardmateapi/repository/mappings/MessageMapper.java +++ b/api/src/main/java/be/naaturel/boardmateapi/repository/mappings/MessageMapper.java @@ -3,6 +3,9 @@ package be.naaturel.boardmateapi.repository.mappings; import be.naaturel.boardmateapi.common.models.Message; import be.naaturel.boardmateapi.repository.dtos.MessageDto; +import java.util.Collection; +import java.util.List; + public class MessageMapper { public static Message toModel(MessageDto dto){ @@ -18,4 +21,8 @@ public class MessageMapper { return dto; } + public static List toModels(List dtos){ + return dtos.stream().map(MessageMapper::toModel).toList(); + } + } diff --git a/api/src/main/java/be/naaturel/boardmateapi/services/MessageService.java b/api/src/main/java/be/naaturel/boardmateapi/services/MessageService.java index 258a243c..6977c3f2 100644 --- a/api/src/main/java/be/naaturel/boardmateapi/services/MessageService.java +++ b/api/src/main/java/be/naaturel/boardmateapi/services/MessageService.java @@ -1,12 +1,19 @@ package be.naaturel.boardmateapi.services; import be.naaturel.boardmateapi.common.exceptions.ServiceException; +import be.naaturel.boardmateapi.common.helpers.Logger; import be.naaturel.boardmateapi.common.models.Message; import be.naaturel.boardmateapi.repository.MessageRepo; import be.naaturel.boardmateapi.repository.dtos.MessageDto; import be.naaturel.boardmateapi.repository.mappings.MessageMapper; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; +import java.util.Arrays; +import java.util.List; + @Service public class MessageService { @@ -23,9 +30,21 @@ public class MessageService { MessageDto result = repo.save(dto); return result.getId(); } catch (Exception e){ + Logger.displayError(Arrays.toString(e.getStackTrace())); throw new ServiceException("Failed to save message : " + e.getMessage(), e); } } + public List getHistory(String clientId, int limit) throws ServiceException { + try { + Pageable pageable = PageRequest.of(0, limit, Sort.by(Sort.Direction.DESC, "timeStamp")); + List messages = repo.findByClientId(clientId, pageable); + + return MessageMapper.toModels(messages); + } catch (Exception e){ + Logger.displayError(Arrays.toString(e.getStackTrace())); + throw new ServiceException("Failed to retrieve messages history : " + e.getMessage(), e); + } + } }