You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pixelfed/app/Services/UserFilterService.php

185 lines
5.1 KiB
PHTML

<?php
namespace App\Services;
use App\Models\Profile;
use App\Models\UserDomainBlock;
use App\Models\UserFilter;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class UserFilterService
{
const USER_MUTES_KEY = 'pf:services:mutes:ids:';
9 months ago
const USER_BLOCKS_KEY = 'pf:services:blocks:ids:';
9 months ago
const USER_DOMAIN_KEY = 'pf:services:domain-blocks:ids:';
const EMPTY_SENTINEL = '-1';
const FILTER_TTL = 2592000;
public static function mutes(int $profile_id)
{
return self::getFilterIds($profile_id, 'mute', self::USER_MUTES_KEY);
}
public static function blocks(int $profile_id)
{
return self::getFilterIds($profile_id, 'block', self::USER_BLOCKS_KEY);
}
protected static function getFilterIds(int $profile_id, string $type, string $keyPrefix)
{
$key = $keyPrefix.$profile_id;
$ids = Redis::zrevrange($key, 0, -1);
if (! empty($ids)) {
Redis::expire($key, self::FILTER_TTL);
6 days ago
return array_values(array_filter($ids, fn ($id): bool => $id !== self::EMPTY_SENTINEL));
}
Cache::forget($key.':cached-v0');
$ids = UserFilter::whereFilterType($type)
->whereUserId($profile_id)
->pluck('filterable_id')
->map(fn ($id) => AccountService::get($id, true)['id'] ?? false)
->filter()
->values()
->toArray();
if (empty($ids)) {
Redis::zadd($key, 0, self::EMPTY_SENTINEL);
} else {
foreach ($ids as $id) {
Redis::zadd($key, (int) $id, (int) $id);
}
}
Redis::expire($key, self::FILTER_TTL);
9 months ago
return $ids;
}
protected static function addToFilter(string $key, int $filterable_id)
{
Redis::zadd($key, $filterable_id, $filterable_id);
Redis::zrem($key, self::EMPTY_SENTINEL);
Redis::expire($key, self::FILTER_TTL);
}
protected static function removeFromFilter(string $key, $filterable_id)
{
Redis::zrem($key, $filterable_id);
if (Redis::zcard($key) === 0) {
Redis::zadd($key, 0, self::EMPTY_SENTINEL);
}
Redis::expire($key, self::FILTER_TTL);
}
public static function filters(int $profile_id)
{
return array_unique(array_merge(self::mutes($profile_id), self::blocks($profile_id)));
}
/**
* Profile ids that should be excluded from profile search results for the
* given profile: everyone who has blocked this profile, plus the profile
* itself. Returned as a collection so callers can use whereNotIn().
*
* @return Collection<int, int>
*/
public static function searchExcludedProfileIds(int $profile_id)
{
return UserFilter::whereFilterableType(Profile::class)
->whereFilterType('block')
->whereFilterableId($profile_id)
->pluck('user_id')
->push($profile_id);
}
public static function mute(int $profile_id, int $muted_id)
{
6 days ago
if ($profile_id === $muted_id) {
return false;
}
9 months ago
$key = self::USER_MUTES_KEY.$profile_id;
$exists = in_array($muted_id, self::mutes($profile_id));
9 months ago
if (! $exists) {
self::addToFilter($key, $muted_id);
}
9 months ago
return true;
}
public static function unmute(int $profile_id, string $muted_id)
{
9 months ago
if ($profile_id == $muted_id) {
return false;
}
9 months ago
$key = self::USER_MUTES_KEY.$profile_id;
$exists = in_array($muted_id, self::mutes($profile_id));
9 months ago
if ($exists) {
self::removeFromFilter($key, $muted_id);
}
9 months ago
return true;
}
public static function block(int $profile_id, int $blocked_id)
{
6 days ago
if ($profile_id === $blocked_id) {
return false;
}
9 months ago
$key = self::USER_BLOCKS_KEY.$profile_id;
$exists = in_array($blocked_id, self::blocks($profile_id));
9 months ago
if (! $exists) {
self::addToFilter($key, $blocked_id);
}
9 months ago
return true;
}
public static function unblock(int $profile_id, string $blocked_id)
{
9 months ago
if ($profile_id == $blocked_id) {
return false;
}
9 months ago
$key = self::USER_BLOCKS_KEY.$profile_id;
$exists = in_array($blocked_id, self::blocks($profile_id));
9 months ago
if ($exists) {
self::removeFromFilter($key, $blocked_id);
}
9 months ago
return $exists;
}
public static function blockCount(int $profile_id): int
{
return count(self::blocks($profile_id));
}
public static function muteCount(int $profile_id): int
{
return count(self::mutes($profile_id));
}
public static function domainBlocks($pid, $purge = false)
{
9 months ago
if ($purge) {
Cache::forget(self::USER_DOMAIN_KEY.$pid);
}
9 months ago
return Cache::remember(
9 months ago
self::USER_DOMAIN_KEY.$pid,
21600,
9 months ago
function () use ($pid) {
return UserDomainBlock::whereProfileId($pid)->pluck('domain')->toArray();
}
);
}
}