Filter out sharees that already have active shares
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 17 Oct 2022 19:49:12 +0000 (21:49 +0200)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 31 Oct 2022 17:06:09 +0000 (18:06 +0100)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/filedetails/ShareView.qml
src/gui/filedetails/ShareeSearchField.qml
src/gui/filedetails/shareemodel.cpp
src/gui/filedetails/shareemodel.h
src/gui/filedetails/sharemodel.cpp
src/gui/filedetails/sharemodel.h

index e28019c072b375c48cb3b488b762dc1078094c78..73e1636f3a0f650a407000caf0607d11a76ac176 100644 (file)
@@ -158,6 +158,7 @@ ColumnLayout {
 
         accountState: root.accountState
         shareItemIsFolder: root.fileDetails && root.fileDetails.isFolder
+        shareeBlocklist: root.shareModel.sharees
 
         onShareeSelected: {
             root.waitingForSharesToChange = true;
index eedf20daa9adfa888a575789627e751bd4bbf126..286d273918f29a3222317a9f0699ed7a24779411 100644 (file)
@@ -28,10 +28,12 @@ TextField {
 
     property var accountState: ({})
     property bool shareItemIsFolder: false
+    property var shareeBlocklist: ({})
     property ShareeModel shareeModel: ShareeModel {
         accountState: root.accountState
         shareItemIsFolder: root.shareItemIsFolder
         searchString: root.text
+        shareeBlocklist: root.shareeBlocklist
     }
 
     readonly property int horizontalPaddingOffset: Style.trayHorizontalMargin
index ea59204f1a93d4c2c101153881756c4c75c2f85c..2fb9be8f6b2f84eabefe0b8bd07e9c6e994a0f67 100644 (file)
@@ -145,6 +145,26 @@ void ShareeModel::setLookupMode(const ShareeModel::LookupMode lookupMode)
     Q_EMIT lookupModeChanged();
 }
 
+QVariantList ShareeModel::shareeBlocklist() const
+{
+    QVariantList returnSharees;
+    for (const auto &sharee : _shareeBlocklist) {
+        returnSharees.append(QVariant::fromValue(sharee));
+    }
+    return returnSharees;
+}
+
+void ShareeModel::setShareeBlocklist(const QVariantList shareeBlocklist)
+{
+    _shareeBlocklist.clear();
+    for (const auto &sharee : shareeBlocklist) {
+        _shareeBlocklist.append(sharee.value<ShareePtr>());
+    }
+    Q_EMIT shareeBlocklistChanged();
+
+    filterSharees();
+}
+
 // ------------------------- Internal data methods ------------------------- //
 
 void ShareeModel::fetch()
@@ -176,7 +196,7 @@ void ShareeModel::shareesFetched(const QJsonDocument &reply)
     _fetchOngoing = false;
     Q_EMIT fetchOngoingChanged();
 
-    qCInfo(lcShareeModel) << "SearchString: " << _searchString << "resulted in reply: " << reply;
+    qCInfo(lcShareeModel) << "Reply: " << reply;
 
     QVector<ShareePtr> newSharees;
 
@@ -190,15 +210,14 @@ void ShareeModel::shareesFetched(const QJsonDocument &reply)
                 const auto shareeJsonObject = sharee.toObject();
                 const auto parsedSharee = parseSharee(shareeJsonObject);
 
-                // Filter sharees that we have already shared with
-                const auto shareeInBlacklistIt = std::find_if(_shareeBlacklist.cbegin(),
-                                                              _shareeBlacklist.cend(),
+                const auto shareeInBlacklistIt = std::find_if(_shareeBlocklist.cbegin(),
+                                                              _shareeBlocklist.cend(),
                                                               [&parsedSharee](const ShareePtr &blacklistSharee) {
                     return parsedSharee->type() == blacklistSharee->type() &&
                            parsedSharee->shareWith() == blacklistSharee->shareWith();
                 });
 
-                if (shareeInBlacklistIt != _shareeBlacklist.cend()) {
+                if (shareeInBlacklistIt != _shareeBlocklist.cend()) {
                     continue;
                 }
 
@@ -232,4 +251,28 @@ ShareePtr ShareeModel::parseSharee(const QJsonObject &data) const
     return ShareePtr(new Sharee(shareWith, displayName, type));
 }
 
+void ShareeModel::filterSharees()
+{
+    auto it = _sharees.begin();
+
+    while (it != _sharees.end()) {
+        const auto sharee = *it;
+        const auto shareeInBlacklistIt = std::find_if(_shareeBlocklist.cbegin(), _shareeBlocklist.cend(), [&sharee](const ShareePtr &blacklistSharee) {
+            return sharee->type() == blacklistSharee->type() &&
+                   sharee->shareWith() == blacklistSharee->shareWith();
+        });
+
+        if (shareeInBlacklistIt != _shareeBlocklist.end()) {
+            const auto row = it - _sharees.begin();
+            beginRemoveRows({}, row, row);
+            it = _sharees.erase(it);
+            endRemoveRows();
+        } else {
+            ++it;
+        }
+    }
+
+    Q_EMIT shareesReady();
+}
+
 }
index 2ae7846ffa83af5bb19dc64a8de50fe35d546538..26a0b533ef1cc851fd8a780f69e332b426b5d306 100644 (file)
@@ -33,6 +33,7 @@ class ShareeModel : public QAbstractListModel
     Q_PROPERTY(QString searchString READ searchString WRITE setSearchString NOTIFY searchStringChanged)
     Q_PROPERTY(bool fetchOngoing READ fetchOngoing NOTIFY fetchOngoingChanged)
     Q_PROPERTY(LookupMode lookupMode READ lookupMode WRITE setLookupMode NOTIFY lookupModeChanged)
+    Q_PROPERTY(QVariantList shareeBlocklist READ shareeBlocklist WRITE setShareeBlocklist NOTIFY shareeBlocklistChanged)
 
 public:
     enum class LookupMode {
@@ -60,6 +61,7 @@ public:
     [[nodiscard]] QString searchString() const;
     [[nodiscard]] bool fetchOngoing() const;
     [[nodiscard]] LookupMode lookupMode() const;
+    [[nodiscard]] QVariantList shareeBlocklist() const;
 
 signals:
     void accountStateChanged();
@@ -67,20 +69,23 @@ signals:
     void searchStringChanged();
     void fetchOngoingChanged();
     void lookupModeChanged();
+    void shareeBlocklistChanged();
 
     void shareesReady();
-    void displayErrorMessage(int code, const QString &);
+    void displayErrorMessage(const int code, const QString &message);
 
 public slots:
     void setAccountState(AccountState *accountState);
     void setShareItemIsFolder(const bool shareItemIsFolder);
     void setSearchString(const QString &searchString);
     void setLookupMode(const LookupMode lookupMode);
+    void setShareeBlocklist(const QVariantList shareeBlocklist);
 
     void fetch();
 
 private slots:
-     void shareesFetched(const QJsonDocument &reply);
+    void shareesFetched(const QJsonDocument &reply);
+    void filterSharees();
 
 private:
     [[nodiscard]] ShareePtr parseSharee(const QJsonObject &data) const;
@@ -94,7 +99,7 @@ private:
     LookupMode _lookupMode = LookupMode::LocalSearch;
 
     QVector<ShareePtr> _sharees;
-    QVector<ShareePtr> _shareeBlacklist;
+    QVector<ShareePtr> _shareeBlocklist;
 };
 
 }
index cb6d97b3ace0ec9f7bf31aeb2b1444eef809a8c1..19e603b236849ef7f27ec2dd24b14fa2538dd81c 100644 (file)
@@ -194,9 +194,11 @@ void ShareModel::resetData()
     _shares.clear();
     _fetchOngoing = false;
     _hasInitialShareFetchCompleted = false;
+    _sharees.clear();
 
     Q_EMIT fetchOngoingChanged();
     Q_EMIT hasInitialShareFetchCompletedChanged();
+    Q_EMIT shareesChanged();
 
     endResetModel();
 }
