Audacity 3.2.0
Public Member Functions | Private Member Functions | List of all members
audacity::cloud::audiocom::UserService Class Referencefinal

Service for providing information about the user profile. More...

#include <UserService.h>

Inheritance diagram for audacity::cloud::audiocom::UserService:
[legend]
Collaboration diagram for audacity::cloud::audiocom::UserService:
[legend]

Public Member Functions

void UpdateUserData ()
 Request the service to update the data. More...
 
void ClearUserData ()
 Reset the user profile data. More...
 
wxString GetUserId () const
 Gets user id. More...
 
wxString GetUserSlug () const
 "Slug" used to construct shareable URLs More...
 
wxString GetDisplayName () const
 Get the user name to display in the dialog. More...
 
wxString GetAvatarPath () const
 Gets a path to the avatar. More...
 
- Public Member Functions inherited from Observer::Publisher< UserDataChanged >
 Publisher (ExceptionPolicy *pPolicy=nullptr, Alloc a={})
 Constructor supporting type-erased custom allocation/deletion. More...
 
 Publisher (Publisher &&)=default
 
Publisheroperator= (Publisher &&)=default
 
Subscription Subscribe (Callback callback)
 Connect a callback to the Publisher; later-connected are called earlier. More...
 
Subscription Subscribe (Object &obj, Return(Object::*callback)(Args...))
 Overload of Subscribe takes an object and pointer-to-member-function. More...
 

Private Member Functions

void DownloadAvatar (std::string_view url)
 

Additional Inherited Members

- Public Types inherited from Observer::Publisher< UserDataChanged >
using message_type = UserDataChanged
 
using CallbackReturn = std::conditional_t< true, void, bool >
 
using Callback = std::function< CallbackReturn(const UserDataChanged &) >
 Type of functions that can be connected to the Publisher. More...
 
- Static Public Attributes inherited from Observer::Publisher< UserDataChanged >
static constexpr bool notifies_all
 
- Protected Member Functions inherited from Observer::Publisher< UserDataChanged >
CallbackReturn Publish (const UserDataChanged &message)
 Send a message to connected callbacks. More...
 

Detailed Description

Service for providing information about the user profile.

Definition at line 26 of file UserService.h.

Member Function Documentation

◆ ClearUserData()

void audacity::cloud::audiocom::UserService::ClearUserData ( )

Reset the user profile data.

Definition at line 125 of file UserService.cpp.

126{
128 [this]()
129 {
130 // No valid data was present, do not spam Publish()
131 if (GetUserSlug().empty())
132 return;
133
134 userId.Write({});
135 userName.Write({});
136 displayName.Write({});
137 avatarEtag.Write({});
138
139 gPrefs->Flush();
140
141 Publish({});
142 });
143}
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
CallbackReturn Publish(const UserDataChanged &message)
Send a message to connected callbacks.
Definition: Observer.h:207
bool Write(const T &value)
Write value to config and return true if successful.
Definition: Prefs.h:259
virtual bool Flush() noexcept=0
wxString GetUserSlug() const
"Slug" used to construct shareable URLs
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
Definition: BasicUI.cpp:214

References audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::avatarEtag, BasicUI::CallAfter(), audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::displayName, audacity::BasicSettings::Flush(), GetUserSlug(), gPrefs, Observer::Publisher< UserDataChanged >::Publish(), audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::userId, audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::userName, and Setting< T >::Write().

Here is the call graph for this function:

◆ DownloadAvatar()

void audacity::cloud::audiocom::UserService::DownloadAvatar ( std::string_view  url)
private

Definition at line 151 of file UserService.cpp.

152{
153 const auto avatarPath = MakeAvatarPath();
154 const auto avatarTempPath = avatarPath + ".tmp";
155
156 if (url.empty())
157 {
158 if (wxFileExists(avatarPath))
159 wxRemoveFile(avatarPath);
160
161 return;
162 }
163
164 std::shared_ptr<wxFile> avatarFile = std::make_shared<wxFile>();
165
166 if (!avatarFile->Create(avatarTempPath, true))
167 return;
168
169 using namespace audacity::network_manager;
170
171 auto request = Request(std::string(url));
172
173 const auto etag = audacity::ToUTF8(avatarEtag.Read());
174
175 // If ETag is present - use it to prevent re-downloading the same file
176 if (!etag.empty() && wxFileExists(avatarPath))
177 request.setHeader(common_headers::IfNoneMatch, etag);
178
179 auto response = NetworkManager::GetInstance().doGet(request);
180
181 response->setOnDataReceivedCallback(
182 [response, avatarFile](auto)
183 {
184 std::vector<char> buffer(response->getBytesAvailable());
185
186 size_t bytes = response->readData(buffer.data(), buffer.size());
187
188 avatarFile->Write(buffer.data(), buffer.size());
189 });
190
191 response->setRequestFinishedCallback(
192 [response, avatarFile, avatarPath, avatarTempPath, this](auto)
193 {
194 avatarFile->Close();
195
196 const auto httpCode = response->getHTTPCode();
197
198 if (httpCode != 200)
199 {
200 // For any response except 200 just remove the temp file
201 wxRemoveFile(avatarTempPath);
202 return;
203 }
204
205 const auto etag = response->getHeader("ETag");
206 const auto oldPath = avatarPath + ".old";
207
208 if (wxFileExists(avatarPath))
209 if (!wxRenameFile(avatarPath, oldPath))
210 return;
211
212 if (!wxRenameFile(avatarTempPath, avatarPath))
213 {
214 // Try at least to get it back...
215 wxRenameFile(oldPath, avatarPath);
216 return;
217 }
218
219 if (wxFileExists(oldPath))
220 wxRemoveFile(oldPath);
221
223 [this, etag]()
224 {
225 avatarEtag.Write(etag);
226 gPrefs->Flush();
227
228 Publish({});
229 });
230 });
231}
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined *‍/
Definition: Prefs.h:207
ResponsePtr doGet(const Request &request)
std::string ToUTF8(const std::wstring &wstr)

References audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::avatarEtag, BasicUI::CallAfter(), audacity::network_manager::NetworkManager::doGet(), audacity::BasicSettings::Flush(), audacity::network_manager::NetworkManager::GetInstance(), gPrefs, audacity::network_manager::common_headers::IfNoneMatch, audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::MakeAvatarPath(), Observer::Publisher< UserDataChanged >::Publish(), Setting< T >::Read(), audacity::ToUTF8(), and Setting< T >::Write().

Referenced by UpdateUserData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetAvatarPath()

wxString audacity::cloud::audiocom::UserService::GetAvatarPath ( ) const

Gets a path to the avatar.

Definition at line 248 of file UserService.cpp.

249{
250 auto path = MakeAvatarPath();
251
252 if (!wxFileExists(path))
253 return {};
254
255 return path;
256}

References audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::MakeAvatarPath().

Here is the call graph for this function:

◆ GetDisplayName()

wxString audacity::cloud::audiocom::UserService::GetDisplayName ( ) const

Get the user name to display in the dialog.

Definition at line 238 of file UserService.cpp.

239{
240 return displayName.Read();
241}

References audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::displayName, and Setting< T >::Read().

Here is the call graph for this function:

◆ GetUserId()

wxString audacity::cloud::audiocom::UserService::GetUserId ( ) const

Gets user id.

Definition at line 233 of file UserService.cpp.

234{
235 return userId.Read();
236}

References Setting< T >::Read(), and audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::userId.

Here is the call graph for this function:

◆ GetUserSlug()

wxString audacity::cloud::audiocom::UserService::GetUserSlug ( ) const

"Slug" used to construct shareable URLs

Definition at line 243 of file UserService.cpp.

244{
245 return userName.Read();
246}

References Setting< T >::Read(), and audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::userName.

Referenced by ClearUserData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateUserData()

void audacity::cloud::audiocom::UserService::UpdateUserData ( )

Request the service to update the data.

Definition at line 62 of file UserService.cpp.

63{
64 auto& oauthService = GetOAuthService();
65
66 if (!oauthService.HasAccessToken())
67 return;
68
69 using namespace audacity::network_manager;
70
71 Request request(GetServiceConfig().GetAPIUrl("/me"));
72
73 request.setHeader(
75 std::string(oauthService.GetAccessToken()));
76
77 request.setHeader(
79
80 auto response = NetworkManager::GetInstance().doGet(request);
81
82 response->setRequestFinishedCallback(
83 [response, this](auto)
84 {
85 const auto httpCode = response->getHTTPCode();
86
87 if (httpCode != 200)
88 return;
89
90 const auto body = response->readAll<std::string>();
91
92 using namespace rapidjson;
93
94 Document document;
95 document.Parse(body.data(), body.size());
96
97 if (!document.IsObject())
98 return;
99
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();
104
106 [this,
107 id = std::string(id),
108 username = std::string(username),
109 profileName = std::string(profileName),
110 avatar = std::string(avatar)]()
111 {
115
116 gPrefs->Flush();
117
118 DownloadAvatar(avatar);
119
120 Publish({});
121 });
122 });
123}
void DownloadAvatar(std::string_view url)
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
const ServiceConfig & GetServiceConfig()
Returns the instance of the ServiceConfig.
wxString ToWXString(const std::string &str)

References audacity::network_manager::common_headers::Accept, audacity::network_manager::common_content_types::ApplicationJson, audacity::network_manager::common_headers::Authorization, BasicUI::CallAfter(), audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::displayName, audacity::network_manager::NetworkManager::doGet(), DownloadAvatar(), audacity::BasicSettings::Flush(), audacity::network_manager::NetworkManager::GetInstance(), audacity::cloud::audiocom::GetOAuthService(), audacity::cloud::audiocom::GetServiceConfig(), gPrefs, Observer::Publisher< UserDataChanged >::Publish(), audacity::network_manager::Request::setHeader(), audacity::ToWXString(), audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::userId, audacity::cloud::audiocom::anonymous_namespace{UserService.cpp}::userName, and Setting< T >::Write().

Here is the call graph for this function:

The documentation for this class was generated from the following files: