Use proper online status for user ('dnd', 'online', 'invisible', etc.) to enable...
authoralex-z <blackslayer4@gmail.com>
Mon, 9 May 2022 15:30:36 +0000 (18:30 +0300)
committeralex-z <blackslayer4@gmail.com>
Mon, 16 May 2022 07:47:05 +0000 (10:47 +0300)
Signed-off-by: alex-z <blackslayer4@gmail.com>
src/gui/accountstate.cpp
src/gui/accountstate.h
src/gui/tray/notificationhandler.cpp
src/gui/tray/notificationhandler.h
src/gui/userstatusselectormodel.cpp
src/libsync/account.cpp
src/libsync/account.h
src/libsync/networkjobs.cpp
src/libsync/networkjobs.h
src/libsync/ocsuserstatusconnector.cpp
src/libsync/userstatusconnector.h

index eacfec7181f9d725a382d2170a64bb78e5d7e99f..c3e8ef3b08bbb8f22562ade8716fd404aff5ff4c 100644 (file)
@@ -21,6 +21,7 @@
 #include "logger.h"
 #include "configfile.h"
 #include "ocsnavigationappsjob.h"
+#include "ocsuserstatusconnector.h"
 #include "pushnotifications.h"
 
 #include <QSettings>
@@ -58,6 +59,8 @@ AccountState::AccountState(AccountPtr account)
         this, &AccountState::slotCredentialsAsked);
     connect(account.data(), &Account::pushNotificationsReady,
             this, &AccountState::slotPushNotificationsReady);
+    connect(account.data(), &Account::serverUserStatusChanged, this,
+        &AccountState::slotServerUserStatusChanged);
 
     connect(this, &AccountState::isConnectedChanged, [=]{
         // Get the Apps available on the server if we're now connected.
@@ -558,6 +561,11 @@ void AccountState::slotPushNotificationsReady()
     }
 }
 
+void AccountState::slotServerUserStatusChanged()
+{
+    setDesktopNotificationsAllowed(_account->userStatusConnector()->userStatus().state() != UserStatus::OnlineStatus::DoNotDisturb);
+}
+
 void AccountState::slotNavigationAppsFetched(const QJsonDocument &reply, int statusCode)
 {
     if(_account){
index 0a489c14975b66e61621fc98faeae232aa1f0001..2e47fcf6e13aa38bdd0845217cfed6fb8a8aa74d 100644 (file)
@@ -217,6 +217,7 @@ private Q_SLOTS:
 
     void slotCheckConnection();
     void slotPushNotificationsReady();
+    void slotServerUserStatusChanged();
 
 private:
     AccountPtr _account;
index d21f0d7c62a9d6f0c1adb57179597670c46f36f2..3be035a1011364c2af351c9ca6b7f3fd73f8b75e 100644 (file)
@@ -47,8 +47,6 @@ void ServerNotificationHandler::slotFetchNotifications()
         this, &ServerNotificationHandler::slotNotificationsReceived);
     QObject::connect(_notificationJob.data(), &JsonApiJob::etagResponseHeaderReceived,
         this, &ServerNotificationHandler::slotEtagResponseHeaderReceived);
-    QObject::connect(_notificationJob.data(), &JsonApiJob::allowDesktopNotificationsChanged,
-            this, &ServerNotificationHandler::slotAllowDesktopNotificationsChanged);
     _notificationJob->setProperty(propertyAccountStateC, QVariant::fromValue<AccountState *>(_accountState));
     _notificationJob->addRawHeader("If-None-Match", _accountState->notificationsEtagResponseHeader());
     _notificationJob->start();
@@ -63,14 +61,6 @@ void ServerNotificationHandler::slotEtagResponseHeaderReceived(const QByteArray
     }
 }
 
-void ServerNotificationHandler::slotAllowDesktopNotificationsChanged(bool isAllowed)
-{
-    auto *account = qvariant_cast<AccountState *>(sender()->property(propertyAccountStateC));
-    if (account != nullptr) {
-       account->setDesktopNotificationsAllowed(isAllowed);
-    }
-}
-
 void ServerNotificationHandler::slotNotificationsReceived(const QJsonDocument &json, int statusCode)
 {
     if (statusCode != successStatusCode && statusCode != notModifiedStatusCode) {
index fc039137c5ff28796e836cf55174898556707b65..4100df7d156fe0d192532a1c1513fdb7bbc14f07 100644 (file)
@@ -25,7 +25,6 @@ public slots:
 private slots:
     void slotNotificationsReceived(const QJsonDocument &json, int statusCode);
     void slotEtagResponseHeaderReceived(const QByteArray &value, int statusCode);
-    void slotAllowDesktopNotificationsChanged(bool isAllowed);
 
 private:
     QPointer<JsonApiJob> _notificationJob;
index fea9fb61c18ffceb3005e95bb941c126dcbf3762..da0b44c1523c62fc8ceeaae2d7a5820d4061603d 100644 (file)
@@ -182,6 +182,7 @@ void UserStatusSelectorModel::setOnlineStatus(UserStatus::OnlineStatus status)
     }
 
     _userStatus.setState(status);
+    _userStatusConnector->setUserStatus(_userStatus);
     emit onlineStatusChanged();
 }
 
