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.

102 lines
2.0 KiB
C

#include "notice.h"
// Submodules
#include "http/server/request.h"
#include "http/query.h"
#include "ffdb/fs_list.h"
#include "model/notification.h"
#include "collections/array.h"
// Model
#include "model/status.h"
// View
#include "view/api/Notification.h"
// Stdlib
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
void show_notifications( struct http_request* req, struct notification** ns, int count )
{
http_request_send_headers( req, 200, "application/json", true );
FILE* f = http_request_get_response_body( req );
fprintf( f, "[" );
for( int i = 0; i < count; ++i ) {
if( i > 0 ) {
fprintf( f, "," );
}
api_Notification_write( ns[i], f, 1 );
}
fprintf( f, "]" );
}
bool handle_notifications( struct http_request* req )
{
struct {
struct notification** items;
int count;
} notes;
memset(&notes,0,sizeof(notes));
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, "?" ) ) {
http_query_parse( req, fields, &params );
}
if( params.limit > 32 ) {
params.limit = 32;
}
int note_count = fs_list_get("data/notices/HEAD");
for( int note_id = note_count; (notes.count < params.limit) && ( note_id > params.since_id ); note_id -= 1 ) {
struct notification* n = notification_from_id(note_id);
// Apply filters
if( !n ) { goto exclude; }
if( n ) {
// if( n->muted && !params.with_muted ) { goto exclude; }
}
goto include;
include:
array_append( &notes, sizeof(void*), &n );
continue;
exclude:
notification_free(n);
continue;
}
show_notifications( req, notes.items, notes.count );
for( int i = 0; i < notes.count; ++i ) {
notification_free(notes.items[i]);
}
free(notes.items);
return true;
}