From c47e21f3a74e4290bd206c1f7edb377e809d7e42 Mon Sep 17 00:00:00 2001 From: Mario Date: Sat, 4 Sep 2021 07:37:49 +0000 Subject: refactor actor_store and actor cache part 1 --- Zotlabs/Lib/Activity.php | 97 +++++++++++++++++++++++++++++++++++++++++ Zotlabs/Lib/ActivityStreams.php | 13 +----- Zotlabs/Lib/Libzot.php | 6 +++ 3 files changed, 105 insertions(+), 11 deletions(-) (limited to 'Zotlabs') diff --git a/Zotlabs/Lib/Activity.php b/Zotlabs/Lib/Activity.php index 4f63b31da..583141c60 100644 --- a/Zotlabs/Lib/Activity.php +++ b/Zotlabs/Lib/Activity.php @@ -8,6 +8,8 @@ use Zotlabs\Access\PermissionRoles; use Zotlabs\Access\Permissions; use Zotlabs\Daemon\Master; use Zotlabs\Web\HTTPSig; +use Zotlabs\Lib\XConfig; +use Zotlabs\Lib\Libzot; require_once('include/event.php'); require_once('include/html2plain.php'); @@ -1537,6 +1539,48 @@ class Activity { return; } +/* not implemented + if (array_key_exists('movedTo',$person_obj) && $person_obj['movedTo'] && ! is_array($person_obj['movedTo'])) { + $tgt = self::fetch($person_obj['movedTo']); + if (is_array($tgt)) { + self::actor_store($person_obj['movedTo'],$tgt); + ActivityPub::move($person_obj['id'],$tgt); + } + return; + } +*/ + $ap_hubloc = null; + + $hublocs = self::get_actor_hublocs($url); + if ($hublocs) { + foreach ($hublocs as $hub) { + if ($hub['hubloc_network'] === 'activitypub') { + $ap_hubloc = $hub; + } + if ($hub['hubloc_network'] === 'zot6') { + Libzot::update_cached_hubloc($hub); + } + } + } + + if ($ap_hubloc) { + // we already have a stored record. Determine if it needs updating. + if ($ap_hubloc['hubloc_updated'] < datetime_convert('UTC','UTC',' now - 3 days') || $force) { + $person_obj = self::fetch($url); + } + else { + return; + } + } + + if (isset($person_obj['id'])) { + $url = $person_obj['id']; + } + + if (! $url) { + return; + } + $inbox = $person_obj['inbox']; // invalid identity @@ -1545,6 +1589,9 @@ class Activity { return; } + // store the actor record in XConfig + XConfig::Set($url, 'system', 'actor_record', $person_obj); + $name = $person_obj['name']; if (!$name) { $name = $person_obj['preferredUsername']; @@ -3504,4 +3551,54 @@ class Activity { } + static function get_cached_actor($id) { + return (XConfig::Get($id,'system','actor_record')); + } + + + static function get_actor_hublocs($url, $options = 'all') { + + $hublocs = false; + + switch ($options) { + case 'activitypub': + $hublocs = q("select * from hubloc left join xchan on hubloc_hash = xchan_hash where hubloc_hash = '%s' ", + dbesc($url) + ); + break; + case 'zot6': + $hublocs = q("select * from hubloc left join xchan on hubloc_hash = xchan_hash where hubloc_id_url = '%s' ", + dbesc($url) + ); + break; + case 'all': + default: + $hublocs = q("select * from hubloc left join xchan on hubloc_hash = xchan_hash where ( hubloc_id_url = '%s' OR hubloc_hash = '%s' ) ", + dbesc($url), + dbesc($url) + ); + break; + } + + return $hublocs; + } + + static function get_actor_collections($url) { + $ret = []; + $actor_record = XConfig::Get($url,'system','actor_record'); + if (! $actor_record) { + return $ret; + } + + foreach ( [ 'inbox','outbox','followers','following' ] as $collection) { + if (isset($actor_record[$collection]) && $actor_record[$collection]) { + $ret[$collection] = $actor_record[$collection]; + } + } + if (array_path_exists('endpoints/sharedInbox',$actor_record) && $actor_record['endpoints']['sharedInbox']) { + $ret['sharedInbox'] = $actor_record['endpoints']['sharedInbox']; + } + + return $ret; + } } diff --git a/Zotlabs/Lib/ActivityStreams.php b/Zotlabs/Lib/ActivityStreams.php index 2324a8136..fa38c569e 100644 --- a/Zotlabs/Lib/ActivityStreams.php +++ b/Zotlabs/Lib/ActivityStreams.php @@ -300,17 +300,8 @@ class ActivityStreams { function get_actor($property, $base = '', $namespace = '') { $x = $this->get_property_obj($property, $base, $namespace); if ($this->is_url($x)) { - - // SECURITY: If we have already stored the actor profile, re-generate it - // from cached data - don't refetch it from the network - - $r = q("select * from xchan join hubloc on xchan_hash = hubloc_hash where hubloc_id_url = '%s'", - dbesc($x) - ); - if ($r) { - $r = Libzot::zot_record_preferred($r); - $y = Activity::encode_person($r); - $y['cached'] = true; + $y = Activity::get_cached_actor($x); + if ($y) { return $y; } } diff --git a/Zotlabs/Lib/Libzot.php b/Zotlabs/Lib/Libzot.php index f7d8c417a..cb55a4e67 100644 --- a/Zotlabs/Lib/Libzot.php +++ b/Zotlabs/Lib/Libzot.php @@ -3154,4 +3154,10 @@ class Libzot { } + static function update_cached_hubloc($hubloc) { + if ($hubloc['hubloc_updated'] > datetime_convert('UTC','UTC','now - 1 week') || $hubloc['hubloc_url'] === z_root()) { + return; + } + self::refresh( [ 'hubloc_id_url' => $hubloc['hubloc_id_url'] ] ); + } } -- cgit v1.2.3