Skip to content

Commit

Permalink
Use proper online status for user ('dnd', 'online', 'invisible', etc.…
Browse files Browse the repository at this point in the history
…) to enable or disable desktop notifications.

Signed-off-by: alex-z <[email protected]>
  • Loading branch information
allexzander committed May 13, 2022
1 parent 2f75a11 commit c7b63a0
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 23 deletions.
8 changes: 8 additions & 0 deletions src/gui/accountstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "logger.h"
#include "configfile.h"
#include "ocsnavigationappsjob.h"
#include "ocsuserstatusconnector.h"
#include "pushnotifications.h"

#include <QSettings>
Expand Down Expand Up @@ -58,6 +59,8 @@ AccountState::AccountState(AccountPtr account)
this, &AccountState::slotCredentialsAsked);
connect(account.data(), &Account::pushNotificationsReady,
this, &AccountState::slotPushNotificationsReady);
connect(account.data(), &Account::userOnlineStatusChanged, this,
&AccountState::slotUserOnlineStatusChanged);

connect(this, &AccountState::isConnectedChanged, [=]{
// Get the Apps available on the server if we're now connected.
Expand Down Expand Up @@ -558,6 +561,11 @@ void AccountState::slotPushNotificationsReady()
}
}

void AccountState::slotUserOnlineStatusChanged()
{
setDesktopNotificationsAllowed(_account->userStatusConnector()->userStatus().state() != UserStatus::OnlineStatus::DoNotDisturb);
}

void AccountState::slotNavigationAppsFetched(const QJsonDocument &reply, int statusCode)
{
if(_account){
Expand Down
1 change: 1 addition & 0 deletions src/gui/accountstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ private Q_SLOTS:

void slotCheckConnection();
void slotPushNotificationsReady();
void slotUserOnlineStatusChanged();

private:
AccountPtr _account;
Expand Down
10 changes: 0 additions & 10 deletions src/gui/tray/notificationhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/gui/tray/notificationhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/gui/userstatusselectormodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ void UserStatusSelectorModel::setOnlineStatus(UserStatus::OnlineStatus status)
}

_userStatus.setState(status);
_userStatusConnector->setUserStatus(_userStatus);
emit onlineStatusChanged();
}

Expand Down
3 changes: 3 additions & 0 deletions src/libsync/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,12 @@ void Account::setupUserStatusConnector()
connect(_userStatusConnector.get(), &UserStatusConnector::userStatusFetched, this, [this](const UserStatus &) {
emit userStatusChanged();
});
connect(_userStatusConnector.get(), &UserStatusConnector::userOnlineStatusChanged, this, &Account::userOnlineStatusChanged);
connect(_userStatusConnector.get(), &UserStatusConnector::messageCleared, this, [this] {
emit userStatusChanged();
});

_userStatusConnector->fetchUserStatus();
}

QString Account::serverVersion() const
Expand Down
2 changes: 2 additions & 0 deletions src/libsync/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ public slots:

void userStatusChanged();

void userOnlineStatusChanged();

void capabilitiesChanged();

void lockFileSuccess();
Expand Down
5 changes: 0 additions & 5 deletions src/libsync/networkjobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions src/libsync/networkjobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,6 @@ public slots:
*/
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;
Expand Down
22 changes: 21 additions & 1 deletion src/libsync/ocsuserstatusconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 userOnlineStatusChanged();
}
}

void OcsUserStatusConnector::startFetchPredefinedStatuses()
Expand Down Expand Up @@ -396,7 +402,9 @@ void OcsUserStatusConnector::setUserStatus(const UserStatus &userStatus)
return;
}

setUserStatusOnlineStatus(userStatus.state());
if (userStatus.state() != _userStatus.state()) {
setUserStatusOnlineStatus(userStatus.state());
}
setUserStatusMessage(userStatus);
}

Expand All @@ -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 userOnlineStatusChanged();
}
}

void OcsUserStatusConnector::onUserStatusMessageSet(const QJsonDocument &json, int statusCode)
Expand Down Expand Up @@ -449,7 +466,10 @@ void OcsUserStatusConnector::onMessageCleared(const QJsonDocument &json, int sta
return;
}

const auto onlineState = _userStatus.state();

_userStatus = {};
_userStatus.setState(onlineState);
emit messageCleared();
}
}
1 change: 1 addition & 0 deletions src/libsync/userstatusconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class OWNCLOUDSYNC_EXPORT UserStatusConnector : public QObject
void userStatusFetched(const UserStatus &userStatus);
void predefinedStatusesFetched(const std::vector<UserStatus> &statuses);
void userStatusSet();
void userOnlineStatusChanged();
void messageCleared();
void error(Error error);
};
Expand Down

0 comments on commit c7b63a0

Please sign in to comment.