Convert account over json layout loader, start implementing account reverse index (needed for account uri to account id lookup), more work on ap_activity, convert owner over to json layout loader

master
teknomunk 1 year ago
parent 6706606cf8
commit 65756c5795

@ -1 +1 @@
Subproject commit 8853784e4d4cc3954c0bf12ad46ede4b1eabb56a
Subproject commit 9baefa441da46e353691c0e12ac0b008afd4802d

@ -1,9 +1,13 @@
#include "account.h"
#include "json/json.h"
#include "json/layout.h"
#include "sha256/sha256.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
static const char* safe( const char* value, const char* other )
{
@ -15,63 +19,106 @@ static const char* b(bool value)
return value ? "true" : "false";
}
static struct json_object_field account_layout[] = {
{ "handle", offsetof( struct account, handle ), true, &json_field_string },
{ "server", offsetof( struct account, server ), true, &json_field_string },
{ "display_name", offsetof( struct account, display_name ), true, &json_field_string },
{ "avatar", offsetof( struct account, avatar.url ), true, &json_field_string },
{ "avatar_static", offsetof( struct account, avatar.static_url ), true, &json_field_string },
{ "account_url", offsetof( struct account, account_url ), true, &json_field_string },
{ NULL },
};
struct account* account_from_id( unsigned int id )
{
struct account* a = NULL;
char filename[512];
snprintf( filename, 512, "data/accounts/%d.json", id );
FILE* f = fopen(filename,"r");
if( !f) { return NULL; }
struct json_pull_parser* jpp = json_pull_parser_new( f );
if( !jpp ) { return NULL; }
a = malloc(sizeof(struct account));
struct account* a = malloc(sizeof(struct account));
memset( a, 0, sizeof(struct account) );
a->id = id;
a->handle = NULL;
a->server = NULL;
a->display_name = NULL;
a->avatar.url = NULL;
a->avatar.static_url = NULL;
a->bot = false;
int save;
if( !json_pull_parser_begin_object( jpp, &save ) ) { goto failed; }
if( !json_read_object_layout_from_file( filename, account_layout, a ) ) {
account_free(a);
return NULL;
}
char* key;
while( key = json_pull_parser_read_object_key(jpp) ) {
if( 0 == strcmp( key, "handle" ) ) {
a->handle = json_pull_parser_read_string(jpp);
return a;
}
} else if( 0 == strcmp( key, "server" ) ) {
a->server = json_pull_parser_read_string(jpp);
struct reverse_index_entry
{
char* uri;
int account_id;
};
static struct json_object_field reverse_index_entry_layout[] = {
{ "uri", offsetof( struct reverse_index_entry, uri ), true, &json_field_string },
{ "id", offsetof( struct reverse_index_entry, account_id ), true, &json_field_integer },
{ NULL },
};
static void* alloc_reverse_index_entry()
{
void* ptr = malloc(sizeof(struct reverse_index_entry));
memset( ptr, 0, sizeof(struct reverse_index_entry));
return ptr;
}
static void free_reverse_index_entry( void* ptr )
{
struct reverse_index_entry* rie = ptr;
free(rie->uri);
free(rie);
}
} else if( 0 == strcmp( key, "display_name" ) ) {
a->display_name = json_pull_parser_read_string(jpp);
static struct json_field_type reverse_index_entry_type = {
.reader = json_field_object_type_reader,
.writer = json_field_object_type_writer,
.size = 4,
.alloc = alloc_reverse_index_entry,
.free = free_reverse_index_entry,
};
} else if( 0 == strcmp( key, "avatar" ) ) {
a->avatar.url = json_pull_parser_read_string(jpp);
struct account_reverse_index
{
struct {
struct reverse_index_entry** items;
int count;
} entries;
};
static struct json_object_field account_reverse_index_layout[] = {
{ "entries", offsetof( struct account_reverse_index, entries ), true, &json_field_array_of, &reverse_index_entry_type },
{ NULL },
};
struct account* account_from_uri( const char* uri )
{
struct account* result = NULL;
} else if( 0 == strcmp( key, "account_url" ) ) {
a->account_url = json_pull_parser_read_string(jpp);
char hash[65] = "";
sha256_easy_hash_hex( uri, strlen(uri), hash );
hash[5] = '\0';
} else if( 0 == strcmp( key, "avatar_static" ) ) {
a->avatar.static_url = json_pull_parser_read_string(jpp);
}
}
char filename[512];
snprintf( filename, 512, "data/accounts/revese_index/%s.json", hash );
if( !json_pull_parser_end_object(jpp, &save ) ) { goto failed; }
struct account_reverse_index ari;
if( !json_read_object_layout_from_file( filename, account_reverse_index_layout, &ari ) ) {
return NULL;
}
cleanup:
json_pull_parser_release(jpp);
fclose(f);
for( int i = 0; i < ari.entries.count; ++i ) {
struct reverse_index_entry* entry = ari.entries.items[i];
if( 0 == strcmp(entry->uri,uri) ) {
result = account_from_id( entry->account_id );
}
free(entry->uri);
free(entry);
}
free(ari.entries.items);
return a;
failed:
account_free(a);
a = NULL;
goto cleanup;
return result;
}
void account_free( struct account* a )
@ -80,6 +127,7 @@ void account_free( struct account* a )
}
// TODO: move to controller/view
void account_write_as_json( struct account* a, FILE* f )
{
#define RENDER

@ -22,6 +22,7 @@ struct account
};
struct account* account_from_id( unsigned int id );
struct account* account_from_uri( const char* uri );
struct account* account_new();
void account_free( struct account* a );
void account_save( struct account* a );

