Implement repost handling, cleanup some debug code, introduce account_from_uri_or_fetch helper function

master
teknomunk 1 year ago
parent 056c627d22
commit 57af3c43c0

@ -27,6 +27,12 @@ void api_status_write_as_json( struct status* s, FILE* f )
in_reply_to_account = account_from_id( in_reply_to->account_id );
}
struct status* repost = NULL;
if( s->repost_id ) {
printf( "s->repost_id = %d\n", s->repost_id );
repost = status_from_id( s->repost_id );
}
#include "src/view/api/status.json.inc"
cleanup:

@ -147,10 +147,10 @@ static struct status* lookup_object_status( struct ap_activity* act )
switch( act->object.tag ) {
case apaot_ref:
printf( "! Status %s doesn't exist locally\n", act->object.ref );
printf( "! Status %s doesn't exist locally (object.ref)\n", act->object.ref );
break;
case apaot_activity:
printf( "! Status %s doesn't exist locally\n", act->object.ptr->id );
printf( "! Status %s doesn't exist locally (object.ptr)\n", act->object.ptr->id );
break;
}
return NULL;
@ -170,6 +170,10 @@ static bool route_like( struct ap_activity* act )
struct status* s = lookup_object_status(act);
struct account* liker = lookup_actor_account(act);
if( !s ) {
return false;
};
status_add_like( s, liker );
return true;
@ -187,6 +191,7 @@ static bool route_emoji_react( struct ap_activity* act )
static bool route_create( struct ap_activity* act )
{
struct status* s = NULL;
bool result = false;
// Requires an object
@ -214,10 +219,7 @@ static bool route_create( struct ap_activity* act )
check_is_follower:
// Get actor account
struct account* actor_account = account_from_uri( obj->actor );
if( !actor_account ) {
actor_account = account_fetch_from_uri( obj->actor );
}
struct account* actor_account = account_from_uri_or_fetch( obj->actor );
if( !actor_account ) {
printf( "! Unable to fetch %s\n", obj->actor );
goto failed;
@ -234,7 +236,6 @@ check_is_follower:
}
// Create local status
struct status* s;
s = status_from_uri( obj->id );
if( !s ) {
s = status_from_activity(obj);
@ -281,15 +282,53 @@ static bool route_accept( struct ap_activity* act )
static bool route_announce( struct ap_activity* act )
{
struct account* owner_account = account_from_id( owner_account_id );
struct account* actor_account = account_from_uri( act->actor );
if( !account_does_follow( owner_account, actor_account->id ) ) {
// Not following, discard
printf( "%s does not follow %s\n", owner_account->handle, actor_account->account_url );
return true;
}
// Reposts do not show up in notifications unless notifications for the user are enabled
if( actor_account->notify_for_posts ) {
printf( "? TODO: notification\n" );
}
if( act->object.tag != apaot_ref ) {
printf( "! object not reference\n" );
return false;
}
struct status* original_post = status_fetch_from_uri( act->object.ref );
// TODO: handle repost
ap_activity_write_to_FILE( act, stdout );
struct status* s;
s = malloc(sizeof(*s));
memset(s,0,sizeof(*s));
return false;
s->url = strdup(act->id);
s->published = act->published;
s->repost_id = original_post->id;
s->account_id = actor_account->id;
s->sensitive = act->sensitive;
status_save_new(s);
status_add_to_timeline( s, home_timeline_id );
status_add_to_timeline( s, public_timeline_id );
status_add_to_timeline( s, actor_account->id );
status_free(s);
status_free(original_post);
return true;
}
bool route_activity( struct ap_activity* act )
{
printf( "Handling %s\n", act->id );
ap_activity_write_to_FILE( act, stdout );
switch( act->type ) {
case apat_undo: return route_undo_activity(act);

@ -1 +1 @@
Subproject commit add9b7ab24f7db20009565596e526eb64764a3ca
Subproject commit 7031acbfc47d71d648d647451a9bd90cb5af7c2b

@ -146,10 +146,20 @@ struct account* account_from_uri( const char* uri )
// TODO: handle bots
int account_id = lookup_account_id_from_uri( uri );
if( account_id == -1 ) { return NULL; }
if( account_id == -1 ) {
printf( "Failed to lookup local account id for %s\n", uri );
return NULL;
}
return account_from_id( account_id );
}
struct account* account_from_uri_or_fetch( const char* uri )
{
struct account* res = account_from_uri(uri);
if( res ) { return res; }
return account_fetch_from_uri( uri );
}
struct account* account_from_webfinger( const char* handle )
{
printf( "account_from_webfinger( %s )\n", handle );
@ -287,7 +297,6 @@ struct account* account_fetch_from_uri( const char* uri )
return NULL;
}
printf( "status_code = %d\n", status_code );
fclose(f);
rename(tmp_filename,filename);
}

@ -42,12 +42,14 @@ struct account
bool bot;
bool locked;
bool notify_for_posts;
};
void account_reindex();
struct account* account_from_id( int id );
struct account* account_from_uri( const char* uri );
struct account* account_from_uri_or_fetch( const char* uri );
struct account* account_from_webfinger( const char* handle );
struct account* account_fetch_from_uri( const char* uri );
struct account* account_new();

@ -13,7 +13,7 @@
static bool context_reader( struct json_pull_parser* jpp, void* field_data, struct json_object_field* layout_field_data )
{
printf( "context_reader\n" );
//printf( "context_reader\n" );
struct ap_activity_context* ctx = field_data;
char* str = json_pull_parser_read_string(jpp);
@ -62,7 +62,7 @@ static bool context_reader( struct json_pull_parser* jpp, void* field_data, stru
};
ctx->language = clang_unknown;
for( int i = 0; i < sizeof(langs)/sizeof(langs[0]); ++i ) {
printf( "candidate: %s ==? %s\n", langs[i], lang );
//printf( "candidate: %s ==? %s\n", langs[i], lang );
if( 0 == strcmp(langs[i],lang) ) {
ctx->language = i+1;
}

@ -31,7 +31,6 @@ void ap_activity_tag_free( struct ap_activity_tag* tag )
static void* alloc()
{
struct ap_activity_tag* ptr = malloc(sizeof(struct ap_activity_tag));
printf( "alloc -> %p\n", ptr );
memset(ptr,0,sizeof(*ptr));
return ptr;
}

@ -223,8 +223,11 @@ bool http_signature_validate( struct ap_envelope* env, const char* request_targe
}
// Get the account
struct account* actor = account_fetch_from_uri(actor_uri);
if( !actor ) { printf( "! failed to load account for %s\n", actor_uri ); goto failed; }
struct account* actor = account_from_uri_or_fetch(actor_uri);
if( !actor ) {
printf( "! failed to load account for %s\n", actor_uri );
goto failed;
}
// Get the public key
keys = account_get_public_key( actor, key_name );

@ -36,6 +36,7 @@ static struct json_object_field status_layout[] = {
JSON_FIELD_DATETIME( published, false ),
JSON_FIELD_INTEGER( in_reply_to, false ),
JSON_FIELD_INTEGER( repost_id, false ),
JSON_FIELD_INTEGER( root_status_id, false ),
JSON_FIELD_ARRAY_OF_STRINGS( media, false ),
@ -87,8 +88,9 @@ struct status* status_from_uri( const char* uri )
}
static bool status_sync_from_activity_pub( struct status* s, struct ap_activity* act )
{
printf( "Syncing status from activity %s\n", act->id );
bool result = false;
struct account* a = account_from_uri(act->actor);
struct account* a = account_from_uri_or_fetch(act->actor);
if( !a ) {
printf( "! Unable to get account for %s\n", act->actor );
goto failed;
@ -153,6 +155,7 @@ struct status* status_fetch_from_uri( const char* uri )
if( status_code != 200 ) {
return NULL;
}
fclose(f);
rename(tmp_filename,filename);
}
@ -171,7 +174,9 @@ cleanup:
return s;
failed:
if( s ) {
printf( "Creating stub status for later sync\n" );
status_flag_for_async_fetch(s);
goto cleanup;
}
status_free(s);
@ -262,6 +267,10 @@ void status_save( struct status* s )
hash_index_set( "data/statuses/uri", s->url, s->id );
}
}
void status_write_to_FILE( struct status* s, FILE* f )
{
json_write_object_layout_to_FILE( f, "\t", status_layout, s );
}
void status_free( struct status* s )
{

@ -22,6 +22,7 @@ struct status
time_t published;
int in_reply_to;
int repost_id;
int root_status_id;
struct {
@ -60,6 +61,7 @@ struct ap_activity;
struct status* status_from_activity( struct ap_activity* act );
bool status_save_new( struct status* s );
void status_write_to_FILE( struct status* s, FILE* f );
void status_save( struct status* s );
void status_free( struct status* s );

@ -80,7 +80,12 @@
"thread_muted": false
},
"poll": null,
"reblog": null,
%(
if( repost ) { )
"reblog": %( api_status_write_as_json( repost, f ); ),%(
} else { )
"reblog": null,%(
} )
"reblogged": %s{ s->reposts.count > 0 ? "true": "false" },
"reblogs_count": %d{ s->reposts.count },
"replies_count": 0,

Loading…
Cancel
Save