From 211cd11c2a310e336ecd8760d8f33fd04dc99590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sun, 5 May 2024 22:30:04 +0200 Subject: relay/api: add parameter "lines_free" in GET /api/buffers This parameter is the number of lines to return for buffers with free content. Its default value is `0` if "lines" is set to `0`, otherwise all buffer lines are returned. --- src/plugins/relay/api/relay-api-msg.c | 4 ++ src/plugins/relay/api/relay-api-msg.h | 1 + src/plugins/relay/api/relay-api-protocol.c | 23 ++++++---- .../unit/plugins/relay/api/test-relay-api-msg.cpp | 51 +++++++++++++++++++--- 4 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/plugins/relay/api/relay-api-msg.c b/src/plugins/relay/api/relay-api-msg.c index d02868301..71b990b02 100644 --- a/src/plugins/relay/api/relay-api-msg.c +++ b/src/plugins/relay/api/relay-api-msg.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -355,6 +356,7 @@ relay_api_msg_buffer_add_local_vars_cb (void *data, cJSON * relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer, long lines, + long lines_free, int nicks, enum t_relay_api_colors colors) { @@ -379,6 +381,8 @@ relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer, MSG_ADD_HDATA_STR("short_name", "short_name"); MSG_ADD_HDATA_VAR(Number, "number", integer, "number"); ptr_string = weechat_buffer_get_string (buffer, "type"); + if (weechat_strcmp (ptr_string, "free") == 0) + lines = lines_free; MSG_ADD_STR_PTR("type", ptr_string); MSG_ADD_HDATA_STR_COLORS("title", "title"); MSG_ADD_HDATA_VAR(Bool, "nicklist", integer, "nicklist"); diff --git a/src/plugins/relay/api/relay-api-msg.h b/src/plugins/relay/api/relay-api-msg.h index 3dc72620c..8d171ef78 100644 --- a/src/plugins/relay/api/relay-api-msg.h +++ b/src/plugins/relay/api/relay-api-msg.h @@ -40,6 +40,7 @@ extern int relay_api_msg_send_event (struct t_relay_client *client, cJSON *json_body); extern cJSON *relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer, long lines, + long lines_free, int nicks, enum t_relay_api_colors colors); extern cJSON *relay_api_msg_key_to_json (struct t_gui_key *key); diff --git a/src/plugins/relay/api/relay-api-protocol.c b/src/plugins/relay/api/relay-api-protocol.c index d73001b28..a025db3cc 100644 --- a/src/plugins/relay/api/relay-api-protocol.c +++ b/src/plugins/relay/api/relay-api-protocol.c @@ -71,7 +71,7 @@ relay_api_protocol_signal_buffer_cb (const void *pointer, void *data, struct t_gui_line *ptr_line; struct t_gui_line_data *ptr_line_data; cJSON *json; - long lines; + long lines, lines_free; long long buffer_id; int nicks; const char *ptr_id; @@ -142,18 +142,21 @@ relay_api_protocol_signal_buffer_cb (const void *pointer, void *data, /* we get all lines and nicks when a buffer is opened, otherwise none */ if (strcmp (signal, "buffer_opened") == 0) { - lines = LONG_MIN; + lines = LONG_MAX; + lines_free = LONG_MAX; nicks = 1; } else { lines = 0; + lines_free = 0; nicks = 0; } /* build body with buffer info */ json = relay_api_msg_buffer_to_json ( - ptr_buffer, lines, nicks, RELAY_API_DATA(ptr_client, sync_colors)); + ptr_buffer, lines, lines_free, nicks, + RELAY_API_DATA(ptr_client, sync_colors)); /* send to client */ if (json) @@ -462,7 +465,7 @@ RELAY_API_PROTOCOL_CALLBACK(buffers) { cJSON *json; struct t_gui_buffer *ptr_buffer; - long lines; + long lines, lines_free; int nicks; enum t_relay_api_colors colors; @@ -489,7 +492,7 @@ RELAY_API_PROTOCOL_CALLBACK(buffers) /* sub-resource of buffers */ if (strcmp (client->http_req->path_items[3], "lines") == 0) { - lines = relay_http_get_param_long (client->http_req, "lines", -100L); + lines = relay_http_get_param_long (client->http_req, "lines", LONG_MAX); json = relay_api_msg_lines_to_json (ptr_buffer, lines, colors); } else if (strcmp (client->http_req->path_items[3], "nicks") == 0) @@ -511,9 +514,13 @@ RELAY_API_PROTOCOL_CALLBACK(buffers) else { lines = relay_http_get_param_long (client->http_req, "lines", 0L); + lines_free = relay_http_get_param_long (client->http_req, + "lines_free", + (lines == 0) ? 0 : LONG_MAX); if (ptr_buffer) { - json = relay_api_msg_buffer_to_json (ptr_buffer, lines, nicks, colors); + json = relay_api_msg_buffer_to_json (ptr_buffer, lines, lines_free, + nicks, colors); } else { @@ -526,8 +533,8 @@ RELAY_API_PROTOCOL_CALLBACK(buffers) { cJSON_AddItemToArray ( json, - relay_api_msg_buffer_to_json (ptr_buffer, - lines, nicks, colors)); + relay_api_msg_buffer_to_json (ptr_buffer, lines, lines_free, + nicks, colors)); ptr_buffer = weechat_hdata_move (relay_hdata_buffer, ptr_buffer, 1); } } diff --git a/tests/unit/plugins/relay/api/test-relay-api-msg.cpp b/tests/unit/plugins/relay/api/test-relay-api-msg.cpp index 239dd80f8..696bf1fc8 100644 --- a/tests/unit/plugins/relay/api/test-relay-api-msg.cpp +++ b/tests/unit/plugins/relay/api/test-relay-api-msg.cpp @@ -118,7 +118,8 @@ TEST(RelayApiMsg, SendEvent) TEST(RelayApiMsg, BufferToJson) { - cJSON *json, *json_obj, *json_local_vars, *json_keys, *json_key, *json_lines; + cJSON *json, *json_obj, *json_local_vars, *json_keys, *json_key; + cJSON *json_lines, *json_line; cJSON *json_nicklist_root, *json_nicks, *json_groups, *json_group; cJSON *json_group_nicks, *json_nick; struct t_gui_buffer *buffer; @@ -126,7 +127,7 @@ TEST(RelayApiMsg, BufferToJson) long long group_id; char *color; - json = relay_api_msg_buffer_to_json (NULL, 0, 0, RELAY_API_COLORS_ANSI); + json = relay_api_msg_buffer_to_json (NULL, 0L, 0L, 0, RELAY_API_COLORS_ANSI); CHECK(json); CHECK(cJSON_IsObject (json)); POINTERS_EQUAL(NULL, cJSON_GetObjectItem (json, "name")); @@ -136,7 +137,7 @@ TEST(RelayApiMsg, BufferToJson) gui_buffer_set (gui_buffers, "key_bind_meta-y,2", "/test2 arg"); /* buffer without lines and nicks */ - json = relay_api_msg_buffer_to_json (gui_buffers, 0, 0, RELAY_API_COLORS_ANSI); + json = relay_api_msg_buffer_to_json (gui_buffers, 0L, 0L, 0, RELAY_API_COLORS_ANSI); CHECK(json); CHECK(cJSON_IsObject (json)); WEE_CHECK_OBJ_NUM(gui_buffers->id, json, "id"); @@ -169,7 +170,7 @@ TEST(RelayApiMsg, BufferToJson) cJSON_Delete (json); /* buffer with 2 lines, without nicks */ - json = relay_api_msg_buffer_to_json (gui_buffers, 2, 0, RELAY_API_COLORS_ANSI); + json = relay_api_msg_buffer_to_json (gui_buffers, 2L, 0L, 0, RELAY_API_COLORS_ANSI); CHECK(json); CHECK(cJSON_IsObject (json)); json_lines = cJSON_GetObjectItem (json, "lines"); @@ -192,7 +193,7 @@ TEST(RelayApiMsg, BufferToJson) CHECK(gui_nicklist_add_nick (buffer, NULL, "root_nick_hidden", "cyan", "+", "yellow", 0)); /* buffer with no lines and 1 group / 4 nicks */ - json = relay_api_msg_buffer_to_json (buffer, 1, 1, RELAY_API_COLORS_ANSI); + json = relay_api_msg_buffer_to_json (buffer, 1L, 0L, 1, RELAY_API_COLORS_ANSI); CHECK(json); CHECK(cJSON_IsObject (json)); WEE_CHECK_OBJ_BOOL(1, json, "nicklist"); @@ -315,6 +316,46 @@ TEST(RelayApiMsg, BufferToJson) gui_buffer_set (gui_buffers, "key_unbind_meta-y", ""); gui_buffer_close (buffer); + + buffer = gui_buffer_new_user ("test", GUI_BUFFER_TYPE_FREE); + CHECK(buffer); + gui_chat_printf_y (buffer, 0, "test line 1"); + gui_chat_printf_y (buffer, 1, "test line 2"); + gui_chat_printf_y (buffer, 2, "test line 3"); + gui_chat_printf_y (buffer, 3, "test line 4"); + gui_chat_printf_y (buffer, 4, "test line 5"); + + json = relay_api_msg_buffer_to_json (buffer, 1L, 2L, 0, RELAY_API_COLORS_ANSI); + CHECK(json); + CHECK(cJSON_IsObject (json)); + json_lines = cJSON_GetObjectItem (json, "lines"); + CHECK(json_lines); + CHECK(cJSON_IsArray (json_lines)); + LONGS_EQUAL(2, cJSON_GetArraySize (json_lines)); + json_line = cJSON_GetArrayItem (json_lines, 0); + CHECK(json_line); + WEE_CHECK_OBJ_STR("test line 1", json_line, "message"); + json_line = cJSON_GetArrayItem (json_lines, 1); + CHECK(json_line); + WEE_CHECK_OBJ_STR("test line 2", json_line, "message"); + cJSON_Delete (json); + + json = relay_api_msg_buffer_to_json (buffer, 1L, -2L, 0, RELAY_API_COLORS_ANSI); + CHECK(json); + CHECK(cJSON_IsObject (json)); + json_lines = cJSON_GetObjectItem (json, "lines"); + CHECK(json_lines); + CHECK(cJSON_IsArray (json_lines)); + LONGS_EQUAL(2, cJSON_GetArraySize (json_lines)); + json_line = cJSON_GetArrayItem (json_lines, 0); + CHECK(json_line); + WEE_CHECK_OBJ_STR("test line 4", json_line, "message"); + json_line = cJSON_GetArrayItem (json_lines, 1); + CHECK(json_line); + WEE_CHECK_OBJ_STR("test line 5", json_line, "message"); + cJSON_Delete (json); + + gui_buffer_close (buffer); } /* -- cgit v1.2.3