Skip to content

Commit

Permalink
Handle "new" in open links
Browse files Browse the repository at this point in the history
  • Loading branch information
crsib committed Feb 20, 2024
1 parent 2e89458 commit ca40fd2
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 69 deletions.
11 changes: 10 additions & 1 deletion libraries/lib-cloud-audiocom/CloudSyncService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ void PerformProjectGetRequest(
{
assert(oAuthService.HasAccessToken());

if (!oAuthService.HasAccessToken())
{
dataCallback({ ResponseResultCode::Unauthorized });
return;
}

using namespace audacity::network_manager;

auto request = Request(std::move(url));
Expand Down Expand Up @@ -444,6 +450,9 @@ void CloudSyncService::CompleteSync(std::string path)

void CloudSyncService::CompleteSync(sync::ProjectSyncResult result)
{
if (mRemoteSnapshot)
result.Stats = mRemoteSnapshot->GetTransferStats();

mSyncPromise.set_value(std::move(result));
mRemoteSnapshot.reset();
mSyncInProcess.store(false);
Expand Down Expand Up @@ -508,7 +517,7 @@ void CloudSyncService::SyncCloudSnapshot(
sync::ProjectSyncResult::StatusCode::Failed,
std::move(state.Result), std::move(path) });
}
});
}, mode == SyncMode::ForceNew);
}

void CloudSyncService::UpdateDowloadProgress(double downloadProgress)
Expand Down
1 change: 1 addition & 0 deletions libraries/lib-cloud-audiocom/CloudSyncService.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct ProjectSyncResult final
StatusCode Status {};
ResponseResult Result;
std::string ProjectPath;
TransferStats Stats;
}; // struct ProjectSyncResult

using ProgressCallback = std::function<bool(double)>;
Expand Down
25 changes: 25 additions & 0 deletions libraries/lib-cloud-audiocom/NetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,29 @@ void CancellationContext::OnCancelled(
OnCancelled([response = std::move(response)]() { response->abort(); });
}

TransferStats& TransferStats::SetBytesTransferred(int64_t bytesTransferred)
{
BytesTransferred = bytesTransferred;
return *this;
}

TransferStats& TransferStats::SetBlocksTransferred(int64_t blocksTransferred)
{
BlocksTransferred = blocksTransferred;
return *this;
}

TransferStats&
TransferStats::SetProjectFilesTransferred(int64_t projectFilesTransferred)
{
ProjectFilesTransferred = projectFilesTransferred;
return *this;
}

TransferStats& TransferStats::SetTransferDuration(Duration transferDuration)
{
TransferDuration = transferDuration;
return *this;
}

} // namespace cloud::audiocom
20 changes: 19 additions & 1 deletion libraries/lib-cloud-audiocom/NetworkUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#pragma once

#include <atomic>
#include <chrono>
#include <functional>
#include <memory>
#include <mutex>
Expand All @@ -26,6 +27,22 @@ class Request;

namespace cloud::audiocom
{
struct CLOUD_AUDIOCOM_API TransferStats final
{
using Duration = std::chrono::milliseconds;

int64_t BytesTransferred {};
int64_t BlocksTransferred {};
int64_t ProjectFilesTransferred {};

Duration TransferDuration {};

TransferStats& SetBytesTransferred(int64_t bytesTransferred);
TransferStats& SetBlocksTransferred(int64_t blocksTransferred);
TransferStats& SetProjectFilesTransferred(int64_t projectFilesTransferred);
TransferStats& SetTransferDuration(Duration transferDuration);
}; // struct TransferStats

struct CLOUD_AUDIOCOM_API CancellationContext final
{
struct Tag final
Expand All @@ -45,7 +62,8 @@ struct CLOUD_AUDIOCOM_API CancellationContext final
void Cancel();

void OnCancelled(std::function<void()> callback);
void OnCancelled(std::shared_ptr<audacity::network_manager::IResponse> response);
void
OnCancelled(std::shared_ptr<audacity::network_manager::IResponse> response);

private:
std::atomic<bool> mCancelled { false };
Expand Down
7 changes: 7 additions & 0 deletions libraries/lib-cloud-audiocom/sync/CloudProjectsDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ CREATE TABLE IF NOT EXISTS block_hashes
CREATE INDEX IF NOT EXISTS block_hashes_index ON block_hashes (hash);
CREATE TABLE IF NOT EXISTS migrations
(
version INTEGER PRIMARY KEY
);
INSERT OR IGNORE INTO migrations (version) VALUES (0);
)";

}

