Add Mutual Followers API endpoint

This commit is contained in:
Daniel Supernault 2023-12-11 01:34:46 -07:00
parent 041c01359b
commit 33dbbe467d
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
3 changed files with 221 additions and 198 deletions

View file

@ -20,6 +20,7 @@ use App\StatusArchived;
use App\User; use App\User;
use App\UserSetting; use App\UserSetting;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\FollowerService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Services\ProfileStatusService; use App\Services\ProfileStatusService;
use App\Services\LikeService; use App\Services\LikeService;
@ -897,4 +898,19 @@ class ApiV1Dot1Controller extends Controller
return [200]; return [200];
} }
public function getMutualAccounts(Request $request, $id)
{
abort_if(!$request->user(), 403);
$account = AccountService::get($id, true);
if(!$account || !isset($account['id'])) { return []; }
$res = collect(FollowerService::mutualAccounts($request->user()->profile_id, $id))
->map(function($accountId) {
return AccountService::get($accountId, true);
})
->filter()
->take(24)
->values();
return $this->json($res);
}
} }

View file

@ -6,222 +6,239 @@ use Illuminate\Support\Facades\Redis;
use Cache; use Cache;
use DB; use DB;
use App\{ use App\{
Follower, Follower,
Profile, Profile,
User User
}; };
use App\Jobs\FollowPipeline\FollowServiceWarmCache; use App\Jobs\FollowPipeline\FollowServiceWarmCache;
class FollowerService class FollowerService
{ {
const CACHE_KEY = 'pf:services:followers:'; const CACHE_KEY = 'pf:services:followers:';
const FOLLOWERS_SYNC_KEY = 'pf:services:followers:sync-followers:'; const FOLLOWERS_SYNC_KEY = 'pf:services:followers:sync-followers:';
const FOLLOWING_SYNC_KEY = 'pf:services:followers:sync-following:'; const FOLLOWING_SYNC_KEY = 'pf:services:followers:sync-following:';
const FOLLOWING_KEY = 'pf:services:follow:following:id:'; const FOLLOWING_KEY = 'pf:services:follow:following:id:';
const FOLLOWERS_KEY = 'pf:services:follow:followers:id:'; const FOLLOWERS_KEY = 'pf:services:follow:followers:id:';
const FOLLOWERS_LOCAL_KEY = 'pf:services:follow:local-follower-ids:'; const FOLLOWERS_LOCAL_KEY = 'pf:services:follow:local-follower-ids:';
const FOLLOWERS_INTER_KEY = 'pf:services:follow:followers:inter:id:';
public static function add($actor, $target, $refresh = true) public static function add($actor, $target, $refresh = true)
{ {
$ts = (int) microtime(true); $ts = (int) microtime(true);
if($refresh) { if($refresh) {
RelationshipService::refresh($actor, $target); RelationshipService::refresh($actor, $target);
} else { } else {
RelationshipService::forget($actor, $target); RelationshipService::forget($actor, $target);
} }
Redis::zadd(self::FOLLOWING_KEY . $actor, $ts, $target); Redis::zadd(self::FOLLOWING_KEY . $actor, $ts, $target);
Redis::zadd(self::FOLLOWERS_KEY . $target, $ts, $actor); Redis::zadd(self::FOLLOWERS_KEY . $target, $ts, $actor);
Cache::forget('profile:following:' . $actor); Cache::forget('profile:following:' . $actor);
} }
public static function remove($actor, $target) public static function remove($actor, $target)
{ {
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:follower:audience:' . $actor); Cache::forget('pf:services:follower:audience:' . $actor);
Cache::forget('pf:services:follower:audience:' . $target); Cache::forget('pf:services:follower:audience:' . $target);
AccountService::del($actor); AccountService::del($actor);
AccountService::del($target); AccountService::del($target);
RelationshipService::refresh($actor, $target); RelationshipService::refresh($actor, $target);
Cache::forget('profile:following:' . $actor); Cache::forget('profile:following:' . $actor);
} }
public static function followers($id, $start = 0, $stop = 10) public static function followers($id, $start = 0, $stop = 10)
{ {
self::cacheSyncCheck($id, 'followers'); self::cacheSyncCheck($id, 'followers');
return Redis::zrevrange(self::FOLLOWERS_KEY . $id, $start, $stop); return Redis::zrevrange(self::FOLLOWERS_KEY . $id, $start, $stop);
} }
public static function following($id, $start = 0, $stop = 10) public static function following($id, $start = 0, $stop = 10)
{ {
self::cacheSyncCheck($id, 'following'); self::cacheSyncCheck($id, 'following');
return Redis::zrevrange(self::FOLLOWING_KEY . $id, $start, $stop); return Redis::zrevrange(self::FOLLOWING_KEY . $id, $start, $stop);
} }
public static function followersPaginate($id, $page = 1, $limit = 10) public static function followersPaginate($id, $page = 1, $limit = 10)
{ {
$start = $page == 1 ? 0 : $page * $limit - $limit; $start = $page == 1 ? 0 : $page * $limit - $limit;
$end = $start + ($limit - 1); $end = $start + ($limit - 1);
return self::followers($id, $start, $end); return self::followers($id, $start, $end);
} }
public static function followingPaginate($id, $page = 1, $limit = 10) public static function followingPaginate($id, $page = 1, $limit = 10)
{ {
$start = $page == 1 ? 0 : $page * $limit - $limit; $start = $page == 1 ? 0 : $page * $limit - $limit;
$end = $start + ($limit - 1); $end = $start + ($limit - 1);
return self::following($id, $start, $end); return self::following($id, $start, $end);
} }
public static function followerCount($id, $warmCache = true) public static function followerCount($id, $warmCache = true)
{ {
if($warmCache) { if($warmCache) {
self::cacheSyncCheck($id, 'followers'); self::cacheSyncCheck($id, 'followers');
} }
return Redis::zCard(self::FOLLOWERS_KEY . $id); return Redis::zCard(self::FOLLOWERS_KEY . $id);
} }
public static function followingCount($id, $warmCache = true) public static function followingCount($id, $warmCache = true)
{ {
if($warmCache) { if($warmCache) {
self::cacheSyncCheck($id, 'following'); self::cacheSyncCheck($id, 'following');
} }
return Redis::zCard(self::FOLLOWING_KEY . $id); return Redis::zCard(self::FOLLOWING_KEY . $id);
} }
public static function follows(string $actor, string $target) public static function follows(string $actor, string $target)
{ {
if($actor == $target) { if($actor == $target) {
return false; return false;
} }
if(self::followerCount($target, false) && self::followingCount($actor, false)) { if(self::followerCount($target, false) && self::followingCount($actor, false)) {
self::cacheSyncCheck($target, 'followers'); self::cacheSyncCheck($target, 'followers');
return (bool) Redis::zScore(self::FOLLOWERS_KEY . $target, $actor); return (bool) Redis::zScore(self::FOLLOWERS_KEY . $target, $actor);
} else { } else {
self::cacheSyncCheck($target, 'followers'); self::cacheSyncCheck($target, 'followers');
self::cacheSyncCheck($actor, 'following'); self::cacheSyncCheck($actor, 'following');
return Follower::whereProfileId($actor)->whereFollowingId($target)->exists(); return Follower::whereProfileId($actor)->whereFollowingId($target)->exists();
} }
} }
public static function cacheSyncCheck($id, $scope = 'followers') public static function cacheSyncCheck($id, $scope = 'followers')
{ {
if($scope === 'followers') { if($scope === 'followers') {
if(Cache::get(self::FOLLOWERS_SYNC_KEY . $id) != null) { if(Cache::get(self::FOLLOWERS_SYNC_KEY . $id) != null) {
return; return;
} }
FollowServiceWarmCache::dispatch($id)->onQueue('low'); FollowServiceWarmCache::dispatch($id)->onQueue('low');
} }
if($scope === 'following') { if($scope === 'following') {
if(Cache::get(self::FOLLOWING_SYNC_KEY . $id) != null) { if(Cache::get(self::FOLLOWING_SYNC_KEY . $id) != null) {
return; return;
} }
FollowServiceWarmCache::dispatch($id)->onQueue('low'); FollowServiceWarmCache::dispatch($id)->onQueue('low');
} }
return; return;
} }
public static function audience($profile, $scope = null) public static function audience($profile, $scope = null)
{ {
return (new self)->getAudienceInboxes($profile, $scope); return (new self)->getAudienceInboxes($profile, $scope);
} }
public static function softwareAudience($profile, $software = 'pixelfed') public static function softwareAudience($profile, $software = 'pixelfed')
{ {
return collect(self::audience($profile)) return collect(self::audience($profile))
->filter(function($inbox) use($software) { ->filter(function($inbox) use($software) {
$domain = parse_url($inbox, PHP_URL_HOST); $domain = parse_url($inbox, PHP_URL_HOST);
if(!$domain) { if(!$domain) {
return false; return false;
} }
return InstanceService::software($domain) === strtolower($software); return InstanceService::software($domain) === strtolower($software);
}) })
->unique() ->unique()
->values() ->values()
->toArray(); ->toArray();
} }
protected function getAudienceInboxes($pid, $scope = null) protected function getAudienceInboxes($pid, $scope = null)
{ {
$key = 'pf:services:follower:audience:' . $pid; $key = 'pf:services:follower:audience:' . $pid;
$domains = Cache::remember($key, 432000, function() use($pid) { $domains = Cache::remember($key, 432000, function() use($pid) {
$profile = Profile::whereNull(['status', 'domain'])->find($pid); $profile = Profile::whereNull(['status', 'domain'])->find($pid);
if(!$profile) { if(!$profile) {
return []; return [];
} }
return $profile return $profile
->followers() ->followers()
->get() ->get()
->map(function($follow) { ->map(function($follow) {
return $follow->sharedInbox ?? $follow->inbox_url; return $follow->sharedInbox ?? $follow->inbox_url;
}) })
->filter() ->filter()
->unique() ->unique()
->values(); ->values();
}); });
if(!$domains || !$domains->count()) { if(!$domains || !$domains->count()) {
return []; return [];
} }
$banned = InstanceService::getBannedDomains(); $banned = InstanceService::getBannedDomains();
if(!$banned || count($banned) === 0) { if(!$banned || count($banned) === 0) {
return $domains->toArray(); return $domains->toArray();
} }
$res = $domains->filter(function($domain) use($banned) { $res = $domains->filter(function($domain) use($banned) {
$parsed = parse_url($domain, PHP_URL_HOST); $parsed = parse_url($domain, PHP_URL_HOST);
return !in_array($parsed, $banned); return !in_array($parsed, $banned);
}) })
->values() ->values()
->toArray(); ->toArray();
return $res; return $res;
} }
public static function mutualCount($pid, $mid) public static function mutualCount($pid, $mid)
{ {
return Cache::remember(self::CACHE_KEY . ':mutualcount:' . $pid . ':' . $mid, 3600, function() use($pid, $mid) { return Cache::remember(self::CACHE_KEY . ':mutualcount:' . $pid . ':' . $mid, 3600, function() use($pid, $mid) {
return DB::table('followers as u') return DB::table('followers as u')
->join('followers as s', 'u.following_id', '=', 's.following_id') ->join('followers as s', 'u.following_id', '=', 's.following_id')
->where('s.profile_id', $mid) ->where('s.profile_id', $mid)
->where('u.profile_id', $pid) ->where('u.profile_id', $pid)
->count(); ->count();
}); });
} }
public static function mutualIds($pid, $mid, $limit = 3) public static function mutualIds($pid, $mid, $limit = 3)
{ {
$key = self::CACHE_KEY . ':mutualids:' . $pid . ':' . $mid . ':limit_' . $limit; $key = self::CACHE_KEY . ':mutualids:' . $pid . ':' . $mid . ':limit_' . $limit;
return Cache::remember($key, 3600, function() use($pid, $mid, $limit) { return Cache::remember($key, 3600, function() use($pid, $mid, $limit) {
return DB::table('followers as u') return DB::table('followers as u')
->join('followers as s', 'u.following_id', '=', 's.following_id') ->join('followers as s', 'u.following_id', '=', 's.following_id')
->where('s.profile_id', $mid) ->where('s.profile_id', $mid)
->where('u.profile_id', $pid) ->where('u.profile_id', $pid)
->limit($limit) ->limit($limit)
->pluck('s.following_id') ->pluck('s.following_id')
->toArray(); ->toArray();
}); });
} }
public static function delCache($id) public static function mutualAccounts($actorId, $profileId)
{ {
Redis::del(self::CACHE_KEY . $id); if($actorId == $profileId) {
Redis::del(self::FOLLOWING_KEY . $id); return [];
Redis::del(self::FOLLOWERS_KEY . $id); }
Cache::forget(self::FOLLOWERS_SYNC_KEY . $id); $actorKey = self::FOLLOWING_KEY . $actorId;
Cache::forget(self::FOLLOWING_SYNC_KEY . $id); $profileKey = self::FOLLOWERS_KEY . $profileId;
} $key = self::FOLLOWERS_INTER_KEY . $actorId . ':' . $profileId;
$res = Redis::zinterstore($key, [$actorKey, $profileKey]);
if($res) {
return Redis::zrange($key, 0, -1);
} else {
return [];
}
}
public static function localFollowerIds($pid, $limit = 0) public static function delCache($id)
{ {
$key = self::FOLLOWERS_LOCAL_KEY . $pid; Redis::del(self::CACHE_KEY . $id);
$res = Cache::remember($key, 7200, function() use($pid) { Redis::del(self::FOLLOWING_KEY . $id);
return DB::table('followers')->whereFollowingId($pid)->whereLocalProfile(true)->pluck('profile_id')->sort(); Redis::del(self::FOLLOWERS_KEY . $id);
}); Cache::forget(self::FOLLOWERS_SYNC_KEY . $id);
return $limit ? Cache::forget(self::FOLLOWING_SYNC_KEY . $id);
$res->take($limit)->values()->toArray() : }
$res->values()->toArray();
} public static function localFollowerIds($pid, $limit = 0)
{
$key = self::FOLLOWERS_LOCAL_KEY . $pid;
$res = Cache::remember($key, 7200, function() use($pid) {
return DB::table('followers')->whereFollowingId($pid)->whereLocalProfile(true)->pluck('profile_id')->sort();
});
return $limit ?
$res->take($limit)->values()->toArray() :
$res->values()->toArray();
}
} }

