diff options
author | Tobias Markmann <tm@ayena.de> | 2015-09-14 15:08:39 (GMT) |
---|---|---|
committer | Kevin Smith <kevin.smith@isode.com> | 2015-10-16 14:58:03 (GMT) |
commit | 9e6ee0f262e7dc663f4c706b16a346a268f425aa (patch) | |
tree | 5926fd9cc9dec1233f95239b259a7730f99a5a72 /Swiften | |
parent | 8da6e35afab55d7232d47575faea2bde7cd44ece (diff) | |
download | swift-9e6ee0f262e7dc663f4c706b16a346a268f425aa.zip swift-9e6ee0f262e7dc663f4c706b16a346a268f425aa.tar.bz2 |
Mark removed contacts as unavailable in Swift
When removing a contact, a XMPP client will not receive an explicit
unavailable presence stanza for the contact from the server. Because
of that Swift used to show removed contacts still with their old
presence in the Chats tab or the chat view.
With this patch, the PresenceOracle will flush all known presence
of a contact as soon as the JID is removed from the roster. An
unavailable presence will stored under the removed bare JID and
is emitted via the PresenceOracle::onPresenceChange signal.
Test-Information:
Added a unit test verifying this behavior. Tested the behavior with
two scenarios:
a) Account A and B adding each other and accepting the subscription
request. Starting a chat between A and B. After removing B in A's
account, B used to be shown as available in the chat view and the
Chats tab. With this patch B is shown as unavailable.
b) Account A and B adding each other and accepting the subscription
request. A removing B, and B removing A. After A adds B again,
B used to be shown with the old presence even before B accepted
the subscription request. This behavior is also fixed with this
patch, not showing B as online until B accepted the subscription
request.
Change-Id: Iba97d3bedd0ac962ea00b25a0d2ed6106ed55a55
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Client/Client.cpp | 2 | ||||
-rw-r--r-- | Swiften/Presence/PresenceOracle.cpp | 27 | ||||
-rw-r--r-- | Swiften/Presence/PresenceOracle.h | 10 | ||||
-rw-r--r-- | Swiften/Presence/UnitTest/PresenceOracleTest.cpp | 17 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterImpl.cpp | 7 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterImpl.h | 3 |
6 files changed, 48 insertions, 18 deletions
diff --git a/Swiften/Client/Client.cpp b/Swiften/Client/Client.cpp index 3bfdd3f..f1266e9 100644 --- a/Swiften/Client/Client.cpp +++ b/Swiften/Client/Client.cpp @@ -49,7 +49,7 @@ Client::Client(const JID& jid, const SafeString& password, NetworkFactories* net subscriptionManager = new SubscriptionManager(getStanzaChannel()); - presenceOracle = new PresenceOracle(getStanzaChannel()); + presenceOracle = new PresenceOracle(getStanzaChannel(), roster); presenceOracle->onPresenceChange.connect(boost::ref(onPresenceChange)); stanzaChannelPresenceSender = new StanzaChannelPresenceSender(getStanzaChannel()); diff --git a/Swiften/Presence/PresenceOracle.cpp b/Swiften/Presence/PresenceOracle.cpp index 59dff41..e4129fb 100644 --- a/Swiften/Presence/PresenceOracle.cpp +++ b/Swiften/Presence/PresenceOracle.cpp @@ -1,26 +1,28 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include "PresenceOracle.h" +#include <Swiften/Presence/PresenceOracle.h> #include <boost/bind.hpp> #include <Swiften/Client/StanzaChannel.h> +#include <Swiften/Roster/XMPPRoster.h> namespace Swift { -PresenceOracle::PresenceOracle(StanzaChannel* stanzaChannel) { - stanzaChannel_ = stanzaChannel; +PresenceOracle::PresenceOracle(StanzaChannel* stanzaChannel, XMPPRoster* roster) : stanzaChannel_(stanzaChannel), xmppRoster_(roster) { stanzaChannel_->onPresenceReceived.connect(boost::bind(&PresenceOracle::handleIncomingPresence, this, _1)); stanzaChannel_->onAvailableChanged.connect(boost::bind(&PresenceOracle::handleStanzaChannelAvailableChanged, this, _1)); + xmppRoster_->onJIDRemoved.connect(boost::bind(&PresenceOracle::handleJIDRemoved, this, _1)); } PresenceOracle::~PresenceOracle() { stanzaChannel_->onPresenceReceived.disconnect(boost::bind(&PresenceOracle::handleIncomingPresence, this, _1)); stanzaChannel_->onAvailableChanged.disconnect(boost::bind(&PresenceOracle::handleStanzaChannelAvailableChanged, this, _1)); + xmppRoster_->onJIDRemoved.disconnect(boost::bind(&PresenceOracle::handleJIDRemoved, this, _1)); } void PresenceOracle::handleStanzaChannelAvailableChanged(bool available) { @@ -29,7 +31,6 @@ void PresenceOracle::handleStanzaChannelAvailableChanged(bool available) { } } - void PresenceOracle::handleIncomingPresence(Presence::ref presence) { JID bareJID(presence->getFrom().toBare()); if (presence->getType() == Presence::Subscribe) { @@ -43,7 +44,7 @@ void PresenceOracle::handleIncomingPresence(Presence::ref presence) { passedPresence->setFrom(bareJID); passedPresence->setStatus(presence->getStatus()); } - std::map<JID, boost::shared_ptr<Presence> > jidMap = entries_[bareJID]; + PresenceMap jidMap = entries_[bareJID]; if (passedPresence->getFrom().isBare() && presence->getType() == Presence::Unavailable) { /* Have a bare-JID only presence of offline */ jidMap.clear(); @@ -61,6 +62,20 @@ void PresenceOracle::handleIncomingPresence(Presence::ref presence) { } } +void PresenceOracle::handleJIDRemoved(const JID& removedJID) { + /* 3921bis says that we don't follow up with an unavailable, so simulate this ourselves */ + Presence::ref unavailablePresence = Presence::ref(new Presence()); + unavailablePresence->setType(Presence::Unavailable); + unavailablePresence->setFrom(removedJID); + + if (entries_.find(removedJID) != entries_.end()) { + entries_[removedJID].clear(); + entries_[removedJID][removedJID] = unavailablePresence; + } + + onPresenceChange(unavailablePresence); +} + Presence::ref PresenceOracle::getLastPresence(const JID& jid) const { PresencesMap::const_iterator i = entries_.find(jid.toBare()); if (i == entries_.end()) { diff --git a/Swiften/Presence/PresenceOracle.h b/Swiften/Presence/PresenceOracle.h index 84d5b3c..f312506 100644 --- a/Swiften/Presence/PresenceOracle.h +++ b/Swiften/Presence/PresenceOracle.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -7,19 +7,19 @@ #pragma once #include <map> - #include <string> -#include <Swiften/Elements/Presence.h> #include <Swiften/Base/API.h> #include <Swiften/Base/boost_bsignals.h> +#include <Swiften/Elements/Presence.h> namespace Swift { class StanzaChannel; + class XMPPRoster; class SWIFTEN_API PresenceOracle { public: - PresenceOracle(StanzaChannel* stanzaChannel); + PresenceOracle(StanzaChannel* stanzaChannel, XMPPRoster* roster); ~PresenceOracle(); Presence::ref getLastPresence(const JID&) const; @@ -32,12 +32,14 @@ namespace Swift { private: void handleIncomingPresence(Presence::ref presence); void handleStanzaChannelAvailableChanged(bool); + void handleJIDRemoved(const JID& removedJID); private: typedef std::map<JID, Presence::ref> PresenceMap; typedef std::map<JID, PresenceMap> PresencesMap; PresencesMap entries_; StanzaChannel* stanzaChannel_; + XMPPRoster* xmppRoster_; }; } diff --git a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp index 41857e1..85dcca9 100644 --- a/Swiften/Presence/UnitTest/PresenceOracleTest.cpp +++ b/Swiften/Presence/UnitTest/PresenceOracleTest.cpp @@ -1,17 +1,20 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include <cppunit/extensions/HelperMacros.h> -#include <cppunit/extensions/TestFactoryRegistry.h> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> -#include <Swiften/Presence/PresenceOracle.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + #include <Swiften/Client/DummyStanzaChannel.h> +#include <Swiften/Presence/PresenceOracle.h> #include <Swiften/Presence/SubscriptionManager.h> +#include <Swiften/Roster/XMPPRoster.h> +#include <Swiften/Roster/XMPPRosterImpl.h> using namespace Swift; @@ -30,7 +33,9 @@ class PresenceOracleTest : public CppUnit::TestFixture { public: void setUp() { stanzaChannel_ = new DummyStanzaChannel(); - oracle_ = new PresenceOracle(stanzaChannel_); + xmppRoster_ = new XMPPRosterImpl(); + + oracle_ = new PresenceOracle(stanzaChannel_, xmppRoster_); oracle_->onPresenceChange.connect(boost::bind(&PresenceOracleTest::handlePresenceChange, this, _1)); subscriptionManager_ = new SubscriptionManager(stanzaChannel_); subscriptionManager_->onPresenceSubscriptionRequest.connect(boost::bind(&PresenceOracleTest::handlePresenceSubscriptionRequest, this, _1, _2)); @@ -42,6 +47,7 @@ class PresenceOracleTest : public CppUnit::TestFixture { void tearDown() { delete subscriptionManager_; delete oracle_; + delete xmppRoster_; delete stanzaChannel_; } @@ -186,6 +192,7 @@ class PresenceOracleTest : public CppUnit::TestFixture { PresenceOracle* oracle_; SubscriptionManager* subscriptionManager_; DummyStanzaChannel* stanzaChannel_; + XMPPRoster* xmppRoster_; std::vector<Presence::ref> changes; std::vector<SubscriptionRequestInfo> subscriptionRequests; JID user1; diff --git a/Swiften/Roster/XMPPRosterImpl.cpp b/Swiften/Roster/XMPPRosterImpl.cpp index d438f69..96b9949 100644 --- a/Swiften/Roster/XMPPRosterImpl.cpp +++ b/Swiften/Roster/XMPPRosterImpl.cpp @@ -1,10 +1,11 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Roster/XMPPRosterImpl.h> + #include <Swiften/Base/foreach.h> namespace Swift { @@ -12,6 +13,10 @@ namespace Swift { XMPPRosterImpl::XMPPRosterImpl() { } +XMPPRosterImpl::~XMPPRosterImpl() { + +} + void XMPPRosterImpl::addContact(const JID& jid, const std::string& name, const std::vector<std::string>& groups, RosterItemPayload::Subscription subscription) { JID bareJID(jid.toBare()); std::map<JID, XMPPRosterItem>::iterator i = entries_.find(bareJID); diff --git a/Swiften/Roster/XMPPRosterImpl.h b/Swiften/Roster/XMPPRosterImpl.h index 6a11b36..284b18a 100644 --- a/Swiften/Roster/XMPPRosterImpl.h +++ b/Swiften/Roster/XMPPRosterImpl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -16,6 +16,7 @@ namespace Swift { class SWIFTEN_API XMPPRosterImpl : public XMPPRoster { public: XMPPRosterImpl(); + virtual ~XMPPRosterImpl(); void addContact(const JID& jid, const std::string& name, const std::vector<std::string>& groups, RosterItemPayload::Subscription subscription); void removeContact(const JID& jid); |