add message file object

This commit is contained in:
Green Sky 2024-10-27 14:51:14 +01:00
parent e574c4f779
commit 5d8aa8aef9
No known key found for this signature in database
4 changed files with 61 additions and 15 deletions

View File

@ -2,6 +2,7 @@
#include <solanaceae/message3/components.hpp> #include <solanaceae/message3/components.hpp>
#include <solanaceae/contact/components.hpp> #include <solanaceae/contact/components.hpp>
#include <solanaceae/object_store/meta_components.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@ -52,6 +53,37 @@ Contact3 MessageSerializerNJ::deserlContactByID(const nlohmann::json& j) {
return other_c; return other_c;
} }
nlohmann::json MessageSerializerNJ::serlFileObjByID(ObjectHandle o) const {
if (!o) {
std::cerr << "MSC warning: encountered invalid file object\n";
return nullptr;
}
auto* id_comp = o.try_get<ObjComp::ID>();
if (id_comp == nullptr || id_comp->v.empty()) {
std::cerr << "MSC warning: encountered file object without ID\n";
return nullptr; // throw instead??
}
return nlohmann::json::binary(id_comp->v);
}
ObjectHandle MessageSerializerNJ::deserlFileObjByID(const nlohmann::json& j) {
std::vector<uint8_t> id;
if (j.is_binary()) {
id = j.get_binary();
} else {
j["bytes"].get_to(id);
}
auto o = os.getOneObjectByID(ByteSpan{id});
if (!o) {
// TODO: create empty objet with id, others need to merge if it exist? special tag to prevent arb merging??
}
return o;
}
template<> template<>
bool MessageSerializerNJ::component_get_json<Message::Components::ContactFrom>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j) { bool MessageSerializerNJ::component_get_json<Message::Components::ContactFrom>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j) {
const Contact3 c = h.get<Message::Components::ContactFrom>().c; const Contact3 c = h.get<Message::Components::ContactFrom>().c;
@ -154,3 +186,20 @@ bool MessageSerializerNJ::component_emplace_or_replace_json<Message::Components:
return true; return true;
} }
template<>
bool MessageSerializerNJ::component_get_json<Message::Components::MessageFileObject>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j) {
const auto& comp = h.get<Message::Components::MessageFileObject>();
return false;
}
template<>
bool MessageSerializerNJ::component_emplace_or_replace_json<Message::Components::MessageFileObject>(MessageSerializerNJ& msc, Handle h, const nlohmann::json& j) {
if (j.is_null()) {
std::cerr << "MSC warning: encountered null MessageFileObject\n";
h.emplace_or_replace<Message::Components::MessageFileObject>();
return true;
}
return false;
}

View File

@ -4,6 +4,7 @@
#include <entt/container/dense_map.hpp> #include <entt/container/dense_map.hpp>
#include <solanaceae/message3/registry_message_model.hpp> #include <solanaceae/message3/registry_message_model.hpp>
#include <solanaceae/object_store/object_store.hpp>
#include <nlohmann/json_fwd.hpp> #include <nlohmann/json_fwd.hpp>
@ -14,6 +15,7 @@ struct MessageSerializerNJ {
static constexpr const char* version {"1"}; static constexpr const char* version {"1"};
Contact3Registry& cr; Contact3Registry& cr;
ObjectStore2& os;
// nlohmann // nlohmann
// json/msgpack // json/msgpack
@ -76,6 +78,8 @@ struct MessageSerializerNJ {
// helper // helper
nlohmann::json serlContactByID(Contact3 c) const; nlohmann::json serlContactByID(Contact3 c) const;
Contact3 deserlContactByID(const nlohmann::json& j); Contact3 deserlContactByID(const nlohmann::json& j);
nlohmann::json serlFileObjByID(ObjectHandle o) const;
ObjectHandle deserlFileObjByID(const nlohmann::json& j);
}; };
// fwd // fwd
@ -83,6 +87,7 @@ namespace Message::Components {
struct ContactFrom; struct ContactFrom;
struct ContactTo; struct ContactTo;
struct ReceivedBy; struct ReceivedBy;
struct MessageFileObject;
} }
// make specializations known // make specializations known
@ -98,3 +103,7 @@ template<>
bool MessageSerializerNJ::component_get_json<Message::Components::ReceivedBy>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j); bool MessageSerializerNJ::component_get_json<Message::Components::ReceivedBy>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j);
template<> template<>
bool MessageSerializerNJ::component_emplace_or_replace_json<Message::Components::ReceivedBy>(MessageSerializerNJ& msc, Handle h, const nlohmann::json& j); bool MessageSerializerNJ::component_emplace_or_replace_json<Message::Components::ReceivedBy>(MessageSerializerNJ& msc, Handle h, const nlohmann::json& j);
template<>
bool MessageSerializerNJ::component_get_json<Message::Components::MessageFileObject>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j);
template<>
bool MessageSerializerNJ::component_emplace_or_replace_json<Message::Components::MessageFileObject>(MessageSerializerNJ& msc, Handle h, const nlohmann::json& j);

View File

@ -21,13 +21,7 @@ namespace Message::Components {
// SyncedBy // SyncedBy
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MessageText, text) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MessageText, text)
// file is special
// TODO: remove
//namespace Transfer {
//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FileInfo::FileDirEntry, file_name, file_size)
//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FileInfo, file_list, total_size)
//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(FileInfoLocal, file_list)
//} // Transfer
} // Message::Components } // Message::Components

View File

@ -27,13 +27,7 @@ void registerMessageComponents(MessageSerializerNJ& msnj) {
msnj.registerSerializer<Message::Components::TagMessageIsAction>(); msnj.registerSerializer<Message::Components::TagMessageIsAction>();
msnj.registerDeserializer<Message::Components::TagMessageIsAction>(); msnj.registerDeserializer<Message::Components::TagMessageIsAction>();
// files msnj.registerSerializer<Message::Components::MessageFileObject>();
//_sc.registerSerializerJson<Message::Components::Transfer::FileID>() msnj.registerDeserializer<Message::Components::MessageFileObject>();
//_sc.registerSerializerJson<Message::Components::Transfer::FileInfo>();
//_sc.registerDeSerializerJson<Message::Components::Transfer::FileInfo>();
//_sc.registerSerializerJson<Message::Components::Transfer::FileInfoLocal>();
//_sc.registerDeSerializerJson<Message::Components::Transfer::FileInfoLocal>();
//_sc.registerSerializerJson<Message::Components::Transfer::TagHaveAll>();
//_sc.registerDeSerializerJson<Message::Components::Transfer::TagHaveAll>();
} }