@@ -403,6 +405,8 @@ void ShareModel::slotAddShare(const SharePtr &share)
         _shares.append(share);
         endInsertRows();
 
+        slotAddSharee(share->getShareWith());
+
         shareModelIndex = index(shareIndex);
     }
 
@@ -454,6 +458,10 @@ void ShareModel::slotRemoveShareWithId(const QString &shareId)
         return;
     }
 
+    const auto share = shareIndex.data(ShareModel::ShareRole).value<SharePtr>();
+    const auto sharee = share->getShareWith();
+    slotRemoveSharee(sharee);
+
     beginRemoveRows({}, shareIndex.row(), shareIndex.row());
     _shares.removeAt(shareIndex.row());
     endRemoveRows();
@@ -469,6 +477,22 @@ void ShareModel::slotServerError(const int code, const QString &message)
     Q_EMIT serverError(code, message);
 }
 
+void ShareModel::slotAddSharee(const ShareePtr &sharee)
+{
+    if(!sharee) {
+        return;
+    }
+
+    _sharees.append(sharee);
+    Q_EMIT shareesChanged();
+}
+
+void ShareModel::slotRemoveSharee(const ShareePtr &sharee)
+{
+    _sharees.removeAll(sharee);
+    Q_EMIT shareesChanged();
+}
+
 QString ShareModel::displayStringForShare(const SharePtr &share) const
 {
     if (const auto linkShare = share.objectCast<LinkShare>()) {
@@ -991,4 +1015,13 @@ bool ShareModel::canShare() const
     return _maxSharingPermissions & SharePermissionShare;
 }
 
+QVariantList ShareModel::sharees() const
+{
+    QVariantList returnSharees;
+    for (const auto &sharee : _sharees) {
+        returnSharees.append(QVariant::fromValue(sharee));
+    }
+    return returnSharees;
+}
+
 } // namespace OCC
