aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
m---------depends/odhtdb0
-rw-r--r--include/dchat/Cache.hpp8
-rw-r--r--include/dchat/Gif.hpp2
-rw-r--r--include/dchat/Room.hpp9
-rw-r--r--include/dchat/WebPagePreview.hpp5
-rw-r--r--src/Cache.cpp14
-rw-r--r--src/Gif.cpp2
-rw-r--r--src/Room.cpp4
8 files changed, 28 insertions, 16 deletions
diff --git a/depends/odhtdb b/depends/odhtdb
-Subproject e673a8de53278814a860e9eba03ef776be0a416
+Subproject 004cb37fce4621fc8537146d866081c67045bc9
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 <boost/filesystem/path.hpp>
@@ -63,6 +62,7 @@ namespace dchat
// Returned gif should be allocated with @new
using CreateGifFunc = std::function<Gif*(StringView fileContent)>;
using CreateStaticImageFunc = std::function<StaticImage*(const boost::filesystem::path &filepath)>;
+ using CreateWebPagePreviewFunc = std::function<WebPagePreview*(const std::string &title, const std::string &description)>;
class Cache
{
@@ -70,7 +70,8 @@ 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.
@@ -78,6 +79,8 @@ namespace dchat
// 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
{
TinyProcessLib::Process *process;
@@ -93,5 +96,6 @@ namespace dchat
std::unordered_map<std::string, ContentByUrlResult> 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<User> getUserByPublicKey(const odhtdb::Signature::PublicKey &userPublicKey);
// Returns null if group doesn't exist in room
std::shared_ptr<Group> getGroupById(const odhtdb::DataView groupId);
- void setLocalUser(std::shared_ptr<User> user, std::shared_ptr<odhtdb::Signature::KeyPair> keyPair);
void setAvatarUrl(const std::string &url);
void setNickname(const std::string &nickname);
void publishMessage(const std::string &msg);
+ const odhtdb::Signature::MapPublicKey<std::shared_ptr<User>>& getUsers() const { return userByPublicKey; }
+ const std::vector<std::shared_ptr<RoomMessage>>& getMessages() const { return messages; }
+
Rooms *rooms;
std::shared_ptr<odhtdb::Hash> id;
std::shared_ptr<odhtdb::OwnedByteArray> encryptionKey;
@@ -55,6 +57,8 @@ namespace dchat
// TODO: Move to private when we have proper support for groups
std::vector<std::shared_ptr<Group>> groups;
private:
+ void setLocalUser(std::shared_ptr<User> user, std::shared_ptr<odhtdb::Signature::KeyPair> keyPair);
+ private:
odhtdb::Signature::MapPublicKey<std::shared_ptr<User>> userByPublicKey;
// Used for local users
odhtdb::Signature::MapPublicKey<std::shared_ptr<odhtdb::Signature::KeyPair>> publicKeyToKeyPairMap;
@@ -152,6 +156,9 @@ namespace dchat
void requestJoinRoom(const std::string &inviteKey, const std::string &message);
std::shared_ptr<odhtdb::Database> database;
+
+ const odhtdb::MapHash<std::shared_ptr<Room>>& 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 <string>
-
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::milliseconds>(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::milliseconds>(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<std::recursive_mutex> lock(roomModifyMutex);