Add message history

This commit is contained in:
2026-01-02 01:14:42 +01:00
parent 08afaa19ee
commit a4c72098b3
9 changed files with 366 additions and 7 deletions

Binary file not shown.

View File

@@ -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

View File

@@ -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<ResponseBody<List<MessageDto>>> history(
@PathVariable String clientId,
@RequestParam(value = "$top", required = false, defaultValue = "20") int top
) {
ResponseBody<List<MessageDto>> result = ResponseBody.createEmpty();
try {
List<Message> messages = this.messageService.getHistory(clientId, top);
List<MessageDto> 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);
}
}
}

View File

@@ -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;
}
}

View File

@@ -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<MessageDto, String> {
List<MessageDto> findByClientId(String clientId, Pageable pageable);
}

View File

@@ -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<Message> toModels(List<MessageDto> dtos){
return dtos.stream().map(MessageMapper::toModel).toList();
}
}

View File

@@ -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<Message> getHistory(String clientId, int limit) throws ServiceException {
try {
Pageable pageable = PageRequest.of(0, limit, Sort.by(Sort.Direction.DESC, "timeStamp"));
List<MessageDto> 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);
}
}
}