pixelfed/app/Services/FollowerService.php

101 lines
2.0 KiB
PHP
Raw Normal View History

2019-02-14 21:24:34 +00:00
<?php
namespace App\Services;
2019-12-11 06:04:03 +00:00
use Illuminate\Support\Facades\Redis;
2019-02-14 21:24:34 +00:00
use App\{
Follower,
2021-07-11 13:43:29 +00:00
Profile,
User
2019-02-14 21:24:34 +00:00
};
2021-07-11 13:43:29 +00:00
class FollowerService
{
const FOLLOWING_KEY = 'pf:services:follow:following:id:';
const FOLLOWERS_KEY = 'pf:services:follow:followers:id:';
2019-02-14 21:24:34 +00:00
2021-07-11 13:43:29 +00:00
public static function add($actor, $target)
{
Redis::zadd(self::FOLLOWING_KEY . $actor, $target, $target);
Redis::zadd(self::FOLLOWERS_KEY . $target, $actor, $actor);
}
2019-02-14 21:24:34 +00:00
2021-07-11 13:43:29 +00:00
public static function remove($actor, $target)
2019-02-14 21:24:34 +00:00
{
2021-07-11 13:43:29 +00:00
Redis::zrem(self::FOLLOWING_KEY . $actor, $target);
Redis::zrem(self::FOLLOWERS_KEY . $target, $actor);
2019-02-14 21:24:34 +00:00
}
2021-07-11 13:43:29 +00:00
public static function followers($id, $start = 0, $stop = 10)
2019-02-14 21:24:34 +00:00
{
2021-07-11 13:43:29 +00:00
return Redis::zrange(self::FOLLOWERS_KEY . $id, $start, $stop);
2019-02-14 21:24:34 +00:00
}
2021-07-11 13:43:29 +00:00
public static function following($id, $start = 0, $stop = 10)
2019-02-14 21:24:34 +00:00
{
2021-07-11 13:43:29 +00:00
return Redis::zrange(self::FOLLOWING_KEY . $id, $start, $stop);
2019-02-14 21:24:34 +00:00
}
2021-07-11 13:43:29 +00:00
public static function follows(string $actor, string $target)
{
return Follower::whereProfileId($actor)->whereFollowingId($target)->exists();
}
2019-02-14 21:24:34 +00:00
2021-07-11 13:43:29 +00:00
public static function audience($profile)
2019-02-14 21:24:34 +00:00
{
2021-07-11 13:43:29 +00:00
return (new self)->getAudienceInboxes($profile);
2019-12-05 02:47:00 +00:00
}
2021-07-11 13:43:29 +00:00
protected function getAudienceInboxes($profile)
2019-12-05 02:47:00 +00:00
{
2021-07-11 13:43:29 +00:00
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();
2019-02-14 21:24:34 +00:00
}
2021-07-11 13:43:29 +00:00
if(is_string($profile) || is_integer($profile)) {
$profile = Profile::whereNull('domain')->find($profile);
if(!$profile) {
return [];
}
return $profile
->followers()
->whereLocalProfile(false)
->get()
->map(function($follow) {
return $follow->sharedInbox ?? $follow->inbox_url;
})
->unique()
->values()
->toArray();
}
return [];
2019-02-14 21:24:34 +00:00
}
2020-02-22 11:11:46 +00:00
}