Change tags to timelines and expose to frontend thru API

master
teknomunk 11 months ago
parent 67b6f6b73f
commit 7161566b78

@ -13,8 +13,15 @@
#include <string.h>
bool handle_timeline( struct http_request* req, int timeline_id )
static bool handle_timeline_internal( struct http_request* req, struct timeline* tl )
{
if( !tl ) {
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "[]" );
return true;
}
struct params_t {
int since_id;
int max_id;
@ -47,14 +54,6 @@ bool handle_timeline( struct http_request* req, int timeline_id )
if( params.limit > 100 ) { params.limit = 100; }
struct timeline* tl = timeline_from_id( timeline_id );
if( !tl ) {
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "[]" );
return true;
}
struct status* ss[params.limit];
memset(ss,0,sizeof(ss));
@ -109,9 +108,26 @@ done:
status_free( show.items[i] );
}
timeline_free(tl);
return true;
}
bool handle_timeline( struct http_request* req, int timeline_id )
{
struct timeline* tl = timeline_from_id( timeline_id );
bool res = handle_timeline_internal(req,tl);
timeline_free(tl);
return res;
}
bool handle_tag_timeline( struct http_request* req, char* tag )
{
char filename[512];
snprintf( filename,sizeof(filename), "data/statuses/tags/%s", tag );
struct timeline* tl = timeline_from_path( filename );
bool res = handle_timeline_internal(req,tl);
timeline_free(tl);
return res;
}

@ -11,4 +11,5 @@ enum timeline_ids {
};
bool handle_timeline( struct http_request* req, int timeline_id );
bool handle_tag_timeline( struct http_request* req, char* tag );

@ -201,6 +201,13 @@ bool route_mastodon_api( struct http_request* req )
}
} else if( http_request_route( req, "timelines/public" ) ) {
return handle_timeline( req, tli_public );
} else if( http_request_route( req, "timelines/tag/" ) ) {
char* tag = http_request_route_get_dir_or_file(req);
bool res = handle_tag_timeline( req, tag );
free(tag);
return res;
} else if( http_request_route( req, "instance" ) ) {
if( http_request_route_term( req, "/peers" ) ) {
http_request_send_headers( req, 200, "application/json", true );

@ -576,7 +576,6 @@ bool status_save_new( struct status* s )
return true;
}
void key_for_post( struct status* s, char* key, int sizeof_key );
void status_save( struct status* s )
{
char filename[512];
@ -599,9 +598,10 @@ void status_save( struct status* s )
for( int i = 0; i < s->tags.count; ++i ) {
char tag_file[512];
ffdb_trie_set( "data/statuses/tags", s->tags.items[i], "T" );
char time[512];
key_for_post( s, time, 512 );
ffdb_trie_set( format(tag_file,512, "data/statuses/tags/%s", s->tags.items[i] ), time, format( filename,512,"%d",s->id) );
struct timeline* tl = timeline_from_path( format(tag_file,512,"data/statuses/tags/%s", s->tags.items[i] ) );
timeline_add_post( tl, s );
timeline_free(tl);
}
}
void status_clean_tags()

@ -12,26 +12,37 @@
#include <stddef.h>
#include <string.h>
struct timeline* timeline_from_id( int id )
struct timeline* timeline_new()
{
struct timeline* tl;
tl = malloc(sizeof(*tl));
memset(tl,0,sizeof(*tl));
return tl;
}
struct timeline* timeline_from_id( int id )
{
struct timeline* tl = timeline_new();
tl->id = id;
tl->path = aformat( "data/accounts/%d/timeline", tl->id );
return tl;
}
struct timeline* timeline_new()
struct timeline* timeline_from_path( const char* path )
{
struct timeline* tl;
tl = malloc(sizeof(*tl));
memset(tl,0,sizeof(*tl));
struct timeline* tl = timeline_new();
tl->id = -1;
tl->path = strdup(path);
return tl;
}
void timeline_free( struct timeline* tl )
{
if( !tl ) { return; }
free( tl->path );
free(tl);
}
@ -43,10 +54,7 @@ int timeline_load_statuses( struct timeline* tl, int offset_from_head, int count
} values;
memset(&values,0,sizeof(values));
char filename[512];
snprintf( filename, sizeof(filename), "data/accounts/%d/timeline", tl->id );
ffdb_trie_list( filename, offset_from_head, count, NULL, &values );
ffdb_trie_list( tl->path, offset_from_head, count, NULL, &values );
int result_count = 0;
for( int i = 0; i < values.count && result_count < count; ++i ) {
char* id_str = values.items[i];
@ -61,7 +69,7 @@ int timeline_load_statuses( struct timeline* tl, int offset_from_head, int count
free(values.items);
return result_count;
}
void key_for_post( struct status* s, char* key, int sizeof_key )
static void key_for_post( struct status* s, char* key, int sizeof_key )
{
struct tm gmtime_data;
gmtime_r( &s->published, &gmtime_data );
@ -76,23 +84,17 @@ void key_for_post( struct status* s, char* key, int sizeof_key )
}
void timeline_add_post( struct timeline* tl, struct status* s )
{
char filename[512];
snprintf( filename, sizeof(filename), "data/accounts/%d/timeline", tl->id );
char key[512];
key_for_post(s,key,sizeof(key));
char value[32];
ffdb_trie_set( filename, key, format( value, sizeof(value), "%d", s->id ) );
ffdb_trie_set( tl->path, key, format( value, sizeof(value), "%d", s->id ) );
}
void timeline_remove_post( struct timeline* tl, struct status* s )
{
char filename[512];
snprintf( filename, sizeof(filename), "data/accounts/%d/timeline", tl->id );
char key[512];
key_for_post(s,key,sizeof(key));
ffdb_trie_remove( filename, key );
ffdb_trie_remove( tl->path, key );
}

@ -4,9 +4,11 @@ struct status;
struct timeline
{
int id;
char* path;
};
struct timeline* timeline_from_id( int id );
struct timeline* timeline_from_path( const char* path );
struct timeline* timeline_new();
void timeline_free( struct timeline* tl );

Loading…
Cancel
Save