Implement status context

master
teknomunk 1 year ago
parent 783ac01a20
commit 46ad17fb66

@ -2,6 +2,7 @@
#include "json/json.h"
#include "json/layout.h"
#include "collections/array.h"
#include "http/server/request.h"
#include "model/status.h"
@ -40,12 +41,49 @@ void show_status( struct http_request* req, struct status* s )
api_status_write_as_json(s,f);
}
// Route: /api/v1/statuses/%d{s->id}/context
void show_status_context( struct http_request* req, struct status* s )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "{\"ancestors\":[],\"descendants\":[]}" );
struct {
struct status** items;
int count;
} ancestors, replies;
status_get_context( s, &ancestors, &replies );
/*
memset( &ancestors, 0, sizeof(ancestors));
memset( &replies, 0, sizeof(replies));
struct status* test = status_from_id( 111 );
array_append( &ancestors, sizeof(test), &test );
*/
fprintf( f, "{\"ancestors\":[" );
for( int i = 0; i < ancestors.count; ++i ) {
struct status* s = ancestors.items[i];
api_status_write_as_json(s,f);
status_free(s);
if( i != ancestors.count - 1 ) {
fprintf( f, "," );
}
}
free(ancestors.items);
fprintf( f, "],\"descendants\":[" );
for( int i = 0; i < replies.count; ++i ) {
struct status* s = replies.items[i];
api_status_write_as_json(s,f);
status_free(s);
if( i != replies.count - 1 ) {
fprintf( f, "," );
}
}
free(replies.items);
fprintf( f, "]}" );
}
void show_statuses( struct http_request* req, struct status** ss, int count )

@ -215,6 +215,37 @@ void status_make_reply_to( struct status* s, int in_reply_to_id )
status_save(in_reply_to);
}
}
void status_get_context( struct status* s, void* ancestors_ptr, void* replies_ptr )
{
struct array_of_status {
struct status** items;
int count;
};
struct array_of_status* ancestors = ancestors_ptr;
struct array_of_status* replies = replies_ptr;
memset(ancestors,0,sizeof(*ancestors));
memset(replies,0,sizeof(*replies));
struct status* parent = NULL;
for( int i = s->in_reply_to; i != 0; i = parent->in_reply_to ) {
parent = status_from_id( i );
array_append( ancestors, sizeof(parent), &parent );
};
// reverse ancestors
for( int i = 0; i < ancestors->count / 2; ++i ) {
struct status* a = ancestors->items[i];
struct status* b = ancestors->items[ ancestors->count - i - 1 ];
ancestors->items[i] = b;
ancestors->items[ ancestors->count - i - 1 ] = a;
}
for( int i = 0; i < s->replies.count; ++i ) {
struct status* reply = status_from_id( s->replies.items[i] );
array_append( replies, sizeof(reply), &reply );
}
}
void status_add_react( struct status* s, const char* react, struct account* a )
{

@ -62,6 +62,7 @@ void status_save( struct status* s );
void status_free( struct status* s );
void status_make_reply_to( struct status* s, int in_reply_to );
void status_get_context( struct status* s, void* ancestors, void* replies );
void status_add_to_timeline( struct status* s, int timeline_id );

Loading…
Cancel
Save