Преглед изворни кода

FlipSocial - v0.6.3

- Fixed issues with Direct Messages
- FlipperHTTP update
jblanked пре 1 година
родитељ
комит
2ec5479196

+ 6 - 12
callback/flip_social_callback.c

@@ -870,17 +870,15 @@ void flip_social_logged_in_profile_change_password_updated(void *context)
     }
 
     // send post request to change password
+    auth_headers_alloc();
     char payload[256];
     snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
-    char *headers = jsmn("Content-Type", "application/json");
-    if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", headers, payload))
+    if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", auth_headers, payload))
     {
         FURI_LOG_E(TAG, "Failed to send post request to change password");
         FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
-        free(headers);
         return;
     }
-    free(headers);
     // Save the settings
     save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->is_logged_in);
 
@@ -992,23 +990,21 @@ void flip_social_logged_in_messages_user_choice_message_updated(void *context)
     app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] = '\0';
 
     // send post request to send message
+    auth_headers_alloc();
     char url[128];
     char payload[256];
     snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
     snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_explore->usernames[flip_social_explore->index], app->message_user_choice_logged_in);
-    char *headers = jsmn("Content-Type", "application/json");
 
-    if (flipper_http_post_request_with_headers(url, headers, payload)) // start the async request
+    if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
     {
         furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
         fhttp.state = RECEIVING;
-        free(headers);
     }
     else
     {
         FURI_LOG_E(TAG, "Failed to send post request to send message");
         FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
-        free(headers);
         return;
     }
     while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
@@ -1056,23 +1052,21 @@ void flip_social_logged_in_messages_new_message_updated(void *context)
     app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
 
     // send post request to send message
+    auth_headers_alloc();
     char url[128];
     char payload[256];
     snprintf(url, sizeof(url), "https://www.flipsocial.net/api/messages/%s/post/", app->login_username_logged_in);
     snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_message_users->usernames[flip_social_message_users->index], app->messages_new_message_logged_in);
-    char *headers = jsmn("Content-Type", "application/json");
 
-    if (flipper_http_post_request_with_headers(url, headers, payload)) // start the async request
+    if (flipper_http_post_request_with_headers(url, auth_headers, payload)) // start the async request
     {
         furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
         fhttp.state = RECEIVING;
-        free(headers);
     }
     else
     {
         FURI_LOG_E(TAG, "Failed to send post request to send message");
         FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
-        free(headers);
         return;
     }
     while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)

+ 17 - 3
flipper_http/flipper_http.c

@@ -16,6 +16,17 @@ bool flipper_http_append_to_file(
 
     if (start_new_file)
     {
+        // Delete the file if it already exists
+        if (storage_file_exists(storage, file_path))
+        {
+            if (!storage_simply_remove_recursive(storage, file_path))
+            {
+                FURI_LOG_E(HTTP_TAG, "Failed to delete file: %s", file_path);
+                storage_file_free(file);
+                furi_record_close(RECORD_STORAGE);
+                return false;
+            }
+        }
         // Open the file in write mode
         if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
         {
@@ -183,12 +194,13 @@ int32_t flipper_http_worker(void *context)
                         if (!flipper_http_append_to_file(
                                 file_buffer,
                                 file_buffer_len,
-                                !fhttp.just_started_get && !fhttp.just_started_post,
+                                fhttp.just_started_bytes,
                                 fhttp.file_path))
                         {
                             FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
                         }
                         file_buffer_len = 0;
+                        fhttp.just_started_bytes = false;
                     }
                 }
 
@@ -462,7 +474,7 @@ bool flipper_http_send_data(const char *data)
     furi_hal_serial_tx(fhttp.serial_handle, (const uint8_t *)send_buffer, send_length);
 
     // Uncomment below line to log the data sent over UART
-    FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
+    // FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
     fhttp.state = IDLE;
     return true;
 }
@@ -1077,7 +1089,7 @@ void flipper_http_rx_callback(const char *line, void *context)
     }
 
     // Uncomment below line to log the data received over UART
-    FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
+    // FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
 
     // Check if we've started receiving data from a GET request
     if (fhttp.started_receiving_get)
@@ -1319,6 +1331,7 @@ void flipper_http_rx_callback(const char *line, void *context)
         fhttp.state = RECEIVING;
         // for GET request, save data only if it's a bytes request
         fhttp.save_bytes = fhttp.is_bytes_request;
+        fhttp.just_started_bytes = true;
         file_buffer_len = 0;
         return;
     }
@@ -1330,6 +1343,7 @@ void flipper_http_rx_callback(const char *line, void *context)
         fhttp.state = RECEIVING;
         // for POST request, save data only if it's a bytes request
         fhttp.save_bytes = fhttp.is_bytes_request;
+        fhttp.just_started_bytes = true;
         file_buffer_len = 0;
         return;
     }

+ 2 - 0
flipper_http/flipper_http.h

@@ -76,6 +76,8 @@ typedef struct
     bool is_bytes_request;     // Flag to indicate if the request is for bytes
     bool save_bytes;           // Flag to save the received data to a file
     bool save_received_data;   // Flag to save the received data to a file
+
+    bool just_started_bytes; // Indicates if bytes data reception has just started
 } FlipperHTTP;
 
 extern FlipperHTTP fhttp;

+ 5 - 0
messages/flip_social_messages.c

@@ -171,6 +171,11 @@ bool flip_social_get_messages_with_user()
         FURI_LOG_E(TAG, "Username is NULL");
         return false;
     }
+    if (!flip_social_message_users->usernames[flip_social_message_users->index] || strlen(flip_social_message_users->usernames[flip_social_message_users->index]) == 0)
+    {
+        FURI_LOG_E(TAG, "Username is NULL");
+        return false;
+    }
     char command[128];
     snprintf(
         fhttp.file_path,