[ActivityPub] Caching of Following/Followers interactions and collections
Follow interaction: - Fixed mini-bug where the subscriber profile was being used as the subscribed - Updated cache subscription-related values in both instances - Tested and working with local GS instances Unfollow interaction: - Updated cache subscription-related values in both instances - Tested and working with local GS instances Followers/Following collections: - Now returning ActivityPub profiles only - Stored collections in cache Misc: - Fix bug concerning the retrieval of public/private-key after in-function generation
This commit is contained in:
committed by
Diogo Cordeiro
parent
735a0023cc
commit
94a4059b4a
@@ -78,6 +78,7 @@ class Activitypub_follow extends Managed_DataObject
|
||||
|
||||
if (!Subscription::exists($actor_profile, $object_profile)) {
|
||||
Subscription::start($actor_profile, $object_profile);
|
||||
Activitypub_profile::subscribeCacheUpdate($actor_profile, $object_profile);
|
||||
common_debug('ActivityPubPlugin: Accepted Follow request from '.ActivityPubPlugin::actor_uri($actor_profile).' to '.$object);
|
||||
} else {
|
||||
common_debug('ActivityPubPlugin: Received a repeated Follow request from '.ActivityPubPlugin::actor_uri($actor_profile).' to '.$object);
|
||||
@@ -85,7 +86,7 @@ class Activitypub_follow extends Managed_DataObject
|
||||
|
||||
// Notify remote instance that we have accepted their request
|
||||
common_debug('ActivityPubPlugin: Notifying remote instance that we have accepted their Follow request request from '.ActivityPubPlugin::actor_uri($actor_profile).' to '.$object);
|
||||
$postman = new Activitypub_postman($actor_profile, [$actor_aprofile]);
|
||||
$postman = new Activitypub_postman($object_profile, [$actor_aprofile]);
|
||||
$postman->accept_follow();
|
||||
}
|
||||
}
|
||||
|
@@ -473,4 +473,196 @@ class Activitypub_profile extends Managed_DataObject
|
||||
|
||||
return $profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the number of subscribers of a
|
||||
* given local profile
|
||||
*
|
||||
* @param Profile $profile profile object
|
||||
* @return int number of subscribers
|
||||
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
||||
*/
|
||||
public static function subscriberCount(Profile $profile): int {
|
||||
$cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id));
|
||||
|
||||
if ($cnt !== false && is_int($cnt)) {
|
||||
return $cnt;
|
||||
}
|
||||
|
||||
$sub = new Subscription();
|
||||
$sub->subscribed = $profile->id;
|
||||
$sub->joinAdd(['subscriber', 'user:id'], 'LEFT');
|
||||
$sub->joinAdd(['subscriber', 'activitypub_profile:profile_id'], 'LEFT');
|
||||
$sub->whereAdd('subscriber != subscribed');
|
||||
$cnt = $sub->count('distinct subscriber');
|
||||
|
||||
self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt);
|
||||
|
||||
return $cnt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the number of subscriptions of a
|
||||
* given local profile
|
||||
*
|
||||
* @param Profile $profile profile object
|
||||
* @return int number of subscriptions
|
||||
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
||||
*/
|
||||
public static function subscriptionCount(Profile $profile): int {
|
||||
$cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id));
|
||||
|
||||
if ($cnt !== false && is_int($cnt)) {
|
||||
return $cnt;
|
||||
}
|
||||
|
||||
$sub = new Subscription();
|
||||
$sub->subscriber = $profile->id;
|
||||
$sub->joinAdd(['subscribed', 'user:id'], 'LEFT');
|
||||
$sub->joinAdd(['subscribed', 'activitypub_profile:profile_id'], 'LEFT');
|
||||
$sub->whereAdd('subscriber != subscribed');
|
||||
$cnt = $sub->count('distinct subscribed');
|
||||
|
||||
self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt);
|
||||
|
||||
return $cnt;
|
||||
}
|
||||
|
||||
public static function updateSubscriberCount(Profile $profile, $adder) {
|
||||
$cnt = self::cacheGet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id));
|
||||
|
||||
if ($cnt !== false && is_int($cnt)) {
|
||||
self::cacheSet(sprintf('activitypub_profile:subscriberCount:%d', $profile->id), $cnt+$adder);
|
||||
}
|
||||
}
|
||||
|
||||
public static function updateSubscriptionCount(Profile $profile, $adder) {
|
||||
$cnt = self::cacheGet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id));
|
||||
|
||||
if ($cnt !== false && is_int($cnt)) {
|
||||
self::cacheSet(sprintf('activitypub_profile:subscriptionCount:%d', $profile->id), $cnt+$adder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the subscriber profiles of a
|
||||
* given local profile
|
||||
*
|
||||
* @param Profile $profile profile object
|
||||
* @param int $offset index of the starting row to fetch from
|
||||
* @param int $limit maximum number of rows allowed for fetching
|
||||
* @return array subscriber profile objects
|
||||
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
||||
*/
|
||||
public static function getSubscribers(Profile $profile, $offset = 0, $limit = null): array {
|
||||
$cache = false;
|
||||
if ($offset + $limit <= Subscription::CACHE_WINDOW) {
|
||||
$subs = self::cacheGet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id));
|
||||
if ($subs !== false && is_array($subs)) {
|
||||
return array_slice($subs, $offset, $limit);
|
||||
}
|
||||
|
||||
$cache = true;
|
||||
}
|
||||
|
||||
$subs = Subscription::getSubscriberIDs($profile->id, $offset, $limit);
|
||||
try {
|
||||
$profiles = [];
|
||||
|
||||
$users = User::multiGet('id', $subs);
|
||||
foreach ($users->fetchAll() as $user) {
|
||||
$profiles[$user->id] = $user->getProfile();
|
||||
}
|
||||
|
||||
$ap_profiles = Activitypub_profile::multiGet('profile_id', $subs);
|
||||
foreach ($ap_profiles->fetchAll() as $ap) {
|
||||
$profiles[$ap->getID()] = $ap->local_profile();
|
||||
}
|
||||
} catch (NoResultException $e) {
|
||||
return $e->obj;
|
||||
}
|
||||
|
||||
if ($cache) {
|
||||
self::cacheSet(sprintf('activitypub_profile:subscriberCollection:%d', $profile->id), $profiles);
|
||||
}
|
||||
|
||||
return $profiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the subscribed profiles of a
|
||||
* given local profile
|
||||
*
|
||||
* @param Profile $profile profile object
|
||||
* @param int $offset index of the starting row to fetch from
|
||||
* @param int $limit maximum number of rows allowed for fetching
|
||||
* @return array subscribed profile objects
|
||||
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
||||
*/
|
||||
public static function getSubscribed(Profile $profile, $offset = 0, $limit = null): array {
|
||||
$cache = false;
|
||||
if ($offset + $limit <= Subscription::CACHE_WINDOW) {
|
||||
$subs = self::cacheGet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id));
|
||||
if (is_array($subs)) {
|
||||
return array_slice($subs, $offset, $limit);
|
||||
}
|
||||
|
||||
$cache = true;
|
||||
}
|
||||
|
||||
$subs = Subscription::getSubscribedIDs($profile->id, $offset, $limit);
|
||||
try {
|
||||
$profiles = [];
|
||||
|
||||
$users = User::multiGet('id', $subs);
|
||||
foreach ($users->fetchAll() as $user) {
|
||||
$profiles[$user->id] = $user->getProfile();
|
||||
}
|
||||
|
||||
$ap_profiles = Activitypub_profile::multiGet('profile_id', $subs);
|
||||
foreach ($ap_profiles->fetchAll() as $ap) {
|
||||
$profiles[$ap->getID()] = $ap->local_profile();
|
||||
}
|
||||
} catch (NoResultException $e) {
|
||||
return $e->obj;
|
||||
}
|
||||
|
||||
if ($cache) {
|
||||
self::cacheSet(sprintf('activitypub_profile:subscribedCollection:%d', $profile->id), $profiles);
|
||||
}
|
||||
|
||||
return $profiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update cached values that are relevant to
|
||||
* the users involved in a subscription
|
||||
*
|
||||
* @param Profile $actor subscriber profile object
|
||||
* @param Profile $other subscribed profile object
|
||||
* @return void
|
||||
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
||||
*/
|
||||
public static function subscribeCacheUpdate(Profile $actor, Profile $other) {
|
||||
self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID());
|
||||
self::blow('activitypub_profile:subscriberCollection:%d', $other->id);
|
||||
self::updateSubscriptionCount($actor, +1);
|
||||
self::updateSubscriberCount($other, +1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update cached values that are relevant to
|
||||
* the users involved in an unsubscription
|
||||
*
|
||||
* @param Profile $actor subscriber profile object
|
||||
* @param Profile $other subscribed profile object
|
||||
* @return void
|
||||
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
||||
*/
|
||||
public static function unsubscribeCacheUpdate(Profile $actor, Profile $other) {
|
||||
self::blow('activitypub_profile:subscribedCollection:%d', $actor->getID());
|
||||
self::blow('activitypub_profile:subscriberCollection:%d', $other->id);
|
||||
self::updateSubscriptionCount($actor, -1);
|
||||
self::updateSubscriberCount($other, -1);
|
||||
}
|
||||
}
|
||||
|
@@ -75,6 +75,7 @@ class Activitypub_rsa extends Managed_DataObject
|
||||
if ($profile->isLocal()) {
|
||||
self::generate_keys($this->private_key, $this->public_key);
|
||||
$this->store_keys();
|
||||
$apRSA->private_key = $this->private_key;
|
||||
} else {
|
||||
throw new Exception('This is a remote Profile, there is no Private Key for this Profile.');
|
||||
}
|
||||
@@ -100,6 +101,7 @@ class Activitypub_rsa extends Managed_DataObject
|
||||
if ($profile->isLocal()) {
|
||||
self::generate_keys($this->private_key, $this->public_key);
|
||||
$this->store_keys();
|
||||
$apRSA->public_key = $this->public_key;
|
||||
} else {
|
||||
// This should never happen, but try to recover!
|
||||
if ($fetch) {
|
||||
|
Reference in New Issue
Block a user