#include "outbox_envelope.h" #include "json/layout.h" #include "ffdb/fs_list.h" #include #include #include #include #define OBJ_TYPE struct outbox_envelope static struct json_object_field layout[] = { JSON_FIELD_STRING( shared_inbox, false ), JSON_FIELD_BOOL( sent, false ), JSON_FIELD_INTEGER( retries, false ), JSON_FIELD_DATETIME( retry_after, false ), { .key = "account", .offset = offsetof( struct outbox_envelope, account_id ), .required = true, .type = &json_field_integer }, { .key = "activity", .offset = offsetof( struct outbox_envelope, activity_id ), .required = true, .type = &json_field_integer }, JSON_FIELD_END }; #undef OBJ_TYPE struct outbox_envelope* outbox_envelope_new() { struct outbox_envelope* env; env = malloc(sizeof(*env)); if( !env ) { return NULL; } memset(env,0,sizeof(*env)); return env; } void outbox_envelope_free( struct outbox_envelope* env ) { if( !env ) { return; } free(env->shared_inbox); free(env); } void outbox_envelope_save( struct outbox_envelope* env ) { if( env->id == 0 ) { int id = fs_list_get( "data/outbox/HEAD" ) + 1; fs_list_set( "data/outbox/HEAD", id ); env->id = id; } char filename[512]; snprintf( filename, sizeof(filename), "data/outbox/%d.json", env->id ); json_write_object_layout_to_file( filename, "\t", layout, env ); } struct outbox_envelope* outbox_envelope_from_id( int id ) { struct outbox_envelope* env; if( !( env = outbox_envelope_new()) ) { return NULL; } env->id = id; char filename[512]; snprintf( filename, sizeof(filename), "data/outbox/%d.json", id ); if( !json_read_object_layout_from_file( filename, layout, env ) ) { outbox_envelope_free( env ); return NULL; } return env; } struct outbox_envelope* outbox_envelope_load_next() { return outbox_envelope_from_id( fs_list_get( "data/outbox/TAIL" ) + 1 ); } void outbox_envelope_delete( struct outbox_envelope* env ) { char filename[512]; snprintf( filename, sizeof(filename), "data/outbox/%d.json", env->id ); remove(filename); outbox_envelope_free(env); } void outbox_envelope_list_save( struct outbox_envelope_list* oel ) { for( int i = 0; i < oel->count; ++i ) { outbox_envelope_save(oel->items[i]); } } void outbox_envelope_list_free_composite( struct outbox_envelope_list* oel ) { for( int i = 0; i < oel->count; ++i ) { free(oel->items[i]); } free(oel->items); }