1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-30 19:55:10 +00:00
pixelfed/app/Services/FollowerService.php

226 lines
6.1 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;
2021-07-25 11:56:35 +00:00
use Cache;
2021-12-05 00:37:44 +00:00
use DB;
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
};
use App\Jobs\FollowPipeline\FollowServiceWarmCache;
2019-02-14 21:24:34 +00:00
2021-07-11 13:43:29 +00:00
class FollowerService
{
2021-12-05 00:37:44 +00:00
const CACHE_KEY = 'pf:services:followers:';
const FOLLOWERS_SYNC_ACTIVE = 'pf:services:followers:sync-active:';
const FOLLOWERS_SYNC_KEY = 'pf:services:followers:sync-followers:';
const FOLLOWING_SYNC_KEY = 'pf:services:followers:sync-following:';
2021-07-11 13:43:29 +00:00
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)
{
$ts = (int) microtime(true);
RelationshipService::refresh($actor, $target);
Redis::zadd(self::FOLLOWING_KEY . $actor, $ts, $target);
Redis::zadd(self::FOLLOWERS_KEY . $target, $ts, $actor);
2022-12-16 07:43:20 +00:00
Cache::forget('profile:following:' . $actor);
2021-07-11 13:43:29 +00:00
}
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);
Cache::forget('pf:services:follower:audience:' . $actor);
Cache::forget('pf:services:follower:audience:' . $target);
AccountService::del($actor);
AccountService::del($target);
RelationshipService::refresh($actor, $target);
2022-12-16 07:43:20 +00:00
Cache::forget('profile:following:' . $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
{
self::cacheSyncCheck($id, 'followers');
return Redis::zrevrange(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
{
self::cacheSyncCheck($id, 'following');
return Redis::zrevrange(self::FOLLOWING_KEY . $id, $start, $stop);
}
public static function followersPaginate($id, $page = 1, $limit = 10)
{
$start = $page == 1 ? 0 : $page * $limit - $limit;
$end = $start + ($limit - 1);
return self::followers($id, $start, $end);
}
public static function followingPaginate($id, $page = 1, $limit = 10)
{
$start = $page == 1 ? 0 : $page * $limit - $limit;
$end = $start + ($limit - 1);
return self::following($id, $start, $end);
}
public static function followerCount($id, $warmCache = true)
{
if($warmCache) {
self::cacheSyncCheck($id, 'followers');
}
return Redis::zCard(self::FOLLOWERS_KEY . $id);
}
public static function followingCount($id, $warmCache = true)
{
if($warmCache) {
self::cacheSyncCheck($id, 'following');
}
return Redis::zCard(self::FOLLOWING_KEY . $id);
2019-02-14 21:24:34 +00:00
}
2021-07-11 13:43:29 +00:00
public static function follows(string $actor, string $target)
{
if($actor == $target) {
return false;
}
if(self::followerCount($target, false) && self::followingCount($actor, false)) {
self::cacheSyncCheck($target, 'followers');
return (bool) Redis::zScore(self::FOLLOWERS_KEY . $target, $actor);
} else {
self::cacheSyncCheck($target, 'followers');
self::cacheSyncCheck($actor, 'following');
return Follower::whereProfileId($actor)->whereFollowingId($target)->exists();
}
}
public static function cacheSyncCheck($id, $scope = 'followers')
{
if($scope === 'followers') {
if(Cache::get(self::FOLLOWERS_SYNC_KEY . $id) != null) {
return;
}
if(Cache::get(self::FOLLOWERS_SYNC_ACTIVE . $id) != null) {
return;
}
FollowServiceWarmCache::dispatch($id)->onQueue('low');
Cache::put(self::FOLLOWERS_SYNC_ACTIVE . $id, 1, 604800);
}
if($scope === 'following') {
if(Cache::get(self::FOLLOWING_SYNC_KEY . $id) != null) {
return;
}
if(Cache::get(self::FOLLOWERS_SYNC_ACTIVE . $id) != null) {
return;
}
FollowServiceWarmCache::dispatch($id)->onQueue('low');
Cache::put(self::FOLLOWERS_SYNC_ACTIVE . $id, 1, 604800);
}
return;
2021-07-11 13:43:29 +00:00
}
2019-02-14 21:24:34 +00:00
2021-07-25 11:56:35 +00:00
public static function audience($profile, $scope = null)
2019-02-14 21:24:34 +00:00
{
2021-09-04 02:51:56 +00:00
return (new self)->getAudienceInboxes($profile, $scope);
2019-12-05 02:47:00 +00:00
}
2021-08-31 06:37:02 +00:00
public static function softwareAudience($profile, $software = 'pixelfed')
2019-12-05 02:47:00 +00:00
{
2021-08-31 06:37:02 +00:00
return collect(self::audience($profile))
->filter(function($inbox) use($software) {
$domain = parse_url($inbox, PHP_URL_HOST);
if(!$domain) {
return false;
}
return InstanceService::software($domain) === strtolower($software);
})
->unique()
2021-09-04 02:51:56 +00:00
->values()
->toArray();
2021-08-31 06:37:02 +00:00
}
2021-07-11 13:43:29 +00:00
2021-08-31 06:37:02 +00:00
protected function getAudienceInboxes($pid, $scope = null)
{
$key = 'pf:services:follower:audience:' . $pid;
$domains = Cache::remember($key, 432000, function() use($pid) {
$profile = Profile::whereNull(['status', 'domain'])->find($pid);
2021-08-31 06:37:02 +00:00
if(!$profile) {
return [];
}
2021-07-11 13:43:29 +00:00
return $profile
->followers()
->get()
->map(function($follow) {
return $follow->sharedInbox ?? $follow->inbox_url;
})
2022-06-05 08:35:14 +00:00
->filter()
2021-07-11 13:43:29 +00:00
->unique()
->values();
2021-07-25 11:56:35 +00:00
});
if(!$domains || !$domains->count()) {
return [];
}
$banned = InstanceService::getBannedDomains();
if(!$banned || count($banned) === 0) {
return $domains->toArray();
}
$res = $domains->filter(function($domain) use($banned) {
$parsed = parse_url($domain, PHP_URL_HOST);
return !in_array($parsed, $banned);
})
->values()
->toArray();
return $res;
2019-02-14 21:24:34 +00:00
}
2021-12-05 00:37:44 +00:00
public static function mutualCount($pid, $mid)
{
return Cache::remember(self::CACHE_KEY . ':mutualcount:' . $pid . ':' . $mid, 3600, function() use($pid, $mid) {
return DB::table('followers as u')
->join('followers as s', 'u.following_id', '=', 's.following_id')
->where('s.profile_id', $mid)
->where('u.profile_id', $pid)
->count();
});
}
public static function mutualIds($pid, $mid, $limit = 3)
{
$key = self::CACHE_KEY . ':mutualids:' . $pid . ':' . $mid . ':limit_' . $limit;
return Cache::remember($key, 3600, function() use($pid, $mid, $limit) {
return DB::table('followers as u')
->join('followers as s', 'u.following_id', '=', 's.following_id')
->where('s.profile_id', $mid)
->where('u.profile_id', $pid)
->limit($limit)
->pluck('s.following_id')
->toArray();
});
}
public static function delCache($id)
{
Redis::del(self::CACHE_KEY . $id);
Redis::del(self::FOLLOWING_KEY . $id);
Redis::del(self::FOLLOWERS_KEY . $id);
Cache::forget(self::FOLLOWERS_SYNC_KEY . $id);
Cache::forget(self::FOLLOWING_SYNC_KEY . $id);
Cache::forget(self::FOLLOWERS_SYNC_ACTIVE . $id);
}
2020-02-22 11:11:46 +00:00
}