Skip to content

Commit

Permalink
Remove default local domain for entries (#1280)
Browse files Browse the repository at this point in the history
Co-authored-by: Melroy van den Berg <melroy@melroy.org>
  • Loading branch information
BentiGorlich and melroy89 authored Dec 26, 2024
1 parent 998f184 commit a65b0d3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
12 changes: 11 additions & 1 deletion docs/02-admin/04-running-mbin/05-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,15 @@ This command removes post and user duplicates by their ActivityPub ID.
Usage:

```bash
php bin/console mbin:user:create [-r|--remove] [--admin] [--moderator] [--] <username> <email> <password>
php bin/console mbin:user:create [-r|--remove] [--admin] [--moderator] <username> <email> <password>
```

### Update-Local-Domain

This command will remove all remote posts from belonging to the local domain. This command is only relevant for instances
created before v1.7.4 as the local domain was the fallback if no domain could be extracted from a post.

Usage:
```bash
php bin/console mbin:update:local-domain
```
63 changes: 63 additions & 0 deletions src/Command/Update/RemoveRemoteEntriesFromLocalDomainCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace App\Command\Update;

use App\Repository\DomainRepository;
use App\Service\SettingsManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
'mbin:update:local-domain',
'This command removes remote entries from the local domain',
)]
class RemoveRemoteEntriesFromLocalDomainCommand extends Command
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly DomainRepository $repository,
private readonly SettingsManager $settingsManager,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$domainName = 'https://'.$this->settingsManager->get('KBIN_DOMAIN');
$domainName = preg_replace('/^www\./i', '', parse_url($domainName)['host']);

$domain = $this->repository->findOneByName($domainName);
if (!$domain) {
$io->warning(\sprintf('There is no local domain like %s', $domainName));

return Command::SUCCESS;
}

$countBeforeSql = 'SELECT COUNT(*) as ctn FROM entry WHERE domain_id = :dId';
$stmt1 = $this->entityManager->getConnection()->prepare($countBeforeSql);
$countBefore = \intval($stmt1->executeQuery(['dId' => $domain->getId()])->fetchOne());

$sql = 'UPDATE entry SET domain_id = NULL WHERE domain_id = :dId AND ap_id IS NOT NULL';
$stmt2 = $this->entityManager->getConnection()->prepare($sql);
$stmt2->executeStatement(['dId' => $domain->getId()]);

$countAfterSql = 'SELECT COUNT(*) as ctn FROM entry WHERE domain_id = :dId';
$stmt3 = $this->entityManager->getConnection()->prepare($countAfterSql);
$countAfter = \intval($stmt3->executeQuery(['dId' => $domain->getId()])->fetchOne());

$sql = 'UPDATE domain SET entry_count = :c WHERE id = :dId';
$stmt4 = $this->entityManager->getConnection()->prepare($sql);
$stmt4->executeStatement(['c' => $countAfter, 'dId' => $domain->getId()]);

$io->success(\sprintf('Removed %d entries from the domain %s, now only %d entries are left', $countBefore - $countAfter, $domainName, $countAfter));

return Command::SUCCESS;
}
}
9 changes: 5 additions & 4 deletions src/Service/DomainManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public function __construct(
) {
}

public function extract(DomainInterface $subject): DomainInterface
public function extract(DomainInterface $subject): void
{
$domainName = $subject->getUrl() ?? 'https://'.$this->settingsManager->get('KBIN_DOMAIN');
$domainName = $subject->getUrl();
if (!$domainName) {
return;
}

$domainName = preg_replace('/^www\./i', '', parse_url($domainName)['host']);

Expand All @@ -41,8 +44,6 @@ public function extract(DomainInterface $subject): DomainInterface
$domain->updateCounts();

$this->entityManager->flush();

return $subject;
}

public function subscribe(Domain $domain, User $user): void
Expand Down

0 comments on commit a65b0d3

Please sign in to comment.