@ -12,6 +12,10 @@
# include "controller/api/timeline.h"
// Submodules
# include "collections/array.h"
// Platform libraries
# include <stdlib.h>
# include <string.h>
@ -54,43 +58,79 @@ bool handle_mastodon_api_show_account( struct http_request* req, struct account*
return true ;
}
struct search_params
{
const char * q ;
int limit ;
bool resolve ;
bool following ;
} ;
static void search_accounts ( struct search_params * params , struct account_list * al )
{
if ( strstr ( params - > q , " https:// " ) | | strstr ( params - > q , " http:// " ) ) {
//// Is URL
struct account * a = NULL ;
// TODO: try to fetch RSS account from url
a = account_from_rss ( params - > q ) ;
if ( a ) {
array_append ( al , sizeof ( a ) , & a ) ;
return ;
}
// TODO: try to fetch account from url
a = account_from_uri_or_fetch ( params - > q ) ;
if ( a ) {
array_append ( al , sizeof ( a ) , & a ) ;
}
} else if ( strstr ( params - > q , " @ " ) ) {
// Has @, assume this is a webfinger search
struct account * a = account_from_webfinger ( params - > q , NULL ) ;
if ( a ) {
array_append ( al , sizeof ( a ) , & a ) ;
}
if ( ! a & & params - > resolve ) {
printf ( " TODO: perform webfinger lookup \n " ) ;
}
}
}
// Route: /api/v1/accounts/search?
static bool handle_search ( struct http_request * req )
{
struct params_t
{
const char * q ;
int limit ;
bool resolve ;
bool following ;
} params ;
memset ( & params , 0 , sizeof ( params ) ) ;
struct search_params params = { 0 } ;
# define OBJ_TYPE struct search_params
static struct http_query_fields fields [ ] = {
{ " q " , offsetof ( struct params_t , q ) , qf_string } ,
{ " limit " , offsetof ( struct params_t , limit ) , qf_integer } ,
{ " resolve " , offsetof ( struct params_t , resolve ) , qf_bool } ,
{ " following " , offsetof ( struct params_t , following ) , qf_bool } ,
{ " q " , offsetof ( OBJ_TYPE , q ) , qf_string } ,
{ " limit " , offsetof ( OBJ_TYPE , limit ) , qf_integer } ,
{ " resolve " , offsetof ( OBJ_TYPE , resolve ) , qf_bool } ,
{ " following " , offsetof ( OBJ_TYPE , following ) , qf_bool } ,
{ NULL } ,
} ;
# undef OBJ_TYPE
http_query_parse ( req , fields , & params ) ;
struct account_list al = { 0 } ;
search_accounts ( & params , & al ) ;
http_request_send_headers ( req , 200 , " application/json " , true ) ;
FILE * f = http_request_get_response_body ( req ) ;
struct account * a = account_from_webfinger ( params . q , NULL ) ;
if ( ! a & & params . resolve ) {
printf ( " TODO: perform webfinger lookup \n " ) ;
return false ;
}
fprintf ( f , " [ " ) ;
if ( a ) {
api_Account_write ( a , f , 1 , false ) ;
for ( int i = 0 ; i < al . count ; + + i ) {
if ( i ! = 0 ) {
fprintf ( f , " , \n " ) ;
}
api_Account_write ( al . items [ i ] , f , 1 , false ) ;
}
fprintf ( f , " ] " ) ;
account_free ( a ) ;
account_ list_ free( & a l ) ;
return true ;
}