Update FollowerService, cache audience

This commit is contained in:
Daniel Supernault 2021-07-25 05:56:35 -06:00
parent 38e5fc43eb
commit 22257cc2a7
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 12 additions and 46 deletions

View file

@ -6,6 +6,7 @@ use Auth, Cache, Storage;
use App\Util\Lexer\PrettyNumber;
use Pixelfed\Snowflake\HasSnowflakePrimary;
use Illuminate\Database\Eloquent\{Model, SoftDeletes};
use App\Services\FollowerService;
class Profile extends Model
{
@ -276,15 +277,7 @@ class Profile extends Model
public function getAudienceInbox($scope = 'public')
{
return $this
->followers()
->whereLocalProfile(false)
->get()
->map(function($follow) {
return $follow->sharedInbox ?? $follow->inbox_url;
})
->unique()
->toArray();
return FollowerService::audience($this->id, $scope);
}
public function circles()

View file

@ -3,7 +3,7 @@
namespace App\Services;
use Illuminate\Support\Facades\Redis;
use Cache;
use App\{
Follower,
Profile,
@ -25,6 +25,8 @@ class FollowerService
{
Redis::zrem(self::FOLLOWING_KEY . $actor, $target);
Redis::zrem(self::FOLLOWERS_KEY . $target, $actor);
Cache::forget('pf:services:follow:audience:' . $actor);
Cache::forget('pf:services:follow:audience:' . $target);
}
public static function followers($id, $start = 0, $stop = 10)
@ -42,46 +44,19 @@ class FollowerService
return Follower::whereProfileId($actor)->whereFollowingId($target)->exists();
}
public static function audience($profile)
public static function audience($profile, $scope = null)
{
return (new self)->getAudienceInboxes($profile);
}
protected function getAudienceInboxes($profile)
protected function getAudienceInboxes($profile, $scope = null)
{
if($profile instanceOf User) {
return $profile
->profile
->followers()
->whereLocalProfile(false)
->get()
->map(function($follow) {
return $follow->sharedInbox ?? $follow->inbox_url;
})
->unique()
->values()
->toArray();
}
if($profile instanceOf Profile) {
return $profile
->followers()
->whereLocalProfile(false)
->get()
->map(function($follow) {
return $follow->sharedInbox ?? $follow->inbox_url;
})
->unique()
->values()
->toArray();
}
if(is_string($profile) || is_integer($profile)) {
$profile = Profile::whereNull('domain')->find($profile);
if(!$profile) {
if(!$profile instanceOf Profile) {
return [];
}
$key = 'pf:services:follow:audience:' . $profile->id;
return Cache::remember($key, 86400, function() use($profile) {
return $profile
->followers()
->whereLocalProfile(false)
@ -92,9 +67,7 @@ class FollowerService
->unique()
->values()
->toArray();
}
return [];
});
}
}