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.

85 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 "view/api/Status.h"
#include "view/api/Account.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
static struct json_enum notification_type_enum[] = {
{ "favorite", nt_favorite },
{ "follow", nt_follow },
{ "mention", nt_mention },
{ "unfollow", nt_unfollow },
{ "block", nt_block },
{ "like", nt_like },
{ "react", nt_react },
{ NULL, -1 },
};
#define OBJ_TYPE struct notification
static struct json_object_field notification_layout[] = {
JSON_FIELD_INTEGER( debug, false ),
JSON_FIELD_INTEGER( account_id, true ),
JSON_FIELD_INTEGER( status_id, false ),
JSON_FIELD_INTEGER( ref_account_id, false ),
JSON_FIELD_DATETIME( created_at, false ),
JSON_FIELD_STRING( react, false ),
JSON_FIELD_ENUM( type, notification_type_enum, true ),
JSON_FIELD_END,
};
#undef OBJ_TYPE
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;
note->created_at = time(NULL);
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 )
{
if( !note ) { return; }
free(note->react);
status_free(note->system_status);
free(note);
}