Add timeline filter handling, correctly write json strings in api response, more memory fixes

master
teknomunk 1 year ago
parent 83405b16d2
commit 2a39ca0791

@ -67,8 +67,7 @@ bool handle_post( struct http_request* req, struct account* a )
if( !json_pull_parser_begin_object( jpp, &save ) ) { return false; }
s = malloc(sizeof(struct status));
s->account_id = 0;
s->sensitive = false;
memset(s,0,sizeof(*s));
char* key;
while( key = json_pull_parser_read_object_key(jpp) ) {

@ -112,37 +112,52 @@ bool route_undo_activity( struct ap_activity* act )
bool route_follow( struct ap_activity* act )
{
struct account* follower = NULL;
bool res = false;
struct ap_activity* accept = NULL;
const char* target = act->object.ref;
struct account* a = account_from_uri( target );
if( !a || 0 != strcmp( a->server, g_server_name ) ) {
printf( "Unfollow not targeted at local account. Discarding.\n" );
return true;
goto success;
}
struct account* follower = account_from_uri( act->actor );
follower = account_from_uri( act->actor );
if( !follower ) {
follower = account_fetch_from_uri( act->actor );
}
if( !follower ) {
printf( "Unable to fetch account for %s\n", act->actor );
return false;
goto failed;
}
// Create Accept activity
struct ap_activity* accept = ap_activity_create_accept(act);
accept = ap_activity_create_accept(act);
char filename[512]; snprintf( filename, 512, "data/outbox/%d.json", accept->local_id );
char tmp_filename[512]; snprintf( tmp_filename, 512, "%s.tmp", filename );
FILE* f = fopen(tmp_filename,"w");
fprintf( f, "to: %d\n", follower->id );
ap_activity_write_to_FILE( accept, f );
fclose(f);
rename( tmp_filename, filename );
success:
res = true;
cleanup:
ap_activity_free(accept);
account_free(a);
account_free(follower);
exit(0);
return false;
return res;
failed:
res = false;
goto cleanup;
}
bool route_activity( struct ap_activity* act )

@ -14,7 +14,7 @@
#include "api/status.h"
#include "api/notice.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -27,8 +27,33 @@ enum timeline_ids {
bool handle_timeline( struct http_request* req, int timeline_id )
{
bool with_muted = false;
unsigned int limit = 100;
bool hide_muted = true;
unsigned int limit = 32;
int max_id = INT_MAX;
int since_id = 0;
// handle query parameters
if( http_request_route( req, "?" ) ) {
const char* key;
while( key = http_request_route_query_key(req) ) {
const char* value = http_request_route_query_value(req);
//printf( "%s = %s\n", key, value );
if( 0 == strcmp(key,"limit") ) {
int new_limit;
sscanf(value,"%d",&new_limit);
if( new_limit < limit ) {
limit = new_limit;
}
} else if( 0 == strcmp(key,"with_muted") ) {
hide_muted = false;
} else if( 0 == strcmp(key,"since_id") ) {
sscanf(value,"%d",&since_id);
} else if( 0 == strcmp(key,"max_id") ) {
sscanf(value,"%d",&max_id);
}
//printf( "Filter: limit=%d, max_id=%d, since_id=%d\n", limit, max_id, since_id );
}
}
struct timeline* tl = timeline_from_id( timeline_id );
if( !tl ) {
@ -38,10 +63,24 @@ bool handle_timeline( struct http_request* req, int timeline_id )
return true;
}
struct status* ss[32];
int count = timeline_load_statuses( tl, 0, 32, ss );
printf( "count=%d\n", count );
show_statuses( req, ss, count );
struct status* ss[limit];
int count = timeline_load_statuses( tl, 0, limit, ss );
for( int i = 0; i < count; ++i ) {
if( ss[i]->id <= since_id ) {
count = i;
break;
}
}
int base = 0;
for( int i = 0; i < count; ++i ) {
if( ss[i]->id >= max_id ) {
base = i + 1;
}
}
//printf( "limit=%d, since_id=%d, base=%d\n", limit, since_id, base );
//printf( "count=%d\n", count - base );
show_statuses( req, &ss[base], count - base );
for( int i = 0; i < count; ++i ) {
status_free( ss[i] );
@ -91,7 +130,7 @@ bool route_mastodon_api( struct http_request* req )
//*
if( !check_bearer_token(req) ) { return false; }
printf( "authorization still valid\n" );
//printf( "authorization still valid\n" );
//*/

@ -197,7 +197,6 @@ struct ap_activity* ap_activity_create_accept( struct ap_activity* act )
accept->object.ptr = ap_activity_dup(act);
accept->signature.type = apst_rsa_signature_2017;
accept->signature.creator = strdup(actor);
accept->signature.creator = strdup(actor);
accept->signature.created = strdup("TBD");
accept->signature.value = strdup("TBD");

@ -76,15 +76,17 @@ int timeline_load_statuses( struct timeline* tl, int offset_from_head, int count
if( count > tl->status_ids.count ) {
count = tl->status_ids.count;
}
printf( "count=%d\n", count );
//printf( "count=%d\n", count );
int found = 0;
for( int i = count - 1; i >= 0; --i ) {
result[found] = status_from_id( tl->status_ids.items[i] );
for( int i = 0; i < count; ++i ) {
int idx = tl->status_ids.count - ( i + 1 );
result[found] = status_from_id( tl->status_ids.items[idx] );
if( result[found] ) {
found += 1;
} else {
printf( "Couldn't load status with id %d\n", tl->status_ids.items[i] );
printf( "Couldn't load status with id %d\n", tl->status_ids.items[idx] );
}
}

@ -3,7 +3,7 @@
"application": null,
"bookmarked": false,
"card": null,
"content": "%s{ s->content }",
"content": %( json_write_string(f,s->content); ),
"created_at": "2022-12-12T02:52:24.000Z",
"emojis": [],
"favourited": false,

Loading…
Cancel
Save