From 88916c50b83bfad1ec1c5dd5f4e4f5d345851cb0 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 1 Nov 2018 07:16:38 +0100 Subject: Move code from dchat_gtk to generic place --- include/dchat/Room.hpp | 102 +++++++++++++++++++++++++++++++++++++++++ include/dchat/RoomDataType.hpp | 16 +++++++ include/dchat/User.hpp | 16 +++++++ 3 files changed, 134 insertions(+) create mode 100644 include/dchat/Room.hpp create mode 100644 include/dchat/RoomDataType.hpp create mode 100644 include/dchat/User.hpp (limited to 'include/dchat') diff --git a/include/dchat/Room.hpp b/include/dchat/Room.hpp new file mode 100644 index 0000000..4422a6f --- /dev/null +++ b/include/dchat/Room.hpp @@ -0,0 +1,102 @@ +#pragma once + +#include "utils.hpp" +#include "types.hpp" +#include "StringView.hpp" +#include "User.hpp" +#include +#include +#include +#include +#include + +namespace dchat +{ + struct RoomMessage + { + odhtdb::Hash id; + std::shared_ptr creator; + uint32_t timestampSeconds; + std::string text; + }; + + class Room + { + DISABLE_COPY(Room) + public: + Room(std::shared_ptr database, std::shared_ptr id, std::shared_ptr encryptionKey); + std::shared_ptr addUser(const odhtdb::Signature::PublicKey &userPublicKey, const odhtdb::DataView groupId); + // Returns null if user doesn't exist in room + std::shared_ptr getUserByPublicKey(const odhtdb::Signature::PublicKey &userPublicKey); + void publishMessage(const std::string &msg); + + std::shared_ptr database; + std::shared_ptr id; + std::shared_ptr encryptionKey; + std::string name; + odhtdb::Signature::MapPublicKey> userByPublicKey; + std::vector messages; + std::shared_ptr localUser; + // Used for local users + odhtdb::Signature::MapPublicKey> publicKeyToKeyPairMap; + void *userdata; + }; + + class Rooms; + + struct RoomAddMessageRequest + { + std::shared_ptr room; + bool loadedFromCache; + RoomMessage message; + }; + + struct UserChangeNicknameRequest + { + std::shared_ptr room; + std::shared_ptr user; + uint32_t timestampSeconds; + bool loadedFromCache; + std::string newNickname; + }; + + // if connection failed then @rooms is null and errMsg contains the error + using ConnectBoostrapNodeCallbackFunc = std::function rooms, const char *errMsg)>; + using CreateRoomCallbackFunc = std::function room)>; + using RoomAddUserCallbackFunc = std::function room, std::shared_ptr user)>; + using RoomAddMessageCallbackFunc = std::function; + using UserChangeNicknameCallbackFunc = std::function; + struct RoomCallbackFuncs + { + ConnectBoostrapNodeCallbackFunc connectCallbackFunc; + CreateRoomCallbackFunc createRoomCallbackFunc; + RoomAddUserCallbackFunc addUserCallbackFunc; + RoomAddMessageCallbackFunc addMessageCallbackFunc; + UserChangeNicknameCallbackFunc userChangeNicknameCallbackFunc; + }; + + class Rooms + { + DISABLE_COPY(Rooms) + public: + // @callbackFuncs.connectCallbackFunc can't be null + static void connect(const char *address, u16 port, RoomCallbackFuncs callbackFuncs); + // Throws on failure + void loginUser(const std::string &username, const std::string &password); + // Throws on failure + void registerUser(const std::string &username, const std::string &password); + + std::shared_ptr database; + private: + Rooms(const char *address, u16 port, RoomCallbackFuncs callbackFuncs); + void createNodeCallbackFunc(const odhtdb::DatabaseCreateNodeRequest &request); + void addNodeCallbackFunc(const odhtdb::DatabaseAddNodeRequest &request); + void addUserCallbackFunc(const odhtdb::DatabaseAddUserRequest &request); + private: + odhtdb::MapHash> roomById; + RoomCallbackFuncs callbackFuncs; + bool loggedIn; + odhtdb::MapHash> roomLocalUser; + odhtdb::MapHash> roomEncryptionKey; + }; +} \ No newline at end of file diff --git a/include/dchat/RoomDataType.hpp b/include/dchat/RoomDataType.hpp new file mode 100644 index 0000000..cef2a8a --- /dev/null +++ b/include/dchat/RoomDataType.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "types.hpp" + +namespace dchat +{ + enum class RoomDataType : u8 + { + ADD_MESSAGE, + EDIT_MESSAGE, + DELETE_MESSAGE, + NICKNAME_CHANGE, + CHANGE_AVATAR, + CHANGE_ROOM_NAME, + }; +} \ No newline at end of file diff --git a/include/dchat/User.hpp b/include/dchat/User.hpp new file mode 100644 index 0000000..4021c78 --- /dev/null +++ b/include/dchat/User.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +namespace dchat +{ + class User + { + public: + User(const odhtdb::Signature::PublicKey &_publicKey) : publicKey(_publicKey), userdata(nullptr) {} + const odhtdb::Signature::PublicKey publicKey; + std::string nickname; + void *userdata; + }; +} \ No newline at end of file -- cgit v1.2.3