Skip to content

Commit

Permalink
Add methods for requesting VCard of a channel participant
Browse files Browse the repository at this point in the history
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.

Test-Information:
Tests added for requesting VCard as in XEP-0369, which passes.
Tested on Ubuntu 16.04 LTS.

Change-Id: I08f550c71066e373955439ca81d61a26b7c254d9
  • Loading branch information
tarun018 committed Jul 18, 2017
1 parent 7ea47f3 commit ba32a35
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Swiften/MIX/MIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <Swiften/JID/JID.h>
#include <Swiften/Elements/MIXJoin.h>
#include <Swiften/Elements/MIXLeave.h>
#include <Swiften/Elements/VCard.h>
#include <Swiften/Elements/ErrorPayload.h>

namespace Swift {
Expand All @@ -34,10 +35,14 @@ namespace Swift {

virtual void leaveChannel() = 0;

virtual void requestVCard(const JID &participant) = 0;

public:
boost::signals2::signal<void (MIXJoin::ref /* joinResponse */)> onJoinComplete;
boost::signals2::signal<void (ErrorPayload::ref /* joinError */)> onJoinFailed;
boost::signals2::signal<void (MIXLeave::ref /* leaveResponse */)> onLeaveComplete;
boost::signals2::signal<void (ErrorPayload::ref /* leaveError */)> onLeaveFailed;
boost::signals2::signal<void (VCard::ref /* vCard */)> onVCardReceived;
boost::signals2::signal<void (ErrorPayload::ref /* vCardRequestError */)> onVCardRequestFailed;
};
}
16 changes: 15 additions & 1 deletion Swiften/MIX/MIXImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <Swiften/MIX/MIXImpl.h>

#include <Swiften/Client/StanzaChannel.h>
#include <Swiften/Elements/IQ.h>
#include <Swiften/Queries/GenericRequest.h>
#include <Swiften/Queries/IQRouter.h>
Expand Down Expand Up @@ -60,4 +59,19 @@ void MIXImpl::handleLeaveResponse(MIXLeave::ref payload, ErrorPayload::ref error
}
}

void MIXImpl::requestVCard(const JID &participant) {
auto vCardPayload = std::make_shared<VCard>();
auto request = std::make_shared<GenericRequest<VCard> >(IQ::Get, participant, vCardPayload, iqRouter_);
request->onResponse.connect(boost::bind(&MIXImpl::handleVCardReceived, this, _1, _2));
request->send();
}

void MIXImpl::handleVCardReceived(VCard::ref payload, ErrorPayload::ref error) {
if (error) {
onVCardRequestFailed(error);
} else {
onVCardReceived(payload);
}
}

}
3 changes: 3 additions & 0 deletions Swiften/MIX/MIXImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ namespace Swift {

virtual void leaveChannel() override;

virtual void requestVCard(const JID &participant) override;

private:
void handleJoinResponse(MIXJoin::ref, ErrorPayload::ref);
void handleLeaveResponse(MIXLeave::ref, ErrorPayload::ref);
void handleVCardReceived(VCard::ref payload, ErrorPayload::ref error);

private:
JID ownJID_;
Expand Down
29 changes: 29 additions & 0 deletions Swiften/MIX/UnitTest/MIXImplTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MIXImplTest : public ::testing::Test {
auto mix = std::make_shared<MIXImpl>(ownJID_, channelJID_, router_);
mix->onJoinComplete.connect(boost::bind(&MIXImplTest::handleJoinComplete, this, _1));
mix->onLeaveComplete.connect(boost::bind(&MIXImplTest::handleLeaveComplete, this, _1));
mix->onVCardReceived.connect(boost::bind(&MIXImplTest::handleVCard, this, _1));
return mix;
}

Expand All @@ -50,6 +51,11 @@ class MIXImplTest : public ::testing::Test {
ASSERT_EQ(static_cast<int>(0), subscribedNodes_.size());
}

void handleVCard(VCard::ref payload) {
ASSERT_TRUE(payload);
vCardResult_ = payload;
}

IQ::ref createJoinResult(const std::unordered_set<std::string>& nodes) {
auto joinResultPayload = std::make_shared<MIXJoin>();
for (auto node : nodes) {
Expand All @@ -72,12 +78,23 @@ class MIXImplTest : public ::testing::Test {
return std::find(subscribedNodes_.begin(), subscribedNodes_.end(), value) != subscribedNodes_.end();
}

IQ::ref createVCardResult() {
auto vCard = std::make_shared<VCard>();
vCard->setFullName("Peter Saint-Andre");
vCard->setFamilyName("Saint-Andre");
vCard->setGivenName("Peter");
vCard->setNickname("stpeter");
vCard->addURL("http://www.xmpp.org/xsf/people/stpeter.shtml");
return IQ::createResult(ownJID_, channel_->sentStanzas[0]->getTo(), channel_->sentStanzas[0]->getID(), vCard);
}

JID ownJID_;
JID channelJID_;
DummyStanzaChannel* channel_;
IQRouter* router_;
int successfulJoins_;
std::unordered_set<std::string> subscribedNodes_;
VCard::ref vCardResult_;
};

TEST_F(MIXImplTest, testJoinChannelOnly) {
Expand Down Expand Up @@ -182,3 +199,15 @@ TEST_F(MIXImplTest, testLeaveChannel) {

channel_->onIQReceived(createLeaveResult());
}

}

TEST_F(MIXImplTest, testRequestVCard) {
MIX::ref testling = createMIXClient(ownJID_);
testling->requestVCard(JID("989898#coven@mix.shakespeare.example"));
ASSERT_EQ(1, static_cast<int>(channel_->sentStanzas.size()));
ASSERT_TRUE(channel_->isRequestAtIndex<VCard>(0, JID("989898#coven@mix.shakespeare.example"), IQ::Get));

channel_->onIQReceived(createVCardResult());
ASSERT_TRUE(vCardResult_);
ASSERT_EQ(vCardResult_->getFullName(), std::string("Peter Saint-Andre"));

0 comments on commit ba32a35

Please sign in to comment.