Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit 1b5b175

Browse files
committed
Add test for pubsub item
1 parent 3e99aba commit 1b5b175

File tree

5 files changed

+98
-9
lines changed

5 files changed

+98
-9
lines changed

src/base/QXmppMessage.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ class QXMPP_EXPORT QXmppMessage : public QXmppStanza
257257
const QVector<QXmppFileShare> &sharedFiles() const;
258258
void setSharedFiles(const QVector<QXmppFileShare> &sharedFiles);
259259

260-
// XEP-0449: Stickers
261-
const std::optional<QString> &stickerPackId() const;
262-
void setStickerPackId(const std::optional<QString> &stickerPackId);
260+
// XEP-0449: Stickers
261+
const std::optional<QString> &stickerPackId() const;
262+
void setStickerPackId(const std::optional<QString> &stickerPackId);
263263

264264
/// \cond
265265
#ifdef BUILD_OMEMO

src/base/QXmppStickerPackItem.cpp

+36-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "QXmppConstants_p.h"
88
#include "QXmppEncryptedFileSource.h"
99
#include "QXmppFileMetadata.h"
10+
#include "qdebug.h"
1011

1112
#include <QXmlStreamWriter>
1213

@@ -16,18 +17,23 @@ class QXmppStickerItemPrivate : public QSharedData
1617
QXmppFileMetadata metadata;
1718
QVector<QXmppHttpFileSource> httpSources;
1819
QVector<QXmppEncryptedFileSource> encryptedSources;
20+
std::optional<QString> suggest;
1921
};
2022

2123
QXmppStickerItem::QXmppStickerItem()
2224
: d(new QXmppStickerItemPrivate())
2325
{
2426
}
2527

28+
QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerItem)
29+
2630
///
2731
/// \class QXmppStickerItem
2832
///
2933
/// This class represents a single sticker when publishing or retrieving it.
3034
///
35+
/// \since QXmpp 1.5
36+
///
3137

3238
///
3339
/// \brief Returns metadata about the sticker file
@@ -77,20 +83,34 @@ void QXmppStickerItem::setEncryptedSources(const QVector<QXmppEncryptedFileSourc
7783
d->encryptedSources = encryptedSources;
7884
}
7985

80-
QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerItem)
86+
const std::optional<QString> &QXmppStickerItem::suggest() const
87+
{
88+
return d->suggest;
89+
}
90+
91+
void QXmppStickerItem::setSuggest(const std::optional<QString> &suggest)
92+
{
93+
d->suggest = suggest;
94+
}
8195

8296
/// \cond
8397
void QXmppStickerItem::toXml(QXmlStreamWriter *writer) const
8498
{
8599
writer->writeStartElement("item");
86100
d->metadata.toXml(writer);
101+
writer->writeStartElement("sources");
102+
writer->writeDefaultNamespace(ns_sfs);
87103
for (const auto &httpSource : d->httpSources) {
88104
httpSource.toXml(writer);
89105
}
90106
for (const auto &encryptedSource : d->encryptedSources) {
91107
encryptedSource.toXml(writer);
92108
}
93109
writer->writeEndElement();
110+
if (d->suggest) {
111+
writer->writeTextElement("suggest", *d->suggest);
112+
}
113+
writer->writeEndElement();
94114
}
95115

96116
bool QXmppStickerItem::parse(const QDomElement &element)
@@ -115,6 +135,10 @@ bool QXmppStickerItem::parse(const QDomElement &element)
115135
}
116136
}
117137

138+
if (auto el = element.firstChildElement("suggest"); !el.isNull()) {
139+
d->suggest = el.text();
140+
}
141+
118142
return true;
119143
}
120144
/// \endcond
@@ -125,17 +149,22 @@ class QXmppStickerPackItemPrivate : public QSharedData
125149
QString name;
126150
QString summary;
127151
QVector<QXmppStickerItem> items;
152+
QXmppHash hash;
128153
};
129154

130155
QXmppStickerPackItem::QXmppStickerPackItem()
131156
: d(new QXmppStickerPackItemPrivate())
132157
{
133158
}
134159

160+
QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerPackItem)
161+
135162
///
136163
/// \class QXmppStickerPackitem
137164
///
138-
/// A pubsub item for a sticker pack.
165+
/// A pubsub item that represents a sticker pack.
166+
///
167+
/// \since QXmpp 1.5
139168
///
140169

141170
///
@@ -186,21 +215,21 @@ void QXmppStickerPackItem::setItems(const QVector<QXmppStickerItem> &items)
186215
d->items = items;
187216
}
188217

189-
QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerPackItem)
190-
191218
void QXmppStickerPackItem::parsePayload(const QDomElement &payloadElement)
192219
{
193220
d->name = payloadElement.firstChildElement("name").text();
194221
d->summary = payloadElement.firstChildElement("summary").text();
195222

196223
for (auto firstChild = payloadElement.firstChildElement("item");
197224
!firstChild.isNull();
198-
firstChild.nextSibling()) {
225+
firstChild = firstChild.nextSiblingElement("item")) {
199226
QXmppStickerItem stickerItem;
200-
stickerItem.parse(payloadElement);
227+
stickerItem.parse(firstChild);
201228

202229
d->items.push_back(std::move(stickerItem));
203230
}
231+
232+
d->hash.parse(payloadElement.firstChildElement("hash"));
204233
}
205234

206235
void QXmppStickerPackItem::serializePayload(QXmlStreamWriter *writer) const
@@ -215,5 +244,6 @@ void QXmppStickerPackItem::serializePayload(QXmlStreamWriter *writer) const
215244
item.toXml(writer);
216245
}
217246

