Download Prusa account Avatar

Missing adding avatar to topbar
This commit is contained in:
David Kocik 2024-01-22 12:34:53 +01:00
parent ce91ef8f49
commit 7011322f62
7 changed files with 86 additions and 8 deletions

View File

@ -296,6 +296,18 @@ void PrusaAuthCommunication::enqueue_connect_printers_action()
wakeup_session_thread();
}
void PrusaAuthCommunication::enqueue_avatar_action(const std::string url)
{
{
std::lock_guard<std::mutex> lock(m_session_mutex);
if (!m_session->is_initialized()) {
BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in.";
return;
}
m_session->enqueue_action(UserActionID::AVATAR, nullptr, nullptr, url);
}
wakeup_session_thread();
}
void PrusaAuthCommunication::init_session_thread()
{
m_thread = std::thread([this]() {

View File

@ -43,8 +43,8 @@ public:
void enqueue_connect_dummy_action();
#endif
void enqueue_connect_printers_action();
void enqueue_avatar_action(const std::string url);
// Callbacks - called from UI after receiving Event from Session thread. Some might use Session thread.
//
// Called when browser returns code via prusaslicer:// custom url.

View File

@ -24,10 +24,12 @@ wxDEFINE_EVENT(EVT_PA_ID_USER_SUCCESS, PrusaAuthSuccessEvent);
wxDEFINE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent);
wxDEFINE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent);
wxDEFINE_EVENT(EVT_PRUSACONNECT_PRINTERS_SUCCESS, PrusaAuthSuccessEvent);
wxDEFINE_EVENT(EVT_PA_AVATAR_SUCCESS, PrusaAuthSuccessEvent);
wxDEFINE_EVENT(EVT_PRUSAAUTH_FAIL, PrusaAuthFailEvent);
wxDEFINE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent);
void UserActionPost::perform(const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input)
void UserActionPost::perform( /*UNUSED*/ const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input)
{
std::string url = m_url;
//BOOST_LOG_TRIVIAL(info) << m_action_name <<" POST " << url << " body: " << input;
@ -73,6 +75,30 @@ void UserActionGetWithEvent::perform(const std::string& access_token, UserAction
http.perform_sync();
}
void UserActionNoAuthGetWithEvent::perform(/*UNUSED*/ const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input)
{
std::string url = m_url + input;
//BOOST_LOG_TRIVIAL(info) << m_action_name << " GET " << url;
auto http = Http::get(url);
http.on_error([&](std::string body, std::string error, unsigned status) {
//BOOST_LOG_TRIVIAL(error) << m_action_name << " action failed. status: " << status << " Body: " << body;
if (fail_callback)
fail_callback(body);
std::string message = GUI::format("%1% action failed (%2%): %3%", m_action_name, std::to_string(status), body);
if (m_succ_evt_type != wxEVT_NULL)
wxQueueEvent(m_evt_handler, new PrusaAuthFailEvent(m_fail_evt_type, std::move(message)));
});
http.on_complete([&, this](std::string body, unsigned status) {
//BOOST_LOG_TRIVIAL(info) << m_action_name << " action success. Status: " << status << " Body: " << body;
if (success_callback)
success_callback(body);
if (m_succ_evt_type != wxEVT_NULL)
wxQueueEvent(m_evt_handler, new PrusaAuthSuccessEvent(m_succ_evt_type, body));
});
http.perform_sync();
}
void AuthSession::process_action_queue()
{
if (!m_proccessing_enabled)

View File

@ -22,6 +22,7 @@ wxDECLARE_EVENT(EVT_PA_ID_USER_SUCCESS, PrusaAuthSuccessEvent);
wxDECLARE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent);
wxDECLARE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent);
wxDECLARE_EVENT(EVT_PRUSACONNECT_PRINTERS_SUCCESS, PrusaAuthSuccessEvent);
wxDECLARE_EVENT(EVT_PA_AVATAR_SUCCESS, PrusaAuthSuccessEvent);
wxDECLARE_EVENT(EVT_PRUSAAUTH_FAIL, PrusaAuthFailEvent);
wxDECLARE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent);
@ -37,6 +38,7 @@ enum class UserActionID {
LOGIN_USER_ID,
CONNECT_DUMMY,
CONNECT_PRINTERS,
AVATAR,
};
class UserAction
{
@ -66,6 +68,23 @@ private:
wxEvtHandler* m_evt_handler;
};
class UserActionNoAuthGetWithEvent : public UserAction
{
public:
UserActionNoAuthGetWithEvent(const std::string name, const std::string url, wxEvtHandler* evt_handler, wxEventType succ_event_type, wxEventType fail_event_type)
: m_succ_evt_type(succ_event_type)
, m_fail_evt_type(fail_event_type)
, m_evt_handler(evt_handler)
, UserAction(name, url)
{}
~UserActionNoAuthGetWithEvent() {}
void perform(const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) override;
private:
wxEventType m_succ_evt_type;
wxEventType m_fail_evt_type;
wxEvtHandler* m_evt_handler;
};
class UserActionPost : public UserAction
{
public:
@ -107,6 +126,7 @@ public:
m_actions[UserActionID::LOGIN_USER_ID] = std::make_unique<UserActionGetWithEvent>("USER_ID", "https://test-account.prusa3d.com/api/v1/me/", evt_handler, EVT_PA_ID_USER_SUCCESS, EVT_PRUSAAUTH_FAIL);
m_actions[UserActionID::CONNECT_DUMMY] = std::make_unique<UserActionGetWithEvent>("CONNECT_DUMMY", "https://dev.connect.prusa3d.com/slicer/dummy"/*"dev.connect.prusa:8000/slicer/dummy"*/, evt_handler, EVT_PRUSAAUTH_SUCCESS, EVT_PRUSAAUTH_FAIL);
m_actions[UserActionID::CONNECT_PRINTERS] = std::make_unique<UserActionGetWithEvent>("CONNECT_PRINTERS", "https://dev.connect.prusa3d.com/slicer/printers"/*"dev.connect.prusa:8000/slicer/printers"*/, evt_handler, EVT_PRUSACONNECT_PRINTERS_SUCCESS, EVT_PRUSAAUTH_FAIL);
m_actions[UserActionID::AVATAR] = std::make_unique<UserActionNoAuthGetWithEvent>("AVATAR", "https://test-media.printables.com/media/", evt_handler, EVT_PA_AVATAR_SUCCESS, EVT_PRUSAAUTH_FAIL);
}
~AuthSession()
{
@ -117,6 +137,7 @@ public:
m_actions[UserActionID::LOGIN_USER_ID].reset(nullptr);
m_actions[UserActionID::CONNECT_DUMMY].reset(nullptr);
m_actions[UserActionID::CONNECT_PRINTERS].reset(nullptr);
m_actions[UserActionID::AVATAR].reset(nullptr);
//assert(m_actions.empty());
}
void clear() {

View File

@ -940,7 +940,14 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
// TODO
}
});
this->q->Bind(EVT_PA_AVATAR_SUCCESS, [this](PrusaAuthSuccessEvent& evt) {
boost::filesystem::path png_path = boost::filesystem::path(Slic3r::data_dir()) / "cache" / "avatar.png";
FILE* file;
file = fopen(png_path.string().c_str(), "wb");
fwrite(evt.data.c_str(), 1, evt.data.size(), file);
fclose(file);
});
wxGetApp().other_instance_message_handler()->init(this->q);
// collapse sidebar according to saved value

