1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-09 11:13:12 +02:00

relay/api: add GET /api/hotlist

This commit is contained in:
Sébastien Helleu
2024-03-17 20:13:19 +01:00
parent 0b0ab94aa3
commit 34a72ac02e
8 changed files with 225 additions and 9 deletions
+52
View File
@@ -614,3 +614,55 @@ relay_api_msg_nick_group_to_json (struct t_gui_nick_group *nick_group)
return json;
}
/*
* Creates a JSON object with a hotlist entry.
*/
cJSON *
relay_api_msg_hotlist_to_json (struct t_gui_hotlist *hotlist)
{
struct t_hdata *hdata;
struct t_gui_hotlist *pointer;
struct t_gui_buffer *buffer;
cJSON *json, *json_count;
time_t time_value;
struct timeval tv;
struct tm gm_time;
char str_time[256], str_key[32];
int i, array_size;
long long buffer_id;
hdata = relay_hdata_hotlist;
pointer = hotlist;
json = cJSON_CreateObject ();
if (!json)
return NULL;
if (!hotlist)
return json;
MSG_ADD_HDATA_VAR(Number, "priority", integer, "priority");
MSG_ADD_HDATA_TIME_USEC("date", "time", "time_usec");
buffer = weechat_hdata_pointer (hdata, hotlist, "buffer");
buffer_id = (buffer) ?
weechat_hdata_longlong (relay_hdata_buffer, buffer, "id") : -1;
cJSON_AddItemToObject (json, "buffer_id", cJSON_CreateNumber (buffer_id));
json_count = cJSON_CreateArray ();
if (json_count)
{
array_size = weechat_hdata_get_var_array_size (hdata, hotlist, "count");
for (i = 0; i < array_size; i++)
{
snprintf (str_key, sizeof (str_key), "%d|count", i);
cJSON_AddItemToArray (
json_count,
cJSON_CreateNumber (weechat_hdata_integer (hdata, hotlist, str_key)));
}
}
cJSON_AddItemToObject (json, "count", json_count);
return json;
}
+1
View File
@@ -47,5 +47,6 @@ extern cJSON *relay_api_msg_line_data_to_json (struct t_gui_line_data *line_data
enum t_relay_api_colors colors);
extern cJSON *relay_api_msg_nick_to_json (struct t_gui_nick *nick);
extern cJSON *relay_api_msg_nick_group_to_json (struct t_gui_nick_group *nick_group);
extern cJSON *relay_api_msg_hotlist_to_json (struct t_gui_hotlist *hotlist);
#endif /* WEECHAT_PLUGIN_RELAY_API_MSG_H */
@@ -459,6 +459,36 @@ error:
return WEECHAT_RC_OK;
}
/*
* Callback for resource "hotlist".
*
* Routes:
* GET /api/hotlist
*/
RELAY_API_PROTOCOL_CALLBACK(hotlist)
{
cJSON *json;
struct t_gui_hotlist *ptr_hotlist;
json = cJSON_CreateArray ();
if (!json)
return WEECHAT_RC_ERROR;
ptr_hotlist = weechat_hdata_get_list (relay_hdata_hotlist, "gui_hotlist");
while (ptr_hotlist)
{
cJSON_AddItemToArray (
json,
relay_api_msg_hotlist_to_json (ptr_hotlist));
ptr_hotlist = weechat_hdata_move (relay_hdata_hotlist, ptr_hotlist, 1);
}
relay_api_msg_send_json (client, RELAY_HTTP_200_OK, json);
cJSON_Delete (json);
return WEECHAT_RC_OK;
}
/*
* Callback for resource "input".
*
@@ -770,6 +800,7 @@ relay_api_protocol_recv_http (struct t_relay_client *client)
{ "POST", "handshake", 0, 0, 0, &relay_api_protocol_cb_handshake },
{ "GET", "version", 1, 0, 0, &relay_api_protocol_cb_version },
{ "GET", "buffers", 1, 0, 3, &relay_api_protocol_cb_buffers },
{ "GET", "hotlist", 1, 0, 3, &relay_api_protocol_cb_hotlist },
{ "POST", "input", 1, 0, 0, &relay_api_protocol_cb_input },
{ "POST", "ping", 1, 0, 0, &relay_api_protocol_cb_ping },
{ "POST", "sync", 1, 0, 0, &relay_api_protocol_cb_sync },
+2
View File
@@ -57,6 +57,7 @@ struct t_hdata *relay_hdata_nick_group = NULL;
struct t_hdata *relay_hdata_nick = NULL;
struct t_hdata *relay_hdata_completion = NULL;
struct t_hdata *relay_hdata_completion_word = NULL;
struct t_hdata *relay_hdata_hotlist = NULL;
int relay_signal_upgrade_received = 0; /* signal "upgrade" received ? */
@@ -220,6 +221,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
relay_hdata_nick = weechat_hdata_get ("nick");
relay_hdata_completion = weechat_hdata_get ("completion");
relay_hdata_completion_word = weechat_hdata_get ("completion_word");
relay_hdata_hotlist = weechat_hdata_get ("hotlist");
relay_signal_upgrade_received = 0;
+1
View File
@@ -34,6 +34,7 @@ extern struct t_hdata *relay_hdata_nick_group;
extern struct t_hdata *relay_hdata_nick;
extern struct t_hdata *relay_hdata_completion;
extern struct t_hdata *relay_hdata_completion_word;
extern struct t_hdata *relay_hdata_hotlist;
extern int relay_signal_upgrade_received;
+11 -9
View File
@@ -37,29 +37,31 @@ extern "C" {
#define PATH_MAX 4096
#endif /* PATH_MAX */
struct timeval;
struct t_arraylist;
struct t_config_option;
struct t_config_section;
struct t_config_file;
struct t_gui_window;
struct t_gui_buffer;
struct t_gui_lines;
struct t_gui_line;
struct t_gui_line_data;
struct t_gui_bar;
struct t_gui_bar_item;
struct t_gui_bar_window;
struct t_gui_buffer;
struct t_gui_completion;
struct t_gui_hotlist;
struct t_gui_line;
struct t_gui_line_data;
struct t_gui_lines;
struct t_gui_nick;
struct t_gui_nick_group;
struct t_gui_window;
struct t_hashtable;
struct t_hdata;
struct t_infolist;
struct t_infolist_item;
struct t_upgrade_file;
struct t_weelist;
struct t_weelist_item;
struct t_arraylist;
struct t_hashtable;
struct t_hdata;
struct timeval;
/*
* IMPORTANT NOTE for WeeChat developers: if you update, add or remove
@@ -30,6 +30,7 @@ extern "C"
#include "src/gui/gui-buffer.h"
#include "src/gui/gui-chat.h"
#include "src/gui/gui-color.h"
#include "src/gui/gui-hotlist.h"
#include "src/gui/gui-line.h"
#include "src/gui/gui-nicklist.h"
#include "src/plugins/relay/relay.h"
@@ -368,3 +369,66 @@ TEST(RelayApiMsg, LinesToJson)
WEE_CHECK_OBJ_STR("this is the second line with green", json_line, "message");
cJSON_Delete (json);
}
/*
* Tests functions:
* relay_api_msg_hotlist_to_json
*/
TEST(RelayApiMsg, HotlistToJson)
{
char str_date[128];
cJSON *json, *json_obj, *json_count;
struct timeval tv;
struct tm gm_time;
json = relay_api_msg_hotlist_to_json (NULL);
CHECK(json);
CHECK(cJSON_IsObject (json));
POINTERS_EQUAL(NULL, cJSON_GetObjectItem (json, "priority"));
cJSON_Delete (json);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_LOW, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_MESSAGE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_MESSAGE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_PRIVATE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_PRIVATE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_PRIVATE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_HIGHLIGHT, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_HIGHLIGHT, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_HIGHLIGHT, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_HIGHLIGHT, NULL, 0);
json = relay_api_msg_hotlist_to_json (gui_hotlist);
CHECK(json);
CHECK(cJSON_IsObject (json));
WEE_CHECK_OBJ_NUM(GUI_HOTLIST_HIGHLIGHT, json, "priority");
gmtime_r (&(gui_hotlist->creation_time.tv_sec), &gm_time);
tv.tv_sec = mktime (&gm_time);
tv.tv_usec = gui_hotlist->creation_time.tv_usec;
util_strftimeval (str_date, sizeof (str_date), "%FT%T.%fZ", &tv);
WEE_CHECK_OBJ_STR(str_date, json, "date");
WEE_CHECK_OBJ_NUM(gui_buffers->id, json, "buffer_id");
json_count = cJSON_GetObjectItem (json, "count");
CHECK(json_count);
CHECK(cJSON_IsArray (json_count));
json_obj = cJSON_GetArrayItem (json_count, 0);
CHECK(json_obj);
CHECK(cJSON_IsNumber (json_obj));
CHECK(1 == cJSON_GetNumberValue (json_obj));
json_obj = cJSON_GetArrayItem (json_count, 1);
CHECK(json_obj);
CHECK(cJSON_IsNumber (json_obj));
CHECK(2 == cJSON_GetNumberValue (json_obj));
json_obj = cJSON_GetArrayItem (json_count, 2);
CHECK(json_obj);
CHECK(cJSON_IsNumber (json_obj));
CHECK(3 == cJSON_GetNumberValue (json_obj));
json_obj = cJSON_GetArrayItem (json_count, 3);
CHECK(json_obj);
CHECK(cJSON_IsNumber (json_obj));
CHECK(4 == cJSON_GetNumberValue (json_obj));
cJSON_Delete (json);
gui_hotlist_remove_buffer (gui_buffers, 1);
}
@@ -34,6 +34,7 @@ extern "C"
#include "src/core/core-version.h"
#include "src/gui/gui-buffer.h"
#include "src/gui/gui-chat.h"
#include "src/gui/gui-hotlist.h"
#include "src/gui/gui-line.h"
#include "src/plugins/weechat-plugin.h"
#include "src/plugins/relay/relay.h"
@@ -476,6 +477,68 @@ TEST(RelayApiProtocolWithClient, CbBuffers)
LONGS_EQUAL(0, cJSON_GetArraySize (json_groups));
}
/*
* Tests functions:
* relay_api_protocol_cb_hotlist
*/
TEST(RelayApiProtocolWithClient, CbHotlist)
{
cJSON *json, *json_obj, *json_count;
/* get hotlist (empty) */
test_client_recv_http ("GET /api/hotlist", NULL);
WEE_CHECK_HTTP_CODE(200, "OK");
CHECK(json_body_sent);
CHECK(cJSON_IsArray (json_body_sent));
LONGS_EQUAL(0, cJSON_GetArraySize (json_body_sent));
gui_hotlist_add (gui_buffers, GUI_HOTLIST_LOW, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_MESSAGE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_MESSAGE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_PRIVATE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_PRIVATE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_PRIVATE, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_HIGHLIGHT, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_HIGHLIGHT, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_HIGHLIGHT, NULL, 0);
gui_hotlist_add (gui_buffers, GUI_HOTLIST_HIGHLIGHT, NULL, 0);
/* get hotlist (one buffer) */
test_client_recv_http ("GET /api/hotlist", NULL);
WEE_CHECK_HTTP_CODE(200, "OK");
CHECK(json_body_sent);
CHECK(cJSON_IsArray (json_body_sent));
LONGS_EQUAL(1, cJSON_GetArraySize (json_body_sent));
json = cJSON_GetArrayItem (json_body_sent, 0);
CHECK(json);
CHECK(cJSON_IsObject (json));
WEE_CHECK_OBJ_NUM(GUI_HOTLIST_HIGHLIGHT, json, "priority");
CHECK(cJSON_IsString (cJSON_GetObjectItem (json, "date")));
WEE_CHECK_OBJ_NUM(gui_buffers->id, json, "buffer_id");
json_count = cJSON_GetObjectItem (json, "count");
CHECK(json_count);
CHECK(cJSON_IsArray (json_count));
json_obj = cJSON_GetArrayItem (json_count, 0);
CHECK(json_obj);
CHECK(cJSON_IsNumber (json_obj));
CHECK(1 == cJSON_GetNumberValue (json_obj));
json_obj = cJSON_GetArrayItem (json_count, 1);
CHECK(json_obj);
CHECK(cJSON_IsNumber (json_obj));
CHECK(2 == cJSON_GetNumberValue (json_obj));
json_obj = cJSON_GetArrayItem (json_count, 2);
CHECK(json_obj);
CHECK(cJSON_IsNumber (json_obj));
CHECK(3 == cJSON_GetNumberValue (json_obj));
json_obj = cJSON_GetArrayItem (json_count, 3);
CHECK(json_obj);
CHECK(cJSON_IsNumber (json_obj));
CHECK(4 == cJSON_GetNumberValue (json_obj));
gui_hotlist_remove_buffer (gui_buffers, 1);
}
/*
* Tests functions:
* relay_api_protocol_cb_input