From 1f4e6ab9f4b552b22b5bb31acd73518927eabc3b Mon Sep 17 00:00:00 2001 From: teknomunk Date: Mon, 17 Jul 2023 20:36:10 -0500 Subject: [PATCH] Add debugging for certain endpoints, start implementing Pleroma notification read endpoint, modify featured output, make is_muted and is_read field function, rework process to use section names --- src/controller/mastodon_api.c | 2 + src/controller/owner.c | 14 +++++- src/controller/pleroma_api.c | 82 +++++++++++++++++++++++++++++++++-- src/json | 2 +- src/main.c | 2 +- src/model/account/ap_data.c | 19 ++++---- src/model/timeline.c | 2 + src/process.c | 26 ++++++----- src/view/api/Notification.c | 4 +- 9 files changed, 126 insertions(+), 27 deletions(-) diff --git a/src/controller/mastodon_api.c b/src/controller/mastodon_api.c index fb9372c..fbf02fd 100644 --- a/src/controller/mastodon_api.c +++ b/src/controller/mastodon_api.c @@ -359,6 +359,8 @@ bool route_mastodon_api( struct http_request* req ) } if( !check_authentication_header(req) ) { + printf( "User-Agent: %s\n", http_request_get_header(req,"user-agent") ); + http_request_send_headers( req, 401, "text/plain", true ); FILE* f = http_request_get_response_body( req ); fprintf( f, "Not authorized to use this endpoint.\n" ); diff --git a/src/controller/owner.c b/src/controller/owner.c index deb3c22..5493d65 100644 --- a/src/controller/owner.c +++ b/src/controller/owner.c @@ -31,7 +31,6 @@ static bool handle_paged_collection( struct http_request* req, struct pager* pg ap_object_write_to_FILE( obj, f ); ap_object_free(obj); } else if( http_request_route( req, "/page-" ) ) { - printf( "/page-\n" ); char* page_str = http_request_route_get_dir_or_file(req); int page = -1; if( !page_str ) { return false; } @@ -52,8 +51,10 @@ static bool handle_paged_collection( struct http_request* req, struct pager* pg static bool handle_featured( struct http_request* req ) { + printf( "User-Agent: %s\n", http_request_get_header(req,"user-agent") ); struct account* owner_account = account_from_id(0); + //* struct pager pg = { .data = owner_account, .get_root = (void*)account_ap_featured, @@ -61,6 +62,13 @@ static bool handle_featured( struct http_request* req ) }; bool result = handle_paged_collection(req,&pg); + /*/ + http_request_add_header( req, "Access-Control-Allow-Origin", "*" ); + http_request_send_headers( req, 200, "application/activity+json", true ); + FILE* f = http_request_get_response_body(req); + #include "src/view/owner/featured.json.inc" + bool result = true; + //*/ account_free( owner_account ); return result; } @@ -95,6 +103,8 @@ static bool handle_following( struct http_request* req ) static bool handle_owner_actor( struct http_request* req ) { + printf( "User-Agent: %s\n", http_request_get_header(req,"user-agent") ); + struct account* owner_account = account_from_id(0); http_request_add_header( req, "Access-Control-Allow-Origin", "*" ); @@ -140,7 +150,7 @@ bool route_owner( struct http_request* req ) return handle_following(req); } else if( http_request_route( req, "/followers" ) ) { return handle_followers(req); - } else if( http_request_route_term( req, "/collections/featured" ) ) { + } else if( http_request_route( req, "/collections/featured" ) ) { return handle_featured(req); } else if( http_request_route_term( req, "/avatar.blob" ) ) { return handle_avatar(req); diff --git a/src/controller/pleroma_api.c b/src/controller/pleroma_api.c index 55eae60..4ca6560 100644 --- a/src/controller/pleroma_api.c +++ b/src/controller/pleroma_api.c @@ -1,14 +1,19 @@ #include "pleroma_api.h" -#include "http/cgi.h" -#include "http/server/request.h" - #include "model/account.h" #include "model/status.h" +#include "model/notification.h" + +#include "view/api/Notification.h" #include "src/controller/api/client_apps.h" #include "src/controller/api/status.h" +#include "http/cgi.h" +#include "http/server/request.h" +#include "json/json.h" +#include "json/layout.h" + #include #include @@ -113,6 +118,73 @@ bool handle_pleroma_birthdays( struct http_request* req ) return true; } +// Route: POST /api/v1/pleroma/notifications/read +struct read_notification_params_t +{ + char* max_id; + char* id; +}; +bool handle_read_notification( struct http_request* req ) +{ + struct read_notification_params_t params; + memset(¶ms,0,sizeof(params)); + #define OBJ_TYPE struct read_notification_params_t + static struct json_object_field params_layout[] = { + JSON_FIELD_STRING( max_id, false ), + JSON_FIELD_STRING( id, false ), + JSON_FIELD_END + }; + #undef OBJ_TYPE + + FILE* req_body = http_request_get_request_data(req); + json_read_object_layout_from_FILE( req_body, params_layout, ¶ms ); + + http_request_send_headers( req, 200, "application/json", true ); + FILE* f = http_request_get_response_body(req); + + if( params.max_id ) { + int max_id = 0; + fprintf( f, "[" ); + bool is_first = true; + if( 1 == sscanf( params.max_id, "%012d", &max_id ) ) { + for( int i = 0; i < 80; ++i ) { + struct notification* n = notification_from_id( max_id - i ); + if( !n ) { break; } + if( n->is_seen ) { goto done_one_max_id; } + + n->is_seen = true; + if( !is_first ) { + fprintf( f, ",\n" ); + } + is_first = false; + api_Notification_write( n, f, 1 ); + notification_save(n); + done_one_max_id: + notification_free(n); + } + } + fprintf( f, "]" ); + } else if( params.id ) { + int id = 0; + if( 1 == sscanf( params.id, "%012d", &id ) ) { + struct notification* n = notification_from_id(id); + if( !n ) { goto done_id; }; + + n->is_seen = true; + api_Notification_write( n, f, 0 ); + notification_save(n); + + done_id: + notification_free(n); + } + } + + free(params.id); + free(params.max_id); + + return true; +} + // Route: /api/v1/pleroma bool route_pleroma_api( struct http_request* req ) { @@ -135,6 +207,10 @@ bool route_pleroma_api( struct http_request* req ) } } else if( http_request_route( req, "/birthdays" ) ) { return handle_pleroma_birthdays(req); + } else if( http_request_route( req, "/notifications/read" ) ) { + if( http_request_route_method( req, "POST" ) ) { + return handle_read_notification(req); + } } return false; diff --git a/src/json b/src/json index 7714930..4b85946 160000 --- a/src/json +++ b/src/json @@ -1 +1 @@ -Subproject commit 77149308f513caa5be458247b509c3fb7f7e5de3 +Subproject commit 4b85946cdf6c632c0ce6133cd69c50841105724f diff --git a/src/main.c b/src/main.c index acefbce..6c48c14 100644 --- a/src/main.c +++ b/src/main.c @@ -21,7 +21,7 @@ void handle_ctrl_c(int) terminate = true; } -int main( int argc, char* argv[] ) +int main( int argc, char* argv[], char* envp[] ) { curl_global_init(CURL_GLOBAL_DEFAULT); model_init(); diff --git a/src/model/account/ap_data.c b/src/model/account/ap_data.c index e900fc5..76b4f87 100644 --- a/src/model/account/ap_data.c +++ b/src/model/account/ap_data.c @@ -313,30 +313,33 @@ struct ap_object* account_ap_featured_page( struct account* a, int page ) struct timeline* tl = timeline_from_path( buffer ); if( !tl ) { return NULL; } + int total_items = ffdb_trie_count( format( buffer,512, "data/accounts/%d/timeline/pinned", a->id ) ); + enum { items_per_page = 20 }; + const int total_pages = ( total_items + items_per_page - 1 ) / items_per_page; struct status* statuses[items_per_page]; int count = timeline_load_statuses( tl, page * items_per_page, items_per_page, statuses ); - if( count == 0 ) { - timeline_free(tl); - return NULL; - } - struct ap_object* o = activity_new_local_activity(); + struct ap_object* o = ap_object_new(); o->type = ap_OrderedCollectionPage; o->part_of = aformat( "https://%s/owner/collections/featured", g_server->domain ); o->id = aformat( "%s/page-%d", o->part_of, page); - o->next.tag = apaot_ref; - o->next.ref = aformat( "%s/page-%d", o->part_of, page+1 ); + if( page+1 < total_pages ) { + o->next.tag = apaot_ref; + o->next.ref = aformat( "%s/page-%d", o->part_of, page+1 ); + } if( page > 0 ) { o->prev = aformat( "%s/page-%d", o->part_of, page-11 ); } + o->ordered_items.items = malloc(1); + for( int i = 0; i < count; ++i ) { struct ap_object_ptr_or_ref r; r.tag = apaot_ref; r.ref = strdup(statuses[i]->url); - array_append( &o->collection_items, sizeof(r), &r ); + array_append( &o->ordered_items, sizeof(r), &r ); status_free(statuses[i]); } diff --git a/src/model/timeline.c b/src/model/timeline.c index 75aceac..323c2d5 100644 --- a/src/model/timeline.c +++ b/src/model/timeline.c @@ -59,6 +59,8 @@ int timeline_load_statuses( struct timeline* tl, int offset_from_head, int count for( int i = 0; i < values.count && result_count < count; ++i ) { char* id_str = values.items[i]; struct status* s = status_from_id( atoi(id_str) ); + + //printf( "found item: %s, s=%p\n", id_str, s ); free( id_str ); if( s ) { diff --git a/src/process.c b/src/process.c index 24b0b97..4c461aa 100644 --- a/src/process.c +++ b/src/process.c @@ -24,6 +24,7 @@ #include #include #include +#include #include struct process @@ -106,23 +107,25 @@ int process_run_section( int id ) return EXIT_FAILURE; } -static const char* logfile_for_section( int section ) +static const char* section_name( int section ) { switch( section ) { - case 0: return "data/logs/webserver.log"; - case 1: return "data/logs/inbox.log"; - case 2: return "data/logs/outbox.log"; - case 5: return "data/logs/fetch.log"; - case 6: return "data/logs/tor.log"; + case 0: return "webserver"; + case 1: return "inbox"; + case 2: return "outbox"; + case 5: return "fetch"; + case 6: return "tor"; } return NULL; } static void redirect_io( int section ) { - const char* filename = logfile_for_section(section); + const char* section_name_str = section_name(section); + if( !section_name_str ) { return; } - if( !filename ) { return; } + char filename[512]; + snprintf( filename,512, "data/logs/%s.log", section_name_str ); int log = open( filename, O_CREAT | O_RDWR | O_APPEND, 0644 ); if( !log ) { @@ -170,8 +173,11 @@ void process_stop() static bool rotate_log( int section ) { - const char* logfile = logfile_for_section(section); - if(!logfile) { return false; } + const char* section_name_str = section_name(section); + if( !section_name_str ) { return false; } + + char logfile[512]; + snprintf( logfile,512, "data/logs/%s.log", section_name_str ); struct stat s; if( -1 == stat( logfile, &s ) ) { return false; } diff --git a/src/view/api/Notification.c b/src/view/api/Notification.c index 99d242e..c38d70e 100644 --- a/src/view/api/Notification.c +++ b/src/view/api/Notification.c @@ -28,8 +28,8 @@ static struct json_enum type_enum[] = { #define OBJ_TYPE struct notification static struct json_object_field Notification_Pleroma_layout[] = { - JSON_FIELD_FIXED_BOOL( is_muted, false ), - JSON_FIELD_FIXED_BOOL( is_seen, false ), + JSON_FIELD_BOOL( is_muted, false ), + JSON_FIELD_BOOL( is_seen, false ), JSON_FIELD_END, }; static struct json_object_field api_Notification_layout[] = {