Skip to content

Commit

Permalink
Fixes the audio.com upload flow
Browse files Browse the repository at this point in the history
1. Anonymous uploads are allowed once again
2. After the upload is completed URL is opened in the browser immediately
  • Loading branch information
crsib committed May 29, 2023
1 parent 7a258a0 commit 0035558
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 53 deletions.
8 changes: 7 additions & 1 deletion libraries/lib-cloud-audiocom/ServiceConfig.cpp
Expand Up @@ -46,11 +46,17 @@ std::string ServiceConfig::GetAPIUrl(std::string_view apiURI) const
std::string ServiceConfig::GetFinishUploadPage(
std::string_view audioID, std::string_view token) const
{
return "http://audio.com/audacity/upload?audioId=" + std::string(audioID) +
return "https://audio.com/audacity/upload?audioId=" + std::string(audioID) +
"&token=" + std::string(token) +
"&clientId=" + std::string(GetOAuthClientID());
}

std::string ServiceConfig::GetAudioURL(
std::string_view userSlug, std::string_view audioSlug) const
{
return "https://audio.com/" + std::string(userSlug) + "/" + std::string(audioSlug) + "/edit";
}

std::chrono::milliseconds ServiceConfig::GetProgressCallbackTimeout() const
{
return std::chrono::seconds(3);
Expand Down
2 changes: 2 additions & 0 deletions libraries/lib-cloud-audiocom/ServiceConfig.h
Expand Up @@ -35,6 +35,8 @@ class CLOUD_AUDIOCOM_API ServiceConfig final
std::string GetAPIUrl(std::string_view apiURI) const;
//! Helper to construct the page URL for the anonymous upload last stage
std::string GetFinishUploadPage(std::string_view audioID, std::string_view token) const;
//! Helper to construct the page URL for the authorised upload
std::string GetAudioURL(std::string_view userSlug, std::string_view audioSlug) const;
//! Timeout between progress callbacks
std::chrono::milliseconds GetProgressCallbackTimeout() const;
//! Preferred audio format
Expand Down
11 changes: 9 additions & 2 deletions libraries/lib-cloud-audiocom/UploadService.cpp
Expand Up @@ -206,6 +206,7 @@ struct AudiocomUploadOperation final :

std::string mAudioID;
std::string mUploadToken;
std::string mUserName;

std::string mAudioSlug;

Expand Down Expand Up @@ -264,10 +265,14 @@ struct AudiocomUploadOperation final :

if (mCompletedCallback)
{
const auto uploadURL =
mAuthToken.empty() ?
mServiceConfig.GetFinishUploadPage(mAudioID, mUploadToken) :
mServiceConfig.GetAudioURL(mUserName, mAudioSlug);

mCompletedCallback(
{ UploadOperationCompleted::Result::Success,
UploadSuccessfulPayload { mAudioID, mAudioSlug } });
UploadSuccessfulPayload { mAudioID, mAudioSlug, mUploadToken, uploadURL } });
}

mProgressCallback = {};
Expand Down Expand Up @@ -395,6 +400,8 @@ struct AudiocomUploadOperation final :

if (extra.HasMember("token"))
mUploadToken = extra["token"].GetString();

mUserName = extra["audio"]["username"].GetString();
}

const auto encType = document.HasMember("enctype") ?
Expand Down Expand Up @@ -579,7 +586,7 @@ UploadOperationHandle UploadService::Upload(
mServiceConfig, fileName, projectName, isPublic,
std::move(completedCallback), std::move(progressCallback));

mOAuthService.ValidateAuth([operation](std::string_view authToken)
mOAuthService.ValidateAuth([operation, this](std::string_view authToken)
{ operation->InitiateUpload(authToken); });

return UploadOperationHandle { operation };
Expand Down
4 changes: 4 additions & 0 deletions libraries/lib-cloud-audiocom/UploadService.h
Expand Up @@ -43,6 +43,10 @@ struct CLOUD_AUDIOCOM_API UploadSuccessfulPayload final
std::string audioId;
//! "Slug" to be used for shareable URL construction
std::string audioSlug;
//! Upload token, if any
std::string uploadToken;
//! URL to the uploaded audio
std::string audioUrl;
};

