#include "timeline.h" #include "json/json.h" #include "json/layout.h" #include "ffdb/trie.h" #include "util/format.h" #include "model/status.h" #include #include #include #include struct timeline* timeline_from_id( int id ) { struct timeline* tl; tl = malloc(sizeof(*tl)); memset(tl,0,sizeof(*tl)); tl->id = id; return tl; } struct timeline* timeline_new() { struct timeline* tl; tl = malloc(sizeof(*tl)); memset(tl,0,sizeof(*tl)); return tl; } void timeline_free( struct timeline* tl ) { if( !tl ) { return; } free(tl); } int timeline_load_statuses( struct timeline* tl, int offset_from_head, int count, struct status** result ) { struct { char** items; int count; } values; memset(&values,0,sizeof(values)); char filename[512]; snprintf( filename, sizeof(filename), "data/accounts/%d/timeline", tl->id ); ffdb_trie_list( filename, offset_from_head, count, NULL, &values ); int result_count = 0; for( int i = 0; i < values.count && result_count < count; ++i ) { char* id_str = values.items[i]; struct status* s = status_from_id( atoi(id_str) ); free( id_str ); if( s ) { result[result_count] = s; result_count += 1; } } free(values.items); return result_count; } static void key_for_post( struct status* s, char* key, int sizeof_key ) { struct tm gmtime_data; gmtime_r( &s->published, &gmtime_data ); snprintf( key, sizeof_key, "%04d-%02d-%02dT%02d:%02d:%02dZ", gmtime_data.tm_year + 1900, gmtime_data.tm_mon + 1, gmtime_data.tm_mday, gmtime_data.tm_hour, gmtime_data.tm_min, gmtime_data.tm_sec ); } void timeline_add_post( struct timeline* tl, struct status* s ) { char filename[512]; snprintf( filename, sizeof(filename), "data/accounts/%d/timeline", tl->id ); char key[512]; key_for_post(s,key,sizeof(key)); char value[32]; ffdb_trie_set( filename, key, format( value, sizeof(value), "%d", s->id ) ); } void timeline_remove_post( struct timeline* tl, struct status* s ) { char filename[512]; snprintf( filename, sizeof(filename), "data/accounts/%d/timeline", tl->id ); char key[512]; key_for_post(s,key,sizeof(key)); ffdb_trie_remove( filename, key ); }