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.

110 lines
2.4 KiB
C

#include "timeline.h"
#include "json/json.h"
#include "json/layout.h"
#include "ffdb/trie.h"
#include "util/format.h"
#include "model/status.h"
#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_id( int id )
{
struct timeline* tl = timeline_new();
tl->id = id;
tl->path = aformat( "data/accounts/%d/timeline", tl->id );
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;
}
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 key[512];
key_for_post(s,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];
key_for_post(s,key,sizeof(key));
ffdb_trie_remove( tl->path, key );
}