diff options
author | Dr. David Alan Gilbert <dave@treblig.org> | 2016-09-01 00:22:44 +0100 |
---|---|---|
committer | Dr. David Alan Gilbert <dave@treblig.org> | 2016-09-03 00:05:09 +0100 |
commit | 3857950b0b8c0310f1608d391db47d2e9f592e1a (patch) | |
tree | f7783cdc891de6429d8c2ec8f4bf719a2e4dec56 | |
parent | 511fa3c1e886fd968c6e29fcafd80f840b0b6f51 (diff) | |
download | purple-matrix-3857950b0b8c0310f1608d391db47d2e9f592e1a.tar.gz |
event hooks
Some events need to do something else before the event can be
transmitted, and indeed the contents of the event aren't necessarily
known until that other thing has happened.
Add a hook that gets called when the event is ready to be sent
and when the event is to be free'd.
Signed-off-by: Dr. David Alan Gilbert <dave@treblig.org>
-rw-r--r-- | matrix-event.c | 3 | ||||
-rw-r--r-- | matrix-event.h | 15 | ||||
-rw-r--r-- | matrix-room.c | 10 |
3 files changed, 26 insertions, 2 deletions
diff --git a/matrix-event.c b/matrix-event.c index 3f1b714..3413474 100644 --- a/matrix-event.c +++ b/matrix-event.c @@ -50,5 +50,8 @@ void matrix_event_free(MatrixRoomEvent *event) g_free(event->txn_id); g_free(event->sender); g_free(event->event_type); + if (event->hook) { + event->hook(event, TRUE); + } g_free(event); } diff --git a/matrix-event.h b/matrix-event.h index 2f079b2..6bdb35e 100644 --- a/matrix-event.h +++ b/matrix-event.h @@ -25,7 +25,14 @@ #include <glib.h> struct _JsonObject; +struct _MatrixRoomEvent; +/* Callback by events; + * called with 'just_free' false prior to sending an event. + * called with 'just_free' true when freeing the event. + */ +typedef void (*EventSendHook)(struct _MatrixRoomEvent *event, + gboolean just_free); typedef struct _MatrixRoomEvent { /* for outgoing events, our made-up transaction id. NULL for incoming * events. @@ -37,6 +44,14 @@ typedef struct _MatrixRoomEvent { gchar *event_type; struct _JsonObject *content; + + /* Hook (& data) called when the event is unqueued; the hook should + * do the send itself. + * Useful where a file has to be uploaded before sending the event. + */ + EventSendHook hook; + + void *hook_data; } MatrixRoomEvent; diff --git a/matrix-room.c b/matrix-room.c index ee5cdb5..4e003eb 100644 --- a/matrix-room.c +++ b/matrix-room.c @@ -375,6 +375,9 @@ static void _send_queued_event(PurpleConversation *conv) } else { event = queue -> data; g_assert(event != NULL); + if (event->hook) + return event->hook(event, FALSE); + purple_debug_info("matrixprpl", "Sending %s with txn id %s\n", event->event_type, event->txn_id); @@ -389,7 +392,8 @@ static void _send_queued_event(PurpleConversation *conv) static void _enqueue_event(PurpleConversation *conv, const gchar *event_type, - JsonObject *event_content) + JsonObject *event_content, + EventSendHook hook, void *hook_data) { MatrixRoomEvent *event; GList *event_queue; @@ -398,6 +402,8 @@ static void _enqueue_event(PurpleConversation *conv, const gchar *event_type, event = matrix_event_new(event_type, event_content); event->txn_id = g_strdup_printf("%"G_GINT64_FORMAT"%"G_GUINT32_FORMAT, g_get_monotonic_time(), g_random_int()); + event->hook = hook; + event->hook_data = hook_data; event_queue = _get_event_queue(conv); event_queue = g_list_append(event_queue, event); @@ -803,7 +809,7 @@ void matrix_room_send_message(PurpleConversation *conv, const gchar *message) json_object_set_string_member(content, "msgtype", type_string); json_object_set_string_member(content, "body", message_to_send); - _enqueue_event(conv, "m.room.message", content); + _enqueue_event(conv, "m.room.message", content, NULL, NULL); json_object_unref(content); purple_conv_chat_write(chat, _get_my_display_name(conv), |