Skip to content

Commit

Permalink
Merge pull request #24 from zamaudio/enable-invite
Browse files Browse the repository at this point in the history
Enable room invites when matrix handle is known
  • Loading branch information
richvdh authored Jan 5, 2017
2 parents 28edbbb + 2c17db4 commit ccf4958
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
3 changes: 3 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ Eion Robb <eion at robbmob.com>

Dr. David Alan Gilbert <dave at treblig.org>
* Sending images, emote handling

Damien Zammit <damien at zamaudio.com>
* Invite users
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ implemented. Sending and receiving simple text messages is supported, as is
joining rooms you are invited to by other users.

The following are not yet supported:
* Sending invitations
* Creating new rooms (and one-to-one chats)
* Joining existing rooms by alias instead of room_id
* Presence indication
Expand Down
11 changes: 10 additions & 1 deletion libmatrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "matrix-connection.h"
#include "matrix-room.h"
#include "matrix-api.h"

/**
* Called to get the icon name for the given buddy and account.
Expand Down Expand Up @@ -178,7 +179,15 @@ static void matrixprpl_reject_chat(PurpleConnection *gc, GHashTable *components)
matrix_connection_reject_invite(gc, room_id);
}

static void matrixprpl_chat_invite(PurpleConnection *gc, int id,
const char *message, const char *who)
{
PurpleConversation *conv = purple_find_chat(gc, id);
MatrixConnectionData *conn;
conn = (MatrixConnectionData *)(conv->account->gc->proto_data);

matrix_api_invite_user(conn, conv->name, who, NULL, NULL, NULL, NULL);
}

/**
* handle leaving a chat: notify the server that we are leaving, and
Expand Down Expand Up @@ -279,7 +288,7 @@ static PurplePluginProtocolInfo prpl_info =
matrixprpl_join_chat, /* join_chat */
matrixprpl_reject_chat, /* reject_chat */
matrixprpl_get_chat_name, /* get_chat_name */
NULL, /* chat_invite */
matrixprpl_chat_invite, /* chat_invite */
matrixprpl_chat_leave, /* chat_leave */
NULL, /* chat_whisper */
matrixprpl_chat_send, /* chat_send */
Expand Down
41 changes: 41 additions & 0 deletions matrix-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,47 @@ MatrixApiRequestData *matrix_api_send(MatrixConnectionData *conn,
return fetch_data;
}

void matrix_api_invite_user(MatrixConnectionData *conn,
const gchar *room_id,
const gchar *who,
MatrixApiCallback callback,
MatrixApiErrorCallback error_callback,
MatrixApiBadResponseCallback bad_response_callback,
gpointer user_data)
{
GString *url;
JsonNode *body_node;
JsonGenerator *generator;
gchar *json;

JsonObject *invitee;
invitee = json_object_new();
json_object_set_string_member(invitee, "user_id", who);

url = g_string_new(conn->homeserver);
g_string_append(url, "_matrix/client/r0/rooms/");
g_string_append(url, purple_url_encode(room_id));
g_string_append(url, "/invite?access_token=");
g_string_append(url, purple_url_encode(conn->access_token));

body_node = json_node_new(JSON_NODE_OBJECT);
json_node_set_object(body_node, invitee);

generator = json_generator_new();
json_generator_set_root(generator, body_node);
json = json_generator_to_data(generator, NULL);
g_object_unref(G_OBJECT(generator));
json_node_free(body_node);

purple_debug_info("matrixprpl", "sending an invite on %s\n", room_id);

matrix_api_start(url->str, "POST", json, conn, callback,
error_callback, bad_response_callback,
user_data, 0);
g_free(json);
g_string_free(url, TRUE);
json_object_unref(invitee);
}

MatrixApiRequestData *matrix_api_join_room(MatrixConnectionData *conn,
const gchar *room,
Expand Down
24 changes: 24 additions & 0 deletions matrix-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,30 @@ MatrixApiRequestData *matrix_api_send(MatrixConnectionData *conn,
MatrixApiBadResponseCallback bad_response_callback,
gpointer user_data);

/**
* Invite a user to a room
*
* @param conn The connection with which to make the request
* @param room_id The room id to invite the user to
*
* @param who The mxid of the person to invite
*
* @param callback Function to be called when the request completes
* @param error_callback Function to be called if there is an error making
* the request. If NULL, matrix_api_error will be
* used.
* @param bad_response_callback Function to be called if the API gives a non-200
* response. If NULL, matrix_api_bad_response will be
* used.
* @param user_data Opaque data to be passed to the callbacks
*/
void matrix_api_invite_user(MatrixConnectionData *conn,
const gchar *room_id,
const gchar *who,
MatrixApiCallback callback,
MatrixApiErrorCallback error_callback,
MatrixApiBadResponseCallback bad_response_callback,
gpointer user_data);

/**
* Make a request to join a room
Expand Down

0 comments on commit ccf4958

Please sign in to comment.