From 06d904aa7c2e8de3b598328383debdf7e464d8cb Mon Sep 17 00:00:00 2001 From: Jyrki Gadinger Date: Fri, 10 Jan 2025 13:53:17 +0100 Subject: [PATCH] fetch "shared with me" information separately if the parameter `shared_with_me` is set to `"true"` on the shares API call the response will only include that and not return any other sharing information. therefore let's fetch it inside another job (this is also how the web client does it). Signed-off-by: Jyrki Gadinger --- src/gui/filedetails/sharemodel.cpp | 80 +++++++++++++++++++----------- src/gui/filedetails/sharemodel.h | 1 + src/gui/ocssharejob.cpp | 8 +-- src/gui/ocssharejob.h | 3 +- src/gui/sharemanager.cpp | 28 +++++++++-- src/gui/sharemanager.h | 17 ++++++- 6 files changed, 97 insertions(+), 40 deletions(-) diff --git a/src/gui/filedetails/sharemodel.cpp b/src/gui/filedetails/sharemodel.cpp index 73441a0627b29..879cf9639c4b3 100644 --- a/src/gui/filedetails/sharemodel.cpp +++ b/src/gui/filedetails/sharemodel.cpp @@ -363,6 +363,7 @@ void ShareModel::initShareManager() if (_manager.isNull() && sharingPossible) { _manager.reset(new ShareManager(_accountState->account(), this)); connect(_manager.data(), &ShareManager::sharesFetched, this, &ShareModel::slotSharesFetched); + connect(_manager.data(), &ShareManager::sharedWithMeFetched, this, &ShareModel::slotSharedWithMeFetched); connect(_manager.data(), &ShareManager::shareCreated, this, [&] { _manager->fetchShares(_sharePath); }); @@ -375,6 +376,7 @@ void ShareModel::initShareManager() }); _manager->fetchShares(_sharePath); + _manager->fetchSharedWithMe(_sharePath); } } @@ -489,38 +491,13 @@ void ShareModel::slotSharesFetched(const QList &shares) qCInfo(lcSharing) << "Fetched" << shares.count() << "shares"; for (const auto &share : shares) { - if (share.isNull()) { + if (share.isNull() || + share->account().isNull() || + share->getUidOwner() != share->account()->davUser()) { continue; - } else if (const auto selfUserId = share->account()->davUser(); share->getUidOwner() != selfUserId) { - _displayShareOwner = true; - Q_EMIT displayShareOwnerChanged(); - _shareOwnerDisplayName = share->getOwnerDisplayName(); - Q_EMIT shareOwnerDisplayNameChanged(); - _shareOwnerAvatar = "image://avatars/user-id=" - + share->getUidOwner() - + "/local-account:" - + share->account()->displayName(); - Q_EMIT shareOwnerAvatarChanged(); - - if (share->getShareType() == Share::TypeUser && - share->getShareWith() && - share->getShareWith()->shareWith() == selfUserId) - { - const auto userShare = share.objectCast(); - const auto expireDate = userShare->getExpireDate(); - const auto daysToExpire = QDate::currentDate().daysTo(expireDate); - _sharedWithMeExpires = expireDate.isValid(); - Q_EMIT sharedWithMeExpiresChanged(); - _sharedWithMeRemainingTimeString = daysToExpire > 1 - ? tr("%1 days").arg(daysToExpire) - : daysToExpire > 0 - ? tr("1 day") - : tr("Today"); - Q_EMIT sharedWithMeRemainingTimeStringChanged(); - } - } else { - slotAddShare(share); } + + slotAddShare(share); } // Perform forward pass on shares and check for duplicate display names; store these indeces so @@ -561,6 +538,49 @@ void ShareModel::slotSharesFetched(const QList &shares) handleLinkShare(); } +void ShareModel::slotSharedWithMeFetched(const QList &shares) +{ + qCInfo(lcSharing) << "Fetched" << shares.count() << "shares that have been shared_with_me"; + + for (const auto &share : shares) { + if (share.isNull()) { + continue; + } + + const auto selfUserId = share->account()->davUser(); + if (share->getUidOwner() == selfUserId) { + continue; + } + + _displayShareOwner = true; + Q_EMIT displayShareOwnerChanged(); + _shareOwnerDisplayName = share->getOwnerDisplayName(); + Q_EMIT shareOwnerDisplayNameChanged(); + _shareOwnerAvatar = "image://avatars/user-id=" + + share->getUidOwner() + + "/local-account:" + + share->account()->displayName(); + Q_EMIT shareOwnerAvatarChanged(); + + if (share->getShareType() == Share::TypeUser && + share->getShareWith() && + share->getShareWith()->shareWith() == selfUserId) + { + const auto userShare = share.objectCast(); + const auto expireDate = userShare->getExpireDate(); + const auto daysToExpire = QDate::currentDate().daysTo(expireDate); + _sharedWithMeExpires = expireDate.isValid(); + Q_EMIT sharedWithMeExpiresChanged(); + _sharedWithMeRemainingTimeString = daysToExpire > 1 + ? tr("%1 days").arg(daysToExpire) + : daysToExpire > 0 + ? tr("1 day") + : tr("Today"); + Q_EMIT sharedWithMeRemainingTimeStringChanged(); + } + } +} + void ShareModel::setupInternalLinkShare() { if (!_accountState || diff --git a/src/gui/filedetails/sharemodel.h b/src/gui/filedetails/sharemodel.h index 5357a9366d6f3..b8a944eebb29d 100644 --- a/src/gui/filedetails/sharemodel.h +++ b/src/gui/filedetails/sharemodel.h @@ -220,6 +220,7 @@ private slots: void slotAddShare(const OCC::SharePtr &share); void slotRemoveShareWithId(const QString &shareId); void slotSharesFetched(const QList &shares); + void slotSharedWithMeFetched(const QList &shares); void slotAddSharee(const OCC::ShareePtr &sharee); void slotRemoveSharee(const OCC::ShareePtr &sharee); diff --git a/src/gui/ocssharejob.cpp b/src/gui/ocssharejob.cpp index 1bc81dfd8d732..62a00faaf6410 100644 --- a/src/gui/ocssharejob.cpp +++ b/src/gui/ocssharejob.cpp @@ -34,7 +34,6 @@ void OcsShareJob::getShares(const QString &path, const QMap &p addParam(QString::fromLatin1("path"), path); addParam(QString::fromLatin1("reshares"), QString("true")); - addParam(QString::fromLatin1("shared_with_me"), QString("true")); for (auto it = std::cbegin(params); it != std::cend(params); ++it) { addParam(it.key(), it.value()); @@ -208,10 +207,13 @@ void OcsShareJob::createShare(const QString &path, start(); } -void OcsShareJob::getSharedWithMe() +void OcsShareJob::getSharedWithMe(const QString &path) { setVerb("GET"); - addParam(QLatin1String("shared_with_me"), QLatin1String("true")); + + addParam(QString::fromLatin1("path"), path); + addParam(QString::fromLatin1("shared_with_me"), QString("true")); + start(); } diff --git a/src/gui/ocssharejob.h b/src/gui/ocssharejob.h index 2abaf80a40d48..71e0883a7d63a 100644 --- a/src/gui/ocssharejob.h +++ b/src/gui/ocssharejob.h @@ -135,8 +135,9 @@ class OcsShareJob : public OcsJob /** * Returns information on the items shared with the current user. + * @param path Path to request shares for (default all shares) */ - void getSharedWithMe(); + void getSharedWithMe(const QString &path = ""); static const QString _pathForSharesRequest; diff --git a/src/gui/sharemanager.cpp b/src/gui/sharemanager.cpp index d025d629f87a6..fa36a4e72b680 100644 --- a/src/gui/sharemanager.cpp +++ b/src/gui/sharemanager.cpp @@ -536,13 +536,21 @@ void ShareManager::slotShareCreated(const QJsonDocument &reply) void ShareManager::fetchShares(const QString &path) { - auto *job = new OcsShareJob(_account); + auto job = new OcsShareJob(_account); connect(job, &OcsShareJob::shareJobFinished, this, &ShareManager::slotSharesFetched); connect(job, &OcsJob::ocsError, this, &ShareManager::slotOcsError); job->getShares(path); } -void ShareManager::slotSharesFetched(const QJsonDocument &reply) +void ShareManager::fetchSharedWithMe(const QString &path) +{ + auto sharedWithMeJob = new OcsShareJob(_account); + connect(sharedWithMeJob, &OcsShareJob::shareJobFinished, this, &ShareManager::slotSharedWithMeFetched); + connect(sharedWithMeJob, &OcsJob::ocsError, this, &ShareManager::slotOcsError); + sharedWithMeJob->getSharedWithMe(path); +} + +const QList ShareManager::parseShares(const QJsonDocument &reply) const { qDebug() << reply; auto tmpShares = reply.object().value("ocs").toObject().value("data").toArray(); @@ -570,10 +578,22 @@ void ShareManager::slotSharesFetched(const QJsonDocument &reply) } qCDebug(lcSharing) << "Sending " << shares.count() << "shares"; + return shares; +} + +void ShareManager::slotSharesFetched(const QJsonDocument &reply) +{ + auto shares = parseShares(reply); emit sharesFetched(shares); } -QSharedPointer ShareManager::parseUserGroupShare(const QJsonObject &data) +void ShareManager::slotSharedWithMeFetched(const QJsonDocument &reply) +{ + auto shares = parseShares(reply); + emit sharedWithMeFetched(shares); +} + +QSharedPointer ShareManager::parseUserGroupShare(const QJsonObject &data) const { ShareePtr sharee(new Sharee(data.value("share_with").toString(), data.value("share_with_displayname").toString(), @@ -602,7 +622,7 @@ QSharedPointer ShareManager::parseUserGroupShare(const QJsonObje note)); } -QSharedPointer ShareManager::parseLinkShare(const QJsonObject &data) +QSharedPointer ShareManager::parseLinkShare(const QJsonObject &data) const { QUrl url; diff --git a/src/gui/sharemanager.h b/src/gui/sharemanager.h index e68620e315500..1cb51df467f30 100644 --- a/src/gui/sharemanager.h +++ b/src/gui/sharemanager.h @@ -440,10 +440,21 @@ class ShareManager : public QObject */ void fetchShares(const QString &path); + /** + * Fetch shares with the current user for path + * + * @param path The path to get the shares for relative to the users folder on the server + * + * On success the sharedWithMeFetched signal is emitted + * In case of a server error the serverError signal is emitted + */ + void fetchSharedWithMe(const QString &path); + signals: void shareCreated(const OCC::SharePtr &share); void linkShareCreated(const QSharedPointer &share); void sharesFetched(const QList &shares); + void sharedWithMeFetched(const QList &shares); void serverError(int code, const QString &message); /** Emitted when creating a link share with password fails. @@ -456,15 +467,17 @@ class ShareManager : public QObject private slots: void slotSharesFetched(const QJsonDocument &reply); + void slotSharedWithMeFetched(const QJsonDocument &reply); void slotLinkShareCreated(const QJsonDocument &reply); void slotShareCreated(const QJsonDocument &reply); void slotOcsError(int statusCode, const QString &message); void slotCreateE2eeShareJobFinised(int statusCode, const QString &message); private: - QSharedPointer parseLinkShare(const QJsonObject &data); - QSharedPointer parseUserGroupShare(const QJsonObject &data); + QSharedPointer parseLinkShare(const QJsonObject &data) const; + QSharedPointer parseUserGroupShare(const QJsonObject &data) const; SharePtr parseShare(const QJsonObject &data) const; + const QList parseShares(const QJsonDocument &reply) const; AccountPtr _account; };