diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/dchat/Room.hpp | 102 | ||||
-rw-r--r-- | include/dchat/RoomDataType.hpp | 16 | ||||
-rw-r--r-- | include/dchat/User.hpp | 16 |
3 files changed, 134 insertions, 0 deletions
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 <string> +#include <memory> +#include <vector> +#include <functional> +#include <odhtdb/Database.hpp> + +namespace dchat +{ + struct RoomMessage + { + odhtdb::Hash id; + std::shared_ptr<User> creator; + uint32_t timestampSeconds; + std::string text; + }; + + class Room + { + DISABLE_COPY(Room) + public: + Room(std::shared_ptr<odhtdb::Database> database, std::shared_ptr<odhtdb::Hash> id, std::shared_ptr<odhtdb::OwnedByteArray> encryptionKey); + std::shared_ptr<User> addUser(const odhtdb::Signature::PublicKey &userPublicKey, const odhtdb::DataView groupId); + // Returns null if user doesn't exist in room + std::shared_ptr<User> getUserByPublicKey(const odhtdb::Signature::PublicKey &userPublicKey); + void publishMessage(const std::string &msg); + + std::shared_ptr<odhtdb::Database> database; + std::shared_ptr<odhtdb::Hash> id; + std::shared_ptr<odhtdb::OwnedByteArray> encryptionKey; + std::string name; + odhtdb::Signature::MapPublicKey<std::shared_ptr<User>> userByPublicKey; + std::vector<RoomMessage> messages; + std::shared_ptr<User> localUser; + // Used for local users + odhtdb::Signature::MapPublicKey<std::shared_ptr<odhtdb::Signature::KeyPair>> publicKeyToKeyPairMap; + void *userdata; + }; + + class Rooms; + + struct RoomAddMessageRequest + { + std::shared_ptr<Room> room; + bool loadedFromCache; + RoomMessage message; + }; + + struct UserChangeNicknameRequest + { + std::shared_ptr<Room> room; + std::shared_ptr<User> 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<void(std::shared_ptr<Rooms> rooms, const char *errMsg)>; + using CreateRoomCallbackFunc = std::function<void(std::shared_ptr<Room> room)>; + using RoomAddUserCallbackFunc = std::function<void(std::shared_ptr<Room> room, std::shared_ptr<User> user)>; + using RoomAddMessageCallbackFunc = std::function<void(const RoomAddMessageRequest &request)>; + using UserChangeNicknameCallbackFunc = std::function<void(const UserChangeNicknameRequest &request)>; + 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<odhtdb::Database> 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<std::shared_ptr<Room>> roomById; + RoomCallbackFuncs callbackFuncs; + bool loggedIn; + odhtdb::MapHash<std::shared_ptr<odhtdb::Signature::KeyPair>> roomLocalUser; + odhtdb::MapHash<std::shared_ptr<odhtdb::OwnedByteArray>> 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 <string> +#include <odhtdb/Signature.hpp> + +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 |