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.

101 lines
2.3 KiB
C

#include "model/account.h"
// Submodules
#include "collections/array.h"
#include "format.h"
// Model
#include "model/ap/activity.h"
#include "model/ap/account.h"
// Standard Library
#include <string.h>
#include <stdlib.h>
bool account_sync_from_activity_pub( unsigned int account_id )
{
char filename[512];
struct ap_account* ap = ap_account_from_file(
format( filename, 512, "data/accounts/%d/ap.json", account_id )
);
struct account* a = malloc(sizeof(struct account));
memset(a,0,sizeof(*a));
a->id = account_id;
if( !ap ) {
printf( "? Failed to sync account %d from %s, creating stub\n", account_id, filename );
return false;
}
printf( "ap = " ); ap_account_debug_dump(ap);
a->handle = strdup(ap->preferredUsername);
if( ap->name ) {
a->display_name = strdup(ap->name);
} else {
a->display_name = strdup(ap->preferredUsername);
}
if( ap->avatar ) {
a->avatar.url = strdup(ap->avatar);
a->avatar.static_url = strdup(ap->avatar);
}
a->bot = ( ap->type != apacct_Person );
a->account_type = at_remote_activity_pub;
a->account_url = strdup(ap->url);
a->inbox = strdup(ap->inbox);
if( ap->shared_inbox ) {
a->shared_inbox = strdup(ap->shared_inbox);
}
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;
}
// Extract out the public key
char* id = strdup(ap->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", ap->public_key.pem );
fclose(key_pem);
}
free(id);
account_save(a);
account_index_webfinger(a);
ap_account_free(ap);
account_free(a);
return true;
}
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
}