@ -0,0 +1,36 @@
#include "activity.h"
#include "json/json.h"
#include <stddef.h>
static struct json_enum ap_signature_type_enum[] = {
{ "RsaSignature2017", 1 },
{ NULL, 0 },
};
static struct json_object_field ap_signature_layout[] = {
{ "type", offsetof( struct ap_signature, type ), true, &json_field_enum, ap_signature_type_enum },
{ "creator", offsetof( struct ap_signature, creator ), true, &json_field_string },
{ "created", offsetof( struct ap_signature, created ), true, &json_field_string }, // TODO: date parser
{ "signatureValue", offsetof( struct ap_signature, value ), true, &json_field_string },
{ NULL },
};
static struct json_enum ap_activity_type_enum[] = {
{ "Undo", 1 },
{ "Follow", 2 },
{ NULL, 0 },
};
struct json_object_field ap_activity_layout[] = {
{ "id", offsetof( struct ap_activity, id ), true, &json_field_string },
{ "type", offsetof( struct ap_activity, type ), true, &json_field_enum, ap_activity_type_enum },
{ "actor", offsetof( struct ap_activity, actor ), true, &json_field_string },
{ "to", offsetof( struct ap_activity, to ), true, &json_field_array_of, &json_field_string },
{ "cc", offsetof( struct ap_activity, cc ), false, &json_field_array_of, &json_field_string },
{ "bcc", offsetof( struct ap_activity, bcc ), false, &json_field_array_of, &json_field_string },
{ "object", offsetof( struct ap_activity, object ), false, &json_field_string },
{ "signature", offsetof( struct ap_activity, signature ), true, &json_field_object_composite, ap_signature_layout },
{ NULL },
};

@ -1,20 +1,39 @@
#pragma once
struct ap_signature;
#include "json/layout.h"
enum ap_signature_type
{
apst_rsa_signature_2017 = 1,
};
struct ap_signature
{
int type;
char* creator;
char* created;
char* value;
};
enum ap_activity_type
{
apat_undo = 1,
apat_follow = 2,
};
struct ap_activity
{
char* id;
char* type;
int type;
char* actor;
struct {
char** items;
int count;
} to;
} to, cc, bcc;
char* object;
struct ap_signature* signature;
struct ap_signature signature;
};
struct json_field_type;
extern struct json_field_type json_field_object_activity;
struct json_object_field;
extern struct json_object_field ap_activity_layout[];

@ -1,45 +1,30 @@
#include "owner.h"
#include "json/json.h"
#include "json/layout.h"
#include "sha256/sha256.h"
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
struct owner* owner_new()
{
struct owner* o = malloc(sizeof(struct owner));
FILE* f = fopen("data/owner.json","r");
if( f ) {
struct json_pull_parser* jpp = json_pull_parser_new(f);
if( !jpp ) {
goto reinit;
}
static struct json_object_field owner_layout[] = {
{ "salt", offsetof( struct owner, password_salt ), true, &json_field_string },
{ "hash", offsetof( struct owner, password_hash ), true, &json_field_string },
{ NULL },
};
// Load file from
int save;
if( !json_pull_parser_begin_object( jpp, &save ) ) { goto reinit; }
void* allocate( size_t s );
char* key;
while( key = json_pull_parser_read_object_key(jpp) ) {
if( 0 == strcmp(key,"salt") ) {
o->password_salt = json_pull_parser_read_string(jpp);
} else if( 0 == strcmp(key,"hash") ) {
o->password_hash = json_pull_parser_read_string(jpp);
}
}
if( !json_pull_parser_end_object( jpp, &save ) ) { goto reinit; }
struct owner* owner_new()
{
struct owner* o = allocate(sizeof(struct owner));
o->initialized = true;
return o;
if( !json_read_object_layout_from_file( "data/owner.json", owner_layout, o ) ) {
owner_free(o);
return NULL;
}
reinit:
o->initialized = false;
o->password_salt = strdup("");
o->password_hash = strdup("");
return o;
}
void owner_free( struct owner* o )

@ -5,22 +5,13 @@
#include "json/json.h"
#include "json/layout.h"
#include "sha256/sha256.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
void write_json_escaped( FILE* f, const char* str );
static const char* b( bool value )
{
return value ? "true" : "false";
}
static const char* host()
{
return "apogee.polaris-1.work";
}
static struct json_object_field status_layout[] = {
{ "account_id", offsetof( struct status, account_id ), true, &json_field_integer },
{ "sensitive", offsetof( struct status, sensitive ), true, &json_field_bool },
@ -48,7 +39,7 @@ struct status* status_from_id( unsigned int id )
s->id = id;
if( !json_read_object_layout_from_file( filename, status_layout, s ) ) {
free(s);
status_free(s);
return NULL;
}

Loading…
Cancel
Save