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.

110 lines
2.3 KiB
C

#include "outbox_envelope.h"
#include "json/layout.h"
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <time.h>
#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 ) {
struct timespec ts;
clock_gettime( CLOCK_REALTIME, &ts );
int id = ts.tv_sec * 1000 + ts.tv_nsec / (100*1000*1000) + (rand() % 100);
//int id = fs_list_inc( "data/outbox/HEAD" );
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;
}
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 ) {
outbox_envelope_free(oel->items[i]);
}
free(oel->items);
}