CloudProjectsDatabase::CloudProjectsDatabase()
Expand Down
88 changes: 70 additions & 18 deletions libraries/lib-cloud-audiocom/sync/ProjectCloudExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,22 @@ struct ProjectCloudExtension::UploadQueueElement final
};

struct ProjectCloudExtension::CloudStatusChangedNotifier final :
Observer::Publisher<CloudStatusChangedMessage>
private Observer::Publisher<CloudStatusChangedMessage>
{
using Observer::Publisher<CloudStatusChangedMessage>::Publish;
void PublishSafe(CloudStatusChangedMessage message)
{
auto lock = std::lock_guard { OberverMutex };
Publish(message);
}

Observer::Subscription SubscribeSafe(
std::function<void(const CloudStatusChangedMessage&)> callback)
{
auto lock = std::lock_guard { OberverMutex };
return Subscribe(std::move(callback));
}

std::mutex OberverMutex;
};

ProjectCloudExtension::ProjectCloudExtension(AudacityProject& project)
Expand Down Expand Up @@ -187,8 +200,14 @@ void ProjectCloudExtension::OnSnapshotCreated(

cloudDatabase.UpdateProjectData(dbData);

mProjectId = response.Project.Id;
mSnapshotId = response.Snapshot.Id;
{
auto lock = std::lock_guard { mIdentifiersMutex };

mProjectId = response.Project.Id;
mSnapshotId = response.Snapshot.Id;

mProjectDetached = false;
}

auto lock = std::lock_guard { mUploadQueueMutex };
auto element = UnsafeFindUploadQueueElement(uploadOperation);
Expand Down Expand Up @@ -290,13 +309,17 @@ bool ProjectCloudExtension::IsSyncing() const
return mLastStatus.Status == ProjectSyncStatus::Syncing;
}

std::string_view ProjectCloudExtension::GetCloudProjectId() const
std::string ProjectCloudExtension::GetCloudProjectId() const
{
auto lock = std::lock_guard { mIdentifiersMutex };

return mProjectId;
}

std::string_view ProjectCloudExtension::GetSnapshotId() const
std::string ProjectCloudExtension::GetSnapshotId() const
{
auto lock = std::lock_guard { mIdentifiersMutex };

return mSnapshotId;
}

Expand Down Expand Up @@ -362,7 +385,9 @@ bool ProjectCloudExtension::NeedsMixdownSync() const
if (mNeedsMixdownSync)
return true;

if (!IsCloudProject())
auto lock = std::lock_guard { mIdentifiersMutex };

if (mProjectId.empty())
return false;

auto& cloudDatabase = CloudProjectsDatabase::Get();
Expand All @@ -387,7 +412,9 @@ bool ProjectCloudExtension::NeedsMixdownSync() const

