Skip to content

Commit

Permalink
MixIq: Add element for MIX channel invitation
Browse files Browse the repository at this point in the history
This makes it possible to join a MIX channel with an invitation.
  • Loading branch information
melvo authored and lnjX committed Apr 10, 2024
1 parent 2a15d72 commit f555585
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
39 changes: 36 additions & 3 deletions src/base/QXmppMixIq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,17 @@ class QXmppMixIqPrivate : public QSharedData
QString channelJid;
QXmppMixConfigItem::Nodes subscriptions;
QString nick;
std::optional<QXmppMixInvitation> invitation;
QXmppMixIq::Type actionType = QXmppMixIq::None;
};

///
/// \class QXmppMixIq
///
/// This class represents an IQ used to do actions on a MIX channel as defined by
/// \xep{0369, Mediated Information eXchange (MIX)} and
/// \xep{0405, Mediated Information eXchange (MIX): Participant Server Requirements}.
/// \xep{0369, Mediated Information eXchange (MIX)},
/// \xep{0405, Mediated Information eXchange (MIX): Participant Server Requirements} and
/// \xep{0407, Mediated Information eXchange (MIX): Miscellaneous Capabilities}.
///
/// \since QXmpp 1.1
///
Expand Down Expand Up @@ -570,7 +572,29 @@ void QXmppMixIq::setNick(const QString &nick)
d->nick = nick;
}

/// Returns the MIX channel action type.
///
/// Returns the invitation to the channel being joined via Type::ClientJoin or Type::Join.
///
/// \return the channel invitation
///
/// \since QXmpp 1.7
///
std::optional<QXmppMixInvitation> QXmppMixIq::invitation() const
{
return d->invitation;
}

///
/// Sets the invitation to the channel being joined via Type::ClientJoin or Type::Join.
///
/// \param invitation channel invitation
///
/// \since QXmpp 1.7
///
void QXmppMixIq::setInvitation(const std::optional<QXmppMixInvitation> &invitation)
{
d->invitation = invitation;
}

/// Returns the MIX channel's action type.
///
Expand Down Expand Up @@ -626,6 +650,11 @@ void QXmppMixIq::parseElementFromChild(const QDomElement &element)

d->nick = firstChildElement(child, u"nick").text();

if (const auto invitationElement = firstChildElement(child, u"invitation"); !invitationElement.isNull()) {
d->invitation = QXmppMixInvitation();
d->invitation->parse(invitationElement);
}

QVector<QString> subscriptions;

for (const auto &node : iterChildElements(child, u"subscribe")) {
Expand Down Expand Up @@ -673,6 +702,10 @@ void QXmppMixIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
writer->writeTextElement(QSL65("nick"), d->nick);
}

if (d->invitation) {
d->invitation->toXml(writer);
}

writer->writeEndElement();