247+
d->hash.toXml(writer);
218248
writer->writeEndElement();
219249
}

src/base/QXmppStickerPackItem.h

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <QVector>
1111

12+
#include <optional>
13+
1214
class QXmppFileMetadata;
1315
class QXmppHttpFileSource;
1416
class QXmppEncryptedFileSource;
@@ -31,6 +33,9 @@ class QXMPP_EXPORT QXmppStickerItem
3133
const QVector<QXmppEncryptedFileSource> &encryptedSources() const;
3234
void setEncryptedSources(const QVector<QXmppEncryptedFileSource> &encryptedSources);
3335

36+
const std::optional<QString> &suggest() const;
37+
void setSuggest(const std::optional<QString> &suggest);
38+
3439
/// \cond
3540
bool parse(const QDomElement &element);
3641
void toXml(QXmlStreamWriter *writer) const;

tests/qxmppmessage/tst_qxmppmessage.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,7 @@ void tst_QXmppMessage::testStickers()
12831283
parsePacket(message1, xml);
12841284
QVERIFY(!message1.sharedFiles().empty());
12851285
QVERIFY(message1.stickerPackId().has_value());
1286+
Q_ASSERT(message1.stickerPackId().value() == QStringLiteral("EpRv28DHHzFrE4zd"));
12861287
serializePacket(message1, xml);
12871288
}
12881289

tests/qxmpppubsub/tst_qxmpppubsub.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//
33
// SPDX-License-Identifier: LGPL-2.1-or-later
44

5+
#include "QXmppHttpFileSource.h"
56
#include "QXmppPubSubAffiliation.h"
67
#include "QXmppPubSubSubscription.h"
8+
#include "QXmppStickerPackItem.h"
79

810
#include "pubsubutil.h"
911
#include "util.h"
@@ -55,6 +57,7 @@ class tst_QXmppPubSub : public QObject
5557
Q_SLOT void testIsItem_data();
5658
Q_SLOT void testIsItem();
5759
Q_SLOT void testTestItem();
60+
Q_SLOT void testStickerPackItem();
5861
};
5962

6063
void tst_QXmppPubSub::testAffiliation_data()
@@ -280,5 +283,55 @@ void tst_QXmppPubSub::testTestItem()
280283
QVERIFY(!TestItem::isItem(xmlToDom(invalidXml)));
281284
}
282285

286+
void tst_QXmppPubSub::testStickerPackItem()
287+
{
288+
QByteArray xml(
289+
"<item id='EpRv28DHHzFrE4zd+xaNpVb4'>"
290+
"<pack xmlns='urn:xmpp:stickers:0'>"
291+
"<name>Marsey the Cat</name>"
292+
"<summary>Be cute or be cynical, this little kitten works both ways.</summary>"
293+
"<item>"
294+
"<file xmlns='urn:xmpp:file:metadata:0'>"
295+
"<desc>👍</desc>"
296+
"<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>0AdP8lJOWJrugSKOIAqfEKqFatIpG5JBCjjxY253ojQ=</hash>"
297+
"<height>512</height>"
298+
"<media-type>image/png</media-type>"
299+
"<size>71045</size>"
300+
"<width>512</width>"
301+
"</file>"
302+
"<sources xmlns='urn:xmpp:sfs:0'>"
303+
"<url-data xmlns='http://jabber.org/protocol/url-data' target='https://download.montague.lit/51078299-d071-46e1-b6d3-3de4a8ab67d6/sticker_marsey_thumbs_up.png'/>"
304+
"</sources>"
305+
"<suggest>+1</suggest>"
306+
"</item>"
307+
"<item>"
308+
"<file xmlns='urn:xmpp:file:metadata:0'>"
309+
"<desc>😘</desc>"
310+
"<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>gw+6xdCgOcvCYSKuQNrXH33lV9NMzuDf/s0huByCDsY=</hash>"
311+
"<height>512</height>"
312+
"<media-type>image/png</media-type>"
313+
"<size>67016</size>"
314+
"<width>512</width>"
315+
"</file>"
316+
"<sources xmlns='urn:xmpp:sfs:0'>"
317+
"<url-data xmlns='http://jabber.org/protocol/url-data' target='https://download.montague.lit/51078299-d071-46e1-b6d3-3de4a8ab67d6/sticker_marsey_kiss.png'/>"
318+
"</sources>"
319+
"</item>"
320+
"<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>EpRv28DHHzFrE4zd+xaNpVb4jbu4s74XtioExNjQzZ0=</hash>"
321+
"</pack>"
322+
"</item>");
323+
324+
QXmppStickerPackItem item;
325+
parsePacket(item, xml);
326+
QCOMPARE(item.items().size(), 2);
327+
QCOMPARE(item.name(), QStringLiteral("Marsey the Cat"));
328+
QCOMPARE(item.summary(), QStringLiteral("Be cute or be cynical, this little kitten works both ways."));
329+
330+
auto &firstItem = item.items().front();
331+
QVERIFY(firstItem.suggest().has_value());
332+
QCOMPARE(firstItem.httpSource().front().url(), QUrl("https://download.montague.lit/51078299-d071-46e1-b6d3-3de4a8ab67d6/sticker_marsey_thumbs_up.png"));
333+
serializePacket(item, xml);
334+
}
335+
283336
QTEST_MAIN(tst_QXmppPubSub)
284337
#include "tst_qxmpppubsub.moc"

0 commit comments

Comments
 (0)