//! Message that is sent when upload is finished.
Expand Down
57 changes: 12 additions & 45 deletions src/cloud/audiocom/ShareAudioDialog.cpp
Expand Up @@ -169,7 +169,6 @@ ShareAudioDialog::ShareAudioDialog(AudacityProject& project, wxWindow* parent)
parent, wxID_ANY, XO("Share Audio"), wxDefaultPosition, { 480, 250 },
wxDEFAULT_DIALOG_STYLE)
, mProject(project)
, mInitialStatePanel(*this)
, mServices(std::make_unique<Services>())
{
GetAuthorizationHandler().PushSuppressDialogs();
Expand Down Expand Up @@ -240,7 +239,6 @@ void ShareAudioDialog::Populate(ShuttleGui& s)

mContinueButton = s.AddButton(XXO("C&ontinue"));
mContinueButton->Bind(wxEVT_BUTTON, [this](auto) { OnContinue(); });
mContinueButton->Enable(mIsAuthorised);
}
s.EndHorizontalLay();
}
Expand Down Expand Up @@ -436,28 +434,8 @@ void ShareAudioDialog::StartUploadProcess()
void ShareAudioDialog::HandleUploadSucceeded(
const UploadSuccessfulPayload& payload)
{
mProgressPanel.timePanel->Hide();
mProgressPanel.title->SetLabel(XO("Upload complete!").Translation());
mProgressPanel.info->Show();

mProgressPanel.info->SetLabel(
"By pressing continue, you will be taken to audio.com and given a shareable link.");
mProgressPanel.info->Wrap(mProgressPanel.root->GetSize().GetWidth());

mContinueAction = [this, slug = std::string(payload.audioSlug)]()
{
EndModal(wxID_CLOSE);
auto url = wxString::Format(
"https://audio.com/%s/%s/edit", GetUserService().GetUserSlug(),
audacity::ToWXString(slug));

OpenInDefaultBrowser(url);
};

mContinueButton->Show();

Layout();
Fit();
EndModal(wxID_CLOSE);
OpenInDefaultBrowser(wxString { payload.audioUrl });
}

void ShareAudioDialog::HandleUploadFailed(const UploadFailedPayload& payload)
Expand All @@ -466,12 +444,7 @@ void ShareAudioDialog::HandleUploadFailed(const UploadFailedPayload& payload)

TranslatableString message;

if (payload.status == 401)
{
message = XO(
"We are unable to upload this file. Please try again and make sure to link to your audio.com account before uploading.");
}
else
if (!payload.message.empty())
{
auto details = payload.message;

Expand All @@ -480,6 +453,11 @@ void ShareAudioDialog::HandleUploadFailed(const UploadFailedPayload& payload)

message = XO("Error: %s").Format(details);
}
else
{
message = XO(
"We are unable to upload this file. Please try again and make sure to link to your audio.com account before uploading.");
}

BasicUI::ShowErrorDialog(
{}, XO("Upload error"),
Expand Down Expand Up @@ -574,9 +552,8 @@ void ShareAudioDialog::UpdateProgress(uint64_t current, uint64_t total)
std::chrono::duration_cast<std::chrono::milliseconds>(remains));
}

ShareAudioDialog::InitialStatePanel::InitialStatePanel(ShareAudioDialog& parent)
: parent { parent }
, mUserDataChangedSubscription(
ShareAudioDialog::InitialStatePanel::InitialStatePanel()
: mUserDataChangedSubscription(
GetUserService().Subscribe([this](const auto&) { UpdateUserData(); }))
{
}
Expand Down Expand Up @@ -709,14 +686,9 @@ void ShareAudioDialog::InitialStatePanel::UpdateUserData()
avatar->SetBitmap(theTheme.Bitmap(bmpAnonymousUser));

oauthButton->SetLabel(XXO("&Unlink Account").Translation());

parent.mIsAuthorised = true;


anonInfoPanel->Hide();
authorizedInfoPanel->Show();

if (parent.mContinueButton != nullptr)
parent.mContinueButton->Enable();
}

void ShareAudioDialog::InitialStatePanel::OnLinkButtonPressed()
Expand All @@ -741,18 +713,13 @@ void ShareAudioDialog::InitialStatePanel::OnLinkButtonPressed()
}

void ShareAudioDialog::InitialStatePanel::SetAnonymousState()
{
parent.mIsAuthorised = false;

{
name->SetLabel(XO("Anonymous").Translation());
avatar->SetBitmap(theTheme.Bitmap(bmpAnonymousUser));
oauthButton->SetLabel(XXO("&Link Account").Translation());

anonInfoPanel->Show();
authorizedInfoPanel->Hide();

if (parent.mContinueButton != nullptr)
parent.mContinueButton->Enable(false);
}

void ShareAudioDialog::ProgressPanel::PopulateProgressPanel(ShuttleGui& s)
Expand Down
7 changes: 2 additions & 5 deletions src/cloud/audiocom/ShareAudioDialog.h
Expand Up @@ -68,9 +68,7 @@ class ShareAudioDialog final :

struct InitialStatePanel final
{
explicit InitialStatePanel(ShareAudioDialog& parent);

ShareAudioDialog& parent;
InitialStatePanel();

wxWindow* root { nullptr };

Expand Down Expand Up @@ -126,8 +124,7 @@ class ShareAudioDialog final :
wxString mFilePath;

std::function<void()> mContinueAction;

bool mIsAuthorised { false };

bool mInProgress { false };
};
} // namespace cloud::audiocom

0 comments on commit 0035558

Please sign in to comment.