Rework MatrixPusher in Vala

This commit is contained in:
Gergely Polonkai 2016-02-19 11:58:17 +01:00
parent ecdd3a7dcd
commit 666dce73ab
4 changed files with 128 additions and 553 deletions

View File

@ -107,12 +107,6 @@ namespace Matrix {
GUEST;
}
[CCode (cheader_filename = "matrix-types.h")]
public class Pusher {
public Json.Node? get_json_node()
throws Matrix.Error;
}
[CCode (cheader_filename = "matrix-types.h")]
public class StateEvent {
public Json.Node? get_json_node();

View File

@ -412,4 +412,132 @@ namespace Matrix {
return builder.get_root();
}
}
/**
* Class to hold pusher related data.
*/
public class Pusher : JsonCompact {
/**
* A device display name.
*/
public string? device_display_name { get; set; default = null; }
/**
* An application display name.
*/
public string? app_display_name { get; set; default = null; }
/**
* An application ID.
*/
public string? app_id { get; set; default = null; }
/**
* If {{{true}}}, the homeserver should add another pusher
* with the given push key and app ID in addition to any
* others with different user IDs. Otherwise, the homeserver
* must remove any other pushers with the same App ID and
* pushkey for different users.
*/
public bool append { get; set; default = true; }
/**
* The kind of the pusher. {{{http}}} makes a pusher that
* sends HTTP pokes. {{{null}}} deletes the pusher.
*/
public string? kind { get; set; default = null; }
/**
* The preferred language for receiving notifications,
* e.g. {{{en}}} or {{{en-US}}}.
*/
public string? lang { get; set; default = null; }
/**
* A string that determines what set of device rules will be
* matched when evaluating push rules for this pusher. It is
* an arbitrary string. Multiple devices may use the same
* profile tag. It is advised that when an app's data is
* copied or restored to a different device, this value remain
* the same. Client apps should offer ways to change the
* profile tag, optionally copying rules from the old profile
* tag. Maximum length is 32 bytes. If the profile tag is
* longer than this, it will be truncated
*/
public string? profile_tag { get; set; default = null; }
/**
* A unique identifier for this pusher. The value you should
* use for this is the routing or destination address
* information for the notification, for example, the APNS
* token for APNS or the Registration ID for GCM. If your
* notification client has no such concept, use any unique
* identifier. Maximum length is 512 bytes. If pushkey is
* longer than this, it will be truncated
*/
public string? pushkey { get; set; default = null; }
/**
* a dictionary of information for the pusher implementation
* itself. For example, if kind is {{{http}}}, this should
* contain an {{{url}}} member, which is the URL to use to
* send notifications to. This function creates a deep copy of
* the data, so it can be freed after this call.
*/
public Json.Node? data { get; set; default = null; }
/**
* Get the pusher data as a JSON node.
*/
public override Json.Node?
get_json_node()
throws Matrix.Error
{
if ((device_display_name == null)
|| (app_display_name == null)
|| (app_id == null)
|| (data == null)
|| (kind == null)
|| (lang == null)
|| (profile_tag == null)
|| (pushkey == null)) {
throw new Matrix.Error.INCOMPLETE("Pusher data incomplete");
}
var builder = new Json.Builder();
builder.begin_object();
builder.set_member_name("device_display_name");
builder.add_string_value(device_display_name);
builder.set_member_name("app_display_name");
builder.add_string_value(app_display_name);
builder.set_member_name("app_id");
builder.add_string_value(app_id);
builder.set_member_name("append");
builder.add_boolean_value(append);
builder.set_member_name("kind");
builder.add_string_value(kind);
builder.set_member_name("lang");
builder.add_string_value(lang);
builder.set_member_name("profile_tag");
builder.add_string_value(profile_tag);
builder.set_member_name("pushkey");
builder.add_string_value(pushkey);
builder.set_member_name("data");
builder.add_value(data);
builder.end_object();
return builder.get_root();
}
}
}

View File