index ca431dc32b9b6f88eb22b87d75d7f30a8ab60593..abb94978bcde045d061015b8180c6c47499215f5 100644 (file)
@@ -35,6 +35,7 @@ class ShareModel : public QAbstractListModel
     Q_PROPERTY(bool canShare READ canShare NOTIFY sharePermissionsChanged)
     Q_PROPERTY(bool fetchOngoing READ fetchOngoing NOTIFY fetchOngoingChanged)
     Q_PROPERTY(bool hasInitialShareFetchCompleted READ hasInitialShareFetchCompleted NOTIFY hasInitialShareFetchCompletedChanged)
+    Q_PROPERTY(QVariantList sharees READ sharees NOTIFY shareesChanged)
 
 public:
     enum Roles {
@@ -94,6 +95,8 @@ public:
     [[nodiscard]] bool fetchOngoing() const;
     [[nodiscard]] bool hasInitialShareFetchCompleted() const;
 
+    [[nodiscard]] QVariantList sharees() const;
+
 signals:
     void localPathChanged();
     void accountStateChanged();
@@ -105,6 +108,7 @@ signals:
     void lockExpireStringChanged();
     void fetchOngoingChanged();
     void hasInitialShareFetchCompletedChanged();
+    void shareesChanged();
 
     void serverError(const int code, const QString &message);
     void passwordSetError(const QString &shareId, const int code, const QString &message);
@@ -159,6 +163,8 @@ private slots:
     void slotAddShare(const SharePtr &share);
     void slotRemoveShareWithId(const QString &shareId);
     void slotSharesFetched(const QList<SharePtr> &shares);
+    void slotAddSharee(const ShareePtr &sharee);
+    void slotRemoveSharee(const ShareePtr &sharee);
 
     void slotSharePermissionsSet(const QString &shareId);
     void slotSharePasswordSet(const QString &shareId);
@@ -201,6 +207,7 @@ private:
     QVector<SharePtr> _shares;
     QHash<QString, QPersistentModelIndex> _shareIdIndexHash;
     QHash<QString, QString> _shareIdRecentlySetPasswords;
+    QVector<ShareePtr> _sharees;
 };
 
 } // namespace OCC