You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.7 KiB
C

#include "model/account.h"
// Submodules
#include "collections/array.h"
#include "format.h"
#include "ap/object.h"
// Standard Library
#include <string.h>
#include <stdlib.h>
bool account_sync_from_activity_pub( unsigned int account_id )
{
bool result = false;
char filename[512];
snprintf( filename, 512, "data/accounts/%d/ap.json", account_id );
printf( "ap_object_from_file( %s )\n", filename );
struct ap_object* obj = ap_object_from_file(filename);
printf( "obj = "); ap_object_write_to_FILE(obj,stdout);
printf( "\n" );
struct account* a = malloc(sizeof(struct account));
memset(a,0,sizeof(*a));
a->id = account_id;
if( !obj ) {
printf( "? Failed to sync account %d from %s, creating stub\n", account_id, filename );
goto failed;
}
a->handle = strdup(obj->preferred_username);
if( obj->name ) {
a->display_name = strdup(obj->name);
} else {
a->display_name = strdup(obj->preferred_username);
}
if( obj->avatar ) {
a->avatar.url = strdup(obj->avatar);
a->avatar.static_url = strdup(obj->avatar);
}
if( obj->banner ) {
a->banner = strdup(obj->banner);
}
a->bot = ( obj->type != ap_Person );
a->account_type = at_remote_activity_pub;
if( obj->url ) {
a->account_url = strdup(obj->url);
} else if( obj->id ) {
a->account_url = strdup(obj->id);
} else {
goto failed;
}
a->inbox = strdup(obj->inbox);
if( obj->summary ) {
a->note = strdup(obj->summary);
}
if( obj->endpoints.shared_inbox ) {
a->shared_inbox = strdup(obj->endpoints.shared_inbox);
}
if( 0 == strncmp( obj->id, "https://", 8 ) ) {
char* server_name = strdup(&obj->id[8]);
char* discard;
strtok_r(server_name,"/",&discard);
a->server = server_name;
}
// Extract out the public key
char* id = strdup(obj->public_key->id);
char* key_id = NULL;
strtok_r( id, "#", &key_id );
FILE* key_pem = fopen( format(filename,sizeof(filename),"data/accounts/%d/%s.pem", a->id, key_id), "w" );
if( !key_pem ) {
printf( "Unable to save public key to %s\n", filename );
} else {
printf( "Writing public key to %s\n", filename );
fprintf( key_pem, "%s", obj->public_key->public_key );
fclose(key_pem);
}
free(id);
account_save(a);
account_index_webfinger(a);
goto succeeded;
succeeded:
result = true;
goto cleanup;
cleanup:
ap_object_free(obj);
account_free(a);
return result;
failed:
result = false;
goto cleanup;
}
bool account_sync_from_activity( struct account* a, struct ap_object* act )
{
for( int i = 0; i < a->aliases.count; ++i ) {
free( a->aliases.items[i] );
}
a->aliases.count = 0;
for( int i = 0; i < act->also_known_as.count; ++i ) {
char* str = strdup(act->also_known_as.items[i]);
array_append( &a->aliases, sizeof(str), &str );
}
return true;
}
void account_sync_following_list( struct account* a )
{
// TODO: implement
}