Add code to refetch account activity pub data older than 3 days, pass mime-type thru to api Status results

master
teknomunk 1 year ago
parent 0f6e78ae89
commit 2c1ade6461

@ -16,6 +16,7 @@
#include "model/status.h"
#include "model/crypto/keys.h"
#include "model/notification.h"
#include "model/fetch.h"
// View
#include "view/api/Relationship.h"
@ -27,7 +28,6 @@
#include <stddef.h>
#include <sys/stat.h>
#include <time.h>
bool pull_remote_file( const char* filename, const char* uri );
static struct json_enum account_types_enum[] = {
{ "owner", at_owner },
@ -298,10 +298,17 @@ struct account* account_fetch_from_uri( const char* uri )
// Fetch the ActivityPub actor data if we don't already have it
char filename[512];
struct stat s;
bool fetch_attempted = false;
if( !stat( filename, &s ) || ( time(NULL) - s.st_mtime > 60*60*24*3 ) ) {
pull_remote_file( filename, uri );
fetch_attempted = true;
}
FILE* f = fopen( format( filename, 512, "data/accounts/%d/ap.json", account_id ), "r" );
if( !f ) {
if( a && a->stub && ( time(NULL) < a->next_stub_recheck ) ) { goto next; }
if( pull_remote_file( filename, uri ) ) { goto next; }
if( !fetch_attempted && pull_remote_file( filename, uri ) ) { goto next; }
// Mark as stub
if( !a ) {

@ -1,5 +1,8 @@
#include "model/account.h"
// Model
#include "model/fetch.h"
// Submodules
#include "collections/array.h"
#include "util/format.h"
@ -8,6 +11,7 @@
// Standard Library
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
bool account_sync_from_activity_pub( unsigned int account_id )
{
@ -18,6 +22,16 @@ bool account_sync_from_activity_pub( unsigned int account_id )
printf( "ap_object_from_file( %s )\n", filename );
struct ap_object* obj = ap_object_from_file(filename);
struct stat s;
stat( filename, &s );
if( time(NULL) - s.st_mtime > 60*60*24*3 ) {
// Refresh if over 3 days old
pull_remote_file( filename, obj->id );
ap_object_free(obj);
obj = ap_object_from_file(filename);
}
printf( "obj = "); ap_object_write_to_FILE(obj,stdout);
printf( "\n" );

@ -0,0 +1,48 @@
#include "fetch.h"
// Submodules
#include "http/client/client.h"
#include "util/format.h"
// Standard Library
#include <stdio.h>
bool pull_remote_file( const char* filename, const char* uri )
{
printf( "* Fetching %s\n", uri );
char tmp_filename[512];
FILE* f = fopen(format(tmp_filename,512,"%s.tmp",filename),"w");
long status_code = -1;
const void* request[] = {
HTTP_REQ_URL, uri,
HTTP_REQ_HEADER, "Accept: application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
HTTP_REQ_OUTFILE, f,
HTTP_REQ_RESULT_STATUS, &status_code,
NULL,
};
if( !http_client_do( request ) ) {
printf( "! Unable to fetch %s\n", uri );
fclose(f);
return false;
}
printf( "status_code = %ld\n", status_code );
if( status_code == 200 ) {
// success
fclose(f);
rename(tmp_filename,filename);
return true;
} else if( status_code == 401 ) {
// Not Authorized
printf( "TODO: perform signed fetch\n" );
fclose(f);
return false;
}
// Failure
fclose(f);
return false;
}

@ -0,0 +1,6 @@
#pragma once
#include <stdbool.h>
bool pull_remote_file( const char* filename, const char* uri );

@ -2,6 +2,16 @@
#include "status.h"
#include "status/react.h"
// Model
#include "model/server.h"
#include "model/account.h"
#include "model/activity.h"
#include "model/notification.h"
#include "model/timeline.h"
#include "model/emoji.h"
#include "model/media.h"
#include "model/fetch.h"
// Submodules
#include "json/json.h"
#include "json/layout.h"
@ -10,19 +20,9 @@
#include "ffdb/trie.h"
#include "sha256/sha256.h"
#include "collections/array.h"
#include "http/client/client.h"
#include "util/format.h"
#include "ap/object.h"
// Model
#include "model/server.h"
#include "model/account.h"
#include "model/activity.h"
#include "model/notification.h"
#include "model/timeline.h"
#include "model/emoji.h"
#include "model/media.h"
// Standard Library
#include <stdio.h>
#include <string.h>
@ -369,44 +369,6 @@ failed:
goto cleanup;
};
bool pull_remote_file( const char* filename, const char* uri )
{
printf( "* Fetching %s\n", uri );
char tmp_filename[512];
FILE* f = fopen(format(tmp_filename,512,"%s.tmp",filename),"w");
long status_code = -1;
const void* request[] = {
HTTP_REQ_URL, uri,
HTTP_REQ_HEADER, "Accept: application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
HTTP_REQ_OUTFILE, f,
HTTP_REQ_RESULT_STATUS, &status_code,
NULL,
};
if( !http_client_do( request ) ) {
printf( "! Unable to fetch %s\n", uri );
fclose(f);
return false;
}
printf( "status_code = %ld\n", status_code );
if( status_code == 200 ) {
// success
fclose(f);
rename(tmp_filename,filename);
return true;
} else if( status_code == 401 ) {
// Not Authorized
// TODO: perform signed fetch
fclose(f);
return false;
}
// Failure
fclose(f);
return false;
}
bool status_sync_from_uri( struct status* s, const char* uri )
{
struct ap_object* act = NULL;

@ -105,12 +105,34 @@ bool does_react_include_me( void* field_data, bool is_read, bool* val )
*val = false;
return true;
}
bool is_image_mime_type( void* field_data, bool is_read, bool* val )
{
char* mime_type = *(char**)field_data;
if( is_read ) { return false; }
*val = ( 0 == strncmp( "image/", mime_type, 6 ) );
return true;
}
bool attachment_media_type( void* field_data, bool is_read, char** val )
{
char* mime_type = *(char**)field_data;
if( is_read ) { return false; }
int length = index(mime_type,'/') - mime_type;
*val = strndup( mime_type, length );
return true;
}
#define OBJ_TYPE struct media
static struct json_object_field MediaAttachment_Pleroma_layout[] = {
JSON_FIELD_FIXED_STRING( mime_type, "image/png", true ),
{
.key = "mime_type",
.offset = offsetof( OBJ_TYPE, content_type ),
.required = true,
.type = &json_field_string
},
JSON_FIELD_END
};
#define OBJ_TYPE struct media
static struct json_object_field MediaAttachment_layout[] = {
JSON_FIELD_FIXED_STRING( blurhash, "eRH.A}xs0Kxv00xYR,R+t5R+9Gt6xaNG%%2-;xaM{NGRjD%%Rjs:xaxu", true ),
JSON_FIELD_FIXED_NULL( description ),
@ -140,7 +162,19 @@ static struct json_object_field MediaAttachment_layout[] = {
.offset = offsetof( OBJ_TYPE, remote_url ),
.type = &json_field_string,
},
JSON_FIELD_FIXED_STRING( type, "image", true ),
{
.key = "image",
.offset = offsetof( OBJ_TYPE, content_type ),
.type = &json_field_bool_callback,
.bool_callback = is_image_mime_type,
},
{
.key = "type",
.offset = offsetof( OBJ_TYPE, content_type ),
.type = &json_field_callback_string,
.string_callback = attachment_media_type,
},
{
.key = "url",
.offset = offsetof( OBJ_TYPE, remote_url ),

Loading…
Cancel
Save