void ProjectCloudExtension::MixdownSynced()
{
if (!IsCloudProject())
auto lock = std::lock_guard { mIdentifiersMutex };

if (mProjectId.empty())
return;

mNeedsMixdownSync = false;
Expand All @@ -404,8 +431,10 @@ void ProjectCloudExtension::MixdownSynced()

int64_t ProjectCloudExtension::GetSavesCount() const
{
if (!IsCloudProject())
return 0;
auto lock = std::lock_guard { mIdentifiersMutex };

if (mProjectId.empty())
return -1;

auto& cloudDatabase = CloudProjectsDatabase::Get();
auto dbData = cloudDatabase.GetProjectData(mProjectId);
Expand All @@ -418,7 +447,9 @@ int64_t ProjectCloudExtension::GetSavesCount() const

int64_t ProjectCloudExtension::GetSavesCountSinceMixdown() const
{
if (!IsCloudProject())
auto lock = std::lock_guard { mIdentifiersMutex };

if (mProjectId.empty())
return 0;

auto& cloudDatabase = CloudProjectsDatabase::Get();
Expand All @@ -434,8 +465,8 @@ Observer::Subscription ProjectCloudExtension::SubscribeStatusChanged(
std::function<void(const CloudStatusChangedMessage&)> callback,
bool onUIThread)
{
return onUIThread ? mUIStateNotifier->Subscribe(std::move(callback)) :
mAsyncStateNotifier->Subscribe(std::move(callback));
return onUIThread ? mUIStateNotifier->SubscribeSafe(std::move(callback)) :
mAsyncStateNotifier->SubscribeSafe(std::move(callback));
}

void ProjectCloudExtension::UpdateIdFromDatabase()
Expand All @@ -449,8 +480,12 @@ void ProjectCloudExtension::UpdateIdFromDatabase()
if (!projectData)
return;

mProjectId = projectData->ProjectId;
mSnapshotId = projectData->SnapshotId;
{
auto lock = std::lock_guard { mIdentifiersMutex };

mProjectId = projectData->ProjectId;
mSnapshotId = projectData->SnapshotId;
}

Publish({
projectData->SyncStatus ==
Expand Down Expand Up @@ -488,12 +523,12 @@ void ProjectCloudExtension::Publish(CloudStatusChangedMessage cloudStatus)
mLastStatus = cloudStatus;
}

mAsyncStateNotifier->Publish(cloudStatus);
mAsyncStateNotifier->PublishSafe(cloudStatus);

if (BasicUI::IsUiThread())
{
mUINotificationPending.store(false);
mUIStateNotifier->Publish(cloudStatus);
mUIStateNotifier->PublishSafe(cloudStatus);
}
else if (!mUINotificationPending.exchange(true))
{
Expand All @@ -503,7 +538,7 @@ void ProjectCloudExtension::Publish(CloudStatusChangedMessage cloudStatus)
if (mUINotificationPending.exchange(false))
{
auto lock = std::lock_guard { mStatusMutex };
mUIStateNotifier->Publish(mLastStatus);
mUIStateNotifier->PublishSafe(mLastStatus);
}
});
}
Expand Down Expand Up @@ -553,14 +588,31 @@ ProjectCloudExtension::UnsafeFindUploadQueueElement(

void ProjectCloudExtension::MarkPendingCloudSave()
{
auto lock = std::lock_guard { mIdentifiersMutex };

mPendingCloudSave = true;
}

bool ProjectCloudExtension::IsPendingCloudSave() const
{
auto lock = std::lock_guard { mIdentifiersMutex };

return mPendingCloudSave;
}

void ProjectCloudExtension::MarkProjectDetached()
{
auto lock = std::lock_guard { mIdentifiersMutex };

mProjectDetached = true;
}

bool ProjectCloudExtension::IsProjectDetached() const
{
auto lock = std::lock_guard { mIdentifiersMutex };
return mProjectDetached;
}

bool CloudStatusChangedMessage::IsSyncing() const noexcept
{
return Status == ProjectSyncStatus::Syncing;
Expand Down
9 changes: 7 additions & 2 deletions libraries/lib-cloud-audiocom/sync/ProjectCloudExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class CLOUD_AUDIOCOM_API ProjectCloudExtension final :

bool IsSyncing() const;

std::string_view GetCloudProjectId() const;
std::string_view GetSnapshotId() const;
std::string GetCloudProjectId() const;
std::string GetSnapshotId() const;

bool OnUpdateSaved(const ProjectSerializer& serializer);

Expand All @@ -114,6 +114,9 @@ class CLOUD_AUDIOCOM_API ProjectCloudExtension final :
void MarkPendingCloudSave();
bool IsPendingCloudSave() const;

void MarkProjectDetached();
bool IsProjectDetached() const;

private:
struct UploadQueueElement;
struct CloudStatusChangedNotifier;
Expand All @@ -132,6 +135,7 @@ class CLOUD_AUDIOCOM_API ProjectCloudExtension final :

AudacityProject& mProject;

mutable std::mutex mIdentifiersMutex;
std::string mProjectId;
std::string mSnapshotId;

Expand All @@ -148,5 +152,6 @@ class CLOUD_AUDIOCOM_API ProjectCloudExtension final :
bool mSuppressAutoDownload { false };
bool mPendingCloudSave { false };
bool mNeedsMixdownSync { false };
bool mProjectDetached { false };
};
} // namespace cloud::audiocom::sync
Loading

0 comments on commit ca40fd2

Please sign in to comment.