From a8d7e7f0415e40e57360738101daa51115b31ce6 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sat, 15 Jul 2023 05:48:30 -0500 Subject: [PATCH] Get marker update working correctly (so notification count is now functional) --- src/controller/mastodon_api.c | 61 ++++++++++++++++++++++++++++++++++- src/model/marker.c | 20 ++++++++++-- src/model/marker.h | 6 +++- 3 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/controller/mastodon_api.c b/src/controller/mastodon_api.c index 0f0bd2d..69f6997 100644 --- a/src/controller/mastodon_api.c +++ b/src/controller/mastodon_api.c @@ -155,6 +155,15 @@ bool handle_announcements( struct http_request* req ) return true; } +// route: GET /api/v1/directory +bool handle_directory( struct http_request* req ) +{ + http_request_send_headers( req, 200, "application/json", true ); + FILE* f = http_request_get_response_body( req ); + fprintf( f, "[]" ); + return true; +} + // route: GET /api/v1/blocks bool handle_blocks( struct http_request* req ) { @@ -167,7 +176,55 @@ bool handle_blocks( struct http_request* req ) // route: POST /api/v1/markers bool handle_update_markers( struct http_request* req ) { - return false; + FILE* f = http_request_get_request_data(req); + struct json_pull_parser* jpp = json_pull_parser_new(f); + + int save = 0; + if( !json_pull_parser_begin_object(jpp,&save) ) { return false; } + + char* key = json_pull_parser_read_object_key(jpp); + if( !key ) { return false; } + char* marker_name = key; + + int save2 = 0; + if( !json_pull_parser_begin_object(jpp,&save2) ) { return false; } + + char* last_read_id; + + while( (key=json_pull_parser_read_object_key(jpp)) ) { + if( 0 == strcmp(key,"last_read_id") ) { + last_read_id = json_pull_parser_read_string(jpp); + } else { + json_pull_parser_read_value(jpp,NULL); + } + } + if( !json_pull_parser_end_object(jpp,&save2) ) { return false; } + + struct marker* m = marker_from_name(marker_name); + + if( last_read_id ) { + free(m->last_read_id); + m->last_read_id = last_read_id; + m->version += 1; + + marker_save(m); + } + + http_request_send_headers( req, 200, "application/json", true ); + FILE* o = http_request_get_response_body(req); + fprintf(o,"{"); + fprintf(o,"\"%s\":",key); + marker_write_to_FILE(m,o); + fprintf(o,"}"); + + marker_free(m); + + if( !json_pull_parser_end_object(jpp,&save) ) { + return false; + } + json_pull_parser_release(jpp); + + return true; } // route: GET /api/v1/markers @@ -313,6 +370,8 @@ bool route_mastodon_api( struct http_request* req ) return handle_announcements(req); } else if( http_request_route( req, "blocks" ) ) { return handle_blocks(req); + } else if( http_request_route( req, "directory" ) ) { + return handle_directory(req); } else if( http_request_route( req, "markers" ) ) { if( http_request_route_method( req, "POST" ) ) { return handle_update_markers(req); diff --git a/src/model/marker.c b/src/model/marker.c index b422ae9..dfe7e20 100644 --- a/src/model/marker.c +++ b/src/model/marker.c @@ -6,10 +6,11 @@ #include #include #include +#include #define OBJ_TYPE struct marker static struct json_object_field marker_layout[] = { - JSON_FIELD_STRING( last_read_marker, false ), + JSON_FIELD_STRING( last_read_id, false ), JSON_FIELD_INTEGER( version, true ), JSON_FIELD_DATETIME( updated_at, false ), JSON_FIELD_END @@ -25,9 +26,10 @@ struct marker* marker_from_name( const char* name ) memset(m,0,sizeof(*m)); m->updated_at = time(NULL); + m->name = strdup(name); char filename[512]; - snprintf(filename,512,"data/owner/marker-%s.json", name ); + snprintf(filename,512,"data/owner/markers/%s.json", name ); json_read_object_layout_from_file( filename, marker_layout, m ); return m; @@ -37,7 +39,8 @@ void marker_free( struct marker* m ) { if( !m ) { return; } - free(m->last_read_marker); + free(m->last_read_id); + free(m->name); free(m); } @@ -45,4 +48,15 @@ void marker_write_to_FILE( struct marker* m, FILE* f ) { json_write_object_layout_to_FILE( f, "", marker_layout, m ); } +void marker_save( struct marker* m ) +{ + if( !m->name ) { return; } + + mkdir( "data/owner", 0755 ); + mkdir( "data/owner/markers", 0755 ); + + char filename[512]; + snprintf(filename,512,"data/owner/markers/%s.json", m->name ); + json_write_object_layout_to_file( filename, "\t", marker_layout, m ); +} diff --git a/src/model/marker.h b/src/model/marker.h index 72ba8ce..2d6a4a6 100644 --- a/src/model/marker.h +++ b/src/model/marker.h @@ -5,13 +5,17 @@ struct marker { - char* last_read_marker; + char* last_read_id; int version; time_t updated_at; + + char* name; }; struct marker* marker_from_name( const char* name ); void marker_free( struct marker* m ); +void marker_save( struct marker* m ); + void marker_write_to_FILE( struct marker* m, FILE* f );