View File

@ -72,6 +72,11 @@ void UserAccount::enqueue_connect_printers_action()
m_auth_communication->enqueue_connect_printers_action();
}
void UserAccount::enqueue_avatar_action(const std::string& url)
{
m_auth_communication->enqueue_avatar_action(url);
}
bool UserAccount::on_login_code_recieved(const std::string& url_message)
{
m_auth_communication->on_login_code_recieved(url_message);
@ -93,20 +98,26 @@ bool UserAccount::on_user_id_success(const std::string data, AppConfig* app_conf
for (const auto& section : ptree) {
const auto opt = ptree.get_optional<std::string>(section.first);
if (opt) {
BOOST_LOG_TRIVIAL(debug) << static_cast<std::string>(section.first) << *opt;
BOOST_LOG_TRIVIAL(debug) << static_cast<std::string>(section.first) << " " << *opt;
m_user_data[section.first] = *opt;
}
}
assert(m_user_data.find("public_username") != m_user_data.end());
if (m_user_data.find("public_username") == m_user_data.end())
{
BOOST_LOG_TRIVIAL(error) << "User ID message from Connect did not contain public_username. Login failed. Message data: " << data;
if (m_user_data.find("public_username") == m_user_data.end()) {
BOOST_LOG_TRIVIAL(error) << "User ID message from PrusaAuth did not contain public_username. Login failed. Message data: " << data;
return false;
}
std::string public_username = m_user_data["public_username"];
set_username(public_username, app_config);
out_username = public_username;
// equeue GET with avatar url
if (m_user_data.find("avatar") != m_user_data.end()) {
enqueue_avatar_action(m_user_data["avatar"]);
}
else {
BOOST_LOG_TRIVIAL(warning) << "User ID message from PrusaAcuth did not contain avatar.";
}
// update printers list
enqueue_connect_printers_action();
return true;

View File

@ -41,6 +41,7 @@ public:
void enqueue_connect_dummy_action();
#endif
void enqueue_connect_printers_action();
void enqueue_avatar_action(const std::string& url);
// Functions called from UI where events emmited from AuthSession are binded
// Returns bool if data were correctly proccessed