Skip to content

Commit

Permalink
fetch "shared with me" information separately
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nilsding committed Jan 13, 2025
1 parent 9d860d0 commit 06d904a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 40 deletions.
80 changes: 50 additions & 30 deletions src/gui/filedetails/sharemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -375,6 +376,7 @@ void ShareModel::initShareManager()
});

_manager->fetchShares(_sharePath);
_manager->fetchSharedWithMe(_sharePath);
}
}

Expand Down Expand Up @@ -489,38 +491,13 @@ void ShareModel::slotSharesFetched(const QList<SharePtr> &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<UserGroupShare>();
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
Expand Down Expand Up @@ -561,6 +538,49 @@ void ShareModel::slotSharesFetched(const QList<SharePtr> &shares)
handleLinkShare();
}

void ShareModel::slotSharedWithMeFetched(const QList<OCC::SharePtr> &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<UserGroupShare>();
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()

Check warning on line 584 in src/gui/filedetails/sharemodel.cpp

View workflow job for this annotation

GitHub Actions / build

src/gui/filedetails/sharemodel.cpp:584:18 [readability-convert-member-functions-to-static]

method 'setupInternalLinkShare' can be made static
{
if (!_accountState ||
Expand Down
1 change: 1 addition & 0 deletions src/gui/filedetails/sharemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ private slots:
void slotAddShare(const OCC::SharePtr &share);
void slotRemoveShareWithId(const QString &shareId);
void slotSharesFetched(const QList<OCC::SharePtr> &shares);
void slotSharedWithMeFetched(const QList<OCC::SharePtr> &shares);
void slotAddSharee(const OCC::ShareePtr &sharee);
void slotRemoveSharee(const OCC::ShareePtr &sharee);

Expand Down
8 changes: 5 additions & 3 deletions src/gui/ocssharejob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void OcsShareJob::getShares(const QString &path, const QMap<QString, QString> &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());
Expand Down Expand Up @@ -208,10 +207,13 @@ void OcsShareJob::createShare(const QString &path,
start();
}

void OcsShareJob::getSharedWithMe()
void OcsShareJob::getSharedWithMe(const QString &path)

Check warning on line 210 in src/gui/ocssharejob.cpp

View workflow job for this annotation

GitHub Actions / build

src/gui/ocssharejob.cpp:210:19 [readability-convert-member-functions-to-static]

method 'getSharedWithMe' can be made static
{
setVerb("GET");
addParam(QLatin1String("shared_with_me"), QLatin1String("true"));

addParam(QString::fromLatin1("path"), path);
addParam(QString::fromLatin1("shared_with_me"), QString("true"));

start();
}

Expand Down
3 changes: 2 additions & 1 deletion src/gui/ocssharejob.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
28 changes: 24 additions & 4 deletions src/gui/sharemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharePtr> ShareManager::parseShares(const QJsonDocument &reply) const
{
qDebug() << reply;
auto tmpShares = reply.object().value("ocs").toObject().value("data").toArray();
Expand Down Expand Up @@ -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<UserGroupShare> ShareManager::parseUserGroupShare(const QJsonObject &data)
void ShareManager::slotSharedWithMeFetched(const QJsonDocument &reply)
{
auto shares = parseShares(reply);
emit sharedWithMeFetched(shares);
}

QSharedPointer<UserGroupShare> ShareManager::parseUserGroupShare(const QJsonObject &data) const
{
ShareePtr sharee(new Sharee(data.value("share_with").toString(),
data.value("share_with_displayname").toString(),
Expand Down Expand Up @@ -602,7 +622,7 @@ QSharedPointer<UserGroupShare> ShareManager::parseUserGroupShare(const QJsonObje
note));
}

QSharedPointer<LinkShare> ShareManager::parseLinkShare(const QJsonObject &data)
QSharedPointer<LinkShare> ShareManager::parseLinkShare(const QJsonObject &data) const
{
QUrl url;

Expand Down
17 changes: 15 additions & 2 deletions src/gui/sharemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<OCC::LinkShare> &share);
void sharesFetched(const QList<OCC::SharePtr> &shares);
void sharedWithMeFetched(const QList<OCC::SharePtr> &shares);
void serverError(int code, const QString &message);

/** Emitted when creating a link share with password fails.
Expand All @@ -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<LinkShare> parseLinkShare(const QJsonObject &data);
QSharedPointer<UserGroupShare> parseUserGroupShare(const QJsonObject &data);
QSharedPointer<LinkShare> parseLinkShare(const QJsonObject &data) const;
QSharedPointer<UserGroupShare> parseUserGroupShare(const QJsonObject &data) const;
SharePtr parseShare(const QJsonObject &data) const;
const QList<OCC::SharePtr> parseShares(const QJsonDocument &reply) const;

AccountPtr _account;
};
Expand Down

0 comments on commit 06d904a

Please sign in to comment.