From 983be4253f2ea3827d78db97afcc666664ae75ed Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 7 Apr 2019 17:15:42 +0200 Subject: Update cache signature, update odhtdb --- depends/odhtdb | 2 +- include/dchat/Cache.hpp | 8 ++++++-- include/dchat/Gif.hpp | 2 +- include/dchat/Room.hpp | 9 ++++++++- include/dchat/WebPagePreview.hpp | 5 +---- src/Cache.cpp | 14 ++++++++------ src/Gif.cpp | 2 +- src/Room.cpp | 4 +++- 8 files changed, 29 insertions(+), 17 deletions(-) diff --git a/depends/odhtdb b/depends/odhtdb index e673a8d..004cb37 160000 --- a/depends/odhtdb +++ b/depends/odhtdb @@ -1 +1 @@ -Subproject commit e673a8de53278814a860e9eba03ef776be0a4169 +Subproject commit 004cb37fce4621fc8537146d866081c67045bc9b diff --git a/include/dchat/Cache.hpp b/include/dchat/Cache.hpp index 8d3f28e..71bd785 100644 --- a/include/dchat/Cache.hpp +++ b/include/dchat/Cache.hpp @@ -1,7 +1,6 @@ #pragma once #include "types.hpp" -#include "WebPagePreview.hpp" #include "StringView.hpp" #include "utils.hpp" #include @@ -63,6 +62,7 @@ namespace dchat // Returned gif should be allocated with @new using CreateGifFunc = std::function; using CreateStaticImageFunc = std::function; + using CreateWebPagePreviewFunc = std::function; class Cache { @@ -70,13 +70,16 @@ namespace dchat public: // @createGifFunc can't be null // @createStaticImageFunc can't be null - Cache(CreateGifFunc createGifFunc, CreateStaticImageFunc createStaticImageFunc); + // @createWebPagePreviewFunc can't be null + Cache(CreateGifFunc createGifFunc, CreateStaticImageFunc createStaticImageFunc, CreateWebPagePreviewFunc createWebPagePreviewFunc); ~Cache(); // Get cached content or download it. // Default download file limit is 12MB // Returns ContentByUrlResult describing texture status. const ContentByUrlResult getContentByUrl(const std::string &url, int downloadLimitBytes = 12582912); + private: + ContentByUrlResult loadImageFromFile(const boost::filesystem::path &filepath, bool loadFromCache); private: struct ImageDownloadInfo { @@ -93,5 +96,6 @@ namespace dchat std::unordered_map contentUrlCache; CreateGifFunc createGifFunc; CreateStaticImageFunc createStaticImageFunc; + CreateWebPagePreviewFunc createWebPagePreviewFunc; }; } diff --git a/include/dchat/Gif.hpp b/include/dchat/Gif.hpp index c870d1c..b7d2efb 100644 --- a/include/dchat/Gif.hpp +++ b/include/dchat/Gif.hpp @@ -35,7 +35,7 @@ namespace dchat static bool isDataGif(const StringView &data); protected: // Return false if texture creation failed - virtual bool createTexture(int width, int height) = 0; + virtual bool createTexture() = 0; // Size of texture data is same as the size that the texture was created with (also same size returned by @getSize function) virtual void updateTexture(void *textureData) = 0; private: diff --git a/include/dchat/Room.hpp b/include/dchat/Room.hpp index 6980bfc..56b60e5 100644 --- a/include/dchat/Room.hpp +++ b/include/dchat/Room.hpp @@ -39,11 +39,13 @@ namespace dchat 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 setAvatarUrl(const std::string &url); void setNickname(const std::string &nickname); void publishMessage(const std::string &msg); + const odhtdb::Signature::MapPublicKey>& getUsers() const { return userByPublicKey; } + const std::vector>& getMessages() const { return messages; } + Rooms *rooms; std::shared_ptr id; std::shared_ptr encryptionKey; @@ -54,6 +56,8 @@ namespace dchat // TODO: Move to private when we have proper support for groups std::vector> groups; + private: + void setLocalUser(std::shared_ptr user, std::shared_ptr keyPair); private: odhtdb::Signature::MapPublicKey> userByPublicKey; // Used for local users @@ -152,6 +156,9 @@ namespace dchat void requestJoinRoom(const std::string &inviteKey, const std::string &message); std::shared_ptr database; + + const odhtdb::MapHash>& getRooms() const { return roomById; } + bool isLoggedIn() const { return loggedIn; } private: Rooms(const char *address, u16 port, RoomCallbackFuncs callbackFuncs); void createNodeCallbackFunc(const odhtdb::DatabaseCreateNodeRequest &request); diff --git a/include/dchat/WebPagePreview.hpp b/include/dchat/WebPagePreview.hpp index df75419..8be497a 100644 --- a/include/dchat/WebPagePreview.hpp +++ b/include/dchat/WebPagePreview.hpp @@ -1,13 +1,10 @@ #pragma once -#include - namespace dchat { class WebPagePreview { public: - std::string title; - std::string description; + virtual ~WebPagePreview() {} }; } diff --git a/src/Cache.cpp b/src/Cache.cpp index 0d5dd05..82f31b9 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -67,7 +67,7 @@ namespace dchat return success; } - static ContentByUrlResult loadImageFromFile(const boost::filesystem::path &filepath, bool loadFromCache, CreateGifFunc createGifFunc, CreateStaticImageFunc createStaticImageFunc) + ContentByUrlResult Cache::loadImageFromFile(const boost::filesystem::path &filepath, bool loadFromCache) { StringView fileContent; try @@ -126,7 +126,7 @@ namespace dchat if(foundHtmlContent) { - WebPagePreview *webPagePreview = new WebPagePreview { webPageTitle, webPageDescription }; + WebPagePreview *webPagePreview = createWebPagePreviewFunc(webPageTitle, webPageDescription); return { webPagePreview, ContentByUrlResult::Type::CACHED }; } } @@ -137,13 +137,15 @@ namespace dchat return { (StaticImage*)nullptr, ContentByUrlResult::Type::FAILED_DOWNLOAD }; } - Cache::Cache(CreateGifFunc _createGifFunc, CreateStaticImageFunc _createStaticImageFunc) : + Cache::Cache(CreateGifFunc _createGifFunc, CreateStaticImageFunc _createStaticImageFunc, CreateWebPagePreviewFunc _createWebPagePreviewFunc) : alive(true), createGifFunc(_createGifFunc), - createStaticImageFunc(_createStaticImageFunc) + createStaticImageFunc(_createStaticImageFunc), + createWebPagePreviewFunc(_createWebPagePreviewFunc) { assert(createGifFunc); assert(createStaticImageFunc); + assert(createWebPagePreviewFunc); downloadWaitThread = thread([this] { while(alive) @@ -165,7 +167,7 @@ namespace dchat } else { - contentByUrlResult = loadImageFromFile(filepath, false, createGifFunc, createStaticImageFunc); + contentByUrlResult = loadImageFromFile(filepath, false); contentByUrlResult.lastAccessed = chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count(); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { @@ -328,7 +330,7 @@ namespace dchat } // TODO: Do not load content in this thread. Return LOADING status and load it in another thread, because with a lot of images, chat can freeze - ContentByUrlResult contentByUrlResult = loadImageFromFile(filepath, true, createGifFunc, createStaticImageFunc); + ContentByUrlResult contentByUrlResult = loadImageFromFile(filepath, true); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { contentByUrlResult.lastAccessed = chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count(); diff --git a/src/Gif.cpp b/src/Gif.cpp index e98718f..a35b6c2 100644 --- a/src/Gif.cpp +++ b/src/Gif.cpp @@ -139,7 +139,7 @@ namespace dchat if(!created) { created = true; - if(!createTexture(gif.width, gif.height)) + if(!createTexture()) throw GifLoadException("Failed to create texture for gif"); } diff --git a/src/Room.cpp b/src/Room.cpp index b526833..b558f87 100644 --- a/src/Room.cpp +++ b/src/Room.cpp @@ -101,10 +101,11 @@ namespace dchat publicKeyToKeyPairMap[user->publicKey] = keyPair; odhtdb::Log::debug("Local user set to %s for room %s", user->publicKey.toString().c_str(), id->toString().c_str()); - // Room id + room encryption key + invite key checksum (to verify invite key is not types incorrectly) + // Room id + room encryption key + invite key checksum (to verify invite key is not typed incorrectly) inviteKey = id->toString() + odhtdb::bin2hex((const char*)encryptionKey->data, encryptionKey->size); inviteKey += getInviteKeyChecksum(inviteKey); odhtdb::InfoHash inviteInfoHash = odhtdb::InfoHash::generateHash((const u8*)inviteKey.data(), inviteKey.size()); + // TODO: Use database instead of custom message. Then both parties dont have to be online to add user to room rooms->database->receiveCustomMessage(inviteInfoHash, [this](const void *data, usize size) { sibs::SafeSerializer serializer; @@ -464,6 +465,7 @@ namespace dchat }, address, port, callbackFuncs).detach(); } + // TODO: Add logout void Rooms::loginUser(const std::string &username, const std::string &password) { std::lock_guard lock(roomModifyMutex); -- cgit v1.2.3