Fix SEGV when loading timeline

master
teknomunk 1 year ago
parent 57af3c43c0
commit e14dce833a

@ -2,6 +2,7 @@
// Submodules
#include "http/server/request.h"
#include "http/query.h"
#include "ffdb/fs_list.h"
#include "model/notification.h"
#include "collections/array.h"
@ -13,6 +14,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
void show_notifications( struct http_request* req, struct notification** ns, int count )
{
@ -31,57 +33,59 @@ void show_notifications( struct http_request* req, struct notification** ns, int
bool handle_notifications( struct http_request* req )
{
int note_count = fs_list_get("data/notices/HEAD");
struct {
struct notification** items;
int count;
} notes;
memset(&notes,0,sizeof(notes));
int limit = 32;
bool hide_muted = true;
struct params_t
{
bool with_muted;
int limit;
int since_id;
} params = {
.limit = 32,
.since_id = 0,
.with_muted = false,
};
#define OBJ_TYPE struct params_t
static struct http_query_fields fields[] = {
HTTP_QUERY_FIELD_INT( limit )
HTTP_QUERY_FIELD_INT( since_id )
HTTP_QUERY_FIELD_BOOL( with_muted )
HTTP_QUERY_FIELD_END
};
#undef OBJ_TYPE
// Handle query parameters
if( http_request_route( req, "?" ) ) {
const char* key;
while( key = http_request_route_query_key(req) ) {
if( 0 == strcmp(key,"limit") ) {
int new_limit;
sscanf(http_request_route_query_value(req),"%d",&new_limit);
if( new_limit < limit ) {
limit = new_limit;
}
} else if( 0 == strcmp(key,"with_muted") ) {
hide_muted = false;
} else if( 0 == strcmp(key,"since_id") ) {
int since_id = 0;
sscanf(http_request_route_query_value(req),"%d",&since_id);
if( since_id <= note_count ) {
limit = note_count - since_id;
} else {
limit = 0;
}
}
}
http_query_parse( req, fields, &params );
}
if( params.limit > 32 ) {
params.limit = 32;
}
int note_count = fs_list_get("data/notices/HEAD");
for( int i = 0; (i < limit) && (i < note_count); ++i ) {
for( int i = params.since_id; (i < params.limit) && (i < note_count); ++i ) {
int note_id = note_count - i;
struct notification* n = notification_from_id(note_id);
bool include = true;
// Apply filters
if( !n ) { include = false; }
if( !n ) { goto exclude; }
if( n ) {
// if( n->muted && !params.with_muted ) { goto exclude; }
}
if( include ) { // TODO: filter notes here
include:
array_append( &notes, sizeof(void*), &n );
} else {
printf( "Failed to load notice #%d\n", note_id );
continue;
exclude:
notification_free(n);
}
continue;
}
show_notifications( req, notes.items, notes.count );

Loading…
Cancel
Save