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; } public static function audience($profile, $scope = null) { return (new self)->getAudienceInboxes($profile, $scope); } public static function softwareAudience($profile, $software = 'pixelfed') { 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() ->values() ->toArray(); } 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); if(!$profile) { return []; } return $profile ->followers() ->get() ->map(function($follow) { return $follow->sharedInbox ?? $follow->inbox_url; }) ->filter() ->unique() ->values(); }); 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; } 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); } }