19#include <rapidjson/document.h>
42 return avatarFileName.GetFullPath();
66 if (!oauthService.HasAccessToken())
75 std::string(oauthService.GetAccessToken()));
82 response->setRequestFinishedCallback(
83 [response,
this](
auto)
85 const auto httpCode = response->getHTTPCode();
90 const auto body = response->readAll<std::string>();
92 using namespace rapidjson;
95 document.Parse(body.data(), body.size());
97 if (!document.IsObject())
100 const auto id = document[
"id"].GetString();
101 const auto username = document[
"username"].GetString();
102 const auto avatar = document[
"avatar"].GetString();
103 const auto profileName = document[
"profile"][
"name"].GetString();
107 id = std::string(
id),
108 username = std::string(username),
109 profileName = std::string(profileName),
110 avatar = std::string(avatar)]()
154 const auto avatarTempPath = avatarPath +
".tmp";
158 if (wxFileExists(avatarPath))
159 wxRemoveFile(avatarPath);
164 std::shared_ptr<wxFile> avatarFile = std::make_shared<wxFile>();
166 if (!avatarFile->Create(avatarTempPath,
true))
171 auto request =
Request(std::string(url));
176 if (!etag.empty() && wxFileExists(avatarPath))
181 response->setOnDataReceivedCallback(
182 [response, avatarFile](
auto)
184 std::vector<char> buffer(response->getBytesAvailable());
186 size_t bytes = response->readData(buffer.data(), buffer.size());
188 avatarFile->Write(buffer.data(), buffer.size());
191 response->setRequestFinishedCallback(
192 [response, avatarFile, avatarPath, avatarTempPath,
this](
auto)
196 const auto httpCode = response->getHTTPCode();
201 wxRemoveFile(avatarTempPath);
205 const auto etag = response->getHeader(
"ETag");
206 const auto oldPath = avatarPath +
".old";
208 if (wxFileExists(avatarPath))
209 if (!wxRenameFile(avatarPath, oldPath))
212 if (!wxRenameFile(avatarTempPath, avatarPath))
215 wxRenameFile(oldPath, avatarPath);
219 if (wxFileExists(oldPath))
220 wxRemoveFile(oldPath);
252 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 GetUserId() const
Gets user id.
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)