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.

112 lines
2.3 KiB
C

#include "status.h"
#include "status/react.h"
#include "model/account.h"
#include "model/ap/activity.h"
// Submodules
#include "json/json.h"
#include "json/layout.h"
#include "sha256/sha256.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
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 },
{ "content", offsetof( struct status, content ), true, &json_field_string },
{ "media", offsetof( struct status, media ), false, &json_field_array_of, &json_field_string },
{ "reacts", offsetof( struct status, reacts ), false, &json_field_array_of, &status_react_type },
{ NULL }
};
void* allocate( size_t s )
{
void* ptr = malloc(s);
memset( ptr, 0, s );
return ptr;
}
struct status* status_from_id( unsigned int id )
{
struct status* s = NULL;
char filename[512];
snprintf( filename, 512, "data/statuses/%d.json", id );
s = allocate(sizeof(struct status));
s->id = id;
if( !json_read_object_layout_from_file( filename, status_layout, s ) ) {
status_free(s);
return NULL;
}
return s;
}
struct status* status_from_activity( struct ap_activity* act )
{
struct status* s;
s = malloc(sizeof(*s));
memset(s,0,sizeof(*s));
struct account* a = account_from_uri(act->actor);
s->account_id = a->id;
account_free(a);
s->content = status_render_source( act->source );
return s;
}
bool status_save_new( struct status* s )
{
int head = -1;
FILE* f = fopen("data/statuses/HEAD","r");
fscanf(f,"%d",&head);
if( head == -1 ) { return false; }
fclose(f);
s->id = head + 1;
f = fopen("data/statuses/HEAD.tmp","w");
fprintf( f, "%d", s->id );
fclose(f);
rename( "data/statuses/HEAD.tmp", "data/statuses/HEAD" );
status_save( s );
return true;
}
void status_save( struct status* s )
{
char filename[512];
snprintf( filename, 512, "data/statuses/%d.json", s->id );
json_write_object_layout_to_file( filename, "\t", status_layout, s );
}
void status_free( struct status* s )
{
if( !s ) { return; }
for( int i = 0; i < s->media.count; ++i ) {
free(s->media.items[i]);
}
free(s->media.items);
for( int i = 0; i < s->reacts.count; ++i ) {
free(s->reacts.items[i]);
}
free(s->reacts.items);
free(s->content);
free(s);
}