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.

166 lines
3.5 KiB
C

#define _GNU_SOURCE
#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 },
{ "url", offsetof( struct status, url ), false, &json_field_string },
{ "sensitive", offsetof( struct status, sensitive ), true, &json_field_bool },
{ "source", offsetof( struct status, source ), false, &json_field_string },
{ "content", offsetof( struct status, source ), false, &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;
}
if( !s->source ) {
s->source = strdup("");
}
return s;
}
struct status* status_new_system_unfollow( int account_id )
{
struct account* a = account_from_id(account_id);
if( !a ) { return NULL; }
struct status* s;
s = malloc(sizeof(*s));
memset(s,0,sizeof(*s));
s->id = -1;
s->account_id = -1;
asprintf( &s->content, "%s unfollowed you\n", a->display_name );
s->sensitive = true;
account_free(a);
return s;
}
struct status* status_new_system_block( int account_id )
{
struct account* a = account_from_id(account_id);
if( !a ) { return NULL; }
struct status* s;
s = malloc(sizeof(*s));
memset(s,0,sizeof(*s));
s->id = -1;
s->account_id = -1;
asprintf( &s->content, "%s blocked you\n", a->display_name );
s->sensitive = true;
account_free(a);
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->source = strdup(act->source);
//s->content = status_render_source(s);
s->url = strdup( act->id );
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);
}
void status_add_react( struct status* s, const char* react )
{
printf( "TODO: add reaction '%s' to status #%d\n", react, s->id );
if( s->url ) {
// TODO generate outbox element
}
}