1
0
Fork 0
forked from mirror/pixelfed

Update FollowerService

This commit is contained in:
Daniel Supernault 2019-12-04 19:47:00 -07:00
parent 10a182c1cd
commit 96a6c063f2
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -12,8 +12,8 @@ use App\{
class FollowerService { class FollowerService {
protected $profile; protected $profile;
protected $follower_prefix; static $follower_prefix = 'px:profile:followers-v1.3:';
protected $following_prefix; static $following_prefix = 'px:profile:following-v1.3:';
public static function build() public static function build()
{ {
@ -23,35 +23,48 @@ class FollowerService {
public function profile(Profile $profile) public function profile(Profile $profile)
{ {
$this->profile = $profile; $this->profile = $profile;
$this->follower_prefix = config('cache.prefix').':profile:followers:'.$profile->id; self::$follower_prefix .= $profile->id;
$this->following_prefix = config('cache.prefix').':profile:following:'.$profile->id; self::$following_prefix .= $profile->id;
return $this; return $this;
} }
public function followers($limit = 100, $offset = 0) public function followers($limit = 100, $offset = 1)
{ {
if(Redis::llen($this->follower_prefix) == 0) { if(Redis::zcard(self::$follower_prefix) == 0) {
$followers = $this->profile->followers; $followers = $this->profile->followers()->pluck('profile_id');
$followers->map(function($i) { $followers->map(function($i) {
Redis::lpush($this->follower_prefix, $i->id); Redis::zadd(self::$follower_prefix, $i, $i);
}); });
return $followers; return Redis::zrevrange(self::$follower_prefix, $offset, $limit);
} else { } else {
return Redis::lrange($this->follower_prefix, $offset, $limit); return Redis::zrevrange(self::$follower_prefix, $offset, $limit);
} }
} }
public function following($limit = 100, $offset = 0) public function following($limit = 100, $offset = 1)
{ {
if(Redis::llen($this->following_prefix) == 0) { if(Redis::zcard(self::$following_prefix) == 0) {
$following = $this->profile->following; $following = $this->profile->following()->pluck('following_id');
$following->map(function($i) { $following->map(function($i) {
Redis::lpush($this->following_prefix, $i->id); Redis::zadd(self::$following_prefix, $i, $i);
}); });
return $following; return Redis::zrevrange(self::$following_prefix, $offset, $limit);
} else { } else {
return Redis::lrange($this->following_prefix, $offset, $limit); return Redis::zrevrange(self::$following_prefix, $offset, $limit);
}
}
public static function follows(string $actor, string $target)
{
$key = self::$follower_prefix . $target;
if(Redis::zcard($key) == 0) {
$p = Profile::findOrFail($target);
self::build()->profile($p)->followers(1);
self::build()->profile($p)->following(1);
return (bool) Redis::zrank($key, $actor);
} else {
return (bool) Redis::zrank($key, $actor);
} }
} }