v0.7.40 - Removed event_broadcasts table and related code to fix FOREIGN KEY constraint failures preventing event insertion
This commit is contained in:
110
src/config.c
110
src/config.c
@@ -85,6 +85,7 @@ int migrate_config_from_events_to_table(void);
|
||||
int populate_config_table_from_event(const cJSON* event);
|
||||
int handle_config_query_unified(cJSON* event, const char* query_type, char* error_message, size_t error_size, struct lws* wsi);
|
||||
int handle_config_set_unified(cJSON* event, const char* config_key, const char* config_value, char* error_message, size_t error_size, struct lws* wsi);
|
||||
int handle_create_relay_event_unified(cJSON* event, const char* kind_str, const char* event_data_json, char* error_message, size_t error_size, struct lws* wsi);
|
||||
|
||||
// Forward declarations for tag parsing utilities
|
||||
const char* get_first_tag_name(cJSON* event);
|
||||
@@ -2549,51 +2550,17 @@ int handle_kind_23456_unified(cJSON* event, char* error_message, size_t error_si
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Check if decrypted content is a direct command array (DM control system)
|
||||
cJSON* potential_command_array = cJSON_Parse(decrypted_text);
|
||||
|
||||
if (potential_command_array && cJSON_IsArray(potential_command_array)) {
|
||||
// Route to DM admin system
|
||||
int dm_result = process_dm_admin_command(potential_command_array, event, error_message, error_size, wsi);
|
||||
cJSON_Delete(potential_command_array);
|
||||
memset(decrypted_text, 0, sizeof(decrypted_text)); // Clear sensitive data
|
||||
return dm_result;
|
||||
}
|
||||
|
||||
// If not a direct command array, try parsing as inner event JSON (NIP-17)
|
||||
cJSON* inner_event = potential_command_array; // Reuse the parsed JSON
|
||||
|
||||
if (!inner_event || !cJSON_IsObject(inner_event)) {
|
||||
DEBUG_ERROR("error: decrypted content is not valid inner event JSON");
|
||||
cJSON_Delete(inner_event);
|
||||
snprintf(error_message, error_size, "error: decrypted content is not valid inner event JSON");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Extract content from inner event
|
||||
cJSON* inner_content_obj = cJSON_GetObjectItem(inner_event, "content");
|
||||
if (!inner_content_obj || !cJSON_IsString(inner_content_obj)) {
|
||||
DEBUG_ERROR("error: inner event missing content field");
|
||||
cJSON_Delete(inner_event);
|
||||
snprintf(error_message, error_size, "error: inner event missing content field");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* inner_content = cJSON_GetStringValue(inner_content_obj);
|
||||
|
||||
// Parse inner content as JSON array (the command array)
|
||||
decrypted_content = cJSON_Parse(inner_content);
|
||||
// Parse decrypted content as command array directly (NOT as NIP-17 inner event)
|
||||
// Kind 23456 events contain direct command arrays: ["command_name", arg1, arg2, ...]
|
||||
decrypted_content = cJSON_Parse(decrypted_text);
|
||||
|
||||
if (!decrypted_content || !cJSON_IsArray(decrypted_content)) {
|
||||
DEBUG_ERROR("error: inner content is not valid JSON array");
|
||||
cJSON_Delete(inner_event);
|
||||
snprintf(error_message, error_size, "error: inner content is not valid JSON array");
|
||||
DEBUG_ERROR("error: decrypted content is not valid command array");
|
||||
cJSON_Delete(decrypted_content);
|
||||
snprintf(error_message, error_size, "error: decrypted content is not valid command array");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Clean up inner event
|
||||
cJSON_Delete(inner_event);
|
||||
|
||||
// Replace event content with decrypted command array for processing
|
||||
cJSON_DeleteItemFromObject(event, "content");
|
||||
cJSON_AddStringToObject(event, "content", "decrypted");
|
||||
@@ -2610,10 +2577,26 @@ int handle_kind_23456_unified(cJSON* event, char* error_message, size_t error_si
|
||||
cJSON_AddItemToArray(command_tag, cJSON_Duplicate(first_item, 1));
|
||||
|
||||
// Add remaining items as tag values
|
||||
// Convert non-string items (objects, arrays, numbers) to JSON strings
|
||||
for (int i = 1; i < cJSON_GetArraySize(decrypted_content); i++) {
|
||||
cJSON* item = cJSON_GetArrayItem(decrypted_content, i);
|
||||
if (item) {
|
||||
cJSON_AddItemToArray(command_tag, cJSON_Duplicate(item, 1));
|
||||
if (cJSON_IsString(item)) {
|
||||
// Keep strings as-is
|
||||
cJSON_AddItemToArray(command_tag, cJSON_Duplicate(item, 1));
|
||||
} else if (cJSON_IsNumber(item)) {
|
||||
// Convert numbers to strings
|
||||
char num_str[32];
|
||||
snprintf(num_str, sizeof(num_str), "%.0f", cJSON_GetNumberValue(item));
|
||||
cJSON_AddItemToArray(command_tag, cJSON_CreateString(num_str));
|
||||
} else if (cJSON_IsObject(item) || cJSON_IsArray(item)) {
|
||||
// Convert objects/arrays to JSON strings
|
||||
char* json_str = cJSON_PrintUnformatted(item);
|
||||
if (json_str) {
|
||||
cJSON_AddItemToArray(command_tag, cJSON_CreateString(json_str));
|
||||
free(json_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2690,6 +2673,16 @@ int handle_kind_23456_unified(cJSON* event, char* error_message, size_t error_si
|
||||
else if (strcmp(action_type, "stats_query") == 0) {
|
||||
return handle_stats_query_unified(event, error_message, error_size, wsi);
|
||||
}
|
||||
else if (strcmp(action_type, "create_relay_event") == 0) {
|
||||
const char* kind_str = get_tag_value(event, action_type, 1);
|
||||
const char* event_data_json = get_tag_value(event, action_type, 2);
|
||||
if (!kind_str || !event_data_json) {
|
||||
DEBUG_ERROR("invalid: missing kind or event data");
|
||||
snprintf(error_message, error_size, "invalid: missing kind or event data");
|
||||
return -1;
|
||||
}
|
||||
return handle_create_relay_event_unified(event, kind_str, event_data_json, error_message, error_size, wsi);
|
||||
}
|
||||
else if (strcmp(action_type, "whitelist") == 0 || strcmp(action_type, "blacklist") == 0) {
|
||||
// Handle auth rule modifications (existing logic from process_admin_auth_event)
|
||||
return handle_auth_rule_modification_unified(event, error_message, error_size, wsi);
|
||||
@@ -3489,6 +3482,41 @@ int handle_stats_query_unified(cJSON* event, char* error_message, size_t error_s
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Unified create relay event handler
|
||||
int handle_create_relay_event_unified(cJSON* event, const char* kind_str, const char* event_data_json, char* error_message, size_t error_size, struct lws* wsi) {
|
||||
// Suppress unused parameter warning
|
||||
(void)wsi;
|
||||
|
||||
if (!event || !kind_str || !event_data_json) {
|
||||
snprintf(error_message, error_size, "invalid: missing parameters for create_relay_event");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Parse kind string to integer
|
||||
char* endptr;
|
||||
int kind = (int)strtol(kind_str, &endptr, 10);
|
||||
if (endptr == kind_str || *endptr != '\0') {
|
||||
snprintf(error_message, error_size, "invalid: kind must be a valid integer");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Parse event data JSON
|
||||
cJSON* event_data = cJSON_Parse(event_data_json);
|
||||
if (!event_data) {
|
||||
snprintf(error_message, error_size, "invalid: event_data must be valid JSON");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Call the existing implementation from api.c
|
||||
extern int handle_create_relay_event_command(cJSON* event, int kind, cJSON* event_data, char* error_message, size_t error_size, struct lws* wsi);
|
||||
int result = handle_create_relay_event_command(event, kind, event_data, error_message, error_size, wsi);
|
||||
|
||||
// Clean up
|
||||
cJSON_Delete(event_data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Unified config update handler - handles multiple config objects in single atomic command
|
||||
int handle_config_update_unified(cJSON* event, char* error_message, size_t error_size, struct lws* wsi) {
|
||||
// Suppress unused parameter warning
|
||||
|
||||
Reference in New Issue
Block a user