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.

93 lines
2.0 KiB
C

#include "timeline.h"
// Model
#include "model/status.h"
// Submodules
#include "json/json.h"
#include "json/layout.h"
#include "ffdb/trie.h"
#include "util/format.h"
#include "util/time.h"
// Standard Library
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
struct timeline* timeline_new()
{
struct timeline* tl;
tl = malloc(sizeof(*tl));
memset(tl,0,sizeof(*tl));
return tl;
}
struct timeline* timeline_from_path( const char* path )
{
struct timeline* tl = timeline_new();
tl->id = -1;
tl->path = strdup(path);
return tl;
}
void timeline_free( struct timeline* tl )
{
if( !tl ) { return; }
free( tl->path );
free(tl);
}
int timeline_load_statuses( struct timeline* tl, int offset_from_head, int count, struct status** result )
{
struct {
char** items;
int count;
} keys, values;
memset(&values,0,sizeof(values));
memset(&keys,0,sizeof(keys));
ffdb_trie_list( tl->path, offset_from_head, count, &keys, &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) );
//printf( "found item: %s, s=%p\n", id_str, s );
if( s ) {
result[result_count] = s;
result_count += 1;
} else {
// Drop the item from the timeline if the status no longer exists locally
printf( "Unable to load status %s, dropping from timeline %s\n", id_str, tl->path );
ffdb_trie_set( tl->path, keys.items[i], NULL );
}
free(keys.items[i]);
free( id_str );
}
free(values.items);
free(keys.items);
return result_count;
}
void timeline_add_post( struct timeline* tl, struct status* s )
{
//printf( "- timeline_add_post( tl=%s, s=%d )\n", tl->path, s->id );
char key[512];
rfc3339_time_string( s->published, key, sizeof(key) );
char value[32];
ffdb_trie_set( tl->path, key, format( value, sizeof(value), "%d", s->id ) );
}
void timeline_remove_post( struct timeline* tl, struct status* s )
{
char key[512];
rfc3339_time_string( s->published, key, sizeof(key) );
ffdb_trie_remove( tl->path, key );
}