Implement WebSocket for chat messages
This commit is contained in:
@@ -57,7 +57,7 @@ add_executable(${PROJECT_NAME}
|
||||
src/storages/IUserStorage.h
|
||||
src/mock_storage_implementation/UserStorage.cpp
|
||||
src/mock_storage_implementation/UserStorage.h
|
||||
"src/controllers/ProfileController.h" "src/controllers/ProfileController.cpp")
|
||||
"src/controllers/ProfileController.h" "src/controllers/ProfileController.cpp" "src/dto/AddressDto.h" "src/models/Address.h" "src/storages/IAddressStorage.h" "src/database_implementation/AddressStorage.h" "src/database_implementation/AddressStorage.cpp" "src/services/ChatService.h" "src/dto/ChatDto.h" "src/storages/IChatStorage.h" "src/models/Message.h" "src/models/Attachment.h" "src/database_implementation/ChatStorage.h" "src/database_implementation/ChatStorage.cpp" "src/controllers/ChatController.h" "src/controllers/ChatController.cpp" "src/services/ChatService.cpp" "src/dto/MessageDto.h" "src/controllers/ChatWebSocketController.h" "src/controllers/ChatWebSocketController.cpp")
|
||||
|
||||
# Copy config dir to build dir
|
||||
set(CONFIG_SOURCE_DIR ${CMAKE_SOURCE_DIR}/config)
|
||||
|
||||
@@ -37,12 +37,14 @@ CREATE TABLE attachments (
|
||||
id SERIAL PRIMARY KEY,
|
||||
type TEXT NOT NULL,
|
||||
file_path TEXT NOT NULL,
|
||||
size_bytes INT NOT NULL
|
||||
size_bytes INT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INT REFERENCES users(id) NOT NULL
|
||||
user_id INT REFERENCES users(id) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE messages (
|
||||
@@ -52,7 +54,8 @@ CREATE TABLE messages (
|
||||
type TEXT NOT NULL,
|
||||
content_text TEXT,
|
||||
thumbnail TEXT,
|
||||
attachment_id INT REFERENCES attachments(id)
|
||||
attachment_id INT REFERENCES attachments(id),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE employee_chat (
|
||||
|
||||
697
src/base64.hpp
Normal file
697
src/base64.hpp
Normal file
@@ -0,0 +1,697 @@
|
||||
#ifndef BASE64_HPP_
|
||||
#define BASE64_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
#if defined(__cpp_lib_bit_cast)
|
||||
#include <bit> // For std::bit_cast.
|
||||
#endif
|
||||
|
||||
namespace base64 {
|
||||
|
||||
namespace detail {
|
||||
|
||||
#if defined(__cpp_lib_bit_cast)
|
||||
using std::bit_cast;
|
||||
#else
|
||||
template <class To, class From>
|
||||
std::enable_if_t<sizeof(To) == sizeof(From) &&
|
||||
std::is_trivially_copyable_v<From> &&
|
||||
std::is_trivially_copyable_v<To>,
|
||||
To>
|
||||
bit_cast(const From& src) noexcept {
|
||||
static_assert(std::is_trivially_constructible_v<To>,
|
||||
"This implementation additionally requires "
|
||||
"destination type to be trivially constructible");
|
||||
|
||||
To dst;
|
||||
std::memcpy(&dst, &src, sizeof(To));
|
||||
return dst;
|
||||
}
|
||||
#endif
|
||||
|
||||
inline constexpr char padding_char{'='};
|
||||
inline constexpr uint32_t bad_char{0x01FFFFFF};
|
||||
|
||||
#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
|
||||
#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
|
||||
(defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN) || \
|
||||
(defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN) || \
|
||||
(defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN) || \
|
||||
(defined(__sun) && defined(__SVR4) && defined(_BIG_ENDIAN)) || \
|
||||
defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
|
||||
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__) || \
|
||||
defined(_M_PPC)
|
||||
#define __BIG_ENDIAN__
|
||||
#elif (defined(__BYTE_ORDER__) && \
|
||||
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || /* gcc */ \
|
||||
(defined(__BYTE_ORDER) && \
|
||||
__BYTE_ORDER == __LITTLE_ENDIAN) /* linux header */ \
|
||||
|| (defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN) || \
|
||||
(defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN) /* mingw header */ || \
|
||||
(defined(__sun) && defined(__SVR4) && \
|
||||
defined(_LITTLE_ENDIAN)) || /* solaris */ \
|
||||
defined(__ARMEL__) || \
|
||||
defined(__THUMBEL__) || defined(__AARCH64EL__) || defined(_MIPSEL) || \
|
||||
defined(__MIPSEL) || defined(__MIPSEL__) || defined(_M_IX86) || \
|
||||
defined(_M_X64) || defined(_M_IA64) || /* msvc for intel processors */ \
|
||||
defined(_M_ARM) /* msvc code on arm executes in little endian mode */
|
||||
#define __LITTLE_ENDIAN__
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined(__LITTLE_ENDIAN__) & !defined(__BIG_ENDIAN__)
|
||||
#error "UNKNOWN Platform / endianness. Configure endianness explicitly."
|
||||
#endif
|
||||
|
||||
#if defined(__LITTLE_ENDIAN__)
|
||||
std::array<std::uint32_t, 256> constexpr decode_table_0 = {
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x000000f8, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x000000fc,
|
||||
0x000000d0, 0x000000d4, 0x000000d8, 0x000000dc, 0x000000e0, 0x000000e4,
|
||||
0x000000e8, 0x000000ec, 0x000000f0, 0x000000f4, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
|
||||
0x00000004, 0x00000008, 0x0000000c, 0x00000010, 0x00000014, 0x00000018,
|
||||
0x0000001c, 0x00000020, 0x00000024, 0x00000028, 0x0000002c, 0x00000030,
|
||||
0x00000034, 0x00000038, 0x0000003c, 0x00000040, 0x00000044, 0x00000048,
|
||||
0x0000004c, 0x00000050, 0x00000054, 0x00000058, 0x0000005c, 0x00000060,
|
||||
0x00000064, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x00000068, 0x0000006c, 0x00000070, 0x00000074, 0x00000078,
|
||||
0x0000007c, 0x00000080, 0x00000084, 0x00000088, 0x0000008c, 0x00000090,
|
||||
0x00000094, 0x00000098, 0x0000009c, 0x000000a0, 0x000000a4, 0x000000a8,
|
||||
0x000000ac, 0x000000b0, 0x000000b4, 0x000000b8, 0x000000bc, 0x000000c0,
|
||||
0x000000c4, 0x000000c8, 0x000000cc, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff};
|
||||
|
||||
std::array<std::uint32_t, 256> constexpr decode_table_1 = {
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x0000e003, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x0000f003,
|
||||
0x00004003, 0x00005003, 0x00006003, 0x00007003, 0x00008003, 0x00009003,
|
||||
0x0000a003, 0x0000b003, 0x0000c003, 0x0000d003, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
|
||||
0x00001000, 0x00002000, 0x00003000, 0x00004000, 0x00005000, 0x00006000,
|
||||
0x00007000, 0x00008000, 0x00009000, 0x0000a000, 0x0000b000, 0x0000c000,
|
||||
0x0000d000, 0x0000e000, 0x0000f000, 0x00000001, 0x00001001, 0x00002001,
|
||||
0x00003001, 0x00004001, 0x00005001, 0x00006001, 0x00007001, 0x00008001,
|
||||
0x00009001, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x0000a001, 0x0000b001, 0x0000c001, 0x0000d001, 0x0000e001,
|
||||
0x0000f001, 0x00000002, 0x00001002, 0x00002002, 0x00003002, 0x00004002,
|
||||
0x00005002, 0x00006002, 0x00007002, 0x00008002, 0x00009002, 0x0000a002,
|
||||
0x0000b002, 0x0000c002, 0x0000d002, 0x0000e002, 0x0000f002, 0x00000003,
|
||||
0x00001003, 0x00002003, 0x00003003, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff};
|
||||
|
||||
std::array<std::uint32_t, 256> constexpr decode_table_2 = {
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x00800f00, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00c00f00,
|
||||
0x00000d00, 0x00400d00, 0x00800d00, 0x00c00d00, 0x00000e00, 0x00400e00,
|
||||
0x00800e00, 0x00c00e00, 0x00000f00, 0x00400f00, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
|
||||
0x00400000, 0x00800000, 0x00c00000, 0x00000100, 0x00400100, 0x00800100,
|
||||
0x00c00100, 0x00000200, 0x00400200, 0x00800200, 0x00c00200, 0x00000300,
|
||||
0x00400300, 0x00800300, 0x00c00300, 0x00000400, 0x00400400, 0x00800400,
|
||||
0x00c00400, 0x00000500, 0x00400500, 0x00800500, 0x00c00500, 0x00000600,
|
||||
0x00400600, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x00800600, 0x00c00600, 0x00000700, 0x00400700, 0x00800700,
|
||||
0x00c00700, 0x00000800, 0x00400800, 0x00800800, 0x00c00800, 0x00000900,
|
||||
0x00400900, 0x00800900, 0x00c00900, 0x00000a00, 0x00400a00, 0x00800a00,
|
||||
0x00c00a00, 0x00000b00, 0x00400b00, 0x00800b00, 0x00c00b00, 0x00000c00,
|
||||
0x00400c00, 0x00800c00, 0x00c00c00, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff};
|
||||
|
||||
std::array<std::uint32_t, 256> constexpr decode_table_3 = {
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x003e0000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x003f0000,
|
||||
0x00340000, 0x00350000, 0x00360000, 0x00370000, 0x00380000, 0x00390000,
|
||||
0x003a0000, 0x003b0000, 0x003c0000, 0x003d0000, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
|
||||
0x00010000, 0x00020000, 0x00030000, 0x00040000, 0x00050000, 0x00060000,
|
||||
0x00070000, 0x00080000, 0x00090000, 0x000a0000, 0x000b0000, 0x000c0000,
|
||||
0x000d0000, 0x000e0000, 0x000f0000, 0x00100000, 0x00110000, 0x00120000,
|
||||
0x00130000, 0x00140000, 0x00150000, 0x00160000, 0x00170000, 0x00180000,
|
||||
0x00190000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x001a0000, 0x001b0000, 0x001c0000, 0x001d0000, 0x001e0000,
|
||||
0x001f0000, 0x00200000, 0x00210000, 0x00220000, 0x00230000, 0x00240000,
|
||||
0x00250000, 0x00260000, 0x00270000, 0x00280000, 0x00290000, 0x002a0000,
|
||||
0x002b0000, 0x002c0000, 0x002d0000, 0x002e0000, 0x002f0000, 0x00300000,
|
||||
0x00310000, 0x00320000, 0x00330000, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff};
|
||||
|
||||
// TODO fix decoding tables to avoid the need for different indices in big
|
||||
// endian?
|
||||
inline constexpr size_t decidx0{0};
|
||||
inline constexpr size_t decidx1{1};
|
||||
inline constexpr size_t decidx2{2};
|
||||
|
||||
#elif defined(__BIG_ENDIAN__)
|
||||
|
||||
std::array<std::uint32_t, 256> constexpr decode_table_0 = {
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x00f80000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00fc0000,
|
||||
0x00d00000, 0x00d40000, 0x00d80000, 0x00dc0000, 0x00e00000, 0x00e40000,
|
||||
0x00e80000, 0x00ec0000, 0x00f00000, 0x00f40000, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
|
||||
0x00040000, 0x00080000, 0x000c0000, 0x00100000, 0x00140000, 0x00180000,
|
||||
0x001c0000, 0x00200000, 0x00240000, 0x00280000, 0x002c0000, 0x00300000,
|
||||
0x00340000, 0x00380000, 0x003c0000, 0x00400000, 0x00440000, 0x00480000,
|
||||
0x004c0000, 0x00500000, 0x00540000, 0x00580000, 0x005c0000, 0x00600000,
|
||||
0x00640000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x00680000, 0x006c0000, 0x00700000, 0x00740000, 0x00780000,
|
||||
0x007c0000, 0x00800000, 0x00840000, 0x00880000, 0x008c0000, 0x00900000,
|
||||
0x00940000, 0x00980000, 0x009c0000, 0x00a00000, 0x00a40000, 0x00a80000,
|
||||
0x00ac0000, 0x00b00000, 0x00b40000, 0x00b80000, 0x00bc0000, 0x00c00000,
|
||||
0x00c40000, 0x00c80000, 0x00cc0000, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff};
|
||||
|
||||
std::array<std::uint32_t, 256> constexpr decode_table_1 = {
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x0003e000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x0003f000,
|
||||
0x00034000, 0x00035000, 0x00036000, 0x00037000, 0x00038000, 0x00039000,
|
||||
0x0003a000, 0x0003b000, 0x0003c000, 0x0003d000, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
|
||||
0x00001000, 0x00002000, 0x00003000, 0x00004000, 0x00005000, 0x00006000,
|
||||
0x00007000, 0x00008000, 0x00009000, 0x0000a000, 0x0000b000, 0x0000c000,
|
||||
0x0000d000, 0x0000e000, 0x0000f000, 0x00010000, 0x00011000, 0x00012000,
|
||||
0x00013000, 0x00014000, 0x00015000, 0x00016000, 0x00017000, 0x00018000,
|
||||
0x00019000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x0001a000, 0x0001b000, 0x0001c000, 0x0001d000, 0x0001e000,
|
||||
0x0001f000, 0x00020000, 0x00021000, 0x00022000, 0x00023000, 0x00024000,
|
||||
0x00025000, 0x00026000, 0x00027000, 0x00028000, 0x00029000, 0x0002a000,
|
||||
0x0002b000, 0x0002c000, 0x0002d000, 0x0002e000, 0x0002f000, 0x00030000,
|
||||
0x00031000, 0x00032000, 0x00033000, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff};
|
||||
|
||||
std::array<std::uint32_t, 256> constexpr decode_table_2 = {
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x00000f80, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000fc0,
|
||||
0x00000d00, 0x00000d40, 0x00000d80, 0x00000dc0, 0x00000e00, 0x00000e40,
|
||||
0x00000e80, 0x00000ec0, 0x00000f00, 0x00000f40, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
|
||||
0x00000040, 0x00000080, 0x000000c0, 0x00000100, 0x00000140, 0x00000180,
|
||||
0x000001c0, 0x00000200, 0x00000240, 0x00000280, 0x000002c0, 0x00000300,
|
||||
0x00000340, 0x00000380, 0x000003c0, 0x00000400, 0x00000440, 0x00000480,
|
||||
0x000004c0, 0x00000500, 0x00000540, 0x00000580, 0x000005c0, 0x00000600,
|
||||
0x00000640, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x00000680, 0x000006c0, 0x00000700, 0x00000740, 0x00000780,
|
||||
0x000007c0, 0x00000800, 0x00000840, 0x00000880, 0x000008c0, 0x00000900,
|
||||
0x00000940, 0x00000980, 0x000009c0, 0x00000a00, 0x00000a40, 0x00000a80,
|
||||
0x00000ac0, 0x00000b00, 0x00000b40, 0x00000b80, 0x00000bc0, 0x00000c00,
|
||||
0x00000c40, 0x00000c80, 0x00000cc0, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff};
|
||||
|
||||
std::array<std::uint32_t, 256> constexpr decode_table_3 = {
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x0000003e, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x0000003f,
|
||||
0x00000034, 0x00000035, 0x00000036, 0x00000037, 0x00000038, 0x00000039,
|
||||
0x0000003a, 0x0000003b, 0x0000003c, 0x0000003d, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
|
||||
0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006,
|
||||
0x00000007, 0x00000008, 0x00000009, 0x0000000a, 0x0000000b, 0x0000000c,
|
||||
0x0000000d, 0x0000000e, 0x0000000f, 0x00000010, 0x00000011, 0x00000012,
|
||||
0x00000013, 0x00000014, 0x00000015, 0x00000016, 0x00000017, 0x00000018,
|
||||
0x00000019, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x0000001a, 0x0000001b, 0x0000001c, 0x0000001d, 0x0000001e,
|
||||
0x0000001f, 0x00000020, 0x00000021, 0x00000022, 0x00000023, 0x00000024,
|
||||
0x00000025, 0x00000026, 0x00000027, 0x00000028, 0x00000029, 0x0000002a,
|
||||
0x0000002b, 0x0000002c, 0x0000002d, 0x0000002e, 0x0000002f, 0x00000030,
|
||||
0x00000031, 0x00000032, 0x00000033, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
|
||||
0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff};
|
||||
|
||||
// TODO fix decoding tables to avoid the need for different indices in big
|
||||
// endian?
|
||||
inline constexpr size_t decidx0{1};
|
||||
inline constexpr size_t decidx1{2};
|
||||
inline constexpr size_t decidx2{3};
|
||||
|
||||
#endif
|
||||
|
||||
std::array<char, 256> constexpr encode_table_0 = {
|
||||
'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'D', 'D', 'D',
|
||||
'D', 'E', 'E', 'E', 'E', 'F', 'F', 'F', 'F', 'G', 'G', 'G', 'G', 'H', 'H',
|
||||
'H', 'H', 'I', 'I', 'I', 'I', 'J', 'J', 'J', 'J', 'K', 'K', 'K', 'K', 'L',
|
||||
'L', 'L', 'L', 'M', 'M', 'M', 'M', 'N', 'N', 'N', 'N', 'O', 'O', 'O', 'O',
|
||||
'P', 'P', 'P', 'P', 'Q', 'Q', 'Q', 'Q', 'R', 'R', 'R', 'R', 'S', 'S', 'S',
|
||||
'S', 'T', 'T', 'T', 'T', 'U', 'U', 'U', 'U', 'V', 'V', 'V', 'V', 'W', 'W',
|
||||
'W', 'W', 'X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z', 'Z', 'a',
|
||||
'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd',
|
||||
'e', 'e', 'e', 'e', 'f', 'f', 'f', 'f', 'g', 'g', 'g', 'g', 'h', 'h', 'h',
|
||||
'h', 'i', 'i', 'i', 'i', 'j', 'j', 'j', 'j', 'k', 'k', 'k', 'k', 'l', 'l',
|
||||
'l', 'l', 'm', 'm', 'm', 'm', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'p',
|
||||
'p', 'p', 'p', 'q', 'q', 'q', 'q', 'r', 'r', 'r', 'r', 's', 's', 's', 's',
|
||||
't', 't', 't', 't', 'u', 'u', 'u', 'u', 'v', 'v', 'v', 'v', 'w', 'w', 'w',
|
||||
'w', 'x', 'x', 'x', 'x', 'y', 'y', 'y', 'y', 'z', 'z', 'z', 'z', '0', '0',
|
||||
'0', '0', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '3', '4',
|
||||
'4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7',
|
||||
'8', '8', '8', '8', '9', '9', '9', '9', '+', '+', '+', '+', '/', '/', '/',
|
||||
'/'};
|
||||
|
||||
std::array<char, 256> constexpr encode_table_1 = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
||||
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
|
||||
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
|
||||
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', '+', '/', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
|
||||
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
||||
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
||||
'4', '5', '6', '7', '8', '9', '+', '/', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
||||
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
|
||||
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
|
||||
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', 'A', 'B', 'C',
|
||||
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
|
||||
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
||||
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+',
|
||||
'/'};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <class OutputBuffer, class InputIterator>
|
||||
inline OutputBuffer encode_into(InputIterator begin, InputIterator end) {
|
||||
typedef std::decay_t<decltype(*begin)> input_value_type;
|
||||
static_assert(std::is_same_v<input_value_type, char> ||
|
||||
std::is_same_v<input_value_type, signed char> ||
|
||||
std::is_same_v<input_value_type, unsigned char> ||
|
||||
std::is_same_v<input_value_type, std::byte>);
|
||||
typedef typename OutputBuffer::value_type output_value_type;
|
||||
static_assert(std::is_same_v<output_value_type, char> ||
|
||||
std::is_same_v<output_value_type, signed char> ||
|
||||
std::is_same_v<output_value_type, unsigned char> ||
|
||||
std::is_same_v<output_value_type, std::byte>);
|
||||
const size_t binarytextsize = end - begin;
|
||||
const size_t encodedsize = (binarytextsize / 3 + (binarytextsize % 3 > 0))
|
||||
<< 2;
|
||||
OutputBuffer encoded(encodedsize, detail::padding_char);
|
||||
|
||||
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&*begin);
|
||||
char* currEncoding = reinterpret_cast<char*>(&encoded[0]);
|
||||
|
||||
for (size_t i = binarytextsize / 3; i; --i) {
|
||||
const uint8_t t1 = *bytes++;
|
||||
const uint8_t t2 = *bytes++;
|
||||
const uint8_t t3 = *bytes++;
|
||||
*currEncoding++ = detail::encode_table_0[t1];
|
||||
*currEncoding++ =
|
||||
detail::encode_table_1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
|
||||
*currEncoding++ =
|
||||
detail::encode_table_1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
|
||||
*currEncoding++ = detail::encode_table_1[t3];
|
||||
}
|
||||
|
||||
switch (binarytextsize % 3) {
|
||||
case 0: {
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
const uint8_t t1 = bytes[0];
|
||||
*currEncoding++ = detail::encode_table_0[t1];
|
||||
*currEncoding++ = detail::encode_table_1[(t1 & 0x03) << 4];
|
||||
// *currEncoding++ = detail::padding_char;
|
||||
// *currEncoding++ = detail::padding_char;
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
const uint8_t t1 = bytes[0];
|
||||
const uint8_t t2 = bytes[1];
|
||||
*currEncoding++ = detail::encode_table_0[t1];
|
||||
*currEncoding++ =
|
||||
detail::encode_table_1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
|
||||
*currEncoding++ = detail::encode_table_1[(t2 & 0x0F) << 2];
|
||||
// *currEncoding++ = detail::padding_char;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw std::runtime_error{"Invalid base64 encoded data"};
|
||||
}
|
||||
}
|
||||
|
||||
return encoded;
|
||||
}
|
||||
|
||||
template <class OutputBuffer>
|
||||
inline OutputBuffer encode_into(std::string_view data) {
|
||||
return encode_into<OutputBuffer>(std::begin(data), std::end(data));
|
||||
}
|
||||
|
||||
inline std::string to_base64(std::string_view data) {
|
||||
return encode_into<std::string>(std::begin(data), std::end(data));
|
||||
}
|
||||
|
||||
template <class OutputBuffer>
|
||||
inline OutputBuffer decode_into(std::string_view base64Text) {
|
||||
typedef typename OutputBuffer::value_type output_value_type;
|
||||
static_assert(std::is_same_v<output_value_type, char> ||
|
||||
std::is_same_v<output_value_type, signed char> ||
|
||||
std::is_same_v<output_value_type, unsigned char> ||
|
||||
std::is_same_v<output_value_type, std::byte>);
|
||||
if (base64Text.empty()) {
|
||||
return OutputBuffer();
|
||||
}
|
||||
|
||||
if ((base64Text.size() & 3) != 0) {
|
||||
throw std::runtime_error{
|
||||
"Invalid base64 encoded data - Size not divisible by 4"};
|
||||
}
|
||||
|
||||
const size_t numPadding =
|
||||
std::count(base64Text.rbegin(), base64Text.rbegin() + 4, '=');
|
||||
if (numPadding > 2) {
|
||||
throw std::runtime_error{
|
||||
"Invalid base64 encoded data - Found more than 2 padding signs"};
|
||||
}
|
||||
|
||||
const size_t decodedsize = (base64Text.size() * 3 >> 2) - numPadding;
|
||||
OutputBuffer decoded(decodedsize, '.');
|
||||
|
||||
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&base64Text[0]);
|
||||
char* currDecoding = reinterpret_cast<char*>(&decoded[0]);
|
||||
|
||||
for (size_t i = (base64Text.size() >> 2) - (numPadding != 0); i; --i) {
|
||||
const uint8_t t1 = *bytes++;
|
||||
const uint8_t t2 = *bytes++;
|
||||
const uint8_t t3 = *bytes++;
|
||||
const uint8_t t4 = *bytes++;
|
||||
|
||||
const uint32_t d1 = detail::decode_table_0[t1];
|
||||
const uint32_t d2 = detail::decode_table_1[t2];
|
||||
const uint32_t d3 = detail::decode_table_2[t3];
|
||||
const uint32_t d4 = detail::decode_table_3[t4];
|
||||
|
||||
const uint32_t temp = d1 | d2 | d3 | d4;
|
||||
|
||||
if (temp >= detail::bad_char) {
|
||||
throw std::runtime_error{
|
||||
"Invalid base64 encoded data - Invalid character"};
|
||||
}
|
||||
|
||||
// Use bit_cast instead of union and type punning to avoid
|
||||
// undefined behaviour risk:
|
||||
// https://en.wikipedia.org/wiki/Type_punning#Use_of_union
|
||||
const std::array<char, 4> tempBytes =
|
||||
detail::bit_cast<std::array<char, 4>, uint32_t>(temp);
|
||||
|
||||
*currDecoding++ = tempBytes[detail::decidx0];
|
||||
*currDecoding++ = tempBytes[detail::decidx1];
|
||||
*currDecoding++ = tempBytes[detail::decidx2];
|
||||
}
|
||||
|
||||
switch (numPadding) {
|
||||
case 0: {
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
const uint8_t t1 = *bytes++;
|
||||
const uint8_t t2 = *bytes++;
|
||||
const uint8_t t3 = *bytes++;
|
||||
|
||||
const uint32_t d1 = detail::decode_table_0[t1];
|
||||
const uint32_t d2 = detail::decode_table_1[t2];
|
||||
const uint32_t d3 = detail::decode_table_2[t3];
|
||||
|
||||
const uint32_t temp = d1 | d2 | d3;
|
||||
|
||||
if (temp >= detail::bad_char) {
|
||||
throw std::runtime_error{
|
||||
"Invalid base64 encoded data - Invalid character"};
|
||||
}
|
||||
|
||||
// Use bit_cast instead of union and type punning to avoid
|
||||
// undefined behaviour risk:
|
||||
// https://en.wikipedia.org/wiki/Type_punning#Use_of_union
|
||||
const std::array<char, 4> tempBytes =
|
||||
detail::bit_cast<std::array<char, 4>, uint32_t>(temp);
|
||||
*currDecoding++ = tempBytes[detail::decidx0];
|
||||
*currDecoding++ = tempBytes[detail::decidx1];
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
const uint8_t t1 = *bytes++;
|
||||
const uint8_t t2 = *bytes++;
|
||||
|
||||
const uint32_t d1 = detail::decode_table_0[t1];
|
||||
const uint32_t d2 = detail::decode_table_1[t2];
|
||||
|
||||
const uint32_t temp = d1 | d2;
|
||||
|
||||
if (temp >= detail::bad_char) {
|
||||
throw std::runtime_error{
|
||||
"Invalid base64 encoded data - Invalid character"};
|
||||
}
|
||||
|
||||
const std::array<char, 4> tempBytes =
|
||||
detail::bit_cast<std::array<char, 4>, uint32_t>(temp);
|
||||
*currDecoding++ = tempBytes[detail::decidx0];
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw std::runtime_error{
|
||||
"Invalid base64 encoded data - Invalid padding number"};
|
||||
}
|
||||
}
|
||||
|
||||
return decoded;
|
||||
}
|
||||
|
||||
template <class OutputBuffer, class InputIterator>
|
||||
inline OutputBuffer decode_into(InputIterator begin, InputIterator end) {
|
||||
typedef std::decay_t<decltype(*begin)> input_value_type;
|
||||
static_assert(std::is_same_v<input_value_type, char> ||
|
||||
std::is_same_v<input_value_type, signed char> ||
|
||||
std::is_same_v<input_value_type, unsigned char> ||
|
||||
std::is_same_v<input_value_type, std::byte>);
|
||||
std::string_view data(reinterpret_cast<const char*>(&*begin), end - begin);
|
||||
return decode_into<OutputBuffer>(data);
|
||||
}
|
||||
|
||||
inline std::string from_base64(std::string_view data) {
|
||||
return decode_into<std::string>(data);
|
||||
}
|
||||
|
||||
} // namespace base64
|
||||
|
||||
#endif // BASE64_HPP_
|
||||
124
src/controllers/ChatController.cpp
Normal file
124
src/controllers/ChatController.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
#include "ChatController.h"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "dependency_injection/DependencyContainer.h"
|
||||
#include "dto/MessageDto.h"
|
||||
#include "services/ChatService.h"
|
||||
#include "utils/JsonConversion.h"
|
||||
|
||||
api::v1::Chat::Chat()
|
||||
{
|
||||
m_logger = DependencyContainer::Resolve<spdlog::logger>();
|
||||
m_chatService = DependencyContainer::Resolve<ChatService>();
|
||||
}
|
||||
|
||||
void api::v1::Chat::getChatId(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback)
|
||||
{
|
||||
auto owningRole = req->attributes()->get<std::string>("role");
|
||||
if (owningRole != "RESIDENT")
|
||||
{
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(drogon::k403Forbidden);
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
auto owningUserId = req->attributes()->get<int>("user_id");
|
||||
int chatId = m_chatService->getChatId(owningUserId);
|
||||
|
||||
nlohmann::json j;
|
||||
j["chatId"] = chatId;
|
||||
|
||||
auto resp = drogon::HttpResponse::newHttpJsonResponse(nlohmannToJsonValue(j));
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::Chat::loadMessagesByUserId(
|
||||
const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
|
||||
int page,
|
||||
int pageSize
|
||||
)
|
||||
{
|
||||
auto owningUserId = req->attributes()->get<int>("user_id");
|
||||
auto messages = m_chatService->loadMessagesByUserId(owningUserId, page, pageSize);
|
||||
|
||||
nlohmann::json j = messages;
|
||||
auto resp = drogon::HttpResponse::newHttpJsonResponse(nlohmannToJsonValue(j));
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::Chat::loadMessagesInChat(
|
||||
const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
|
||||
int chatId,
|
||||
int page,
|
||||
int pageSize
|
||||
)
|
||||
{
|
||||
auto messages = m_chatService->loadMessagesByChatId(chatId, page, pageSize);
|
||||
|
||||
nlohmann::json j = messages;
|
||||
auto resp = drogon::HttpResponse::newHttpJsonResponse(nlohmannToJsonValue(j));
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::Chat::loadChats(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback)
|
||||
{
|
||||
auto owningRole = req->attributes()->get<std::string>("role");
|
||||
if (owningRole != "EMPLOYEE")
|
||||
{
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(drogon::k403Forbidden);
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
auto users = m_chatService->loadChats();
|
||||
nlohmann::json j = users;
|
||||
auto resp = drogon::HttpResponse::newHttpJsonResponse(nlohmannToJsonValue(j));
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::Chat::loadAttachment(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback, int attachmentId)
|
||||
{
|
||||
std::string file = m_chatService->loadAttachment(attachmentId);
|
||||
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setBody(file);
|
||||
resp->setContentTypeCode(drogon::CT_APPLICATION_OCTET_STREAM);
|
||||
callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::Chat::postTextMessage(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback)
|
||||
{
|
||||
auto owningUserId = req->attributes()->get<int>("user_id");
|
||||
|
||||
auto body = req->getBody();
|
||||
auto j = nlohmann::json::parse(body);
|
||||
int chatId = j["chatId"];
|
||||
std::string textContent = j["content"];
|
||||
|
||||
auto dto = m_chatService->postTextMessageByChatId(owningUserId, chatId, textContent);
|
||||
nlohmann::json j_resp = dto.value();
|
||||
auto resp = drogon::HttpResponse::newHttpJsonResponse(nlohmannToJsonValue(j_resp));
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::Chat::postFileMessage(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback)
|
||||
{
|
||||
auto owningUserId = req->attributes()->get<int>("user_id");
|
||||
|
||||
drogon::MultiPartParser parser;
|
||||
parser.parse(req);
|
||||
|
||||
auto& file = parser.getFiles()[0];
|
||||
std::string fileExt(file.getFileName());
|
||||
|
||||
auto chatData = parser.getParameter<std::string>("chat_data");
|
||||
auto parsedChatData = nlohmann::json::parse(chatData);
|
||||
int chatId = parsedChatData["chatId"];
|
||||
|
||||
auto dto = m_chatService->postFileMessageByChatId(owningUserId, chatId, fileExt, file.fileData(), file.fileLength());
|
||||
nlohmann::json j_resp = dto.value();
|
||||
auto resp = drogon::HttpResponse::newHttpJsonResponse(nlohmannToJsonValue(j_resp));
|
||||
return callback(resp);
|
||||
}
|
||||
61
src/controllers/ChatController.h
Normal file
61
src/controllers/ChatController.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <drogon/drogon.h>
|
||||
|
||||
class ChatService;
|
||||
class spdlog::logger;
|
||||
|
||||
namespace api
|
||||
{
|
||||
namespace v1
|
||||
{
|
||||
|
||||
class Chat : public drogon::HttpController<Chat>
|
||||
{
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
METHOD_ADD(Chat::getChatId, "/id", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_ADD(Chat::loadMessagesByUserId, "/messages?page={}&pageSize={}", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_ADD(Chat::loadMessagesInChat, "/messagesInChat?chatId={}&page={}&pageSize={}", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_ADD(Chat::loadChats, "/chatList", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_ADD(Chat::loadAttachment, "/attachment?attachmentId={}", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_ADD(Chat::postTextMessage, "/sendText", drogon::Post, "TokenValidationFilter");
|
||||
METHOD_ADD(Chat::postFileMessage, "/sendFile", drogon::Post, "TokenValidationFilter");
|
||||
METHOD_LIST_END
|
||||
|
||||
Chat();
|
||||
|
||||
void getChatId(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback);
|
||||
|
||||
void loadMessagesByUserId(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
|
||||
int page,
|
||||
int pageSize);
|
||||
|
||||
void loadMessagesInChat(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
|
||||
int chatId,
|
||||
int page,
|
||||
int pageSize);
|
||||
|
||||
void loadChats(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback);
|
||||
|
||||
void loadAttachment(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
|
||||
int attachmentId);
|
||||
|
||||
void postTextMessage(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback);
|
||||
|
||||
void postFileMessage(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback);
|
||||
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> m_logger;
|
||||
std::shared_ptr<ChatService> m_chatService;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
47
src/controllers/ChatWebSocketController.cpp
Normal file
47
src/controllers/ChatWebSocketController.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "ChatWebSocketController.h"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "dependency_injection/DependencyContainer.h"
|
||||
|
||||
ChatWebSocket::ChatWebSocket()
|
||||
{
|
||||
m_logger = DependencyContainer::Resolve<spdlog::logger>();
|
||||
}
|
||||
|
||||
void ChatWebSocket::handleNewConnection(const drogon::HttpRequestPtr& req, const drogon::WebSocketConnectionPtr& conn)
|
||||
{
|
||||
auto chatIdStr = req->getParameter("chatId");
|
||||
int chatId = std::stoi(chatIdStr);
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_chatConnections[chatId].insert(conn);
|
||||
|
||||
conn->setContext(std::make_shared<int>(chatId));
|
||||
}
|
||||
|
||||
void ChatWebSocket::handleConnectionClosed(const drogon::WebSocketConnectionPtr& conn)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
for (auto& [chatId, conns] : m_chatConnections)
|
||||
{
|
||||
conns.erase(conn);
|
||||
}
|
||||
}
|
||||
|
||||
void ChatWebSocket::newMessage(int chatId, const std::string& json)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
auto it = m_chatConnections.find(chatId);
|
||||
if (it != m_chatConnections.end())
|
||||
{
|
||||
for (auto& conn : it->second)
|
||||
{
|
||||
// send message to each client currently connected to chatId
|
||||
if (conn && conn->connected())
|
||||
{
|
||||
conn->send(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
43
src/controllers/ChatWebSocketController.h
Normal file
43
src/controllers/ChatWebSocketController.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <drogon/drogon.h>
|
||||
#include <drogon/WebSocketController.h>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <mutex>
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
class logger;
|
||||
}
|
||||
|
||||
class ChatWebSocket : public drogon::WebSocketController<ChatWebSocket>
|
||||
{
|
||||
public:
|
||||
ChatWebSocket();
|
||||
|
||||
void newMessage(int chatId, const std::string& json);
|
||||
|
||||
// This function is called after a new connection of WebSocket is established.
|
||||
virtual void handleNewConnection(const drogon::HttpRequestPtr& req,
|
||||
const drogon::WebSocketConnectionPtr& conn) override;
|
||||
|
||||
// This function is called when a new message is received
|
||||
virtual void handleNewMessage(const drogon::WebSocketConnectionPtr& wsConnPtr,
|
||||
std::string&& message,
|
||||
const drogon::WebSocketMessageType& type) override
|
||||
{ }
|
||||
|
||||
// This function is called after a WebSocket connection is closed
|
||||
virtual void handleConnectionClosed(const drogon::WebSocketConnectionPtr& conn) override;
|
||||
|
||||
WS_PATH_LIST_BEGIN
|
||||
WS_PATH_ADD("/api/v1/chatws", drogon::Get);
|
||||
WS_PATH_LIST_END
|
||||
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> m_logger;
|
||||
std::mutex m_mutex;
|
||||
std::unordered_map<int, std::unordered_set<drogon::WebSocketConnectionPtr>> m_chatConnections;
|
||||
};
|
||||
@@ -14,6 +14,10 @@ api::v1::Profile::Profile()
|
||||
|
||||
void api::v1::Profile::getProfile(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback)
|
||||
{
|
||||
auto owningUserId = req->attributes()->get<int>("userId");
|
||||
auto owningUserId = req->attributes()->get<int>("user_id");
|
||||
auto userData = m_userService->getUserById(owningUserId);
|
||||
|
||||
nlohmann::json j = userData;
|
||||
auto resp = drogon::HttpResponse::newHttpJsonResponse(nlohmannToJsonValue(j));
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace api
|
||||
{
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
METHOD_ADD(Profile::getProfile, "/", drogon::Get);
|
||||
METHOD_ADD(Profile::getProfile, "", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_LIST_END
|
||||
|
||||
Profile();
|
||||
|
||||
@@ -15,6 +15,22 @@ api::v1::User::User()
|
||||
m_userService = DependencyContainer::Resolve<UserService>();
|
||||
}
|
||||
|
||||
void api::v1::User::resident(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback, int userId)
|
||||
{
|
||||
auto owningRole = req->attributes()->get<std::string>("role");
|
||||
if (owningRole != "EMPLOYEE")
|
||||
{
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(drogon::k403Forbidden);
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
auto user = m_userService->getResident(userId);
|
||||
nlohmann::json j = user;
|
||||
auto resp = drogon::HttpResponse::newHttpJsonResponse(nlohmannToJsonValue(j));
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::User::residents(
|
||||
const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
|
||||
@@ -37,6 +53,74 @@ void api::v1::User::residents(
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::User::unboundAddresses(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback, int userId, int page, int pageSize, std::string search)
|
||||
{
|
||||
auto owningRole = req->attributes()->get<std::string>("role");
|
||||
if (owningRole != "EMPLOYEE")
|
||||
{
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(drogon::k403Forbidden);
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
auto addresses = m_userService->getUnboundAddresses(userId, page, pageSize, std::move(search));
|
||||
nlohmann::json j = addresses;
|
||||
auto resp = drogon::HttpResponse::newHttpJsonResponse(nlohmannToJsonValue(j));
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::User::bindAddress(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback)
|
||||
{
|
||||
auto owningRole = req->attributes()->get<std::string>("role");
|
||||
if (owningRole != "EMPLOYEE")
|
||||
{
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(drogon::k403Forbidden);
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
auto body = req->getBody();
|
||||
auto j = nlohmann::json::parse(body);
|
||||
int userId = j["userId"];
|
||||
int addressId = j["addressId"];
|
||||
|
||||
if (!m_userService->bindUserToAddress(userId, addressId))
|
||||
{
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(drogon::k400BadRequest);
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::User::unbindAddress(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback)
|
||||
{
|
||||
auto owningRole = req->attributes()->get<std::string>("role");
|
||||
if (owningRole != "EMPLOYEE")
|
||||
{
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(drogon::k403Forbidden);
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
auto body = req->getBody();
|
||||
auto j = nlohmann::json::parse(body);
|
||||
int userId = j["userId"];
|
||||
int addressId = j["addressId"];
|
||||
|
||||
if (!m_userService->unbindUserFromAddress(userId, addressId))
|
||||
{
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(drogon::k400BadRequest);
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
return callback(resp);
|
||||
}
|
||||
|
||||
void api::v1::User::uploadFile(
|
||||
const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback
|
||||
|
||||
@@ -14,18 +14,39 @@ class User : public drogon::HttpController<User>
|
||||
{
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
METHOD_ADD(User::residents, "/users?page={}&pageSize={}&search={}", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_ADD(User::resident, "/resident?userId={}", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_ADD(User::residents, "/residents?page={}&pageSize={}&search={}", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_ADD(User::unboundAddresses, "/unboundAddresses?userId={}&page={}&pageSize={}&search={}", drogon::Get, "TokenValidationFilter");
|
||||
METHOD_ADD(User::bindAddress, "/bindAddress", drogon::Post, "TokenValidationFilter");
|
||||
METHOD_ADD(User::unbindAddress, "/unbindAddress", drogon::Post, "TokenValidationFilter");
|
||||
METHOD_ADD(User::uploadFile, "/upload", drogon::Post, "TokenValidationFilter");
|
||||
METHOD_LIST_END
|
||||
|
||||
User();
|
||||
|
||||
void resident(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
|
||||
int userId);
|
||||
|
||||
void residents(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
|
||||
int page,
|
||||
int pageSize,
|
||||
std::string search);
|
||||
|
||||
void unboundAddresses(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback,
|
||||
int userId,
|
||||
int page,
|
||||
int pageSize,
|
||||
std::string search);
|
||||
|
||||
void bindAddress(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback);
|
||||
|
||||
void unbindAddress(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback);
|
||||
|
||||
void uploadFile(const drogon::HttpRequestPtr& req,
|
||||
std::function<void(const drogon::HttpResponsePtr&)>&& callback);
|
||||
|
||||
|
||||
94
src/database_implementation/AddressStorage.cpp
Normal file
94
src/database_implementation/AddressStorage.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "AddressStorage.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <pqxx/pqxx>
|
||||
#include <spdlog/logger.h>
|
||||
#include "dependency_injection/DependencyContainer.h"
|
||||
|
||||
static const char* DB_CONN_PARAMS = "host=localhost port=2345 dbname=mydb user=doggo password=confirm";
|
||||
|
||||
static void splitString(const std::string& input, std::vector<std::string>& outComponents, int componentsNum)
|
||||
{
|
||||
std::stringstream ss(input);
|
||||
std::string item;
|
||||
outComponents.clear();
|
||||
|
||||
while (std::getline(ss, item, ' ')) {
|
||||
size_t start = item.find_first_not_of(" ");
|
||||
size_t end = item.find_last_not_of(" ");
|
||||
if (start != std::string::npos && end != std::string::npos)
|
||||
{
|
||||
outComponents.push_back(item.substr(start, end - start + 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
outComponents.push_back("");
|
||||
}
|
||||
}
|
||||
|
||||
while (outComponents.size() < componentsNum) {
|
||||
outComponents.push_back("");
|
||||
}
|
||||
}
|
||||
|
||||
DbAddressStorage::DbAddressStorage()
|
||||
{
|
||||
m_logger = DependencyContainer::Resolve<spdlog::logger>();
|
||||
}
|
||||
|
||||
std::vector<Address> DbAddressStorage::getUnboundAddressesForUserId(int userId, int page, int pageSize, std::string search)
|
||||
{
|
||||
// get "<city> <street> <house>" from search string
|
||||
std::vector<std::string> addressComponents;
|
||||
splitString(search, addressComponents, 3);
|
||||
|
||||
// wrap wildcards with $
|
||||
for (auto& comp : addressComponents)
|
||||
{
|
||||
comp = '%' + comp + '%';
|
||||
}
|
||||
|
||||
std::vector<Address> addresses;
|
||||
try
|
||||
{
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
int pageOffset = (page - 1) * pageSize;
|
||||
auto res = tx.exec(
|
||||
R"(
|
||||
SELECT a.id, a.city, a.street, a.house
|
||||
FROM addresses a
|
||||
LEFT JOIN user_address ua ON a.id = ua.address_id AND ua.user_id = $1
|
||||
WHERE ua.user_id IS NULL
|
||||
AND (
|
||||
a.city ILIKE $2 AND a.street ILIKE $3 AND a.house ILIKE $4
|
||||
)
|
||||
OFFSET $5 LIMIT $6
|
||||
)", pqxx::params{ userId, addressComponents[0], addressComponents[1], addressComponents[2], pageOffset, pageSize });
|
||||
|
||||
for (pqxx::row_size_type i = 0; i < res.columns(); ++i)
|
||||
{
|
||||
m_logger->info("Column {}: {}", i, res.column_name(i));
|
||||
}
|
||||
|
||||
for (const auto row : res)
|
||||
{
|
||||
Address address;
|
||||
address.id = row["id"].as<int>();
|
||||
address.city = row["city"].as<std::string>();
|
||||
address.street = row["street"].as<std::string>();
|
||||
address.house = row["house"].as<std::string>();
|
||||
|
||||
addresses.push_back(std::move(address));
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when querying unbound addresses for userId {}: {}", userId, ex.what());
|
||||
}
|
||||
return addresses;
|
||||
}
|
||||
19
src/database_implementation/AddressStorage.h
Normal file
19
src/database_implementation/AddressStorage.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "storages/IAddressStorage.h"
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
class logger;
|
||||
}
|
||||
|
||||
class DbAddressStorage : public IAddressStorage
|
||||
{
|
||||
public:
|
||||
DbAddressStorage();
|
||||
|
||||
virtual std::vector<Address> getUnboundAddressesForUserId(int userId, int page, int pageSize, std::string search) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> m_logger;
|
||||
};
|
||||
336
src/database_implementation/ChatStorage.cpp
Normal file
336
src/database_implementation/ChatStorage.cpp
Normal file
@@ -0,0 +1,336 @@
|
||||
#include "ChatStorage.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <pqxx/pqxx>
|
||||
#include <spdlog/logger.h>
|
||||
#include "dependency_injection/DependencyContainer.h"
|
||||
|
||||
static const char* DB_CONN_PARAMS = "host=localhost port=2345 dbname=mydb user=doggo password=confirm";
|
||||
|
||||
DbChatStorage::DbChatStorage()
|
||||
{
|
||||
m_logger = DependencyContainer::Resolve<spdlog::logger>();
|
||||
}
|
||||
|
||||
std::optional<Chat> DbChatStorage::getChatForUserId(int userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
auto res = tx.exec(
|
||||
R"(
|
||||
SELECT id, user_id FROM chats
|
||||
WHERE user_id = $1
|
||||
LIMIT 1
|
||||
)", pqxx::params{ userId }).opt_row();
|
||||
|
||||
if (res)
|
||||
{
|
||||
Chat chat;
|
||||
chat.id = (*res)["id"].as<int>();
|
||||
chat.userId = (*res)["user_id"].as<int>();
|
||||
|
||||
return chat;
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
return {};
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when querying unbound addresses for userId {}: {}", userId, ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
Chat DbChatStorage::getOrCreateChatForUserId(int userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
auto res = tx.exec(
|
||||
R"(
|
||||
SELECT id, user_id FROM chats
|
||||
WHERE user_id = $1
|
||||
LIMIT 1
|
||||
)", pqxx::params{ userId }).opt_row();
|
||||
|
||||
if (res)
|
||||
{
|
||||
Chat chat;
|
||||
chat.id = (*res)["id"].as<int>();
|
||||
chat.userId = (*res)["user_id"].as<int>();
|
||||
|
||||
return chat;
|
||||
}
|
||||
|
||||
auto insertRes = tx.exec(
|
||||
R"(
|
||||
INSERT INTO chats (user_id)
|
||||
VALUES ($1)
|
||||
RETURNING id, user_id
|
||||
)", pqxx::params{ userId }).one_row();
|
||||
|
||||
Chat chat;
|
||||
chat.id = insertRes["id"].as<int>();
|
||||
chat.userId = insertRes["user_id"].as<int>();
|
||||
|
||||
tx.commit();
|
||||
return chat;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when querying unbound addresses for userId {}: {}", userId, ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Message> DbChatStorage::loadMessages(int chatId, int page, int pageSize)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::vector<Message> messages;
|
||||
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
int pageOffset = (page - 1) * pageSize;
|
||||
auto res = tx.exec(
|
||||
R"(
|
||||
SELECT messages.id, chat_id, sender_id, type, content_text, thumbnail, attachment_id, messages.created_at, users.first_name, users.last_name, users.role
|
||||
FROM messages
|
||||
JOIN users ON users.id = sender_id
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at DESC
|
||||
OFFSET $2 LIMIT $3
|
||||
)", pqxx::params{ chatId, pageOffset, pageSize });
|
||||
|
||||
for (const auto row : res)
|
||||
{
|
||||
Message message;
|
||||
message.id = row["id"].as<int>();
|
||||
message.chatId = row["chat_id"].as<int>();
|
||||
message.senderId = row["sender_id"].as<int>();
|
||||
message.type = row["type"].as<std::string>();
|
||||
message.contentText = row["content_text"].is_null() ? "" : row["content_text"].as<std::string>();
|
||||
message.thumbnail = row["thumbnail"].is_null() ? "" : row["thumbnail"].as<std::string>();
|
||||
message.attachmentId = row["attachment_id"].is_null() ? -1 : row["attachment_id"].as<int>();
|
||||
|
||||
User user;
|
||||
user.firstName = row["first_name"].as<std::string>();
|
||||
user.lastName = row["last_name"].as<std::string>();
|
||||
user.role = row["role"].as<std::string>();
|
||||
message.sender = user;
|
||||
|
||||
messages.push_back(std::move(message));
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
return messages;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when loading message for chat {}: {}", chatId, ex.what());
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<Attachment> DbChatStorage::insertAttachment(Attachment attachment)
|
||||
{
|
||||
try
|
||||
{
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
auto insertRes = tx.exec(
|
||||
R"(
|
||||
INSERT INTO attachments (type, file_path, size_bytes)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id
|
||||
)", pqxx::params{ attachment.type, attachment.filePath, attachment.sizeBytes }).one_row();
|
||||
|
||||
attachment.id = insertRes["id"].as<int>();
|
||||
|
||||
tx.commit();
|
||||
return attachment;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when inserting attachment of type {}: {}", attachment.type, ex.what());
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<Message> DbChatStorage::insertMessage(Message message)
|
||||
{
|
||||
try
|
||||
{
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
int insertedId = -1;
|
||||
|
||||
if (message.type == "TEXT")
|
||||
{
|
||||
auto insertRes = tx.exec(
|
||||
R"(
|
||||
INSERT INTO messages (chat_id, sender_id, type, content_text)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id
|
||||
)", pqxx::params{ message.chatId, message.senderId, message.type, message.contentText }).one_row();
|
||||
|
||||
insertedId = insertRes["id"].as<int>();
|
||||
}
|
||||
|
||||
else // 'IMAGE' or 'VIDEO'
|
||||
{
|
||||
auto insertRes = tx.exec(
|
||||
R"(
|
||||
INSERT INTO messages (chat_id, sender_id, type, thumbnail, attachment_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id
|
||||
)", pqxx::params{ message.chatId, message.senderId, message.type, message.thumbnail, message.attachmentId }).one_row();
|
||||
|
||||
insertedId = insertRes["id"].as<int>();
|
||||
}
|
||||
|
||||
// repopulate user fields
|
||||
auto row = tx.exec(
|
||||
R"(
|
||||
SELECT messages.id, chat_id, sender_id, type, content_text, thumbnail, attachment_id, messages.created_at, users.first_name, users.last_name, users.role
|
||||
FROM messages
|
||||
JOIN users ON users.id = sender_id
|
||||
WHERE messages.id = $1
|
||||
)", pqxx::params{ insertedId }).one_row();
|
||||
|
||||
Message message;
|
||||
message.id = row["id"].as<int>();
|
||||
message.chatId = row["chat_id"].as<int>();
|
||||
message.senderId = row["sender_id"].as<int>();
|
||||
message.type = row["type"].as<std::string>();
|
||||
message.contentText = row["content_text"].is_null() ? "" : row["content_text"].as<std::string>();
|
||||
message.thumbnail = row["thumbnail"].is_null() ? "" : row["thumbnail"].as<std::string>();
|
||||
message.attachmentId = row["attachment_id"].is_null() ? -1 : row["attachment_id"].as<int>();
|
||||
|
||||
User user;
|
||||
user.firstName = row["first_name"].as<std::string>();
|
||||
user.lastName = row["last_name"].as<std::string>();
|
||||
user.role = row["role"].as<std::string>();
|
||||
message.sender = user;
|
||||
|
||||
tx.commit();
|
||||
return message;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when inserting message in chat {}: {}", message.chatId, ex.what());
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<Attachment> DbChatStorage::loadAttachment(int attachmentId)
|
||||
{
|
||||
try
|
||||
{
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
auto res = tx.exec(
|
||||
R"(
|
||||
SELECT id, type, file_path, size_bytes FROM attachments
|
||||
WHERE id = $1
|
||||
LIMIT 1
|
||||
)", pqxx::params{ attachmentId }).opt_row();
|
||||
|
||||
if (res)
|
||||
{
|
||||
Attachment attachment;
|
||||
attachment.id = (*res)["id"].as<int>();
|
||||
attachment.type = (*res)["type"].as<std::string>();
|
||||
attachment.filePath = (*res)["file_path"].as<std::string>();
|
||||
attachment.sizeBytes = (*res)["size_bytes"].as<int>();
|
||||
|
||||
return attachment;
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
return {};
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when querying attachment {}: {}", attachmentId, ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Chat> DbChatStorage::loadChats()
|
||||
{
|
||||
try
|
||||
{
|
||||
std::vector<Chat> chats;
|
||||
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
auto res = tx.exec(
|
||||
R"(
|
||||
SELECT
|
||||
c.id AS chat_id,
|
||||
u.first_name,
|
||||
u.last_name,
|
||||
m.id AS message_id,
|
||||
m.sender_id,
|
||||
m.type,
|
||||
m.content_text,
|
||||
m.thumbnail,
|
||||
m.attachment_id,
|
||||
m.created_at AS message_created_at
|
||||
FROM chats c
|
||||
JOIN users u ON c.user_id = u.id
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT *
|
||||
FROM messages
|
||||
WHERE messages.chat_id = c.id
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1
|
||||
) m ON true
|
||||
ORDER BY c.created_at DESC;
|
||||
)");
|
||||
|
||||
for (const auto row : res)
|
||||
{
|
||||
Chat chat;
|
||||
User user;
|
||||
Message message;
|
||||
|
||||
chat.id = row["chat_id"].as<int>();
|
||||
user.firstName = row["first_name"].as<std::string>();
|
||||
user.lastName = row["last_name"].as<std::string>();
|
||||
message.id = row["message_id"].is_null() ? -1 : row["message_id"].as<int>();
|
||||
message.senderId = row["sender_id"].is_null() ? -1 : row["sender_id"].as<int>();
|
||||
message.type = row["type"].is_null() ? "" : row["type"].as<std::string>();
|
||||
message.contentText = row["content_text"].is_null() ? "" : row["content_text"].as<std::string>();
|
||||
message.thumbnail = row["thumbnail"].is_null() ? "" : row["thumbnail"].as<std::string>();
|
||||
message.attachmentId = row["attachment_id"].is_null() ? -1 : row["attachment_id"].as<int>();
|
||||
|
||||
chat.user = user;
|
||||
chat.lastMessage = message;
|
||||
|
||||
chats.push_back(std::move(chat));
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
return chats;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when loading chat list: {}", ex.what());
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
30
src/database_implementation/ChatStorage.h
Normal file
30
src/database_implementation/ChatStorage.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "storages/IChatStorage.h"
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
class logger;
|
||||
}
|
||||
|
||||
class DbChatStorage : public IChatStorage
|
||||
{
|
||||
public:
|
||||
DbChatStorage();
|
||||
|
||||
virtual std::optional<Chat> getChatForUserId(int userId) override;
|
||||
virtual Chat getOrCreateChatForUserId(int userId) override;
|
||||
//virtual std::vector<Chat> getChatListWithLastMessage(int userId) override;
|
||||
virtual std::vector<Message> loadMessages(int chatId, int page, int pageSize) override;
|
||||
virtual std::optional<Attachment> insertAttachment(Attachment attachment) override;
|
||||
virtual std::optional<Message> insertMessage(Message message) override;
|
||||
//virtual bool isBoundToChat(int chatId, int userId) override;
|
||||
//virtual bool bindToChat(int chatId, int userId) override;
|
||||
//virtual bool bindToChat(int chatId, int userId) override;
|
||||
//virtual bool markChatAsRead(int chatId, int userId) override;
|
||||
virtual std::optional<Attachment> loadAttachment(int attachmentId) override;
|
||||
virtual std::vector<Chat> loadChats() override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> m_logger;
|
||||
};
|
||||
@@ -1,11 +1,37 @@
|
||||
#include "UserStorage.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <pqxx/pqxx>
|
||||
#include <spdlog/logger.h>
|
||||
#include "dependency_injection/DependencyContainer.h"
|
||||
#include "services/enums/AuthErrorCode.h"
|
||||
|
||||
static const char* DB_CONN_PARAMS = "host=localhost port=5432 dbname=mydb user=doggo password=confirm";
|
||||
static const char* DB_CONN_PARAMS = "host=localhost port=2345 dbname=mydb user=doggo password=confirm";
|
||||
|
||||
static void splitString(const std::string& input, std::vector<std::string>& outComponents, int componentsNum)
|
||||
{
|
||||
std::stringstream ss(input);
|
||||
std::string item;
|
||||
outComponents.clear();
|
||||
|
||||
while (std::getline(ss, item, ' ')) {
|
||||
size_t start = item.find_first_not_of(" ");
|
||||
size_t end = item.find_last_not_of(" ");
|
||||
if (start != std::string::npos && end != std::string::npos)
|
||||
{
|
||||
outComponents.push_back(item.substr(start, end - start + 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
outComponents.push_back("");
|
||||
}
|
||||
}
|
||||
|
||||
while (outComponents.size() < componentsNum) {
|
||||
outComponents.push_back("");
|
||||
}
|
||||
}
|
||||
|
||||
DbUserStorage::DbUserStorage()
|
||||
{
|
||||
@@ -86,6 +112,57 @@ std::optional<User> DbUserStorage::getUserById(int userId)
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<User> DbUserStorage::getUserWithAddresses(int userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
auto res = tx.exec(
|
||||
R"(
|
||||
SELECT users.id AS user_id, users.first_name, users.last_name, users.role,
|
||||
addresses.id AS address_id, addresses.city, addresses.street, addresses.house
|
||||
FROM users
|
||||
JOIN user_address ON user_address.user_id = users.id
|
||||
JOIN addresses ON addresses.id = user_address.address_id
|
||||
WHERE users.id = $1
|
||||
ORDER BY addresses.id;
|
||||
)", pqxx::params{ userId });
|
||||
|
||||
bool userCreated = false;
|
||||
User user;
|
||||
|
||||
for (const auto row : res)
|
||||
{
|
||||
if (!userCreated)
|
||||
{
|
||||
user.id = row["user_id"].as<int>();
|
||||
user.firstName = row["first_name"].as<std::string>();
|
||||
user.lastName = row["last_name"].as<std::string>();
|
||||
user.role = row["role"].as<std::string>();
|
||||
|
||||
userCreated = true;
|
||||
}
|
||||
|
||||
Address address;
|
||||
address.id = row["address_id"].as<int>();
|
||||
address.city = row["city"].as<std::string>();
|
||||
address.street = row["street"].as<std::string>();
|
||||
address.house = row["house"].as<std::string>();
|
||||
user.addresses.push_back(std::move(address));
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
return user;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when querying user by username: {}", ex.what());
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<User> DbUserStorage::getUsersWithRole(const std::string& role)
|
||||
{
|
||||
std::vector<User> users;
|
||||
@@ -164,6 +241,16 @@ std::vector<User> DbUserStorage::getUsersWithRole(const std::string& role, int p
|
||||
|
||||
std::vector<User> DbUserStorage::getUsersWithRole(const std::string& role, int page, int pageSize, std::string search)
|
||||
{
|
||||
// get "<first_name> <last_name>" or "<last_name> <first_name>" from search string
|
||||
std::vector<std::string> nameComponents;
|
||||
splitString(search, nameComponents, 2);
|
||||
|
||||
// wrap wildcards with $
|
||||
for (auto& comp : nameComponents)
|
||||
{
|
||||
comp = '%' + comp + '%';
|
||||
}
|
||||
|
||||
std::vector<User> users;
|
||||
try
|
||||
{
|
||||
@@ -173,24 +260,73 @@ std::vector<User> DbUserStorage::getUsersWithRole(const std::string& role, int p
|
||||
int pageOffset = (page - 1) * pageSize;
|
||||
auto res = tx.exec(
|
||||
R"(
|
||||
SELECT id, username, password, first_name, last_name, role
|
||||
SELECT u.id AS user_id, u.username, u.first_name, u.last_name, u.role,
|
||||
a.id AS address_id, a.city, a.street, a.house
|
||||
FROM (
|
||||
SELECT id, username, first_name, last_name, role
|
||||
FROM users
|
||||
WHERE role = $1
|
||||
LIMIT $2 OFFSET $3
|
||||
WHERE last_name ILIKE %$4%
|
||||
)", pqxx::params{ role, pageSize, pageOffset, search });
|
||||
AND (
|
||||
(first_name ILIKE $2 AND last_name ILIKE $3)
|
||||
OR
|
||||
(last_name ILIKE $4 AND first_name ILIKE $5)
|
||||
)
|
||||
ORDER BY id
|
||||
OFFSET $6 LIMIT $7
|
||||
) u
|
||||
LEFT JOIN user_address ua ON ua.user_id = u.id
|
||||
LEFT JOIN addresses a ON a.id = ua.address_id
|
||||
)", pqxx::params{ role, nameComponents[0], nameComponents[1], nameComponents[0], nameComponents[1], pageOffset, pageSize });
|
||||
|
||||
for (pqxx::row_size_type i = 0; i < res.columns(); ++i)
|
||||
{
|
||||
m_logger->info("Column {}: {}", i, res.column_name(i));
|
||||
}
|
||||
|
||||
std::unordered_map<int, User> userMap;
|
||||
std::unordered_map<int, Address> addressMap;
|
||||
|
||||
for (const auto row : res)
|
||||
{
|
||||
int userId = row["user_id"].as<int>();
|
||||
|
||||
if (userMap.find(userId) == userMap.end())
|
||||
{
|
||||
User user;
|
||||
user.id = row["id"].as<int>();
|
||||
user.id = userId;
|
||||
user.username = row["username"].as<std::string>();
|
||||
user.passwordHash = row["password"].as<std::string>();
|
||||
user.firstName = row["first_name"].as<std::string>();
|
||||
user.lastName = row["last_name"].as<std::string>();
|
||||
user.role = row["role"].as<std::string>();
|
||||
|
||||
users.push_back(std::move(user));
|
||||
userMap[userId] = user;
|
||||
}
|
||||
|
||||
if (!row["address_id"].is_null())
|
||||
{
|
||||
int addressId = row["address_id"].as<int>();
|
||||
|
||||
if (addressMap.find(addressId) == addressMap.end())
|
||||
{
|
||||
Address address;
|
||||
address.id = addressId;
|
||||
address.city = row["city"].as<std::string>();
|
||||
address.street = row["street"].as<std::string>();
|
||||
address.house = row["house"].as<std::string>();
|
||||
|
||||
addressMap[addressId] = address;
|
||||
userMap[userId].addresses.push_back(address);
|
||||
}
|
||||
else
|
||||
{
|
||||
userMap[userId].addresses.push_back(addressMap[addressId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& pair : userMap)
|
||||
{
|
||||
users.push_back(pair.second);
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
@@ -228,3 +364,56 @@ AuthErrorCode DbUserStorage::insertUser(const User& user)
|
||||
return AuthErrorCode::DbError;
|
||||
}
|
||||
}
|
||||
|
||||
bool DbUserStorage::bindUserToAddress(int userId, int addressId)
|
||||
{
|
||||
try
|
||||
{
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
tx.exec(
|
||||
R"(
|
||||
INSERT INTO user_address (user_id, address_id) VALUES
|
||||
($1, $2);
|
||||
)", { userId, addressId }).no_rows();
|
||||
|
||||
tx.commit();
|
||||
return true;
|
||||
}
|
||||
catch (const pqxx::unique_violation& ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when inserting new user: {}", ex.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool DbUserStorage::unbindUserFromAddress(int userId, int addressId)
|
||||
{
|
||||
try
|
||||
{
|
||||
pqxx::connection c{ DB_CONN_PARAMS };
|
||||
pqxx::work tx{ c };
|
||||
|
||||
tx.exec(
|
||||
R"(
|
||||
DELETE FROM user_address WHERE user_id = $1 AND address_id = $2;
|
||||
)", { userId, addressId }).no_rows();
|
||||
|
||||
tx.commit();
|
||||
return true;
|
||||
}
|
||||
catch (const pqxx::unique_violation& ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
m_logger->warn("Something went wrong when inserting new user: {}", ex.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,11 +14,15 @@ public:
|
||||
|
||||
virtual std::optional<User> getUserByUsername(const std::string& username) override;
|
||||
virtual std::optional<User> getUserById(int userId) override;
|
||||
virtual std::optional<User> getUserWithAddresses(int userId) override;
|
||||
virtual std::vector<User> getUsersWithRole(const std::string& role) override;
|
||||
virtual std::vector<User> getUsersWithRole(const std::string& role, int page, int pageSize) override;
|
||||
virtual std::vector<User> getUsersWithRole(const std::string& role, int page, int pageSize, std::string search) override;
|
||||
virtual AuthErrorCode insertUser(const User& user) override;
|
||||
|
||||
virtual bool bindUserToAddress(int userId, int addressId) override;
|
||||
virtual bool unbindUserFromAddress(int userId, int addressId) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> m_logger;
|
||||
};
|
||||
|
||||
26
src/dto/AddressDto.h
Normal file
26
src/dto/AddressDto.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "models/Address.h"
|
||||
|
||||
class AddressDto
|
||||
{
|
||||
public:
|
||||
int id;
|
||||
std::string city;
|
||||
std::string street;
|
||||
std::string house;
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(AddressDto, id, city, street, house);
|
||||
|
||||
static AddressDto toDto(Address&& model)
|
||||
{
|
||||
AddressDto ret;
|
||||
ret.id = model.id;
|
||||
ret.city = std::move(model.city);
|
||||
ret.street = std::move(model.street);
|
||||
ret.house = std::move(model.house);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
31
src/dto/ChatDto.h
Normal file
31
src/dto/ChatDto.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "models/Chat.h"
|
||||
|
||||
class ChatDto
|
||||
{
|
||||
public:
|
||||
int id;
|
||||
int userId;
|
||||
std::string userFirstName;
|
||||
std::string userLastName;
|
||||
std::string lastMessageTextContent;
|
||||
bool lastMessageHasAttachment;
|
||||
int unreadMessagesNum;
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(ChatDto,
|
||||
id, userId, userFirstName, userLastName, lastMessageTextContent, lastMessageHasAttachment, unreadMessagesNum);
|
||||
|
||||
static ChatDto toDto(Chat&& model)
|
||||
{
|
||||
ChatDto dto;
|
||||
dto.id = model.id;
|
||||
dto.userFirstName = model.user.has_value() ? model.user.value().firstName : "";
|
||||
dto.userLastName = model.user.has_value() ? model.user.value().lastName : "";
|
||||
dto.lastMessageTextContent = model.lastMessage.has_value() ? model.lastMessage.value().contentText : "";
|
||||
dto.lastMessageHasAttachment = model.lastMessage.has_value() ? model.lastMessage.value().attachmentId != -1 : false;
|
||||
return dto;
|
||||
}
|
||||
};
|
||||
37
src/dto/MessageDto.h
Normal file
37
src/dto/MessageDto.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "models/Message.h"
|
||||
|
||||
class MessageDto
|
||||
{
|
||||
public:
|
||||
int id;
|
||||
int senderId;
|
||||
std::string senderFirstName;
|
||||
std::string senderLastName;
|
||||
std::string senderRole;
|
||||
std::string type;
|
||||
std::string contentText;
|
||||
std::string thumbnail;
|
||||
int attachmentId;
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(MessageDto, id,
|
||||
senderId, senderFirstName, senderLastName, senderRole, type, contentText, thumbnail, attachmentId);
|
||||
|
||||
static MessageDto toDto(Message&& model)
|
||||
{
|
||||
MessageDto dto;
|
||||
dto.id = model.id;
|
||||
dto.senderId = model.senderId;
|
||||
dto.senderFirstName = model.sender.has_value() ? model.sender.value().firstName : "";
|
||||
dto.senderLastName = model.sender.has_value() ? model.sender.value().lastName : "";
|
||||
dto.senderRole = model.sender.has_value() ? model.sender.value().role : "";
|
||||
dto.type = model.type;
|
||||
dto.contentText = model.contentText;
|
||||
dto.thumbnail = model.thumbnail;
|
||||
dto.attachmentId = model.attachmentId;
|
||||
return dto;
|
||||
}
|
||||
};
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "dto/AddressDto.h"
|
||||
#include "models/User.h"
|
||||
|
||||
class UserDto
|
||||
@@ -12,17 +13,24 @@ public:
|
||||
std::string password;
|
||||
std::string firstName;
|
||||
std::string lastName;
|
||||
std::vector<AddressDto> addresses;
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(UserDto, id, username, password, firstName, lastName);
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(UserDto, id, username, password, firstName, lastName, addresses);
|
||||
|
||||
static UserDto toDto(User&& model)
|
||||
{
|
||||
UserDto ret;
|
||||
ret.id = model.id;
|
||||
ret.username = model.username;
|
||||
ret.password = model.passwordHash;
|
||||
ret.firstName = model.firstName;
|
||||
ret.lastName = model.lastName;
|
||||
ret.username = std::move(model.username);
|
||||
ret.password = std::move(model.passwordHash);
|
||||
ret.firstName = std::move(model.firstName);
|
||||
ret.lastName = std::move(model.lastName);
|
||||
|
||||
for (auto& address : model.addresses)
|
||||
{
|
||||
ret.addresses.push_back(AddressDto::toDto(std::move(address)));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,9 +7,12 @@
|
||||
#include "dependency_injection/DependencyContainer.h"
|
||||
|
||||
#include "services/AuthService.h"
|
||||
#include "services/ChatService.h"
|
||||
#include "services/UserManager.h"
|
||||
#include "services/UserService.h"
|
||||
#include "services/encryption/SHA256Hasher.h"
|
||||
#include "database_implementation/AddressStorage.h"
|
||||
#include "database_implementation/ChatStorage.h"
|
||||
#include "database_implementation/UserStorage.h"
|
||||
|
||||
class LoggerSingleton
|
||||
@@ -71,9 +74,12 @@ int main()
|
||||
|
||||
// Register dependencies
|
||||
DependencyContainer::Register<AuthService, AuthService>();
|
||||
DependencyContainer::Register<ChatService, ChatService>();
|
||||
DependencyContainer::Register<UserManager, UserManager>();
|
||||
DependencyContainer::Register<UserService, UserService>();
|
||||
DependencyContainer::Register<PasswordEncryptor, SHA256Hasher>();
|
||||
DependencyContainer::Register<IAddressStorage, DbAddressStorage>();
|
||||
DependencyContainer::Register<IChatStorage, DbChatStorage>();
|
||||
DependencyContainer::Register<IUserStorage, DbUserStorage>();
|
||||
|
||||
try
|
||||
|
||||
14
src/models/Address.h
Normal file
14
src/models/Address.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "Entity.h"
|
||||
|
||||
class Address : public Entity
|
||||
{
|
||||
public:
|
||||
std::string city;
|
||||
std::string street;
|
||||
std::string house;
|
||||
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(Address, Entity,
|
||||
city, street, house);
|
||||
};
|
||||
14
src/models/Attachment.h
Normal file
14
src/models/Attachment.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "Entity.h"
|
||||
|
||||
class Attachment : public Entity
|
||||
{
|
||||
public:
|
||||
std::string type;
|
||||
std::string filePath;
|
||||
int sizeBytes;
|
||||
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(Attachment, Entity,
|
||||
type, filePath, sizeBytes);
|
||||
};
|
||||
18
src/models/Chat.h
Normal file
18
src/models/Chat.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "Entity.h"
|
||||
#include "User.h"
|
||||
#include "Message.h"
|
||||
|
||||
class Chat : public Entity
|
||||
{
|
||||
public:
|
||||
int userId;
|
||||
std::optional<User> user;
|
||||
int lastMessageId;
|
||||
std::optional<Message> lastMessage;
|
||||
std::optional<int> unreadMessagesNum;
|
||||
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(Chat, Entity,
|
||||
userId, user, lastMessageId, lastMessage, unreadMessagesNum);
|
||||
};
|
||||
19
src/models/Message.h
Normal file
19
src/models/Message.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "Entity.h"
|
||||
#include "User.h"
|
||||
|
||||
class Message : public Entity
|
||||
{
|
||||
public:
|
||||
int chatId;
|
||||
int senderId;
|
||||
std::optional<User> sender;
|
||||
std::string type;
|
||||
std::string contentText;
|
||||
std::string thumbnail;
|
||||
int attachmentId;
|
||||
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(Message, Entity,
|
||||
chatId, senderId, sender, type, contentText, thumbnail, attachmentId);
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Entity.h"
|
||||
#include "Address.h"
|
||||
|
||||
class User : public Entity
|
||||
{
|
||||
@@ -10,7 +11,8 @@ public:
|
||||
std::string firstName;
|
||||
std::string lastName;
|
||||
std::string role;
|
||||
std::vector<Address> addresses;
|
||||
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(User, Entity,
|
||||
username, passwordHash, firstName, lastName, role);
|
||||
username, passwordHash, firstName, lastName, role, addresses);
|
||||
};
|
||||
|
||||
2329
src/mp4_placeholder.h
Normal file
2329
src/mp4_placeholder.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -8,6 +8,9 @@
|
||||
#include "services/enums/AuthErrorCode.h"
|
||||
#include "storages/IUserStorage.h"
|
||||
|
||||
#include <drogon/drogon.h>
|
||||
#include "controllers/AuthController.h"
|
||||
|
||||
AuthService::AuthService()
|
||||
{
|
||||
m_logger = DependencyContainer::Resolve<spdlog::logger>();
|
||||
|
||||
287
src/services/ChatService.cpp
Normal file
287
src/services/ChatService.cpp
Normal file
@@ -0,0 +1,287 @@
|
||||
#include "ChatService.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <optional>
|
||||
#include <drogon/DrClassMap.h>
|
||||
#include <drogon/utils/Utilities.h>
|
||||
#include "UserManager.h"
|
||||
#include "controllers/ChatWebSocketController.h"
|
||||
#include "dependency_injection/DependencyContainer.h"
|
||||
#include "dto/ChatDto.h"
|
||||
#include "dto/MessageDto.h"
|
||||
#include "storages/IChatStorage.h"
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_RESIZE_IMPLEMENTATION
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
#include "stb_image_resize2.h"
|
||||
#include "stb_image_write.h"
|
||||
|
||||
#include "base64.hpp"
|
||||
|
||||
#include "mp4_placeholder.h"
|
||||
|
||||
static bool validateFile(std::string fileExtension, const char* fileData, size_t fileLength)
|
||||
{
|
||||
if (fileExtension == "mp4")
|
||||
{
|
||||
if (fileLength > 536'870'912)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else if (fileExtension == "png")
|
||||
{
|
||||
if (fileLength > 52'428'800)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string generateThumbnailBase64(std::string fileExtension, const char* fileData, size_t fileLength)
|
||||
{
|
||||
if (fileExtension == "mp4")
|
||||
{
|
||||
std::string rawPlaceholder{ (char*)mp4_placeholder, sizeof(mp4_placeholder)};
|
||||
return base64::to_base64(rawPlaceholder);
|
||||
}
|
||||
else if (fileExtension == "png")
|
||||
{
|
||||
const int max_dimension = 180;
|
||||
|
||||
int width, height, channels;
|
||||
unsigned char* image_data = stbi_load_from_memory((unsigned char*)fileData, fileLength, &width, &height, &channels, 0);
|
||||
if (!image_data)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
int new_width, new_height;
|
||||
if (width > height)
|
||||
{
|
||||
new_width = max_dimension;
|
||||
new_height = (int)((float)height * ((float)max_dimension / (float)width));
|
||||
}
|
||||
else
|
||||
{
|
||||
new_height = max_dimension;
|
||||
new_width = (int)((float)width * ((float)max_dimension / (float)height));
|
||||
}
|
||||
|
||||
unsigned char* resized_data = new unsigned char[new_width * new_height * channels];
|
||||
if (!resized_data)
|
||||
{
|
||||
stbi_image_free(image_data);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!stbir_resize_uint8_linear(image_data, width, height, 0, resized_data, new_width, new_height, 0, STBIR_4CHANNEL))
|
||||
{
|
||||
stbi_image_free(image_data);
|
||||
delete[] resized_data;
|
||||
return {};
|
||||
}
|
||||
|
||||
int png_len;
|
||||
unsigned char* png_data = stbi_write_png_to_mem(resized_data, new_width * channels,
|
||||
new_width, new_height, channels, &png_len);
|
||||
|
||||
if (png_len <= 0)
|
||||
{
|
||||
stbi_image_free(image_data);
|
||||
delete[] resized_data;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto encoded = base64::to_base64(std::string_view((char*)png_data, png_len));
|
||||
|
||||
stbi_image_free(image_data);
|
||||
delete[] resized_data;
|
||||
STBI_FREE(png_data);
|
||||
|
||||
return encoded;
|
||||
}
|
||||
}
|
||||
|
||||
std::string getMessageType(const std::string& fileExtension)
|
||||
{
|
||||
if (fileExtension == "mp4")
|
||||
return "VIDEO";
|
||||
else
|
||||
return "IMAGE";
|
||||
}
|
||||
|
||||
ChatService::ChatService()
|
||||
{
|
||||
m_logger = DependencyContainer::Resolve<spdlog::logger>();
|
||||
m_chatStorage = DependencyContainer::Resolve<IChatStorage>();
|
||||
}
|
||||
|
||||
int ChatService::getChatId(int userId)
|
||||
{
|
||||
Chat chat = m_chatStorage->getOrCreateChatForUserId(userId);
|
||||
return chat.id;
|
||||
}
|
||||
|
||||
std::vector<MessageDto> ChatService::loadMessagesByUserId(int userId, int page, int pageSize)
|
||||
{
|
||||
std::optional<Chat> chat = m_chatStorage->getChatForUserId(userId);
|
||||
if (!chat)
|
||||
{
|
||||
return {}; // no chat was created yet
|
||||
}
|
||||
|
||||
std::vector<MessageDto> dtos;
|
||||
auto models = m_chatStorage->loadMessages(chat.value().id, page, pageSize);
|
||||
for (auto& model : models)
|
||||
{
|
||||
dtos.emplace_back(MessageDto::toDto(std::move(model)));
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
std::optional<MessageDto> ChatService::postTextMessageByChatId(int senderId, int chatId, std::string contentText)
|
||||
{
|
||||
Message messageToInsert;
|
||||
messageToInsert.chatId = chatId;
|
||||
messageToInsert.senderId = senderId;
|
||||
messageToInsert.type = "TEXT";
|
||||
messageToInsert.contentText = std::move(contentText);
|
||||
std::optional<Message> insertedMessage = m_chatStorage->insertMessage(messageToInsert);
|
||||
if (insertedMessage)
|
||||
{
|
||||
auto messageDto = MessageDto::toDto(std::move(insertedMessage.value()));
|
||||
|
||||
auto chatWsController = drogon::DrClassMap::getSingleInstance<ChatWebSocket>();
|
||||
nlohmann::json j = messageDto;
|
||||
chatWsController->newMessage(chatId, j.dump());
|
||||
|
||||
return messageDto;
|
||||
}
|
||||
|
||||
return std::optional<MessageDto>();
|
||||
}
|
||||
|
||||
std::optional<MessageDto> ChatService::postFileMessageByChatId(int senderId, int chatId, std::string fileExtension, const char* fileData, size_t fileLength)
|
||||
{
|
||||
// process file
|
||||
if (!validateFile(fileExtension, fileData, fileLength))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
auto thumbnail = generateThumbnailBase64(fileExtension, fileData, fileLength);
|
||||
auto uuidName = drogon::utils::getUuid();
|
||||
uuidName.append(".");
|
||||
uuidName.append(fileExtension);
|
||||
|
||||
std::filesystem::path dirPath = std::filesystem::current_path() / "uploads" / std::to_string(chatId);
|
||||
if (!std::filesystem::exists(dirPath))
|
||||
{
|
||||
std::filesystem::create_directories(dirPath);
|
||||
}
|
||||
|
||||
std::filesystem::path filePath = dirPath / uuidName;
|
||||
std::ofstream outFile(filePath, std::ios::binary);
|
||||
if (!outFile)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
outFile.write(fileData, fileLength);
|
||||
|
||||
Attachment attachmentToInsert;
|
||||
attachmentToInsert.type = getMessageType(fileExtension);
|
||||
attachmentToInsert.filePath = filePath.string();
|
||||
attachmentToInsert.sizeBytes = fileLength;
|
||||
|
||||
std::optional<Attachment> insertedAttachment = m_chatStorage->insertAttachment(attachmentToInsert);
|
||||
|
||||
if (!insertedAttachment)
|
||||
{
|
||||
m_logger->error("Failed to insert attachment of type {} and size {}", attachmentToInsert.type, attachmentToInsert.sizeBytes);
|
||||
return {};
|
||||
}
|
||||
|
||||
Message messageToInsert;
|
||||
messageToInsert.chatId = chatId;
|
||||
messageToInsert.senderId = senderId;
|
||||
messageToInsert.type = getMessageType(fileExtension);
|
||||
messageToInsert.thumbnail = thumbnail;
|
||||
messageToInsert.attachmentId = insertedAttachment.value().id;
|
||||
messageToInsert.type = getMessageType(fileExtension);
|
||||
|
||||
std::optional<Message> insertedMessage = m_chatStorage->insertMessage(messageToInsert);
|
||||
|
||||
if (!insertedMessage)
|
||||
{
|
||||
m_logger->error("Failed to insert message with attachment of type {}", insertedMessage.value().type);
|
||||
}
|
||||
|
||||
if (insertedMessage)
|
||||
{
|
||||
auto messageDto = MessageDto::toDto(std::move(insertedMessage.value()));
|
||||
|
||||
auto chatWsController = drogon::DrClassMap::getSingleInstance<ChatWebSocket>();
|
||||
nlohmann::json j = messageDto;
|
||||
chatWsController->newMessage(chatId, j.dump());
|
||||
|
||||
return messageDto;
|
||||
}
|
||||
|
||||
return std::optional<MessageDto>();
|
||||
}
|
||||
|
||||
std::vector<ChatDto> ChatService::loadChats()
|
||||
{
|
||||
std::vector<ChatDto> dtos;
|
||||
auto models = m_chatStorage->loadChats();
|
||||
for (auto& model : models)
|
||||
{
|
||||
dtos.emplace_back(ChatDto::toDto(std::move(model)));
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
std::vector<MessageDto> ChatService::loadMessagesByChatId(int chatId, int page, int pageSize)
|
||||
{
|
||||
std::vector<MessageDto> dtos;
|
||||
auto models = m_chatStorage->loadMessages(chatId, page, pageSize);
|
||||
for (auto& model : models)
|
||||
{
|
||||
dtos.emplace_back(MessageDto::toDto(std::move(model)));
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
std::string ChatService::loadAttachment(int attachmentId)
|
||||
{
|
||||
auto attachment = m_chatStorage->loadAttachment(attachmentId);
|
||||
if (!attachment)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string fullPath = attachment.value().filePath;
|
||||
|
||||
if (!std::filesystem::exists(fullPath) || std::filesystem::is_directory(fullPath))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
std::ifstream file(fullPath, std::ios::binary);
|
||||
if (!file)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
|
||||
return fileContent;
|
||||
}
|
||||
39
src/services/ChatService.h
Normal file
39
src/services/ChatService.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
|
||||
#include "models/Attachment.h"
|
||||
#include "dto/ChatDto.h"
|
||||
|
||||
class IChatStorage;
|
||||
class MessageDto;
|
||||
|
||||
namespace spdlog
|
||||
{
|
||||
class logger;
|
||||
}
|
||||
|
||||
class ChatService
|
||||
{
|
||||
public:
|
||||
ChatService();
|
||||
|
||||
// for resident
|
||||
int getChatId(int userId);
|
||||
std::vector<MessageDto> loadMessagesByUserId(int userId, int page, int pageSize);
|
||||
std::optional<MessageDto> postTextMessageByChatId(int senderId, int chatId, std::string contentText);
|
||||
std::optional<MessageDto> postFileMessageByChatId(int senderId, int chatId, std::string fileExtension, const char* fileData, size_t fileLength);
|
||||
|
||||
// for employee
|
||||
std::vector<ChatDto> loadChats();
|
||||
std::vector<MessageDto> loadMessagesByChatId(int chatId, int page, int pageSize);
|
||||
|
||||
std::string loadAttachment(int attachmentId);
|
||||
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> m_logger;
|
||||
std::shared_ptr<IChatStorage> m_chatStorage;
|
||||
};
|
||||
@@ -3,19 +3,20 @@
|
||||
#include <spdlog/logger.h>
|
||||
#include "dependency_injection/DependencyContainer.h"
|
||||
#include "dto/UserDto.h"
|
||||
#include "storages/IAddressStorage.h"
|
||||
#include "storages/IUserStorage.h"
|
||||
|
||||
UserService::UserService()
|
||||
{
|
||||
m_logger = DependencyContainer::Resolve<spdlog::logger>();
|
||||
m_addressStorage = DependencyContainer::Resolve<IAddressStorage>();
|
||||
m_userStorage = DependencyContainer::Resolve<IUserStorage>();
|
||||
}
|
||||
|
||||
UserDto UserService::getUserById(int userId)
|
||||
{
|
||||
auto userData = m_userStorage->getUserById(userId);
|
||||
//return UserDto::toDto(std::move(userData.fva));
|
||||
return {};
|
||||
return UserDto::toDto(std::move(userData.value()));
|
||||
}
|
||||
|
||||
std::vector<UserDto> UserService::getUsers(int page, int pageSize)
|
||||
@@ -29,6 +30,12 @@ std::vector<UserDto> UserService::getUsers(int page, int pageSize)
|
||||
return dtos;
|
||||
}
|
||||
|
||||
UserDto UserService::getResident(int userId)
|
||||
{
|
||||
auto model = m_userStorage->getUserWithAddresses(userId);
|
||||
return UserDto::toDto(std::move(model.value()));
|
||||
}
|
||||
|
||||
std::vector<UserDto> UserService::getResidents(int page, int pageSize, std::string search)
|
||||
{
|
||||
std::vector<UserDto> dtos;
|
||||
@@ -39,3 +46,24 @@ std::vector<UserDto> UserService::getResidents(int page, int pageSize, std::stri
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
std::vector<AddressDto> UserService::getUnboundAddresses(int userId, int page, int pageSize, std::string search)
|
||||
{
|
||||
std::vector<AddressDto> dtos;
|
||||
auto models = m_addressStorage->getUnboundAddressesForUserId(userId, page, pageSize, std::move(search));
|
||||
for (auto& model : models)
|
||||
{
|
||||
dtos.emplace_back(AddressDto::toDto(std::move(model)));
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
bool UserService::bindUserToAddress(int userId, int addressId)
|
||||
{
|
||||
return m_userStorage->bindUserToAddress(userId, addressId);
|
||||
}
|
||||
|
||||
bool UserService::unbindUserFromAddress(int userId, int addressId)
|
||||
{
|
||||
return m_userStorage->unbindUserFromAddress(userId, addressId);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class IAddressStorage;
|
||||
class IUserStorage;
|
||||
class AddressDto;
|
||||
class UserDto;
|
||||
|
||||
namespace spdlog
|
||||
@@ -19,9 +21,14 @@ public:
|
||||
|
||||
UserDto getUserById(int userId);
|
||||
std::vector<UserDto> getUsers(int page, int pageSize);
|
||||
UserDto getResident(int userId);
|
||||
std::vector<UserDto> getResidents(int page, int pageSize, std::string search);
|
||||
std::vector<AddressDto> getUnboundAddresses(int userId, int page, int pageSize, std::string search);
|
||||
bool bindUserToAddress(int userId, int addressId);
|
||||
bool unbindUserFromAddress(int userId, int addressId);
|
||||
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> m_logger;
|
||||
std::shared_ptr<IAddressStorage> m_addressStorage;
|
||||
std::shared_ptr<IUserStorage> m_userStorage;
|
||||
};
|
||||
|
||||
7988
src/stb_image.h
Normal file
7988
src/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
10627
src/stb_image_resize2.h
Normal file
10627
src/stb_image_resize2.h
Normal file
File diff suppressed because it is too large
Load Diff
1724
src/stb_image_write.h
Normal file
1724
src/stb_image_write.h
Normal file
File diff suppressed because it is too large
Load Diff
11
src/storages/IAddressStorage.h
Normal file
11
src/storages/IAddressStorage.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "models/Address.h"
|
||||
|
||||
class IAddressStorage
|
||||
{
|
||||
public:
|
||||
virtual std::vector<Address> getUnboundAddressesForUserId(int userId, int page, int pageSize, std::string search) = 0;
|
||||
};
|
||||
24
src/storages/IChatStorage.h
Normal file
24
src/storages/IChatStorage.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include "models/Chat.h"
|
||||
#include "models/Message.h"
|
||||
#include "models/Attachment.h"
|
||||
|
||||
class IChatStorage
|
||||
{
|
||||
public:
|
||||
virtual std::optional<Chat> getChatForUserId(int userId) = 0;
|
||||
virtual Chat getOrCreateChatForUserId(int userId) = 0;
|
||||
//virtual std::vector<Chat> getChatListWithLastMessage(int userId) = 0;
|
||||
virtual std::vector<Message> loadMessages(int chatId, int page, int pageSize) = 0;
|
||||
virtual std::optional<Attachment> insertAttachment(Attachment attachment) = 0;
|
||||
virtual std::optional<Message> insertMessage(Message message) = 0;
|
||||
//virtual bool isBoundToChat(int chatId, int userId) = 0;
|
||||
//virtual bool bindToChat(int chatId, int userId) = 0;
|
||||
//virtual bool markChatAsRead(int chatId, int userId) = 0;
|
||||
virtual std::optional<Attachment> loadAttachment(int attachmentId) = 0;
|
||||
virtual std::vector<Chat> loadChats() = 0;
|
||||
};
|
||||
@@ -11,8 +11,12 @@ class IUserStorage
|
||||
public:
|
||||
virtual std::optional<User> getUserByUsername(const std::string& username) = 0;
|
||||
virtual std::optional<User> getUserById(int userId) = 0;
|
||||
virtual std::optional<User> getUserWithAddresses(int userId) = 0;
|
||||
virtual std::vector<User> getUsersWithRole(const std::string& role) = 0;
|
||||
virtual std::vector<User> getUsersWithRole(const std::string& role, int page, int pageSize) = 0;
|
||||
virtual std::vector<User> getUsersWithRole(const std::string& role, int page, int pageSize, std::string search) = 0;
|
||||
virtual AuthErrorCode insertUser(const User& user) = 0;
|
||||
|
||||
virtual bool bindUserToAddress(int userId, int addressId) = 0;
|
||||
virtual bool unbindUserFromAddress(int userId, int addressId) = 0;
|
||||
};
|
||||
|
||||
@@ -5,11 +5,13 @@ Json::Value nlohmannToJsonValue(const nlohmann::json& j) {
|
||||
Json::Value result;
|
||||
|
||||
if (j.is_object()) {
|
||||
result = Json::Value(Json::objectValue);
|
||||
for (auto& el : j.items()) {
|
||||
result[el.key()] = nlohmannToJsonValue(el.value());
|
||||
}
|
||||
}
|
||||
else if (j.is_array()) {
|
||||
result = Json::Value(Json::arrayValue);
|
||||
for (size_t i = 0; i < j.size(); ++i) {
|
||||
result[Json::ArrayIndex(i)] = nlohmannToJsonValue(j[i]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user