19#include <rapidjson/document.h>
42 return avatarFileName.GetFullPath();
65 if (!oauthService.HasAccessToken())
74 std::string(oauthService.GetAccessToken()));
81 response->setRequestFinishedCallback(
82 [response,
this](
auto)
84 const auto httpCode = response->getHTTPCode();
89 const auto body = response->readAll<std::string>();
91 using namespace rapidjson;
94 document.Parse(body.data(), body.size());
96 if (!document.IsObject())
99 const auto username = document[
"username"].GetString();
100 const auto avatar = document[
"avatar"].GetString();
101 const auto profileName = document[
"profile"][
"name"].GetString();
104 [
this, username = std::string(username),
105 profileName = std::string(profileName),
106 avatar = std::string(avatar)]()
148 const auto avatarTempPath = avatarPath +
".tmp";
152 if (wxFileExists(avatarPath))
153 wxRemoveFile(avatarPath);
158 std::shared_ptr<wxFile> avatarFile = std::make_shared<wxFile>();
160 if (!avatarFile->Create(avatarTempPath,
true))
165 auto request =
Request(std::string(url));
170 if (!etag.empty() && wxFileExists(avatarPath))
175 response->setOnDataReceivedCallback(
176 [response, avatarFile](
auto)
178 std::vector<char> buffer(response->getBytesAvailable());
180 size_t bytes = response->readData(buffer.data(), buffer.size());
182 avatarFile->Write(buffer.data(), buffer.size());
185 response->setRequestFinishedCallback(
186 [response, avatarFile, avatarPath, avatarTempPath,
this](
auto)
190 const auto httpCode = response->getHTTPCode();
195 wxRemoveFile(avatarTempPath);
199 const auto etag = response->getHeader(
"ETag");
200 const auto oldPath = avatarPath +
".old";
202 if (wxFileExists(avatarPath))
203 if (!wxRenameFile(avatarPath, oldPath))
206 if (!wxRenameFile(avatarTempPath, avatarPath))
209 wxRenameFile(oldPath, avatarPath);
213 if (wxFileExists(oldPath))
214 wxRemoveFile(oldPath);
241 if (!wxFileExists(path))
Toolkit-neutral facade for basic user interface services.
Declare functions to perform UTF-8 to std::wstring conversions.
Declare an interface for HTTP response.
Declare a class for performing HTTP requests.
audacity::BasicSettings * gPrefs
Declare a class for constructing HTTP requests.
Subscription Subscribe(Callback callback)
Connect a callback to the Publisher; later-connected are called earlier.
CallbackReturn Publish(const UserDataChanged &message)
Send a message to connected callbacks.
A move-only handle representing a connection to a Publisher.
bool Write(const T &value)
Write value to config and return true if successful.
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined */
Specialization of Setting for strings.
virtual bool Flush() noexcept=0
Service for providing information about the user profile.
void DownloadAvatar(std::string_view url)
wxString GetUserSlug() const
"Slug" used to construct shareable URLs
void ClearUserData()
Reset the user profile data.
wxString GetAvatarPath() const
Gets a path to the avatar.
wxString GetDisplayName() const
Get the user name to display in the dialog.
void UpdateUserData()
Request the service to update the data.
static NetworkManager & GetInstance()
ResponsePtr doGet(const Request &request)
Request & setHeader(const std::string &name, std::string value)
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
FILES_API FilePath ConfigDir()
Audacity user config directory.
StringSetting displayName
Observer::Subscription authStateChangedSubscription
wxString MakeAvatarPath()
UserService & GetUserService()
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
const ServiceConfig & GetServiceConfig()
Returns the instance of the ServiceConfig.
const std::string ApplicationJson
std::string ToUTF8(const std::wstring &wstr)
wxString ToWXString(const std::string &str)