@ -21,6 +21,7 @@
# include <string.h>
# include <string.h>
# include <stdlib.h>
# include <stdlib.h>
# include <stddef.h>
# include <stddef.h>
# include <sys/stat.h>
# define OBJ_TYPE struct status
# define OBJ_TYPE struct status
static struct json_object_field status_layout [ ] = {
static struct json_object_field status_layout [ ] = {
@ -61,17 +62,46 @@ void* allocate( size_t s )
return ptr ;
return ptr ;
}
}
static const char * get_status_data_filename ( unsigned int id , char * filename , int size )
{
int millions = id / 1000000 ;
int thousands = ( id % 1000000 ) / 1000 ;
int ones = id % 1000 ;
mkdir ( format ( filename , size , " data/statuses/%03dm " , millions ) , 0755 ) ;
mkdir ( format ( filename , size , " data/statuses/%03dm/%03dk " , millions , thousands ) , 0755 ) ;
return format ( filename , size , " data/statuses/%03dm/%03dk/%03d.json " , millions , thousands , ones ) ;
}
static FILE * open_status_data_file ( unsigned int id , const char * mode )
{
char filename [ 512 ] ;
FILE * f = NULL ;
f = fopen ( get_status_data_filename ( id , filename , sizeof ( filename ) ) , mode ) ;
if ( f ) {
printf ( " Using new location at %s \n " , filename ) ;
return f ;
}
f = fopen ( format ( filename , sizeof ( filename ) , " data/statuses/%d.json " , id ) , mode ) ;
if ( f ) {
printf ( " Using existing location at %s \n " , filename ) ;
return f ;
}
}
struct status * status_from_id ( unsigned int id )
struct status * status_from_id ( unsigned int id )
{
{
struct status * s = NULL ;
struct status * s = NULL ;
char filename [ 512 ] ;
FILE * f = open_status_data_file ( id , " r " ) ;
snprintf ( filename , 512 , " data/statuses/%d.json " , id ) ;
if ( ! f ) { return NULL ; }
s = allocate ( sizeof ( struct status ) ) ;
s = allocate ( sizeof ( struct status ) ) ;
s - > id = id ;
s - > id = id ;
if ( ! json_read_object_layout_from_file ( filename , status_layout , s ) ) {
if ( ! json_read_object_layout_from_ FILE( f , status_layout , s ) ) {
printf ( " Failed to load status %d \n " , id ) ;
printf ( " Failed to load status %d \n " , id ) ;
status_free ( s ) ;
status_free ( s ) ;
return NULL ;
return NULL ;
@ -359,9 +389,7 @@ bool status_save_new( struct status* s )
void status_save ( struct status * s )
void status_save ( struct status * s )
{
{
char filename [ 512 ] ;
char filename [ 512 ] ;
snprintf ( filename , 512 , " data/statuses/%d.json " , s - > id ) ;
json_write_object_layout_to_file ( get_status_data_filename ( s - > id , filename , sizeof ( filename ) ) , " \t " , status_layout , s ) ;
json_write_object_layout_to_file ( filename , " \t " , status_layout , s ) ;
// Index the status
// Index the status
if ( s - > url ) {
if ( s - > url ) {
@ -449,6 +477,11 @@ void status_make_reply_to( struct status* s, int in_reply_to_id )
s - > in_reply_to = in_reply_to_id ;
s - > in_reply_to = in_reply_to_id ;
s - > root_status_id = in_reply_to - > root_status_id ;
s - > root_status_id = in_reply_to - > root_status_id ;
array_append ( & in_reply_to - > replies , sizeof ( s - > id ) , & s - > id ) ;
array_append ( & in_reply_to - > replies , sizeof ( s - > id ) , & s - > id ) ;
// TODO: full mention handling
int id = in_reply_to - > account_id ;
array_append ( & s - > mentions , sizeof ( id ) , & id ) ;
status_save ( in_reply_to ) ;
status_save ( in_reply_to ) ;
}
}
}
}