View file

@ -111,12 +111,9 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
}); });
Route::group(['prefix' => 'v1.1'], function() use($middleware) { Route::group(['prefix' => 'v1.1'], function() use($middleware) {
$reportMiddleware = $middleware; Route::post('report', 'Api\ApiV1Dot1Controller@report')->middleware($middleware);
$reportMiddleware[] = DeprecatedEndpoint::class;
Route::post('report', 'Api\ApiV1Dot1Controller@report')->middleware($reportMiddleware);
Route::group(['prefix' => 'accounts'], function () use($middleware) { Route::group(['prefix' => 'accounts'], function () use($middleware) {
$middleware[] = DeprecatedEndpoint::class;
Route::get('timelines/home', 'Api\ApiV1Controller@timelineHome')->middleware($middleware); Route::get('timelines/home', 'Api\ApiV1Controller@timelineHome')->middleware($middleware);
Route::delete('avatar', 'Api\ApiV1Dot1Controller@deleteAvatar')->middleware($middleware); Route::delete('avatar', 'Api\ApiV1Dot1Controller@deleteAvatar')->middleware($middleware);
Route::get('{id}/posts', 'Api\ApiV1Dot1Controller@accountPosts')->middleware($middleware); Route::get('{id}/posts', 'Api\ApiV1Dot1Controller@accountPosts')->middleware($middleware);
@ -125,10 +122,10 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
Route::get('two-factor', 'Api\ApiV1Dot1Controller@accountTwoFactor')->middleware($middleware); Route::get('two-factor', 'Api\ApiV1Dot1Controller@accountTwoFactor')->middleware($middleware);
Route::get('emails-from-pixelfed', 'Api\ApiV1Dot1Controller@accountEmailsFromPixelfed')->middleware($middleware); Route::get('emails-from-pixelfed', 'Api\ApiV1Dot1Controller@accountEmailsFromPixelfed')->middleware($middleware);
Route::get('apps-and-applications', 'Api\ApiV1Dot1Controller@accountApps')->middleware($middleware); Route::get('apps-and-applications', 'Api\ApiV1Dot1Controller@accountApps')->middleware($middleware);
Route::get('mutuals/{id}', 'Api\ApiV1Dot1Controller@getMutualAccounts')->middleware($middleware);
}); });
Route::group(['prefix' => 'collections'], function () use($middleware) { Route::group(['prefix' => 'collections'], function () use($middleware) {
$middleware[] = DeprecatedEndpoint::class;
Route::get('accounts/{id}', 'CollectionController@getUserCollections')->middleware($middleware); Route::get('accounts/{id}', 'CollectionController@getUserCollections')->middleware($middleware);
Route::get('items/{id}', 'CollectionController@getItems')->middleware($middleware); Route::get('items/{id}', 'CollectionController@getItems')->middleware($middleware);
Route::get('view/{id}', 'CollectionController@getCollection')->middleware($middleware); Route::get('view/{id}', 'CollectionController@getCollection')->middleware($middleware);
@ -139,7 +136,6 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
}); });
Route::group(['prefix' => 'direct'], function () use($middleware) { Route::group(['prefix' => 'direct'], function () use($middleware) {
$middleware[] = DeprecatedEndpoint::class;
Route::get('thread', 'DirectMessageController@thread')->middleware($middleware); Route::get('thread', 'DirectMessageController@thread')->middleware($middleware);
Route::post('thread/send', 'DirectMessageController@create')->middleware($middleware); Route::post('thread/send', 'DirectMessageController@create')->middleware($middleware);
Route::delete('thread/message', 'DirectMessageController@delete')->middleware($middleware); Route::delete('thread/message', 'DirectMessageController@delete')->middleware($middleware);
@ -151,19 +147,16 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
}); });
Route::group(['prefix' => 'archive'], function () use($middleware) { Route::group(['prefix' => 'archive'], function () use($middleware) {
$middleware[] = DeprecatedEndpoint::class;
Route::post('add/{id}', 'Api\ApiV1Dot1Controller@archive')->middleware($middleware); Route::post('add/{id}', 'Api\ApiV1Dot1Controller@archive')->middleware($middleware);
Route::post('remove/{id}', 'Api\ApiV1Dot1Controller@unarchive')->middleware($middleware); Route::post('remove/{id}', 'Api\ApiV1Dot1Controller@unarchive')->middleware($middleware);
Route::get('list', 'Api\ApiV1Dot1Controller@archivedPosts')->middleware($middleware); Route::get('list', 'Api\ApiV1Dot1Controller@archivedPosts')->middleware($middleware);
}); });
Route::group(['prefix' => 'places'], function () use($middleware) { Route::group(['prefix' => 'places'], function () use($middleware) {
$middleware[] = DeprecatedEndpoint::class;
Route::get('posts/{id}/{slug}', 'Api\ApiV1Dot1Controller@placesById')->middleware($middleware); Route::get('posts/{id}/{slug}', 'Api\ApiV1Dot1Controller@placesById')->middleware($middleware);
}); });
Route::group(['prefix' => 'stories'], function () use($middleware) { Route::group(['prefix' => 'stories'], function () use($middleware) {
$middleware[] = DeprecatedEndpoint::class;
Route::get('carousel', 'Stories\StoryApiV1Controller@carousel')->middleware($middleware); Route::get('carousel', 'Stories\StoryApiV1Controller@carousel')->middleware($middleware);
Route::post('add', 'Stories\StoryApiV1Controller@add')->middleware($middleware); Route::post('add', 'Stories\StoryApiV1Controller@add')->middleware($middleware);
Route::post('publish', 'Stories\StoryApiV1Controller@publish')->middleware($middleware); Route::post('publish', 'Stories\StoryApiV1Controller@publish')->middleware($middleware);
@ -173,20 +166,17 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
}); });
Route::group(['prefix' => 'compose'], function () use($middleware) { Route::group(['prefix' => 'compose'], function () use($middleware) {
$middleware[] = DeprecatedEndpoint::class;
Route::get('search/location', 'ComposeController@searchLocation')->middleware($middleware); Route::get('search/location', 'ComposeController@searchLocation')->middleware($middleware);
Route::get('settings', 'ComposeController@composeSettings')->middleware($middleware); Route::get('settings', 'ComposeController@composeSettings')->middleware($middleware);
}); });
Route::group(['prefix' => 'discover'], function () use($middleware) { Route::group(['prefix' => 'discover'], function () use($middleware) {
$middleware[] = DeprecatedEndpoint::class;
Route::get('accounts/popular', 'Api\ApiV1Controller@discoverAccountsPopular')->middleware($middleware); Route::get('accounts/popular', 'Api\ApiV1Controller@discoverAccountsPopular')->middleware($middleware);
Route::get('posts/trending', 'DiscoverController@trendingApi')->middleware($middleware); Route::get('posts/trending', 'DiscoverController@trendingApi')->middleware($middleware);
Route::get('posts/hashtags', 'DiscoverController@trendingHashtags')->middleware($middleware); Route::get('posts/hashtags', 'DiscoverController@trendingHashtags')->middleware($middleware);
}); });
Route::group(['prefix' => 'directory'], function () use($middleware) { Route::group(['prefix' => 'directory'], function () use($middleware) {
$middleware[] = DeprecatedEndpoint::class;
Route::get('listing', 'PixelfedDirectoryController@get'); Route::get('listing', 'PixelfedDirectoryController@get');
}); });