From 1a1d3b7dc56e173d46d89fd9c05cd295bb8bcbf2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 9 Nov 2018 09:45:13 +0100 Subject: Room join/invite, fix build for mingw --- include/dchat/FileUtil.hpp | 2 +- include/dchat/Group.hpp | 15 +++++++++++++++ include/dchat/Room.hpp | 40 +++++++++++++++++++++++++++++++++++++--- include/dchat/User.hpp | 4 ++++ 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 include/dchat/Group.hpp (limited to 'include/dchat') diff --git a/include/dchat/FileUtil.hpp b/include/dchat/FileUtil.hpp index 097b607..6a7ff00 100644 --- a/include/dchat/FileUtil.hpp +++ b/include/dchat/FileUtil.hpp @@ -13,7 +13,7 @@ namespace dchat }; // Throws FileException on error. - // Returned value is allocated with malloc and should be free'd by caller. + // Returned value is allocated with `new[]` and should be `delete`[]d by caller. StringView getFileContent(const boost::filesystem::path &filepath); // Throws FileException on error diff --git a/include/dchat/Group.hpp b/include/dchat/Group.hpp new file mode 100644 index 0000000..8b47923 --- /dev/null +++ b/include/dchat/Group.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "types.hpp" +#include +#include + +namespace dchat +{ + class Group + { + public: + u8 id[odhtdb::GROUP_ID_LENGTH]; + std::string name; + }; +} \ No newline at end of file diff --git a/include/dchat/Room.hpp b/include/dchat/Room.hpp index 5dd0e3f..a39f2f0 100644 --- a/include/dchat/Room.hpp +++ b/include/dchat/Room.hpp @@ -28,9 +28,17 @@ namespace dchat DISABLE_COPY(Room) public: Room(Rooms *rooms, std::shared_ptr id); - std::shared_ptr addUser(const odhtdb::Signature::PublicKey &userPublicKey, const odhtdb::DataView groupId); + // Throws exception on failure if we are not allowed to add the user to the group + void addUser(const odhtdb::Signature::PublicKey &userPublicKey, std::shared_ptr group); + // Returns null if the user already exists in the room + std::shared_ptr addUserLocally(const odhtdb::Signature::PublicKey &userPublicKey, std::shared_ptr group); + // Returns null if the group already exists in the room + std::shared_ptr addGroupLocally(const odhtdb::DataView groupId); // Returns null if user doesn't exist in room std::shared_ptr getUserByPublicKey(const odhtdb::Signature::PublicKey &userPublicKey); + // Returns null if group doesn't exist in room + std::shared_ptr getGroupById(const odhtdb::DataView groupId); + void setLocalUser(std::shared_ptr user, std::shared_ptr keyPair); void publishMessage(const std::string &msg); Rooms *rooms; @@ -38,10 +46,12 @@ namespace dchat std::shared_ptr encryptionKey; std::string name; odhtdb::Signature::MapPublicKey> userByPublicKey; + std::vector> groups; std::vector messages; std::shared_ptr localUser; // Used for local users odhtdb::Signature::MapPublicKey> publicKeyToKeyPairMap; + std::string inviteKey; void *userdata; }; @@ -70,13 +80,31 @@ namespace dchat std::string newName; }; + struct InviteUserRequest + { + std::shared_ptr room; + odhtdb::Signature::PublicKey userPublicKey; + std::string message; + }; + + struct RoomAddUserRequest + { + std::shared_ptr room; + std::shared_ptr user; + std::shared_ptr addedByUser; + uint32_t timestampSeconds; + bool loadedFromCache; + bool isLocalUser; + }; + // 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 RoomAddUserCallbackFunc = std::function; using RoomAddMessageCallbackFunc = std::function; using UserChangeNicknameCallbackFunc = std::function; using ChangeRoomNameCallbackFunc = std::function; + using ReceiveInviteUserCallbackFunc = std::function; struct RoomCallbackFuncs { ConnectBoostrapNodeCallbackFunc connectCallbackFunc; @@ -85,10 +113,12 @@ namespace dchat RoomAddMessageCallbackFunc addMessageCallbackFunc; UserChangeNicknameCallbackFunc userChangeNicknameCallbackFunc; ChangeRoomNameCallbackFunc changeRoomNameCallbackFunc; + ReceiveInviteUserCallbackFunc receiveInviteUserCallbackFunc; }; class Rooms { + friend Room; DISABLE_COPY(Rooms) public: // @callbackFuncs.connectCallbackFunc can't be null @@ -98,7 +128,8 @@ namespace dchat // Throws on failure void registerUser(const std::string &username, const std::string &password); // Throws on failure - void createRoom(const std::string &name); + std::shared_ptr createRoom(const std::string &name); + void requestJoinRoom(const std::string &inviteKey, const std::string &message); std::shared_ptr database; private: @@ -116,5 +147,8 @@ namespace dchat std::string currentUsername; std::string currentUserPassword; std::recursive_mutex roomModifyMutex; + + odhtdb::MapHash> waitingToJoinRoom; + std::recursive_mutex waitingToJoinRoomMutex; }; } \ No newline at end of file diff --git a/include/dchat/User.hpp b/include/dchat/User.hpp index 4021c78..245197f 100644 --- a/include/dchat/User.hpp +++ b/include/dchat/User.hpp @@ -1,7 +1,10 @@ #pragma once +#include "Group.hpp" #include +#include #include +#include namespace dchat { @@ -11,6 +14,7 @@ namespace dchat User(const odhtdb::Signature::PublicKey &_publicKey) : publicKey(_publicKey), userdata(nullptr) {} const odhtdb::Signature::PublicKey publicKey; std::string nickname; + std::vector> groups; void *userdata; }; } \ No newline at end of file -- cgit v1.2.3