2016-02-02 15:51:57 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
2016-03-09 13:23:36 +00:00
|
|
|
|
* Informs the client of a user's presence state change.
|
2016-02-02 15:51:57 +00:00
|
|
|
|
*/
|
2016-03-07 08:44:06 +00:00
|
|
|
|
public class Matrix.Event.Presence : Matrix.Event.Base {
|
2016-03-09 13:23:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* The current avatar URL for this user, if any.
|
|
|
|
|
*/
|
2016-02-02 15:51:57 +00:00
|
|
|
|
public string? avatar_url { get; set; }
|
2016-03-09 13:23:36 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The current display name for this user, if any.
|
|
|
|
|
*/
|
2016-02-02 15:51:57 +00:00
|
|
|
|
public string? display_name { get; set; }
|
2016-03-09 13:23:36 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The last time since this used performed some action, in
|
|
|
|
|
* milliseconds.
|
2016-03-17 16:02:08 +00:00
|
|
|
|
*
|
|
|
|
|
* This won’t get into the generated event JSON if negative.
|
2016-03-09 13:23:36 +00:00
|
|
|
|
*/
|
2016-03-17 16:02:08 +00:00
|
|
|
|
public long last_active_ago { get; set; default = -1; }
|
2016-03-09 13:23:36 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The user's ID.
|
|
|
|
|
*/
|
2016-03-07 08:44:06 +00:00
|
|
|
|
public string? user_id { get; set; default = null; }
|
2016-03-09 13:23:36 +00:00
|
|
|
|
|
|
|
|
|
public string? event_id { get; set; default = null; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The presence state for this user.
|
|
|
|
|
*/
|
2016-02-02 15:51:57 +00:00
|
|
|
|
public Matrix.Presence presence {
|
2016-03-09 13:23:36 +00:00
|
|
|
|
get; set;
|
|
|
|
|
|
2016-02-02 15:51:57 +00:00
|
|
|
|
default = Matrix.Presence.UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void
|
|
|
|
|
from_json(Json.Node json_data)
|
|
|
|
|
throws Matrix.Error
|
|
|
|
|
{
|
2016-03-09 13:23:36 +00:00
|
|
|
|
var root = json_data.get_object();
|
|
|
|
|
var content_root = root.get_member("content").get_object();
|
2016-02-02 15:51:57 +00:00
|
|
|
|
Json.Node? node;
|
|
|
|
|
|
2016-03-09 13:23:36 +00:00
|
|
|
|
if ((node = root.get_member("event_id")) != null) {
|
|
|
|
|
_event_id = node.get_string();
|
|
|
|
|
} else if (Config.DEBUG) {
|
|
|
|
|
warning("event_id is missing from a m.presence event");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 15:51:57 +00:00
|
|
|
|
if ((node = content_root.get_member("user_id")) != null) {
|
2016-03-07 08:44:06 +00:00
|
|
|
|
_user_id = node.get_string();
|
2016-03-05 07:02:39 +00:00
|
|
|
|
} else if (Config.DEBUG) {
|
2016-03-09 13:23:36 +00:00
|
|
|
|
warning("content.user_id is missing from the m.presence event");
|
2016-03-17 13:56:13 +00:00
|
|
|
|
|
|
|
|
|
// Workaround for having sender instead of content.user_id
|
|
|
|
|
// in most (room-dependent) presence events
|
|
|
|
|
if ((node = root.get_member("sender")) != null) {
|
|
|
|
|
_user_id = node.get_string();
|
|
|
|
|
}
|
2016-02-02 15:51:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((node = content_root.get_member("last_active_ago")) != null) {
|
2016-03-17 16:02:08 +00:00
|
|
|
|
_last_active_ago = (long)node.get_int();
|
2016-02-02 15:51:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2016-03-09 13:23:36 +00:00
|
|
|
|
|
|
|
|
|
if (Config.DEBUG) {
|
|
|
|
|
warning("Unknown value %s for content.presence in a m.presence event",
|
|
|
|
|
node.get_string());
|
|
|
|
|
}
|
2016-02-02 15:51:57 +00:00
|
|
|
|
}
|
2016-03-05 07:02:39 +00:00
|
|
|
|
} else if (Config.DEBUG) {
|
2016-03-09 13:23:36 +00:00
|
|
|
|
warning("content.presence is missing from the m.presence event");
|
2016-02-02 15:51:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.from_json(json_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void
|
|
|
|
|
to_json(Json.Node json_data)
|
|
|
|
|
throws Matrix.Error
|
|
|
|
|
{
|
2016-03-09 13:23:36 +00:00
|
|
|
|
if (_presence == Matrix.Presence.UNKNOWN) {
|
|
|
|
|
throw new Matrix.Error.UNKNOWN_VALUE(
|
|
|
|
|
"Won't generate a m.presence event with an unkwnown presence");
|
2016-02-02 15:51:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 08:44:06 +00:00
|
|
|
|
if (_user_id == null) {
|
2016-03-09 13:23:36 +00:00
|
|
|
|
throw new Matrix.Error.INCOMPLETE(
|
|
|
|
|
"Won't generate a m.presence event without sender");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_event_id == null) {
|
|
|
|
|
throw new Matrix.Error.INCOMPLETE(
|
|
|
|
|
"Won't generate a m.presence event without event_id");
|
2016-02-02 15:51:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 13:23:36 +00:00
|
|
|
|
var root = json_data.get_object();
|
|
|
|
|
var content_root = root.get_member("content").get_object();
|
|
|
|
|
|
|
|
|
|
root.set_string_member("event_id", _event_id);
|
2016-02-02 15:51:57 +00:00
|
|
|
|
|
2016-03-07 08:44:06 +00:00
|
|
|
|
content_root.set_string_member("user_id", _user_id);
|
2016-03-09 13:23:36 +00:00
|
|
|
|
content_root.set_string_member("presence",
|
|
|
|
|
_g_enum_value_to_nick(typeof(Presence),
|
|
|
|
|
_presence));
|
2016-02-02 15:51:57 +00:00
|
|
|
|
|
2016-03-17 16:02:08 +00:00
|
|
|
|
if (last_active_ago >= 0) {
|
2016-02-02 15:51:57 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.to_json(json_data);
|
|
|
|
|
}
|
|
|
|
|
}
|