Skip to content

Commit

Permalink
provide missing publish overload
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Nov 7, 2023
1 parent d5f2b34 commit 6d88ca2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
15 changes: 15 additions & 0 deletions qt/pubnub_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,21 @@ pubnub_res pubnub_qt::publish(QString const& channel, QString const& message)
}


pubnub_res pubnub_qt::publish(QString const& channel, QString const& message, publish_options& opt)
{
QMutexLocker lk(&d_mutex);
d_method = pubnubSendViaGET;
return startRequest(pbcc_publish_prep(d_context.data(),
channel.toLatin1().data(),
message.toLatin1().data(),
opt.data().store,
!opt.data().replicate,
opt.data().meta,
opt.data().method),
PBTT_PUBLISH);
}


pubnub_res pubnub_qt::publish_via_post(QString const& channel,
QByteArray const& message)
{
Expand Down
71 changes: 71 additions & 0 deletions qt/pubnub_qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,65 @@ class QNetworkReply;
class QSslError;
QT_END_NAMESPACE

/** A wrapper class for publish options, enabling a nicer
usage. Something like:
pn.publish(chan, msg, publish_options().store(true).ttl(22));
*/
class publish_options {
pubnub_publish_options d_;
QString d_meta;

public:

publish_options() : d_(pubnub_publish_defopts()) {}

/** If true, the message is stored in history. If false,
the message is _not_ stored in history.
*/
publish_options& store(bool store)
{
d_.store = store;

return *this;
}

/** If `true`, the message is replicated, thus will be received by
all subscribers. If `false`, the message is _not_ replicated
and will be only delivered to BLOCK event handlers. Setting
`false` here and `false` on store is sometimes referred to as
a "fire" (instead of a "publish").
*/
publish_options& replicate(bool replicate)
{
d_.replicate = replicate;

return *this;
}

/** An optional JSON object, used to send additional ("meta") data
about the message, which can be used for stream filtering.
*/
publish_options& meta(QString const& meta)
{
d_meta = meta;
d_.meta = d_meta.isEmpty() ? 0 : d_meta.toLatin1().data();

return *this;
}

/** Defines the method by which publish transaction will be performed */
publish_options& method(pubnub_method method)
{
d_.method = method;

return *this;
}

pubnub_publish_options data() { return d_; }
};


/** A wrapper class for set_state options, enabling a nicer
usage. Something like:
Expand Down Expand Up @@ -613,6 +672,18 @@ class pubnub_qt : public QObject
*/
pubnub_res publish(QString const &channel, QString const &message);

/** Publish the @p message (in JSON format) on @p p channel, using the
@p p context. This actually means "initiate a publish
transaction".
This function is similar to casual publish, but it allows you to
specify additional options for the publish operation.
See @publish and @p publish_options for more details.
*/
pubnub_res publish(QString const &channel, QString const &message, publish_options& opt);


/** Function that initiates 'publish' transaction via POST method
@param channel The string with the channel
@param message The message to publish, expected to be in JSON format
Expand Down

0 comments on commit 6d88ca2

Please sign in to comment.