#include "inbox.h" // Submodules #include "http/server/request.h" #include "json/json.h" #include "ffdb/fs_list.h" // Model #include "model/server.h" #include "model/status.h" #include "model/account.h" #include "model/notification.h" #include "model/ap/activity.h" #include "model/ap/account.h" #include "model/ap/inbox_envelope.h" #include "model/crypto/http_sign.h" //#include "model/timeline.h" // Stdlib #include #include #include #include #include extern bool terminate; bool route_inbox( struct http_request* req ) { // No subroutes if( !http_request_route_term( req, "" ) ) { return false; } if( !http_request_route_method( req, "POST" ) ) { return false; } if( envelope_create_from_request( req ) ) { http_request_send_headers( req, 200, "text/plain", true ); } else { http_request_send_headers( req, 400, "text/plain", true ); } return true; } static bool route_follow( struct ap_activity* act ) { struct account* follower = NULL; bool res = false; struct ap_activity* accept = NULL; const char* target = act->object.ref; struct account* a = account_from_uri( target ); // Don't process follows for remote users if( !a || 0 != strcmp( a->server, g_server_name ) ) { printf( "Unfollow not targeted at local account. Discarding.\n" ); goto success; } // Get account for the follower follower = account_from_uri( act->actor ); if( !follower ) { follower = account_fetch_from_uri( act->actor ); } if( !follower ) { printf( "Unable to fetch account for %s\n", act->actor ); goto failed; } // Add the follower account_add_follower( a, follower ); // Create Accept activity ap_activity_accept( act, follower->id ); success: res = true; cleanup: account_free(a); account_free(follower); return res; failed: res = false; goto cleanup; } static bool route_undo_activity( struct ap_activity* act ) { if( act->object.tag != apaot_activity ) { printf( "Can't undo reference activities, discarding...\n" ); return true; } if( !act->object.ptr ) { printf( "No object in activity\n" ); return false; } switch( act->object.ptr->type ) { case apat_follow: return route_undo_follow( act ); default: printf( "Unhandled object activity type %d in undo\n", act->object.ptr->type ); return false; }; return false; } static struct status* lookup_object_status( struct ap_activity* act ) { struct status* s = NULL; switch( act->object.tag ) { case apaot_ref: s = status_from_uri( act->object.ref ); if( !s ) { s = status_fetch_from_uri( act->object.ref ); } break; case apaot_activity: s = status_from_uri( act->object.ptr->id ); if( !s ) { s = status_from_activity( act->object.ptr ); status_save_new( s ); } break; } if( s ) { return s; } switch( act->object.tag ) { case apaot_ref: printf( "! Status %s doesn't exist locally (object.ref)\n", act->object.ref ); break; case apaot_activity: printf( "! Status %s doesn't exist locally (object.ptr)\n", act->object.ptr->id ); break; } return NULL; } static struct account* lookup_actor_account( struct ap_activity* act ) { struct account* a = account_from_uri( act->actor ); if( a ) { return a; } a = account_fetch_from_uri( act->actor ); return a; } static bool route_like( struct ap_activity* act ) { struct status* s = lookup_object_status(act); struct account* liker = lookup_actor_account(act); if( !s ) { return false; }; status_add_like( s, liker ); return true; } static bool route_emoji_react( struct ap_activity* act ) { struct status* s = lookup_object_status(act); struct account* reactor = lookup_actor_account(act); if( !s || !reactor ) { return false; } status_add_react( s, act->content.content, reactor ); return true; } static bool route_create( struct ap_activity* act ) { struct status* s = NULL; bool result = false; // Requires an object // TODO: if there is a ref here, fetch the object if( act->object.tag != apaot_activity ) { return false; } struct ap_activity* obj = act->object.ptr; bool mentions_me = false; bool follows_me = false; // Does this activity have mention me char owner_url[512]; snprintf( owner_url, sizeof(owner_url), "https://%s/owner/actor", g_server_name ); for( int i = 0; i < obj->tags.count; ++i ) { struct ap_activity_tag* tag = obj->tags.items[i]; //printf( "tag = { &=%p, .type=%d, .href=%s, .name=%s }\n", tag, tag->type, tag->href, tag->name ); if( tag->type != aptag_mention ) { continue; } if( 0 == strcmp(tag->href, owner_url) ) { mentions_me = true; goto check_is_follower; } } check_is_follower: // Get actor account struct account* actor_account = account_from_uri_or_fetch( obj->actor ); if( !actor_account ) { printf( "! Unable to fetch %s\n", obj->actor ); goto failed; } if( account_does_follow( actor_account, owner_account_id ) ) { follows_me = true; } if( !follows_me && !mentions_me ) { // Discard without action result = true; goto cleanup; } // Create local status s = status_from_uri( obj->id ); if( !s ) { s = status_from_activity(obj); status_save_new(s); } // Add to timelines if( follows_me ) { status_add_to_timeline( s, home_timeline_id ); // TODO: create notification if user notifications are on or this is part of a watched conversation } if( mentions_me ) { // Create notification struct notification* note = notification_new(); note->type = nt_mention; note->status_id = s->id; note->account_id = actor_account->id; notification_save( note ); notification_free( note ); } // Add to standard timelines status_add_to_timeline( s, public_timeline_id ); //status_add_to_timeline( s, federated_timeline_id ); status_add_to_timeline( s, actor_account->id ); result = true; cleanup: account_free(actor_account); status_free(s); return result; failed: result = false; goto cleanup; } static bool route_accept( struct ap_activity* act ) { // TODO: actually handle accepts // In particular, this needs to change a follow request to a following accepted return true; } bool route_activity( struct ap_activity* act ) { printf( "Handling %s\n", act->id ); ap_activity_write_to_FILE( act, stdout ); switch( act->type ) { case apat_undo: return route_undo_activity(act); case apat_follow: return route_follow(act); case apat_like: return route_like(act); case apat_create: return route_create(act); case apat_accept: return route_accept(act); case apat_announce: return route_announce(act); case apat_add: return route_add(act); case apat_emoji_react: return route_emoji_react(act); default: printf( "Unhandled activity type: %d\n", act->type ); } return false; } static bool process_one() { // Items requiring cleanup struct ap_activity* act = NULL; struct ap_envelope* env = NULL; bool result = false; int tail_pos = fs_list_get("data/inbox/TAIL"); int head_pos = fs_list_get("data/inbox/HEAD"); if( head_pos <= tail_pos ) { return false; } // We have data to process printf( "Inbox has %d items pending processing...\n", (head_pos - tail_pos) ); int id = tail_pos + 1; env = ap_envelope_from_id(id); bool step_tail = false; if( !env ) { printf( "Failed to parse envelope+activity for data/inbox/%d.json\n", id ); return false; } // Load activity FILE* f = fmemopen( env->body, strlen(env->body), "r" ); act = ap_activity_from_FILE(f); if( !act ) { goto failed; } // Discard delete requests if( act->type == apat_delete ) { step_tail = true; goto discard; } // Validate signature env->validated = http_signature_validate( env, "post /inbox" ); if( !env->validated ) { goto failed; } printf( "Processing %d\n", id ); step_tail = route_activity( act ); finished: printf( "step_tail=%c\n", step_tail ? 'T' : 'F' ); if( step_tail ) { fs_list_set( "data/inbox/TAIL", id ); result = true; } ap_activity_free(act); ap_envelope_free(env); printf( "result=%c\n", result ? 'T' : 'F' ); return result; failed: result = false; goto finished; discard: result = true; goto finished; } static bool cleanup_inbox() { int tail_pos = fs_list_get("data/inbox/TAIL"); int dead_pos = fs_list_get("data/inbox/DEAD"); if( dead_pos < tail_pos ) { char filename[512]; snprintf( filename, 512, "data/inbox/%d.json", dead_pos + 1 ); FILE* f = fopen( filename, "r" ); if( f ) { fclose(f); if( 0 == remove(filename) ) { // File successfully remove, advance fs_list_set( "data/inbox/DEAD", dead_pos + 1 ); return true; } } else { // File already doesn't exist, advance fclose(f); fs_list_set( "data/inbox/DEAD", dead_pos + 1 ); return true; } } return false; } void process_inbox() { while( !terminate ) { bool activity = false; activity |= process_one(); activity |= cleanup_inbox(); if( !activity ) { fflush(stdout); sleep(1); } } }