Skip to content

Commit

Permalink
XEP-0444 message reactions support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Jun 18, 2024
1 parent bfb37f5 commit 0318b53
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/xmpp/xmpp-im/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,8 @@ class Message::Private : public QSharedData {
QString encryptionProtocol; // XEP-0380
Message::StanzaId stanzaId; // XEP-0359
QList<Reference> references; // XEP-0385 and XEP-0372

std::optional<QStringList> reactions; // XEP-0444
};

#define MessageD() (d ? d : (d = new Private))
Expand Down Expand Up @@ -1096,6 +1098,10 @@ void Message::addReference(const Reference &r) { MessageD()->references.append(r

void Message::setReferences(const QList<Reference> &r) { MessageD()->references = r; }

void Message::setReactions(const QStringList &reactions) { MessageD()->reactions = reactions; }

std::optional<QStringList> Message::reactions() const { return d ? d->reactions : QStringList {}; }

QString Message::invite() const { return d ? d->invite : QString(); }

void Message::setInvite(const QString &s) { MessageD()->invite = s; }
Expand Down Expand Up @@ -1433,6 +1439,16 @@ Stanza Message::toStanza(Stream *stream) const
s.appendChild(r.toXml(&s.doc()));
}

// XEP-0444
auto reactionsNS = QStringLiteral("urn:xmpp:reactions:0");
if (d->reactions) {
auto e = s.createElement(reactionsNS, QStringLiteral("reactions"));
for (const QString &reaction : *d->reactions) {
e.appendChild(s.createTextElement(reactionsNS, QStringLiteral("reaction"), reaction));
}
s.appendChild(e);
}

return s;
}

Expand Down Expand Up @@ -1785,6 +1801,20 @@ bool Message::fromStanza(const Stanza &s, bool useTimeZoneOffset, int timeZoneOf
}
}

// XEP-0444 message reactions
auto reactionStanza
= childElementsByTagNameNS(root, "urn:xmpp:reactions:0", QStringLiteral("reactions")).item(0).toElement();
if (!reactionStanza.isNull()) {
auto reactionTag = QStringLiteral("reaction");
QStringList reactions;
auto reaction = reactionStanza.firstChildElement(reactionTag);
while (!reaction.isNull()) {
reactions.append(reaction.text().trimmed());
reaction = reaction.nextSiblingElement(reactionTag);
}
d->reactions = reactions;
}

return true;
}

Expand Down
6 changes: 6 additions & 0 deletions src/xmpp/xmpp-im/xmpp_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include <QExplicitlySharedDataPointer>

#include <optional>

class QDateTime;
class QString;

Expand Down Expand Up @@ -220,6 +222,10 @@ class Message {
void addReference(const Reference &r);
void setReferences(const QList<Reference> &r);

// XEP-0444 message reaction
void setReactions(const QStringList &reactions);
std::optional<QStringList> reactions() const;

// Obsolete invitation
QString invite() const;
void setInvite(const QString &s);
Expand Down

0 comments on commit 0318b53

Please sign in to comment.