diff --git a/src/controller/mastodon_api.c b/src/controller/mastodon_api.c index ccdac6e..0f0bd2d 100644 --- a/src/controller/mastodon_api.c +++ b/src/controller/mastodon_api.c @@ -15,6 +15,7 @@ #include "model/notification.h" #include "model/server.h" #include "model/media.h" +#include "model/marker.h" // View #include "view/api/Attachement.h" @@ -163,6 +164,40 @@ bool handle_blocks( struct http_request* req ) return true; } +// route: POST /api/v1/markers +bool handle_update_markers( struct http_request* req ) +{ + return false; +} + +// route: GET /api/v1/markers +bool handle_markers( struct http_request* req ) +{ + http_request_send_headers( req, 200, "application/json", true ); + FILE* f = http_request_get_response_body( req ); + fprintf( f, "{" ); + bool is_first = true; + if( http_request_route( req, "?" ) ) { + if( 0 == strcmp("timeline[]",http_request_route_query_key(req) ) ) { + if( !is_first ) { + fprintf( f, "," ); + } + + const char* marker_name = http_request_route_query_value(req); + struct marker* m = marker_from_name(marker_name); + + if( m ) { + fprintf( f, "\"%s\":", marker_name ); + marker_write_to_FILE( m, f ); + marker_free(m); + is_first = false; + } + } + } + fprintf( f, "}" ); + return true; +} + // route: POST /api/v1/media bool handle_media( struct http_request* req ) { @@ -278,6 +313,12 @@ 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, "markers" ) ) { + if( http_request_route_method( req, "POST" ) ) { + return handle_update_markers(req); + } else { + return handle_markers(req); + } } else if( http_request_route( req, "filters" ) ) { http_request_send_headers( req, 200, "application/json", true ); FILE* f = http_request_get_response_body( req ); diff --git a/src/model/marker.c b/src/model/marker.c new file mode 100644 index 0000000..b422ae9 --- /dev/null +++ b/src/model/marker.c @@ -0,0 +1,48 @@ +#include "marker.h" + +#include "json/json.h" +#include "json/layout.h" + +#include +#include +#include + +#define OBJ_TYPE struct marker +static struct json_object_field marker_layout[] = { + JSON_FIELD_STRING( last_read_marker, false ), + JSON_FIELD_INTEGER( version, true ), + JSON_FIELD_DATETIME( updated_at, false ), + JSON_FIELD_END +}; +#undef OBJ_TYPE + +struct marker* marker_from_name( const char* name ) +{ + if( strstr( name, "../" ) ) { return NULL; } + + struct marker* m; + m = malloc(sizeof(*m)); + memset(m,0,sizeof(*m)); + + m->updated_at = time(NULL); + + char filename[512]; + snprintf(filename,512,"data/owner/marker-%s.json", name ); + json_read_object_layout_from_file( filename, marker_layout, m ); + + return m; +} + +void marker_free( struct marker* m ) +{ + if( !m ) { return; } + + free(m->last_read_marker); + free(m); +} + +void marker_write_to_FILE( struct marker* m, FILE* f ) +{ + json_write_object_layout_to_FILE( f, "", marker_layout, m ); +} + diff --git a/src/model/marker.h b/src/model/marker.h new file mode 100644 index 0000000..72ba8ce --- /dev/null +++ b/src/model/marker.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +struct marker +{ + char* last_read_marker; + int version; + time_t updated_at; +}; + +struct marker* marker_from_name( const char* name ); +void marker_free( struct marker* m ); + +void marker_write_to_FILE( struct marker* m, FILE* f ); +