Redesign MatrixPresenceEvent

It is now a MatrixEvent subclass.
This commit is contained in:
Gergely Polonkai 2016-02-02 16:51:57 +01:00
parent 3e6a021a0f
commit 1073eecb2f
7 changed files with 151 additions and 130 deletions

1
.gitignore vendored
View File

@ -67,5 +67,6 @@ Makefile.in
/src/matrix-http-client.c
/src/matrix-compacts.c
/src/matrix-event.c
/src/matrix-presence-event.c
/src/matrix-room-event.c
/src/matrix-room-member-event.c

View File

@ -22,6 +22,7 @@
<title>Types</title>
<xi:include href="xml/matrix-types.xml"/>
<xi:include href="xml/matrix-event.xml"/>
<xi:include href="xml/matrix-presence-event.xml"/>
<xi:include href="xml/matrix-room-member-event.xml"/>
</section>

View File

@ -1,3 +1,27 @@
<FILE>matrix-presence-event</FILE>
<TITLE>MatrixPresenceEvent</TITLE>
matrix_presence_event_set_display_name
matrix_presence_event_get_display_name
matrix_presence_event_set_avatar_url
matrix_presence_event_get_avatar_url
matrix_presence_event_set_last_active_ago
matrix_presence_event_get_last_active_ago
matrix_presence_event_set_presence
matrix_presence_event_get_presence
<SUBSECTION Standard>
MatrixPresenceEvent
MatrixPresenceEventPrivate
MATRIX_TYPE_PRESENCE_EVENT
MATRIX_IS_PRESENCE_EVENT
MATRIX_IS_PRESENCE_EVENT_CLASS
MATRIX_PRESENCE_EVENT
MATRIX_PRESENCE_EVENT_CLASS
MATRIX_PRESENCE_EVENT_GET_CLASS
matrix_presence_event_get_type
matrix_presence_event_construct
</SECTION>
<SECTION>
<FILE>matrix-room-member-event</FILE>
<TITLE>MatrixRoomMemberEvent</TITLE>
@ -236,23 +260,6 @@ matrix_state_event_get_content
matrix_state_event_get_json_node
matrix_state_event_get_json_data
<SUBSECTION>
MatrixPresenceEvent
matrix_presence_event_new
matrix_presence_event_new_from_json
matrix_presence_event_ref
matrix_presence_event_unref
matrix_presence_event_set_user_id
matrix_presence_event_get_user_id
matrix_presence_event_set_display_name
matrix_presence_event_get_display_name
matrix_presence_event_set_avatar_url
matrix_presence_event_get_avatar_url
matrix_presence_event_set_presence
matrix_presence_event_get_presence
matrix_presence_event_set_last_active_ago
matrix_presence_event_get_last_active_ago
<SUBSECTION>
MatrixUnsignedEventData
matrix_unsigned_event_data_new
@ -302,8 +309,6 @@ MATRIX_TYPE_PUSHER
matrix_pusher_get_type
MATRIX_TYPE_STATE_EVENT
matrix_state_event_get_type
MATRIX_TYPE_PRESENCE_EVENT
matrix_presence_event_get_type
MATRIX_TYPE_UNSIGNED_EVENT_DATA
matrix_unsigned_event_data_get_type
</SECTION>

View File

@ -23,6 +23,7 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \
matrix-http-client.vala \
matrix-compacts.vala \
matrix-event.vala \
matrix-presence-event.vala \
matrix-room-event.vala \
matrix-room-member-event.vala \
$(NULL)

View File

