mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 14:31:26 +00:00
Update FollowerService, cache audience
This commit is contained in:
parent
38e5fc43eb
commit
22257cc2a7
2 changed files with 12 additions and 46 deletions
|
@ -6,6 +6,7 @@ use Auth, Cache, Storage;
|
||||||
use App\Util\Lexer\PrettyNumber;
|
use App\Util\Lexer\PrettyNumber;
|
||||||
use Pixelfed\Snowflake\HasSnowflakePrimary;
|
use Pixelfed\Snowflake\HasSnowflakePrimary;
|
||||||
use Illuminate\Database\Eloquent\{Model, SoftDeletes};
|
use Illuminate\Database\Eloquent\{Model, SoftDeletes};
|
||||||
|
use App\Services\FollowerService;
|
||||||
|
|
||||||
class Profile extends Model
|
class Profile extends Model
|
||||||
{
|
{
|
||||||
|
@ -276,15 +277,7 @@ class Profile extends Model
|
||||||
|
|
||||||
public function getAudienceInbox($scope = 'public')
|
public function getAudienceInbox($scope = 'public')
|
||||||
{
|
{
|
||||||
return $this
|
return FollowerService::audience($this->id, $scope);
|
||||||
->followers()
|
|
||||||
->whereLocalProfile(false)
|
|
||||||
->get()
|
|
||||||
->map(function($follow) {
|
|
||||||
return $follow->sharedInbox ?? $follow->inbox_url;
|
|
||||||
})
|
|
||||||
->unique()
|
|
||||||
->toArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function circles()
|
public function circles()
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
|
use Cache;
|
||||||
use App\{
|
use App\{
|
||||||
Follower,
|
Follower,
|
||||||
Profile,
|
Profile,
|
||||||
|
@ -25,6 +25,8 @@ class FollowerService
|
||||||
{
|
{
|
||||||
Redis::zrem(self::FOLLOWING_KEY . $actor, $target);
|
Redis::zrem(self::FOLLOWING_KEY . $actor, $target);
|
||||||
Redis::zrem(self::FOLLOWERS_KEY . $target, $actor);
|
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)
|
public static function followers($id, $start = 0, $stop = 10)
|
||||||
|
@ -42,46 +44,19 @@ class FollowerService
|
||||||
return Follower::whereProfileId($actor)->whereFollowingId($target)->exists();
|
return Follower::whereProfileId($actor)->whereFollowingId($target)->exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function audience($profile)
|
public static function audience($profile, $scope = null)
|
||||||
{
|
{
|
||||||
return (new self)->getAudienceInboxes($profile);
|
return (new self)->getAudienceInboxes($profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getAudienceInboxes($profile)
|
protected function getAudienceInboxes($profile, $scope = null)
|
||||||
{
|
{
|
||||||
if($profile instanceOf User) {
|
if(!$profile instanceOf Profile) {
|
||||||
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) {
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$key = 'pf:services:follow:audience:' . $profile->id;
|
||||||
|
return Cache::remember($key, 86400, function() use($profile) {
|
||||||
return $profile
|
return $profile
|
||||||
->followers()
|
->followers()
|
||||||
->whereLocalProfile(false)
|
->whereLocalProfile(false)
|
||||||
|
@ -92,9 +67,7 @@ class FollowerService
|
||||||
->unique()
|
->unique()
|
||||||
->values()
|
->values()
|
||||||
->toArray();
|
->toArray();
|
||||||
}
|
});
|
||||||
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue