2019-09-04 01:29:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-12-11 06:04:03 +00:00
|
|
|
use Cache;
|
2022-06-09 10:09:05 +00:00
|
|
|
use App\UserFilter;
|
2019-12-11 06:04:03 +00:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
2019-09-04 01:29:25 +00:00
|
|
|
|
2022-06-09 10:09:05 +00:00
|
|
|
class UserFilterService
|
|
|
|
{
|
2019-09-04 01:29:25 +00:00
|
|
|
const USER_MUTES_KEY = 'pf:services:mutes:ids:';
|
|
|
|
const USER_BLOCKS_KEY = 'pf:services:blocks:ids:';
|
|
|
|
|
2022-06-09 10:09:05 +00:00
|
|
|
public static function mutes(int $profile_id)
|
2019-09-04 01:29:25 +00:00
|
|
|
{
|
|
|
|
$key = self::USER_MUTES_KEY . $profile_id;
|
2023-03-01 11:16:42 +00:00
|
|
|
$warm = Cache::has($key . ':cached-v0');
|
2022-06-09 10:09:05 +00:00
|
|
|
if($warm) {
|
|
|
|
return Redis::zrevrange($key, 0, -1) ?? [];
|
2019-09-04 01:29:25 +00:00
|
|
|
} else {
|
2022-06-09 10:09:05 +00:00
|
|
|
if(Redis::zrevrange($key, 0, -1)) {
|
|
|
|
return Redis::zrevrange($key, 0, -1);
|
|
|
|
}
|
2019-09-04 01:29:25 +00:00
|
|
|
$ids = UserFilter::whereFilterType('mute')
|
|
|
|
->whereUserId($profile_id)
|
|
|
|
->pluck('filterable_id')
|
2023-03-01 11:16:42 +00:00
|
|
|
->map(function($id) {
|
|
|
|
$acct = AccountService::get($id, true);
|
|
|
|
if(!$acct) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $acct['id'];
|
|
|
|
})
|
|
|
|
->filter(function($res) {
|
|
|
|
return $res;
|
|
|
|
})
|
|
|
|
->values()
|
2019-09-04 01:29:25 +00:00
|
|
|
->toArray();
|
|
|
|
foreach ($ids as $muted_id) {
|
|
|
|
Redis::zadd($key, (int) $muted_id, (int) $muted_id);
|
|
|
|
}
|
2023-03-01 11:16:42 +00:00
|
|
|
Cache::set($key . ':cached-v0', 1, 7776000);
|
2019-09-04 01:29:25 +00:00
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 10:09:05 +00:00
|
|
|
public static function blocks(int $profile_id)
|
2019-09-04 01:29:25 +00:00
|
|
|
{
|
|
|
|
$key = self::USER_BLOCKS_KEY . $profile_id;
|
2023-03-01 11:16:42 +00:00
|
|
|
$warm = Cache::has($key . ':cached-v0');
|
2022-06-09 10:09:05 +00:00
|
|
|
if($warm) {
|
|
|
|
return Redis::zrevrange($key, 0, -1) ?? [];
|
2019-09-04 01:29:25 +00:00
|
|
|
} else {
|
2022-06-09 10:09:05 +00:00
|
|
|
if(Redis::zrevrange($key, 0, -1)) {
|
|
|
|
return Redis::zrevrange($key, 0, -1);
|
|
|
|
}
|
2019-09-04 01:29:25 +00:00
|
|
|
$ids = UserFilter::whereFilterType('block')
|
|
|
|
->whereUserId($profile_id)
|
|
|
|
->pluck('filterable_id')
|
2023-03-01 11:16:42 +00:00
|
|
|
->map(function($id) {
|
|
|
|
$acct = AccountService::get($id, true);
|
|
|
|
if(!$acct) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $acct['id'];
|
|
|
|
})
|
|
|
|
->filter(function($res) {
|
|
|
|
return $res;
|
|
|
|
})
|
|
|
|
->values()
|
2019-09-04 01:29:25 +00:00
|
|
|
->toArray();
|
|
|
|
foreach ($ids as $blocked_id) {
|
2022-06-09 10:09:05 +00:00
|
|
|
Redis::zadd($key, (int) $blocked_id, (int) $blocked_id);
|
2019-09-04 01:29:25 +00:00
|
|
|
}
|
2023-03-01 11:16:42 +00:00
|
|
|
Cache::set($key . ':cached-v0', 1, 7776000);
|
2019-09-04 01:29:25 +00:00
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 10:09:05 +00:00
|
|
|
public static function filters(int $profile_id)
|
2019-09-04 01:29:25 +00:00
|
|
|
{
|
2021-07-16 02:48:39 +00:00
|
|
|
return array_unique(array_merge(self::mutes($profile_id), self::blocks($profile_id)));
|
2019-09-04 01:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function mute(int $profile_id, int $muted_id)
|
|
|
|
{
|
2023-03-01 11:16:42 +00:00
|
|
|
if($profile_id == $muted_id) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-04 01:29:25 +00:00
|
|
|
$key = self::USER_MUTES_KEY . $profile_id;
|
|
|
|
$mutes = self::mutes($profile_id);
|
|
|
|
$exists = in_array($muted_id, $mutes);
|
|
|
|
if(!$exists) {
|
|
|
|
Redis::zadd($key, $muted_id, $muted_id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function unmute(int $profile_id, string $muted_id)
|
|
|
|
{
|
2023-03-01 11:16:42 +00:00
|
|
|
if($profile_id == $muted_id) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-04 01:29:25 +00:00
|
|
|
$key = self::USER_MUTES_KEY . $profile_id;
|
|
|
|
$mutes = self::mutes($profile_id);
|
|
|
|
$exists = in_array($muted_id, $mutes);
|
|
|
|
if($exists) {
|
|
|
|
Redis::zrem($key, $muted_id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function block(int $profile_id, int $blocked_id)
|
|
|
|
{
|
2023-03-01 11:16:42 +00:00
|
|
|
if($profile_id == $blocked_id) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-04 01:29:25 +00:00
|
|
|
$key = self::USER_BLOCKS_KEY . $profile_id;
|
|
|
|
$exists = in_array($blocked_id, self::blocks($profile_id));
|
|
|
|
if(!$exists) {
|
|
|
|
Redis::zadd($key, $blocked_id, $blocked_id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function unblock(int $profile_id, string $blocked_id)
|
|
|
|
{
|
2023-03-01 11:16:42 +00:00
|
|
|
if($profile_id == $blocked_id) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-04 01:29:25 +00:00
|
|
|
$key = self::USER_BLOCKS_KEY . $profile_id;
|
|
|
|
$exists = in_array($blocked_id, self::blocks($profile_id));
|
|
|
|
if($exists) {
|
|
|
|
Redis::zrem($key, $blocked_id);
|
|
|
|
}
|
|
|
|
return $exists;
|
|
|
|
}
|
2021-10-20 10:31:07 +00:00
|
|
|
|
|
|
|
public static function blockCount(int $profile_id)
|
|
|
|
{
|
|
|
|
return Redis::zcard(self::USER_BLOCKS_KEY . $profile_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function muteCount(int $profile_id)
|
|
|
|
{
|
|
|
|
return Redis::zcard(self::USER_MUTES_KEY . $profile_id);
|
|
|
|
}
|
2021-07-16 02:48:39 +00:00
|
|
|
}
|