Change show_statuses to take an array of pointers to statuses instead an array of statuses, start implementing timeline model

master
teknomunk 1 year ago
parent 99cf9a4abb
commit 16f4e1911e

@ -37,7 +37,7 @@ void show_status_context( struct http_request* req, struct status* s )
fprintf( f, "{\"ancestors\":[],\"descendants\":[]}" );
}
void show_statuses( struct http_request* req, struct status* ss, int count )
void show_statuses( struct http_request* req, struct status** ss, int count )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
@ -47,7 +47,7 @@ void show_statuses( struct http_request* req, struct status* ss, int count )
if( i > 0 ) {
fprintf( f, "," );
}
api_status_write_as_json(&ss[i],f);
api_status_write_as_json(ss[i],f);
}
fprintf( f, "]" );
}

@ -9,7 +9,7 @@ struct account;
void show_status( struct http_request* req, struct status* s );
void show_status_context( struct http_request* req, struct status* s );
void show_statuses( struct http_request* req, struct status* ss, int count );
void show_statuses( struct http_request* req, struct status** ss, int count );
bool handle_post( struct http_request* req, struct account* a );

@ -10,6 +10,7 @@
#include "model/status.h"
#include "model/account.h"
#include "model/notification.h"
#include "model/timeline.h"
#include "api/client_apps.h"
#include "api/status.h"
@ -21,8 +22,12 @@ bool handle_timeline( struct http_request* req, const char* which )
bool with_muted = false;
unsigned int limit = 100;
struct timeline* tl = timeline_from_id( 0 );
timeline_free(tl);
struct status* s = status_from_id(1);
show_statuses( req, s, 1 );
struct status* ss[] = { s };
show_statuses( req, ss, 1 );
status_free(s);
return true;

@ -1 +1 @@
Subproject commit ec58393b08f85ae9b73f76e8d0aa73d309e2a629
Subproject commit b779f4b44fc35c54e9f77913e65af72b3d783b29

@ -0,0 +1,77 @@
#include "timeline.h"
#include "json/json.h"
#include "json/layout.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
static struct json_object_field timeline_block_layout[] = {
{ "status_ids", offsetof( struct timeline, status_ids ), true, &json_field_array_of, &json_field_integer },
{ NULL },
};
struct timeline* timeline_load_block( int id, unsigned int head, unsigned int block )
{
struct timeline* tl = NULL;
char filename[512];
FILE* f = NULL;
struct json_pull_parser* jpp = NULL;
printf( "timeline_load_block( %d, %u, %u )\n", id, head, block );
snprintf( filename, 512, "data/timelines/%d/%u.json", id, block );
f = fopen(filename,"r");
if( !f ) { goto failed; }
tl = malloc(sizeof(struct timeline));
tl->id = id;
tl->block = head;
tl->head = head;
tl->status_ids.items = NULL;
tl->status_ids.count = 0;
jpp = json_pull_parser_new( f );
if( !jpp ) { goto failed; }
if( !json_read_object_layout( jpp, timeline_block_layout, tl ) ) { goto failed; }
for( int i = 0; i < tl->status_ids.count; ++i ) {
printf( "tl->status_ids[%d] = %d\n", i, tl->status_ids.items[i] );
}
cleanup:
json_pull_parser_release(jpp);
fclose(f);
return tl;
failed:
timeline_free(tl);
tl = NULL;
goto cleanup;
}
struct timeline* timeline_from_id( int id )
{
char filename[512];
snprintf( filename, 512, "data/timelines/%d/HEAD", id );
FILE* f = fopen(filename,"r");
unsigned int head = 0;
if( f ) {
fscanf( f, "%u", &head );
fclose(f);
}
return timeline_load_block( id, head, head );
}
struct timeline* timeline_new()
{
}
void timeline_free( struct timeline* tl )
{
if( !tl ) { return; }
free(tl->status_ids.items);
free(tl);
}

@ -0,0 +1,24 @@
#pragma once
struct timeline_block_ref
{
int starting_offset;
int block_id;
};
struct timeline
{
int id;
unsigned int block;
unsigned int head;
struct {
int* items;
int count;
} status_ids;
};
struct timeline* timeline_from_id( int id );
struct timeline* timeline_new();
void timeline_free( struct timeline* tl );
Loading…
Cancel
Save