if (d->actionType == ClientJoin || d->actionType == ClientLeave) {
Expand Down
4 changes: 4 additions & 0 deletions src/base/QXmppMixIq.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <QSharedDataPointer>

class QXmppMixInvitation;
class QXmppMixIqPrivate;

class QXMPP_EXPORT QXmppMixIq : public QXmppIq
Expand Down Expand Up @@ -71,6 +72,9 @@ class QXMPP_EXPORT QXmppMixIq : public QXmppIq
QString nick() const;
void setNick(const QString &);

std::optional<QXmppMixInvitation> invitation() const;
void setInvitation(const std::optional<QXmppMixInvitation> &);

/// \cond
static bool isMixIq(const QDomElement &);
/// \endcond
Expand Down
51 changes: 46 additions & 5 deletions tests/qxmppmixiq/tst_qxmppmixiq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ void tst_QXmppMixIq::testBase_data()
"<subscribe node=\"urn:xmpp:mix:nodes:info\"/>"
"<subscribe node=\"urn:xmpp:mix:nodes:messages\"/>"
"<nick>third witch</nick>"
"<invitation xmlns=\"urn:xmpp:mix:misc:0\">"
"<inviter>hag66@shakespeare.example</inviter>"
"<invitee>cat@shakespeare.example</invitee>"
"<channel>coven@mix.shakespeare.example</channel>"
"<token>ABCDEF</token>"
"</invitation>"
"</join>"
"</client-join>"
"</iq>");
Expand All @@ -60,6 +66,12 @@ void tst_QXmppMixIq::testBase_data()
"<subscribe node=\"urn:xmpp:mix:nodes:info\"/>"
"<subscribe node=\"urn:xmpp:mix:nodes:messages\"/>"
"<nick>stpeter</nick>"
"<invitation xmlns=\"urn:xmpp:mix:misc:0\">"
"<inviter>hag66@shakespeare.example</inviter>"
"<invitee>cat@shakespeare.example</invitee>"
"<channel>coven@mix.shakespeare.example</channel>"
"<token>ABCDEF</token>"
"</invitation>"
"</join>"
"</iq>");
QByteArray joinS2sResultXml(
Expand Down Expand Up @@ -201,6 +213,7 @@ void tst_QXmppMixIq::testBase_data()
QTest::addColumn<QStringList>("nodes");
QTest::addColumn<QXmppMixConfigItem::Nodes>("subscriptions");
QTest::addColumn<QString>("nick");
QTest::addColumn<QString>("invitationToken");

QTest::newRow("join-c2s-set")
<< joinC2sSetXml
Expand All @@ -213,7 +226,8 @@ void tst_QXmppMixIq::testBase_data()
<< "coven@mix.shakespeare.example"
<< nodeList
<< subscriptions
<< "third witch";
<< "third witch"
<< "ABCDEF";
QTest::newRow("join-s2s-set")
<< joinS2sSetXml
<< QXmppIq::Set
Expand All @@ -225,7 +239,8 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< nodeList
<< subscriptions
<< "stpeter";
<< "stpeter"
<< "ABCDEF";
QTest::newRow("join-s2s-result")
<< joinS2sResultXml
<< QXmppIq::Result
Expand All @@ -237,7 +252,8 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< nodeList
<< subscriptions
<< "third witch";
<< "third witch"
<< "";
QTest::newRow("join-c2s-result")
<< joinC2sResultXml
<< QXmppIq::Result
Expand All @@ -249,6 +265,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< nodeList
<< subscriptions
<< ""
<< "";
QTest::newRow("leave-c2s-set")
<< leaveC2sSetXml
Expand All @@ -261,6 +278,7 @@ void tst_QXmppMixIq::testBase_data()
<< "coven@mix.shakespeare.example"
<< emptyNodeList
<< noNodes
<< ""
<< "";
QTest::newRow("leave-s2s-set")
<< leaveS2sSetXml
Expand All @@ -273,6 +291,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< emptyNodeList
<< noNodes
<< ""
<< "";
QTest::newRow("leave-s2s-result")
<< leaveS2sResultXml
Expand All @@ -285,6 +304,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< emptyNodeList
<< noNodes
<< ""
<< "";
QTest::newRow("leave-c2s-result")
<< leaveC2sResultXml
Expand All @@ -297,6 +317,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< emptyNodeList
<< noNodes
<< ""
<< "";
// Using QXmppMixIq::UpdateSubscription is deprecated since QXmpp 1.7.
QT_WARNING_PUSH
Expand All @@ -312,6 +333,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< nodeList
<< subscriptions
<< ""
<< "";
QTest::newRow("update-subscription-result")
<< updateSubscriptionResultXml
Expand All @@ -324,6 +346,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< nodeList
<< subscriptions
<< ""
<< "";
QT_WARNING_POP
QTest::newRow("setnick-set")
Expand All @@ -337,7 +360,8 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< emptyNodeList
<< noNodes
<< "thirdwitch";
<< "thirdwitch"
<< "";
QTest::newRow("setnick-result")
<< setNickResultXml
<< QXmppIq::Result
Expand All @@ -349,7 +373,8 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< emptyNodeList
<< noNodes
<< "thirdwitch";
<< "thirdwitch"
<< "";
QTest::newRow("create")
<< createXml
<< QXmppIq::Set
Expand All @@ -361,6 +386,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< emptyNodeList
<< noNodes
<< ""
<< "";
QTest::newRow("create-without-id")
<< createWithoutIdXml
Expand All @@ -373,6 +399,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< emptyNodeList
<< noNodes
<< ""
<< "";
QTest::newRow("destroy")
<< destroyXml
Expand All @@ -385,6 +412,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< emptyNodeList
<< noNodes
<< ""
<< "";
QTest::newRow("empty")
<< emptyXml
Expand All @@ -397,6 +425,7 @@ void tst_QXmppMixIq::testBase_data()
<< ""
<< emptyNodeList
<< noNodes
<< ""
<< "";
}

Expand All @@ -413,6 +442,7 @@ void tst_QXmppMixIq::testBase()
QFETCH(QStringList, nodes);
QFETCH(QXmppMixConfigItem::Nodes, subscriptions);
QFETCH(QString, nick);
QFETCH(QString, invitationToken);

QXmppMixIq iq;
parsePacket(iq, xml);
Expand All @@ -435,6 +465,10 @@ void tst_QXmppMixIq::testBase()
QT_WARNING_POP
QCOMPARE(iq.subscriptions(), subscriptions);
QCOMPARE(iq.nick(), nick);
QCOMPARE(iq.invitation().has_value(), !invitationToken.isEmpty());
if (iq.invitation()) {
QCOMPARE(iq.invitation()->token(), invitationToken);
}
serializePacket(iq, xml);
}

Expand All @@ -459,6 +493,7 @@ void tst_QXmppMixIq::testDefaults()
QT_WARNING_POP
QCOMPARE(iq.subscriptions(), QXmppMixConfigItem::Nodes {});
QCOMPARE(iq.nick(), QString());
QVERIFY(!iq.invitation());
}

void tst_QXmppMixIq::testSetters()
Expand Down Expand Up @@ -500,6 +535,12 @@ void tst_QXmppMixIq::testSetters()

iq.setNick("third witch");
QCOMPARE(iq.nick(), QStringLiteral("third witch"));

QXmppMixInvitation invitation;
invitation.setToken(QStringLiteral("ABCDEF"));

iq.setInvitation(invitation);
QCOMPARE(iq.invitation()->token(), QStringLiteral("ABCDEF"));
}

void tst_QXmppMixIq::testInvalidActionType()
Expand Down

0 comments on commit f555585

Please sign in to comment.