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.

81 lines
2.0 KiB
C

#include "notification.h"
#include "json/json.h"
#include "json/layout.h"
#include "ffdb/fs_list.h"
#include "model/account.h"
#include "model/status.h"
#include "controller/api/status.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
static struct json_enum notification_type_enum[] = {
{ "favorite", 1 },
{ "follow", 2 },
{ NULL, -1 },
};
static struct json_object_field notification_layout[] = {
{ "account_id", offsetof( struct notification, account_id ), true, &json_field_integer },
{ "status_id", offsetof( struct notification, status_id ), false, &json_field_integer },
{ "type", offsetof( struct notification, type ), true, &json_field_enum, notification_type_enum },
{ NULL }
};
struct notification* notification_from_id( int id )
{
struct notification* note = malloc(sizeof(struct notification));
memset(note,0,sizeof(*note));
note->id = id;
char filename[512];
snprintf( filename, 512, "data/notices/%d.json", id );
if( !json_read_object_layout_from_file( filename, notification_layout, note ) ) {
free(note);
return NULL;
}
return note;
}
struct notification* notification_new()
{
int id = fs_list_get( "data/notices/HEAD" ) + 1;
struct notification* note = malloc(sizeof(struct notification));
memset(note,0,sizeof(*note));
note->id = id;
fs_list_set( "data/notices/HEAD", id );
return note;
}
void notification_save( struct notification* note )
{
char filename[512];
snprintf( filename, 512, "data/notices/%d.json", note->id );
json_write_object_layout_to_file( filename, "\t", notification_layout, note );
}
void notification_free( struct notification* note )
{
free(note);
}
void notification_write_as_json( struct notification* n, FILE* f )
{
// TODO: move to controller/view
struct account* a = account_from_id( n->account_id );
struct account* owner = account_from_id( 0 );
struct status* s = status_from_id( n->status_id );
#include "src/model/notification.json.inc"
status_free(s);
account_free(owner);
account_free(a);
}