@ -247,518 +247,6 @@ G_DEFINE_QUARK(matrix-error-quark, matrix_error);
* Receipt types of acknowledgment.
*/
/**
* MatrixPusher:
*
* An opaque structure for pusher rulesets.
*/
struct _MatrixPusher {
gchar *device_display_name;
gchar *app_display_name;
gchar *app_id;
gboolean append;
gchar *kind;
gchar *lang;
gchar *profile_tag;
gchar *pushkey;
JsonNode *data;
guint refcount;
};
G_DEFINE_BOXED_TYPE(MatrixPusher, matrix_pusher,
(GBoxedCopyFunc)matrix_pusher_ref,
(GBoxedFreeFunc)matrix_pusher_unref);
/**
* matrix_pusher_new:
*
* Create a new #MatrixPusher object with reference count of 1.
*
* Returns: (transfer full): a new #MatrixPusher
*/
MatrixPusher *
matrix_pusher_new(void)
{
MatrixPusher *pusher;
pusher = g_new0(MatrixPusher, 1);
pusher->refcount = 1;
return pusher;
}
static void
matrix_pusher_free(MatrixPusher *pusher)
{
g_free(pusher->device_display_name);
g_free(pusher->app_display_name);
g_free(pusher->app_id);
g_free(pusher->kind);
g_free(pusher->lang);
g_free(pusher->profile_tag);
g_free(pusher->pushkey);
json_node_free(pusher->data);
g_free(pusher);
}
/**
* matrix_pusher_ref:
* @pusher: a #MatrixPusher
*
* Increase reference count of @pusher by one.
*
* Returns: (transfer none): the same #MatrixPusher
*/
MatrixPusher *
matrix_pusher_ref(MatrixPusher *pusher)
{
pusher->refcount++;
return pusher;
}
/**
* matrix_pusher_unref:
* @pusher: a #MatrixPusher
*
* Decrease reference count of @pusher by one. If reference count
* reaches zero, @pusher is freed.
*/
void
matrix_pusher_unref(MatrixPusher *pusher)
{
if (--pusher->refcount == 0) {
matrix_pusher_free(pusher);
}
}
/**
* matrix_pusher_set_device_display_name:
* @pusher: a #MatrixPusher
* @device_display_name: a string that will allow the user to identify
* what device owns this pusher
*
* Set the device display name for @pusher.
*/
void
matrix_pusher_set_device_display_name(MatrixPusher *pusher,
const gchar *device_display_name)
{
g_free(pusher->device_display_name);
pusher->device_display_name = g_strdup(device_display_name);
}
/**
* matrix_pusher_get_device_display_name:
* @pusher: a #MatrixPusher
*
* Get the device display name from pusher.
*
* Returns: the device display name. The returned value is owned by
* @pusher and should not be freed nor modified.
*/
const gchar *
matrix_pusher_get_device_display_name(MatrixPusher *pusher)
{
return pusher->device_display_name;
}
/**
* matrix_pusher_set_app_display_name:
* @pusher: a #MatrixPusher
* @app_display_name: a string that will allow the user to identify
* what application owns the pusher
*
* Sets the app display name for the pusher.
*/
void
matrix_pusher_set_app_display_name(MatrixPusher *pusher,
const gchar *app_display_name)
{
g_free(pusher->app_display_name);
pusher->app_display_name = g_strdup(app_display_name);
}
/**
* matrix_pusher_get_app_display_name:
* @pusher: a #MatrixPusher
*
* Get the app display name for this pusher.
*
* Returns: the app display name. The returned value is owned by
* @pusher and should not be freed nor modified.
*/
const gchar *
matrix_pusher_get_app_display_name(MatrixPusher *pusher)
{
return pusher->app_display_name;
}
/**
* matrix_pusher_set_app_id:
* @pusher: a #MatrixPusher
* @app_id: a reverse DNS style identifier for the application. It is
* recommended that this ends with the platform, such that
* different platform versions get different app
* identifiers. Maximum length is 64 characters. If @app_id
* is longer than this, it will be truncated.
*
* Sets the application ID for this pusher.
*/
void
matrix_pusher_set_app_id(MatrixPusher *pusher, const gchar *app_id)
{
g_free(pusher->app_id);
pusher->app_id = g_strndup(app_id, 64);
}
/**
* matrix_pusher_get_app_id:
* @pusher: a #MatrixPusher
*
* Get the application ID for this pusher.
*
* Returns: the application ID. The returned value is owned by @pusher
* and should not be freed nor modified.
*/
const gchar *
matrix_pusher_get_app_id(MatrixPusher *pusher)
{
return pusher->app_id;
}
/**
* matrix_pusher_set_append:
* @pusher: a #MatrixPusher
* @append: if %TRUE, the homeserver should add another pusher with
* the given push key and app ID in addition to any others
* with different user IDs. Otherwise, the homeserver must
* remove any other pushers with the same App ID and pushkey
* for different users
*
* Set the appending rule for this pusher.
*/
void
matrix_pusher_set_append(MatrixPusher *pusher, gboolean append)
{
pusher->append = append;
}
/**
* matrix_pusher_get_append:
* @pusher: a #MatrixPusher
*
* Get the appending rule for this pusher. See
* matrix_pusher_set_append() for details.
*
* Returns: the append rule
*/
gboolean
matrix_pusher_get_append(MatrixPusher *pusher)
{
return pusher->append;
}
/**
* matrix_pusher_set_kind:
* @pusher: a #MatrixPusher
* @kind: the kind of pusher to configure. "http" makes a pusher that
* sends HTTP pokes. %NULL deletes the pusher.
*
* Set the kind of pusher to configure.
*/
void
matrix_pusher_set_kind(MatrixPusher *pusher, const gchar *kind)
{
g_free(pusher->kind);
pusher->kind = g_strdup(kind);
}
/**
* matrix_pusher_get_kind:
* @pusher: a #MatrixPusher
*
* Get the kind of pusher in @pusher.
*
* Returns: the kind of this pusher. The returned value is owned by
* @pusher and should not be freed nor modified
*/
const gchar *
matrix_pusher_get_kind(MatrixPusher *pusher)
{
return pusher->kind;
}
/**
* matrix_pusher_set_lang:
* @pusher: a #MatrixPusher
* @lang: the preferred language for receiving notifications,
* e.g. "en" or "en-US"
*
* Set the preferred language for receiving notifications.
*/
void
matrix_pusher_set_lang(MatrixPusher *pusher, const gchar *lang)
{
g_free(pusher->lang);
pusher->lang = g_strdup(lang);
}
/**
* matrix_pusher_get_lang:
* @pusher: a #MatrixPusher
*
* Get the preferred language for receiving notifications.
*
* Returns: the preferred language. The returned value is owned by
* @pusher and should not be modified nor freed
*/
const gchar *
matrix_pusher_get_lang(MatrixPusher *pusher)
{
return pusher->lang;
}
/**
* matrix_pusher_set_profile_tag:
* @pusher: a #MatrixPusher
* @profile_tag: a string that determines what set of device rules
* will be matched when evaluating push rules for this
* pusher. It is an arbitrary string. Multiple devices
* may use the same <code>profile_tag</code>. It is
* advised that when an app's data is copied or restored
* to a different device, this value remain the
* same. Client apps should offer ways to change the
* profile_tag, optionally copying rules from the old
* profile tag. Maximum length is 32 bytes. If
* @profile_tag is longer than this, it will be
* truncated
*
* Set the profile tag of this pusher.
*/
void
matrix_pusher_set_profile_tag(MatrixPusher *pusher, const gchar *profile_tag)
{
g_free(pusher->profile_tag);
pusher->profile_tag = g_strndup(profile_tag, 32);
}
/**
* matrix_pusher_get_profile_tag:
* @pusher: a #MatrixPusher
*
* Get the profile tag of this pusher.
*
* Returns: the profile tag. The returned value is owned by @pusher
* and should not be freed nor modified
*/
const gchar *
matrix_pusher_get_profile_tag(MatrixPusher *pusher)
{
return pusher->profile_tag;
}
/**
* matrix_pusher_set_pushkey:
* @pusher: a #MatrixPusher
* @pushkey: a unique identifier for this pusher. The value you should
* use for this is the routing or destination address
* information for the notification, for example, the APNS
* token for APNS or the Registration ID for GCM. If your
* notification client has no such concept, use any unique
* identifier. Maximum length is 512 bytes. If @pushkey is
* longer than this, it will be truncated
*
* Set the pushkey for this pusher.
*/
void
matrix_pusher_set_pushkey(MatrixPusher *pusher, const gchar *pushkey)
{
g_free(pusher->pushkey);
pusher->pushkey = g_strndup(pushkey, 512);
}
/**
* matrix_pusher_get_pushkey:
* @pusher: a #MatrixPusher
*
* Get the pushkey for this pusher.
*
* Returns: the pushkey. The returned value is owned by @pusher and
* should not be freed nor modified
*/
const gchar *
matrix_pusher_get_pushkey(MatrixPusher *pusher)
{
return pusher->pushkey;
}
/**
* matrix_pusher_set_data:
* @pusher: a #MatrixPusher
* @data: (transfer none): a dictionary of information for the pusher
* implementation itself. For example, if kind is "http", this
* should contain an "url" member, which is the URL to use to
* send notifications to. This function creates a deep copy of
* @data, so it can be freed after this call.
*
* Set some extra data for the pusher.
*/
void
matrix_pusher_set_data(MatrixPusher *pusher, const JsonNode *data)
{
if (pusher->data) {
json_node_free(pusher->data);
}
pusher->data = json_node_copy((JsonNode *)data);
}
/**
* matrix_pusher_take_data:
* @pusher: a #MatrixPusher
* @data: (transfer full): extra data for the pusher. See
* matrix_pusher_set_data() for details.
*
* Set some extra data for the pusher. It differs from
* matrix_pusher_set_data() that this call assumes ownership over
* @data, so it should not be freed by the caller.
*/
void
matrix_pusher_take_data(MatrixPusher *pusher, JsonNode *data)
{
if (pusher->data) {
json_node_free(pusher->data);
}
pusher->data = data;
}
/**
* matrix_pusher_get_data:
* @pusher: a #MatrixPusher
*
* Get the extra data of this pusher.
*
* Returns: (transfer none): the extra data. The returned value is
* owned by @pusher and should not be freed nor modified
*/
const JsonNode *
matrix_pusher_get_data(MatrixPusher *pusher)
{
return pusher->data;
}
/**
* matrix_pusher_get_json_node:
* @pusher: a #MatrixPusher
* @err: a #GError
*
* Get the JSON representation of the pusher data as a #JsonNode
* object. As in the pusher object all fields are mandatory, if any of
* them is %NULL, this function returns %NULL, and fills @err with
* %MATRIX_ERROR_INCOMPLETE.
*
* Returns: (transfer full): the JSON representation of the pusher
* data.
*/
JsonNode *
matrix_pusher_get_json_node(MatrixPusher *pusher, GError **err)
{
JsonBuilder *builder;
JsonNode *node;
if (!pusher->device_display_name
|| !pusher->app_display_name
|| !pusher->app_id
|| !pusher->data
|| !pusher->kind
|| !pusher->lang
|| !pusher->profile_tag
|| !pusher->pushkey) {
g_set_error(err,
MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE,
"Pusher data incomplete");
return NULL;
}
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "device_display_name");
json_builder_add_string_value(builder, pusher->device_display_name);
json_builder_set_member_name(builder, "app_display_name");
json_builder_add_string_value(builder, pusher->app_display_name);
json_builder_set_member_name(builder, "app_id");
json_builder_add_string_value(builder, pusher->app_id);
json_builder_set_member_name(builder, "append");
json_builder_add_boolean_value(builder, pusher->append);
json_builder_set_member_name(builder, "kind");
json_builder_add_string_value(builder, pusher->kind);
json_builder_set_member_name(builder, "lang");
json_builder_add_string_value(builder, pusher->lang);
json_builder_set_member_name(builder, "profile_tag");
json_builder_add_string_value(builder, pusher->profile_tag);
json_builder_set_member_name(builder, "pushkey");
json_builder_add_string_value(builder, pusher->pushkey);
json_builder_set_member_name(builder, "data");
json_builder_add_value(builder, pusher->data);
json_builder_end_object(builder);
node = json_builder_get_root(builder);
g_object_unref(builder);
return node;
}
/**
* matrix_pusher_get_json_data:
* @pusher: a #MatrixPusher
* @datalen: (out): storage for the the length of the JSON data or
* %NULL
* @err: a #GError
*
* Get the JSON representation of the pusher data as a string. As in
* the pusher object all fields are mandatory, if any of them is
* %NULL, this function returns %NULL, and fills @err with
* %MATRIX_ERROR_INCOMPLETE.
*
* Returns: (transfer full): the JSON representation of the pusher
* data.
*/
gchar *matrix_pusher_get_json_data(MatrixPusher *pusher,
gsize *datalen,
GError **err)
{
JsonGenerator *generator;
JsonNode *node;
gchar *data;
if ((node = matrix_pusher_get_json_node(pusher, err)) == NULL) {
return NULL;
}
generator = json_generator_new();
json_generator_set_root(generator, node);
json_node_free(node);
data = json_generator_to_data(generator, datalen);
g_object_unref(generator);
return data;
}
/**
* MatrixStateEvent:
*

View File

@ -129,41 +129,6 @@ typedef enum {
MATRIX_ACCOUNT_KIND_GUEST
} MatrixAccountKind;
typedef struct _MatrixPusher MatrixPusher;
GType matrix_pusher_get_type(void);
#define MATRIX_TYPE_PUSHER (matrix_pusher_get_type())
MatrixPusher *matrix_pusher_new(void);
MatrixPusher *matrix_pusher_ref(MatrixPusher *pusher);
void matrix_pusher_unref(MatrixPusher *pusher);
void matrix_pusher_set_device_display_name(MatrixPusher *pusher,
const gchar *device_display_name);
const gchar *matrix_pusher_get_device_display_name(MatrixPusher *pusher);
void matrix_pusher_set_app_display_name(MatrixPusher *pusher,
const gchar *app_display_name);
const gchar *matrix_pusher_get_app_display_name(MatrixPusher *pusher);
void matrix_pusher_set_app_id(MatrixPusher *pusher, const gchar *app_id);
const gchar *matrix_pusher_get_app_id(MatrixPusher *pusher);
void matrix_pusher_set_append(MatrixPusher *pusher, gboolean append);
gboolean matrix_pusher_get_append(MatrixPusher *pusher);
void matrix_pusher_set_kind(MatrixPusher *pusher, const gchar *kind);
const gchar *matrix_pusher_get_kind(MatrixPusher *pusher);
void matrix_pusher_set_lang(MatrixPusher *pusher, const gchar *lang);
const gchar *matrix_pusher_get_lang(MatrixPusher *pusher);
void matrix_pusher_set_profile_tag(MatrixPusher *pusher,
const gchar *profile_tag);
const gchar *matrix_pusher_get_profile_tag(MatrixPusher *pusher);
void matrix_pusher_set_pushkey(MatrixPusher *pusher, const gchar *pushkey);
const gchar *matrix_pusher_get_pushkey(MatrixPusher *pusher);
void matrix_pusher_set_data(MatrixPusher *pusher, const JsonNode *data);
void matrix_pusher_take_data(MatrixPusher *pusher, JsonNode *data);
const JsonNode *matrix_pusher_get_data(MatrixPusher *pusher);
JsonNode *matrix_pusher_get_json_node(MatrixPusher *pusher, GError **err);
gchar *matrix_pusher_get_json_data(MatrixPusher *pusher,
gsize *datalen,
GError **err);
typedef struct _MatrixStateEvent MatrixStateEvent;
GType matrix_state_event_get_type(void);