Skip to content
Snippets Groups Projects
BlockedDomainModel.php 3.12 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    /**
     * @copyright  2021 Podlibre
     * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
     * @link       https://castopod.org/
     */
    
    namespace ActivityPub\Models;
    
    
    use CodeIgniter\Database\BaseResult;
    use ActivityPub\Entities\BlockedDomain;
    
    use CodeIgniter\Model;
    
    class BlockedDomainModel extends Model
    {
    
        protected $table = 'activitypub_blocked_domains';
    
        /**
         * @var string
         */
        protected $returnType = BlockedDomain::class;
    
        /**
         * @var bool
         */
    
    
        /**
         * Retrieves instance or podcast domain blocks depending on whether or not $podcastId param is set.
    
        public function getBlockedDomains(): array
    
            $cacheName = config('ActivityPub')->cachePrefix . 'blocked_domains';
            if (!($found = cache($cacheName))) {
                $found = $this->findAll();
    
                cache()->save($cacheName, $found, DECADE);
            }
            return $found;
    
        public function isDomainBlocked(string $name): bool
    
            $hashedDomainName = md5($name);
    
            $cacheName =
                config('ActivityPub')->cachePrefix .
    
                "domain#{$hashedDomainName}_isBlocked";
    
                $found = (bool) $this->find($name);
    
    
                cache()->save($cacheName, $found, DECADE);
    
        public function blockDomain(string $name): int|bool
    
            $hashedDomain = md5($name);
            $prefix = config('ActivityPub')->cachePrefix;
            cache()->delete($prefix . "domain#{$hashedDomain}_isBlocked");
            cache()->delete($prefix . 'blocked_domains');
    
            cache()->deleteMatching($prefix . '*replies');
    
            Events::trigger('on_block_domain', $name);
    
    
            $this->db->transStart();
    
            // set all actors from the domain as blocked
            model('ActorModel')
                ->where('domain', $name)
                ->set('is_blocked', 1)
                ->update();
    
            $result = $this->insert([
                'name' => $name,
            ]);
    
            $this->db->transComplete();
    
            return $result;
        }
    
    
        public function unblockDomain(string $name): BaseResult|bool
    
            $hashedDomain = md5($name);
            $prefix = config('ActivityPub')->cachePrefix;
            cache()->delete($prefix . "domain#{$hashedDomain}_isBlocked");
            cache()->delete($prefix . 'blocked_domains');
    
            cache()->deleteMatching($prefix . '*replies');
    
            Events::trigger('on_unblock_domain', $name);
    
    
            $this->db->transStart();
            // unblock all actors from the domain
            model('ActorModel')
                ->where('domain', $name)
                ->set('is_blocked', 0)
                ->update();
    
            $result = $this->delete($name);
    
            $this->db->transComplete();
    
            return $result;
        }
    }