Fix offset for public_key in ap/account layout, create local account data for remote actor, add ap/account debug dump

master
teknomunk 1 year ago
parent 6275af369b
commit f92860d564

@ -1,3 +1,4 @@
#include "model/account.h"
#include "model/ap/account.h"
void develop()

@ -1 +1 @@
Subproject commit 549932fbf054ab65664fc054f7daafdb2ad8f82c
Subproject commit c8df4630afa66d98a448f532ad3436843db55db1

@ -8,6 +8,7 @@
#include "fs_list.h"
#include "model/server.h"
#include "model/ap/account.h"
#include <stdio.h>
#include <stdlib.h>
@ -80,13 +81,13 @@ struct reverse_index_entry
free(rie);
}
bool rie_reader( struct json_pull_parser* jpp, void* field_data, void* type_data )
bool rie_reader( struct json_pull_parser* jpp, void* field_data, struct json_object_field* layout_field_data )
{
*(void**)field_data = malloc(sizeof(struct reverse_index_entry));
bool res = json_read_object_layout( jpp, reverse_index_entry_layout, *(void**)field_data );
return res;
}
void rie_writer( struct json_writer* jpp, void* field_data, void* type_data )
void rie_writer( struct json_writer* jpp, void* field_data, struct json_object_field* layout_field_data )
{
json_write_pretty_object_layout( jpp, reverse_index_entry_layout, *(void**)field_data );
}
@ -133,7 +134,6 @@ static int index_uri_to_account_id( const char* uri, int account_id )
struct reverse_index_entry* ptr = &new_entry;
/*
printf( "Before append\n" );
for( int i = 0; i < ari.entries.count; ++i ) {
struct reverse_index_entry* entry = ari.entries.items[i];
printf( "\tentry[%d] = { .uri = %s, .account_id = %d }\n", i, entry->uri, entry->account_id );
@ -142,13 +142,6 @@ static int index_uri_to_account_id( const char* uri, int account_id )
array_append( &ari.entries, sizeof(struct reverse_index_entry), &ptr );
/*
for( int i = 0; i < ari.entries.count; ++i ) {
struct reverse_index_entry* entry = ari.entries.items[i];
printf( "\tentry[%d] = { .uri = %s, .account_id = %d }\n", i, entry->uri, entry->account_id );
}
*/
json_write_object_layout_to_file( filename, "\t", account_reverse_index_layout, &ari );
for( int i = 0; i < ari.entries.count-1; ++i ) {
@ -262,6 +255,38 @@ bool http_get( const void** opt )
return result;
}
void account_sync_from_acitvity_pub( unsigned int account_id )
{
char filename[512];
snprintf( filename, 512, "data/accounts/%d/ap.json", account_id );
struct ap_account* ap = ap_account_from_file( filename );
//printf( "ap = " ); ap_account_debug_dump(ap);
struct account* a = malloc(sizeof(struct account));
memset(a,0,sizeof(*a));
a->id = account_id;
a->handle = strdup(ap->preferredUsername);
a->display_name = strdup(ap->name);
a->avatar.url = strdup(ap->avatar);
a->avatar.static_url = strdup(ap->avatar);
a->bot = ( ap->type != apacct_Person );
a->account_url = strdup(ap->url);
if( 0 == strncmp( ap->id, "https://", 8 ) ) {
char* server_name = strdup(&ap->id[8]);
char* discard;
strtok_r(server_name,"/",&discard);
a->server = server_name;
}
account_save(a);
ap_account_free(ap);
account_free(a);
}
struct account* account_fetch_from_uri( const char* uri )
{
int account_id = lookup_account_id_from_uri( uri );
@ -309,9 +334,9 @@ struct account* account_fetch_from_uri( const char* uri )
rename(tmp_filename,filename);
}
printf( "Have result, need to process\n" );
account_sync_from_acitvity_pub( account_id );
return NULL;
return account_from_id(account_id);
}
void account_free( struct account* a )
@ -319,6 +344,14 @@ void account_free( struct account* a )
if( !a ) { return; }
}
void account_save( struct account* a )
{
char filename[512];
snprintf( filename, 512, "data/accounts/%d.json", a->id );
json_write_object_layout_to_file( filename, "\t", account_layout, a );
}
// TODO: move to controller/view
void account_write_as_json( struct account* a, FILE* f )
{

@ -27,6 +27,7 @@ struct account* account_fetch_from_uri( const char* uri );
struct account* account_new();
void account_free( struct account* a );
void account_save( struct account* a );
void account_sync_from_acitvity_pub( unsigned int id );
void account_add_follower( struct account* a, struct account* follower );
void account_remove_follower( struct account* a, struct account* follower );

@ -19,18 +19,31 @@ static struct json_object_field public_key_layout[] = {
{ NULL, 0 },
};
static struct json_object_field icon_layout[] = {
{ "url", offsetof(struct ap_account,avatar), true, &json_field_string },
{ NULL },
};
static struct json_object_field image_layout[] = {
{ "url", offsetof(struct ap_account,banner), true, &json_field_string },
{ NULL },
};
static struct json_object_field ap_account_layout[] = {
{ "id", offsetof(struct ap_account,id), true, &json_field_string },
{ "type", offsetof(struct ap_account,type), true, &json_field_enum, ap_account_type_enum },
{ "url", offsetof(struct ap_account,url), true, &json_field_string },
{ "name", offsetof(struct ap_account,name), true, &json_field_string },
{ "inbox", offsetof(struct ap_account,inbox), true, &json_field_string },
{ "summary", offsetof(struct ap_account,summary), true, &json_field_string },
{ "featured", offsetof(struct ap_account,featured), false, &json_field_string },
{ "followers", offsetof(struct ap_account,followers), false, &json_field_string },
{ "following", offsetof(struct ap_account,following), false, &json_field_string },
{ "publicKey", offsetof(struct ap_account,following), false, &json_field_object_composite, public_key_layout },
{ "discoverable", offsetof(struct ap_account,discoverable), false, &json_field_bool },
{ "id", offsetof(struct ap_account,id), true, &json_field_string },
{ "type", offsetof(struct ap_account,type), true, &json_field_enum, ap_account_type_enum },
{ "url", offsetof(struct ap_account,url), true, &json_field_string },
{ "name", offsetof(struct ap_account,name), true, &json_field_string },
{ "preferredUsername", offsetof(struct ap_account,preferredUsername), true, &json_field_string },
{ "inbox", offsetof(struct ap_account,inbox), true, &json_field_string },
{ "summary", offsetof(struct ap_account,summary), true, &json_field_string },
{ "featured", offsetof(struct ap_account,featured), false, &json_field_string },
{ "followers", offsetof(struct ap_account,followers), false, &json_field_string },
{ "following", offsetof(struct ap_account,following), false, &json_field_string },
{ "publicKey", offsetof(struct ap_account,public_key), false, &json_field_object_composite, public_key_layout },
{ "discoverable", offsetof(struct ap_account,discoverable), false, &json_field_bool },
{ "icon", 0, false, &json_field_object_composite, icon_layout },
{ "image", 0, false, &json_field_object_composite, image_layout },
{ NULL },
};
@ -57,4 +70,9 @@ void ap_account_free( struct ap_account* acc )
free(acc);
}
void ap_account_debug_dump( struct ap_account* acc )
{
FILE* f = stdout;
#include "src/model/ap/account.debug_dump.txt.inc"
}

