Add MatrixAPI:user-id property

This commit is contained in:
Gergely Polonkai 2016-01-04 17:39:48 +01:00
parent 8f2a8e23a7
commit f6fa56e7b0
4 changed files with 75 additions and 0 deletions

View File

@ -34,6 +34,7 @@ matrix_api_set_token
matrix_api_get_token
matrix_api_set_refresh_token
matrix_api_get_refresh_token
matrix_api_get_user_id
<SUBSECTION>
matrix_api_media_download

View File

@ -451,6 +451,20 @@ matrix_api_default_init(MatrixAPIInterface *iface)
"The token issued by the server for authorization token renewal",
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* MatrixAPI:user-id:
*
* The Matrix user ID that is currently authenticated to the
* server. It is set by the registration and login processes
* automatically, and cannot be set from outside.
*/
g_object_interface_install_property(
iface,
g_param_spec_string("user-id", "User ID",
"The Matrix user ID that is authenticated to the server",
NULL,
G_PARAM_READABLE));
}
/* Property getters and setters */
@ -534,6 +548,30 @@ matrix_api_set_refresh_token(MatrixAPI *api, const gchar *refresh_token)
->set_refresh_token(api, refresh_token);
}
/**
* matrix_api_get_user_id:
* @api: a #MatrixAPI implementation
*
* Get the Matrix user ID that is currently authenticated with the
* server.
*
* Returns: (transfer none) (allow-none): the Matrix user ID
* authenticated by the last successful register or login
* call (ie. matrix_api_login() or
* matrix_api_register_account()). If no user is
* authenticated, this function returns %NULL. The returned
* value is owned by the @api object and should not be freed
* nor modified
*/
const gchar *
matrix_api_get_user_id(MatrixAPI *api)
{
g_return_if_fail(MATRIX_IS_API(api));
return MATRIX_API_GET_IFACE(api)
->get_user_id(api);
}
/* Media */
/**

View File

@ -174,6 +174,8 @@ struct _MatrixAPIInterface {
const gchar *(*get_refresh_token)(MatrixAPI *api);
void (*set_refresh_token)(MatrixAPI *api, const gchar *refresh_token);
const gchar *(*get_user_id)(MatrixAPI *api);
void *properties_reserved[10];
/* Media */
@ -586,6 +588,7 @@ void matrix_api_set_token(MatrixAPI *api, const gchar *token);
const gchar *matrix_api_get_token(MatrixAPI *api);
void matrix_api_set_refresh_token(MatrixAPI *api, const gchar *refresh_token);
const gchar *matrix_api_get_refresh_token(MatrixAPI *api);
const gchar *matrix_api_get_user_id(MatrixAPI *api);
/* API definition */

View File

@ -53,6 +53,7 @@ typedef struct _MatrixHTTPAPIPrivate {
SoupURI *uri;
gchar *token;
gchar *refresh_token;
gchar *user_id;
gboolean validate_certificate;
} MatrixHTTPAPIPrivate;
@ -61,6 +62,7 @@ enum {
PROP_BASE_URL,
PROP_TOKEN,
PROP_REFRESH_TOKEN,
PROP_USER_ID,
N_PROPERTIES
};
@ -78,6 +80,7 @@ static void i_set_token(MatrixAPI *api, const gchar *token);
static const gchar *i_get_token(MatrixAPI *api);
static void i_set_refresh_token(MatrixAPI *api, const gchar *refresh_token);
static const gchar *i_get_refresh_token(MatrixAPI *api);
static const gchar *i_get_user_id(MatrixAPI *api);
G_DEFINE_TYPE_WITH_CODE(MatrixHTTPAPI, matrix_http_api, G_TYPE_OBJECT,
G_ADD_PRIVATE(MatrixHTTPAPI)
@ -92,6 +95,7 @@ matrix_http_api_finalize(GObject *gobject)
g_free(priv->token);
g_free(priv->refresh_token);
g_free(priv->user_id);
g_signal_handlers_destroy(gobject);
G_OBJECT_CLASS(matrix_http_api_parent_class)->finalize(gobject);
@ -214,6 +218,11 @@ matrix_http_api_get_property(GObject *gobject,
break;
case PROP_USER_ID:
g_value_set_string(value, i_get_user_id(MATRIX_API(api)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
}
@ -264,6 +273,7 @@ matrix_http_api_class_init(MatrixHTTPAPIClass *klass)
g_object_class_override_property(gobject_class,
PROP_REFRESH_TOKEN,
"refresh-token");
g_object_class_override_property(gobject_class, PROP_USER_ID, "user-id");
}
static void
@ -274,6 +284,7 @@ matrix_http_api_init(MatrixHTTPAPI *api)
priv->uri = NULL;
priv->token = NULL;
priv->refresh_token = NULL;
priv->user_id = NULL;
priv->validate_certificate = TRUE;
priv->soup_session = soup_session_new();
}
@ -334,6 +345,15 @@ i_get_refresh_token(MatrixAPI *api)
return priv->refresh_token;
}
static const gchar *
i_get_user_id(MatrixAPI *api)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(
MATRIX_HTTP_API(api));
return priv->user_id;
}
/**
* matrix_http_api_set_validate_certificate:
* @api: a #MatrixHTTPAPI implementation
@ -437,6 +457,18 @@ _response_callback(SoupSession *session,
g_debug("Our home server calls itself %s", homeserver);
}
/* Check if the response holds a user ID; if it does,
* set this as our user ID */
if ((node = json_object_get_member(
root_object, "user_id")) != NULL) {
const gchar *user_id = json_node_get_string(node);
g_free(priv->user_id);
priv->user_id = g_strdup(user_id);
g_debug("We are reported to be logged in as %s", user_id);
}
/* Check if the response holds an error code */
if ((node = json_object_get_member(
root_object, "errcode")) != NULL) {
@ -598,5 +630,6 @@ matrix_http_api_matrix_api_init(MatrixAPIInterface *iface)
iface->get_token = i_get_token;
iface->set_refresh_token = i_set_refresh_token;
iface->get_refresh_token = i_get_refresh_token;
iface->get_user_id = i_get_user_id;
iface->login = i_login;
}