Add get_user_presence() and get_user_profile() to Client

This commit is contained in:
Gergely Polonkai 2016-03-14 23:04:39 +00:00
parent 04950f8c34
commit 62eb4e7e21
3 changed files with 105 additions and 0 deletions

View File

@ -172,4 +172,25 @@ public interface Matrix.Client : GLib.Object {
public extern void
connect_event(GLib.Type event_gtype,
owned EventCallback event_callback);
/**
* Get the profile of a user specified by @param user_id.
* If @param room_id is not null, return the room-specific
* profile. If the user's profile is not cached yet,
* Matrix.Error.UNAVAILABLE is thrown.
*/
public abstract Profile?
get_user_profile(string user_id, string? room_id = null)
throws Matrix.Error;
/**
* Get the presence state of a user specified
* by @param user_id. If @param room_id is null, return
* the room specific presence state. If the user's presence
* state is not cached yet, Matrix.Error.UNAVAILABLE is
* thrown.
*/
public abstract Presence?
get_user_presence(string user_id, string? room_id = null)
throws Matrix.Error;
}

View File

@ -24,6 +24,10 @@ public class Matrix.HTTPClient : Matrix.HTTPAPI, Matrix.Client {
private bool _polling = false;
private ulong _event_timeout = 30000;
private string? _last_sync_token;
private Gee.HashMap<string, Profile> _user_global_profiles =
new Gee.HashMap<string, Profile>();
private Gee.HashMap<string, Presence> _user_global_presence =
new Gee.HashMap<string, Presence>();
public
HTTPClient(string base_url)
@ -109,6 +113,42 @@ public class Matrix.HTTPClient : Matrix.HTTPAPI, Matrix.Client {
evt = null;
}
if (evt != null) {
string? user_id = null;
if (evt.get_type().is_a(typeof(Matrix.Event.Presence))) {
var pevt = (Matrix.Event.Presence)evt;
user_id = pevt.user_id;
_user_global_presence[user_id] = pevt.presence;
Profile? profile = _user_global_profiles[user_id];
if (profile == null) {
profile = new Profile();
_user_global_profiles[user_id] = profile;
}
profile.avatar_url = pevt.avatar_url;
profile.display_name = pevt.display_name;
} else if (evt.get_type().is_a(typeof(Matrix.Event.RoomMember))) {
// The following is a temporary hack until per-room
// profiles get implemented in HSes.
var mevt = (Matrix.Event.RoomMember)evt;
user_id = mevt.user_id;
Profile? profile = _user_global_profiles[user_id];
if (profile == null) {
profile = new Profile();
_user_global_profiles[user_id] = profile;
}
profile.avatar_url = mevt.avatar_url;
profile.display_name = mevt.display_name;
}
}
incoming_event(room_id, event_node, evt);
}
@ -281,4 +321,44 @@ public class Matrix.HTTPClient : Matrix.HTTPAPI, Matrix.Client {
abort_pending();
}
}
public Profile?
get_user_profile(string user_id, string? room_id = null)
throws Matrix.Error
{
if (room_id == null) {
var profile = _user_global_profiles[user_id];
if (profile == null) {
throw new Matrix.Error.UNAVAILABLE(
"Global profile for %s is not cached yet.",
user_id);
}
return profile;
}
throw new Matrix.Error.UNSUPPORTED(
"Per-room profiles are not supported yet.");
}
public Presence?
get_user_presence(string user_id, string? room_id = null)
throws Matrix.Error
{
if (room_id == null) {
Presence? presence = _user_global_presence[user_id];
if (presence == null) {
throw new Matrix.Error.UNAVAILABLE(
"Global presence for %s is not cached yet.",
user_id);
}
return presence;
}
throw new Matrix.Error.UNSUPPORTED(
"Per-room presences are not supported yet.");
}
}

View File

@ -38,6 +38,10 @@ namespace Matrix {
INVALID_FORMAT, /// the format of the JSON node is
/// invalid (e.g. it is an array
/// instead of an object)
UNAVAILABLE, /// the requested data is not cached
/// yet. Clients getting this message
/// may go online by some means to get
/// the data
/* Add Matrix-defined error codes under here, prefixing them with
* `MATRIX_ERROR_`, i.e. `M_FORBIDDEN` =>