Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for requesting VCard of a MIX channel participant #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Swiften/MIX/MIX.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#include <Swiften/MIX/MIX.h>

namespace Swift {

MIX::~MIX() {
}

}
48 changes: 48 additions & 0 deletions Swiften/MIX/MIX.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#pragma once

#include <memory>
#include <string>
#include <unordered_set>

#include <boost/signals2.hpp>

#include <Swiften/Base/API.h>
#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 {
class SWIFTEN_API MIX {
public:
using ref = std::shared_ptr<MIX>;

public:
virtual ~MIX();

/**
* Join a MIX channel and subscribe to nodes.
*/
virtual void joinChannelAndSubscribe(const std::unordered_set<std::string>& nodes) = 0;
virtual void joinChannel() = 0;

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;
};
}
77 changes: 77 additions & 0 deletions Swiften/MIX/MIXImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#include <Swiften/MIX/MIXImpl.h>

#include <Swiften/Elements/IQ.h>
#include <Swiften/Queries/GenericRequest.h>
#include <Swiften/Queries/IQRouter.h>

namespace Swift {

MIXImpl::MIXImpl(const JID &ownJID, const JID &channelJID, IQRouter* iqRouter) : ownJID_(ownJID), channelJID_(channelJID), iqRouter_(iqRouter) {

}

MIXImpl::~MIXImpl() {

}

void MIXImpl::joinChannelAndSubscribe(const std::unordered_set<std::string>& nodes) {
auto joinPayload = std::make_shared<MIXJoin>();
joinPayload->setChannel(channelJID_);
for (auto node : nodes) {
joinPayload->addSubscription(node);
}
auto request = std::make_shared<GenericRequest<MIXJoin> >(IQ::Set, getJID(), joinPayload, iqRouter_);
request->onResponse.connect(boost::bind(&MIXImpl::handleJoinResponse, this, _1, _2));
request->send();
}

void MIXImpl::joinChannel() {
joinChannelAndSubscribe(std::unordered_set<std::string>());
}

void MIXImpl::handleJoinResponse(MIXJoin::ref payload, ErrorPayload::ref error) {
if (error) {
onJoinFailed(error);
} else {
onJoinComplete(payload);
}
}

void MIXImpl::leaveChannel() {
auto leavePayload = std::make_shared<MIXLeave>();
leavePayload->setChannel(channelJID_);
auto request = std::make_shared<GenericRequest<MIXLeave> >(IQ::Set, getJID(), leavePayload, iqRouter_);
request->onResponse.connect(boost::bind(&MIXImpl::handleLeaveResponse, this, _1, _2));
request->send();
}

void MIXImpl::handleLeaveResponse(MIXLeave::ref payload, ErrorPayload::ref error) {
if (error) {
onLeaveFailed(error);
} else {
onLeaveComplete(payload);
}
}

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);
}
}

}
54 changes: 54 additions & 0 deletions Swiften/MIX/MIXImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2017 Tarun Gupta
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/

#pragma once

#include <Swiften/MIX/MIX.h>

namespace Swift {
class StanzaChannel;
class IQRouter;

class SWIFTEN_API MIXImpl : public MIX {
public:
using ref = std::shared_ptr<MIXImpl>;

public:
MIXImpl(const JID &ownJID, const JID &channelJID, IQRouter* iqRouter);
virtual ~MIXImpl();

/**
* Returns the (bare) JID of the user.
*/
virtual JID getJID() const {
return ownJID_.toBare();
}

/**
* Returns the JID of MIX channel.
*/
virtual JID getChannelJID() const {
return channelJID_;
}

virtual void joinChannelAndSubscribe(const std::unordered_set<std::string>& nodes) override;
virtual void joinChannel() override;

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_;
JID channelJID_;
IQRouter* iqRouter_;
};
}
Loading