2021-04-02 19:20:02 +02:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 11:52:11 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-02 19:20:02 +02:00
|
|
|
/**
|
2022-02-19 17:06:11 +01:00
|
|
|
* @copyright 2021 Ad Aures
|
2021-04-02 19:20:02 +02:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2021-08-23 13:05:16 +02:00
|
|
|
namespace Modules\Fediverse\Models;
|
2021-04-02 19:20:02 +02:00
|
|
|
|
2021-05-19 18:35:13 +02:00
|
|
|
use CodeIgniter\Database\BaseResult;
|
2021-04-22 19:20:28 +02:00
|
|
|
use CodeIgniter\Events\Events;
|
2023-08-26 15:03:01 +02:00
|
|
|
use CodeIgniter\Model;
|
2023-08-27 15:26:06 +02:00
|
|
|
use Modules\Fediverse\Config\Fediverse;
|
2021-08-23 13:05:16 +02:00
|
|
|
use Modules\Fediverse\Entities\BlockedDomain;
|
2021-04-02 19:20:02 +02:00
|
|
|
|
2023-08-26 15:03:01 +02:00
|
|
|
class BlockedDomainModel extends Model
|
2021-04-02 19:20:02 +02:00
|
|
|
{
|
2021-05-06 16:00:48 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-08-26 15:03:01 +02:00
|
|
|
protected $table = 'fediverse_blocked_domains';
|
2021-05-06 16:00:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-04-02 19:20:02 +02:00
|
|
|
protected $primaryKey = 'name';
|
|
|
|
|
2021-05-06 16:00:48 +02:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-04-02 19:20:02 +02:00
|
|
|
protected $allowedFields = ['name'];
|
|
|
|
|
2021-05-06 16:00:48 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = BlockedDomain::class;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2021-04-02 19:20:02 +02:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
2021-05-06 16:00:48 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2021-04-02 19:20:02 +02:00
|
|
|
protected $useTimestamps = true;
|
2021-05-06 16:00:48 +02:00
|
|
|
|
2024-01-16 10:22:07 +01:00
|
|
|
protected $updatedField = '';
|
2021-04-02 19:20:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves instance or podcast domain blocks depending on whether or not $podcastId param is set.
|
2021-05-19 18:35:13 +02:00
|
|
|
*
|
2021-05-14 19:59:35 +02:00
|
|
|
* @return BlockedDomain[]
|
2021-04-02 19:20:02 +02:00
|
|
|
*/
|
2021-05-14 19:59:35 +02:00
|
|
|
public function getBlockedDomains(): array
|
2021-04-02 19:20:02 +02:00
|
|
|
{
|
2023-08-27 15:26:06 +02:00
|
|
|
$cacheName = config(Fediverse::class)
|
2021-05-19 18:35:13 +02:00
|
|
|
->cachePrefix . 'blocked_domains';
|
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-04-22 19:20:28 +02:00
|
|
|
$found = $this->findAll();
|
|
|
|
|
2021-05-19 18:35:13 +02:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-22 19:20:28 +02:00
|
|
|
}
|
2022-03-04 15:33:48 +01:00
|
|
|
|
2021-04-22 19:20:28 +02:00
|
|
|
return $found;
|
2021-04-02 19:20:02 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:59:35 +02:00
|
|
|
public function isDomainBlocked(string $name): bool
|
2021-04-02 19:20:02 +02:00
|
|
|
{
|
2021-05-14 19:59:35 +02:00
|
|
|
$hashedDomainName = md5($name);
|
2021-04-22 19:20:28 +02:00
|
|
|
$cacheName =
|
2023-08-27 15:26:06 +02:00
|
|
|
config(Fediverse::class)
|
2021-05-19 18:35:13 +02:00
|
|
|
->cachePrefix .
|
2021-05-14 19:59:35 +02:00
|
|
|
"domain#{$hashedDomainName}_isBlocked";
|
2021-05-19 18:35:13 +02:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-05-14 19:59:35 +02:00
|
|
|
$found = (bool) $this->find($name);
|
2021-04-22 19:20:28 +02:00
|
|
|
|
2021-05-19 18:35:13 +02:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-04-02 19:20:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:20:28 +02:00
|
|
|
return $found;
|
2021-04-02 19:20:02 +02:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:35:13 +02:00
|
|
|
public function blockDomain(string $name): int | bool
|
2021-04-02 19:20:02 +02:00
|
|
|
{
|
2021-04-22 19:20:28 +02:00
|
|
|
$hashedDomain = md5($name);
|
2023-08-27 15:26:06 +02:00
|
|
|
$prefix = config(Fediverse::class)
|
2021-05-19 18:35:13 +02:00
|
|
|
->cachePrefix;
|
|
|
|
cache()
|
|
|
|
->delete($prefix . "domain#{$hashedDomain}_isBlocked");
|
|
|
|
cache()
|
|
|
|
->delete($prefix . 'blocked_domains');
|
2021-04-22 19:20:28 +02:00
|
|
|
|
2021-05-19 18:35:13 +02:00
|
|
|
cache()
|
|
|
|
->deleteMatching($prefix . '*replies');
|
2021-04-22 19:20:28 +02:00
|
|
|
|
2021-04-02 19:20:02 +02:00
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
// set all actors from the domain as blocked
|
2022-02-05 12:40:30 +01:00
|
|
|
model('ActorModel', false)
|
2021-04-02 19:20:02 +02:00
|
|
|
->where('domain', $name)
|
2021-06-08 18:57:04 +02:00
|
|
|
->set('is_blocked', '1')
|
2021-04-02 19:20:02 +02:00
|
|
|
->update();
|
|
|
|
|
|
|
|
$result = $this->insert([
|
|
|
|
'name' => $name,
|
|
|
|
]);
|
|
|
|
|
2021-06-08 11:52:11 +02:00
|
|
|
Events::trigger('on_block_domain', $name);
|
|
|
|
|
2021-04-02 19:20:02 +02:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-05-19 18:35:13 +02:00
|
|
|
public function unblockDomain(string $name): BaseResult | bool
|
2021-04-02 19:20:02 +02:00
|
|
|
{
|
2021-04-22 19:20:28 +02:00
|
|
|
$hashedDomain = md5($name);
|
2023-08-27 15:26:06 +02:00
|
|
|
$prefix = config(Fediverse::class)
|
2021-05-19 18:35:13 +02:00
|
|
|
->cachePrefix;
|
|
|
|
cache()
|
|
|
|
->delete($prefix . "domain#{$hashedDomain}_isBlocked");
|
|
|
|
cache()
|
|
|
|
->delete($prefix . 'blocked_domains');
|
|
|
|
|
|
|
|
cache()
|
|
|
|
->deleteMatching($prefix . '*replies');
|
2021-04-22 19:20:28 +02:00
|
|
|
|
2021-04-02 19:20:02 +02:00
|
|
|
$this->db->transStart();
|
|
|
|
// unblock all actors from the domain
|
2022-02-05 12:40:30 +01:00
|
|
|
model('ActorModel', false)
|
2021-04-02 19:20:02 +02:00
|
|
|
->where('domain', $name)
|
2021-06-08 18:57:04 +02:00
|
|
|
->set('is_blocked', '0')
|
2021-04-02 19:20:02 +02:00
|
|
|
->update();
|
|
|
|
|
|
|
|
$result = $this->delete($name);
|
|
|
|
|
2021-06-08 11:52:11 +02:00
|
|
|
Events::trigger('on_unblock_domain', $name);
|
|
|
|
|
2021-04-02 19:20:02 +02:00
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|