@ -0,0 +1,27 @@
%p{acc} = {
.id = %s{acc->id},
.type = %d{acc->type},
.url = %s{acc->url},
.name = %s{acc->name},
.preferredUsername = %s{acc->preferredUsername},
.inbox = %s{acc->inbox},
.outbox = %s{acc->outbox},
.featured = %s{acc->featured},
.followers = %s{acc->followers},
.following = %s{acc->following},
.avatar = %s{acc->avatar},
.banner = %s{acc->banner},
.summary = %s{acc->summary},
.discoverable = %c{ acc->discoverable ? 'T' : 'F' },
.public_key = {
.id = %s{acc->public_key.id},
.owner = %s{acc->public_key.owner},
.pem = %s{acc->public_key.pem}
}
}

@ -21,6 +21,7 @@ struct ap_account
char* url;
char* name;
char* preferredUsername;
char* inbox;
char* outbox;
@ -28,12 +29,18 @@ struct ap_account
char* followers;
char* following;
char* avatar;
char* banner;
char* summary;
struct ap_crypto_key public_key;
bool discoverable;
struct ap_crypto_key public_key;
};
struct ap_account* ap_account_from_file( const char* filename );
void ap_account_free( struct ap_account* acc );
void ap_account_debug_dump( struct ap_account* acc );

Loading…
Cancel
Save