index bdc08c9ba5f654b8e5cb4bbcc6166b75ccf86ec4..eab683dd638305dbe571c253c94128d3fd738e68 100644 (file)
@@ -635,9 +635,12 @@ void Account::setupUserStatusConnector()
     connect(_userStatusConnector.get(), &UserStatusConnector::userStatusFetched, this, [this](const UserStatus &) {
         emit userStatusChanged();
     });
+    connect(_userStatusConnector.get(), &UserStatusConnector::serverUserStatusChanged, this, &Account::serverUserStatusChanged);
     connect(_userStatusConnector.get(), &UserStatusConnector::messageCleared, this, [this] {
         emit userStatusChanged();
     });
+
+    _userStatusConnector->fetchUserStatus();
 }
 
 QString Account::serverVersion() const
index d211d77d014a36a76c8b11de64e7303235df47f3..876fa94b9bc0a0cccdbf6f6f584f267506a2b92a 100644 (file)
@@ -322,6 +322,8 @@ signals:
 
     void userStatusChanged();
 
+    void serverUserStatusChanged();
+
     void capabilitiesChanged();
 
     void lockFileSuccess();
index 82b5e5f4bf76430665bef7e11cc86062718ebff3..335dd1eb548c2a20fe3f8023ad586001579f1cd7 100644 (file)
@@ -915,11 +915,6 @@ bool JsonApiJob::finished()
     if(reply()->rawHeaderList().contains("ETag"))
         emit etagResponseHeaderReceived(reply()->rawHeader("ETag"), statusCode);
 
-    const auto desktopNotificationsAllowed = reply()->rawHeader(QByteArray("X-Nextcloud-User-Status"));
-    if(!desktopNotificationsAllowed.isEmpty()) {
-        emit allowDesktopNotificationsChanged(desktopNotificationsAllowed == "online");
-    }
-
     QJsonParseError error;
     auto json = QJsonDocument::fromJson(jsonStr.toUtf8(), &error);
     // empty or invalid response and status code is != 304 because jsonStr is expected to be empty
index b2c0ad87662d9e87489e127bea0b8d16b3e51809..c169e056ec7de7d3d9718e934dd953eecdd61446 100644 (file)
@@ -449,12 +449,6 @@ signals:
      */
     void etagResponseHeaderReceived(const QByteArray &value, int statusCode);
 
-    /**
-     * @brief desktopNotificationStatusReceived - signal to report if notifications are allowed
-     * @param status - set desktop notifications allowed status
-     */
-    void allowDesktopNotificationsChanged(bool isAllowed);
-
 private:
     QByteArray _body;
     QUrlQuery _additionalParams;
index 19cb34b3dbca08df1347d9ad55e8db0d185f9a42..cbe65c320a2e39a8cf630bc9087beb9a96aeff21 100644 (file)
@@ -256,8 +256,14 @@ void OcsUserStatusConnector::onUserStatusFetched(const QJsonDocument &json, int
         return;
     }
 
+    const auto oldOnlineState = _userStatus.state();
     _userStatus = jsonToUserStatus(json);
+
     emit userStatusFetched(_userStatus);
+
+    if (oldOnlineState != _userStatus.state()) {
+        emit serverUserStatusChanged();
+    }
 }
 
 void OcsUserStatusConnector::startFetchPredefinedStatuses()
@@ -396,7 +402,9 @@ void OcsUserStatusConnector::setUserStatus(const UserStatus &userStatus)
         return;
     }
 
-    setUserStatusOnlineStatus(userStatus.state());
+    if (userStatus.state() != _userStatus.state()) {
+        setUserStatusOnlineStatus(userStatus.state());
+    }
     setUserStatusMessage(userStatus);
 }
 
@@ -408,6 +416,15 @@ void OcsUserStatusConnector::onUserStatusOnlineStatusSet(const QJsonDocument &js
         emit error(Error::CouldNotSetUserStatus);
         return;
     }
+
+    const auto oldOnlineState = _userStatus.state();
+    _userStatus.setState(jsonToUserStatus(json).state());
+
+    emit userStatusSet();
+
+    if (oldOnlineState != _userStatus.state()) {
+        emit serverUserStatusChanged();
+    }
 }
 
 void OcsUserStatusConnector::onUserStatusMessageSet(const QJsonDocument &json, int statusCode)
@@ -449,7 +466,10 @@ void OcsUserStatusConnector::onMessageCleared(const QJsonDocument &json, int sta
         return;
     }
 
+    const auto onlineState = _userStatus.state();
+
     _userStatus = {};
+    _userStatus.setState(onlineState);
     emit messageCleared();
 }
 }
index d5593fe9e50cf96e9c9775bdccd605f3bb6375b9..08de548eb10389327d559199c9fb12c21a0e2b43 100644 (file)
@@ -129,6 +129,7 @@ signals:
     void userStatusFetched(const UserStatus &userStatus);
     void predefinedStatusesFetched(const std::vector<UserStatus> &statuses);
     void userStatusSet();
+    void serverUserStatusChanged();
     void messageCleared();
     void error(Error error);
 };