[DeckShare] Extract the share-creation response handling into DeckShareUtils

This commit is contained in:
Lukas Brübach 2026-09-18 23:56:40 +02:00
parent 389dc8ac17
commit 6bf64ea712
5 changed files with 45 additions and 35 deletions

View file

@ -4,6 +4,8 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QTimeZone> #include <QTimeZone>
#include <libcockatrice/network/client/abstract/abstract_client.h> #include <libcockatrice/network/client/abstract/abstract_client.h>
#include <libcockatrice/protocol/pb/response.pb.h>
#include <libcockatrice/protocol/pb/response_deck_share_create.pb.h>
namespace DeckShareUtils namespace DeckShareUtils
{ {
@ -25,4 +27,21 @@ QString formatShareExpiry(const QDateTime &expiry)
return expiry.toLocalTime().toString(); return expiry.toLocalTime().toString();
} }
ShareResponse handleShareResponse(const AbstractClient *client, const Response &response)
{
const Response_DeckShareCreate &resp = response.GetExtension(Response_DeckShareCreate::ext);
const QString token = QString::fromStdString(resp.token());
const QString link = buildShareLink(client, token);
copyShareLinkToClipboard(link);
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), QTimeZone::UTC);
#else
const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), Qt::UTC);
#endif
return {link, expiry};
}
} // namespace DeckShareUtils } // namespace DeckShareUtils

View file

@ -11,6 +11,7 @@
#include <QString> #include <QString>
class AbstractClient; class AbstractClient;
class Response;
/** /**
* @brief Shared helpers for creating temporary deck shares. * @brief Shared helpers for creating temporary deck shares.
@ -18,6 +19,15 @@ class AbstractClient;
namespace DeckShareUtils namespace DeckShareUtils
{ {
/**
* @brief The outcome of a successful share-create response.
*/
struct ShareResponse
{
QString link; ///< The share link that was copied to the clipboard.
QDateTime expiry; ///< When the share expires (UTC).
};
/** /**
* @brief Builds the cockatrice:// link for a freshly created deck share. * @brief Builds the cockatrice:// link for a freshly created deck share.
* @param client Used to embed the target server's hostname and port. * @param client Used to embed the target server's hostname and port.
@ -36,6 +46,14 @@ QString copyShareLinkToClipboard(const QString &link);
*/ */
QString formatShareExpiry(const QDateTime &expiry); QString formatShareExpiry(const QDateTime &expiry);
/**
* @brief Handles a successful Response_DeckShareCreate: builds the share link,
* copies it to the clipboard, and derives the share expiry.
* @param client Used to embed the target server's hostname and port.
* @param response The successful response carrying the share token and expiry.
*/
ShareResponse handleShareResponse(const AbstractClient *client, const Response &response);
} // namespace DeckShareUtils } // namespace DeckShareUtils
#endif // DECK_SHARE_UTILS_H #endif // DECK_SHARE_UTILS_H

View file

@ -70,20 +70,11 @@ void DlgShareDeck::shareFinished(const Response &response, const CommandContaine
return; return;
} }
const Response_DeckShareCreate &resp = response.GetExtension(Response_DeckShareCreate::ext); const DeckShareUtils::ShareResponse share = DeckShareUtils::handleShareResponse(client, response);
const QString token = QString::fromStdString(resp.token());
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), QTimeZone::UTC);
#else
const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), Qt::UTC);
#endif
const QString link = DeckShareUtils::buildShareLink(client, token);
DeckShareUtils::copyShareLinkToClipboard(link);
QMessageBox::information(this, tr("Share deck"), QMessageBox::information(this, tr("Share deck"),
tr("Share link created and copied to the clipboard:\n\n%1\n\n" tr("Share link created and copied to the clipboard:\n\n%1\n\n"
"The share expires on %2.") "The share expires on %2.")
.arg(link, DeckShareUtils::formatShareExpiry(expiry))); .arg(share.link, DeckShareUtils::formatShareExpiry(share.expiry)));
accept(); accept();
} }

View file

@ -778,19 +778,10 @@ void TabDeckStorage::shareFromTreeFinished(const Response &response, const Comma
true); true);
return; return;
} }
const Response_DeckShareCreate &resp = response.GetExtension(Response_DeckShareCreate::ext); const DeckShareUtils::ShareResponse share = DeckShareUtils::handleShareResponse(client, response);
const QString token = QString::fromStdString(resp.token());
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), QTimeZone::UTC);
#else
const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), Qt::UTC);
#endif
const QString link = DeckShareUtils::buildShareLink(client, token); showShareNotice(tr("Share link copied to the clipboard.\nExpires on %1.")
DeckShareUtils::copyShareLinkToClipboard(link); .arg(DeckShareUtils::formatShareExpiry(share.expiry)));
showShareNotice(
tr("Share link copied to the clipboard.\nExpires on %1.").arg(DeckShareUtils::formatShareExpiry(expiry)));
setShareModeEnabled(false); setShareModeEnabled(false);
} }

View file

@ -166,19 +166,10 @@ void TabDeckStorageVisual::shareFinished(const Response &response, const Command
return; return;
} }
const Response_DeckShareCreate &resp = response.GetExtension(Response_DeckShareCreate::ext); const DeckShareUtils::ShareResponse share = DeckShareUtils::handleShareResponse(client, response);
const QString token = QString::fromStdString(resp.token());
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), QTimeZone::UTC);
#else
const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), Qt::UTC);
#endif
const QString link = DeckShareUtils::buildShareLink(client, token); showShareNotice(tr("Share link copied to the clipboard.\nExpires on %1.")
DeckShareUtils::copyShareLinkToClipboard(link); .arg(DeckShareUtils::formatShareExpiry(share.expiry)));
showShareNotice(
tr("Share link copied to the clipboard.\nExpires on %1.").arg(DeckShareUtils::formatShareExpiry(expiry)));
exitShareMode(); exitShareMode();
} }