@ -541,117 +541,6 @@ namespace Matrix {
}
}
public class PresenceEvent : JsonCompact {
/**
* The user ID in the presence event
*/
public string? user_id { get; set; default = null; }
/**
* The display name in the presence event
*/
public string? display_name { get; set; default = null; }
/**
* The avatar URL in the presence event
*/
public string? avatar_url { get; set; default = null; }
/**
* The amount of time, in seconds, since the user is inactive.
*/
public ulong last_active_ago { get; set; default = 0; }
/**
* The presence status of the user in the presence event.
*/
public Presence presence { get; set; default = Presence.UNKNOWN; }
/**
* Create a PresenceEvent object based on @param json_data.
*/
public
PresenceEvent.from_json(Json.Node json_data)
requires(json_data.get_node_type() == Json.NodeType.OBJECT)
{
var copied_data = _json_node_deep_copy(json_data);
var root = copied_data.get_object();
Json.Node? node;
if ((node = root.get_member("user_id")) != null) {
user_id = node.get_string();
root.remove_member("user_id");
}
if ((node = root.get_member("displayname")) != null) {
display_name = node.get_string();
root.remove_member("displayname");
}
if ((node = root.get_member("avatar_url")) != null) {
avatar_url = node.get_string();
root.remove_member("avatar_url");
}
if ((node = root.get_member("last_active_ago")) != null) {
last_active_ago = (ulong)node.get_int();
root.remove_member("last_active_ago");
}
if ((node = root.get_member("presence")) != null) {
Presence? presence_val = (Presence)_g_enum_nick_to_value(
typeof(Presence),
node.get_string());
if (presence_val != null) {
presence = presence_val;
} else {
warning("Unknown presence value '%s'. Please report it to the Matrix.org GLib SDK project.",
node.get_string());
}
root.remove_member("presence");
}
// By now, root should be empty
if (root.get_size() > 0) {
warning("Some members remained in the received presence event. This may be a bug in Matrix GLib");
}
}
/**
* Get the presence event as a JSON node.
*/
public override Json.Node?
get_json_node()
throws Matrix.Error
{
var builder = new Json.Builder();
builder.begin_object();
builder.set_member_name("user_id");
builder.add_string_value(user_id);
builder.set_member_name("displayname");
builder.add_string_value(display_name);
builder.set_member_name("avatar_url");
builder.add_string_value(avatar_url);
builder.set_member_name("last_active_ago");
builder.add_int_value(last_active_ago);
builder.set_member_name("presence");
builder.add_string_value(
_g_enum_value_to_nick(typeof(Presence), presence));
builder.end_object();
return builder.get_root();
}
}
public class StateEvent : JsonCompact {
public string? state_key { get; set; default = null; }
public Json.Node? content { get; set; default = null; }

View File

@ -155,6 +155,9 @@ matrix_event_types_ctor(void)
matrix_event_register_type("m.room.member",
MATRIX_TYPE_ROOM_MEMBER_EVENT,
NULL);
matrix_event_register_type("m.presence",
MATRIX_TYPE_PRESENCE_EVENT,
NULL);
}
void

View File

@ -0,0 +1,121 @@
/*
* This file is part of matrix-glib-sdk
*
* matrix-glib-sdk is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* matrix-glib-sdk is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with matrix-glib-sdk. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* Class for representing presence events
*
* The presence event class.
*/
public class Matrix.PresenceEvent : Matrix.Event {
public string? avatar_url { get; set; }
public string? display_name { get; set; }
public ulong? last_active_ago { get; set; }
public Matrix.Presence presence {
get;
set;
default = Matrix.Presence.UNKNOWN;
}
protected override void
from_json(Json.Node json_data)
throws Matrix.Error
{
Json.Object content_root = json_data.get_object().get_member("content").get_object();
Json.Node? node;
if ((node = content_root.get_member("user_id")) != null) {
_sender = node.get_string();
} else {
GLib.warning("user_id is missing from the m.presence event");
}
if ((node = content_root.get_member("last_active_ago")) != null) {
_last_active_ago = (ulong)node.get_int();
} else {
_last_active_ago = null;
}
if ((node = content_root.get_member("avatar_url")) != null) {
_avatar_url = node.get_string();
}
if ((node = content_root.get_member("displayname")) != null) {
_display_name = node.get_string();
}
if ((node = content_root.get_member("presence")) != null) {
Matrix.Presence? pres = (Matrix.Presence?)_g_enum_nick_to_value(
typeof(Matrix.Presence), node.get_string());
if (pres != null) {
_presence = pres;
} else {
_presence = Matrix.Presence.UNKNOWN;
}
} else {
GLib.warning("presence is missing from the m.presence event");
}
base.from_json(json_data);
}
protected override void
to_json(Json.Node json_data)
throws Matrix.Error
{
Json.Object content_root;
string? pres;
if (presence == Matrix.Presence.UNKNOWN) {
throw new Matrix.Error.UNKNOWN_VALUE (
"unkwnown presence cannot be added to a presence event");
}
if (sender == null) {
throw new Matrix.Error.INCOMPLETE (
"sender must be set for presence events!");
}
content_root = _json_object_node_ensure_field(json_data,
"content",
Json.NodeType.OBJECT)
.get_object();
content_root.set_string_member("user_id", sender);
if (last_active_ago != null) {
content_root.set_int_member("last_active_ago", last_active_ago);
}
if (avatar_url != null) {
content_root.set_string_member("avatar_url", avatar_url);
}
if (display_name != null) {
content_root.set_string_member("displayname", display_name);
}
pres = _g_enum_value_to_nick(typeof(Matrix.Presence), presence);
if (pres != null) {
content_root.set_string_member("presence", pres);
}
base.to_json(json_data);
}
}