Skip to content

Commit

Permalink
Make related panels respect blocks (#1183)
Browse files Browse the repository at this point in the history
  • Loading branch information
BentiGorlich authored Oct 13, 2024
1 parent 09a90b4 commit 85bd770
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 78 deletions.
28 changes: 5 additions & 23 deletions src/Repository/BookmarkRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Pagination\NativeQueryAdapter;
use App\Pagination\Pagerfanta;
use App\Pagination\Transformation\ContentPopulationTransformer;
use App\Utils\SqlHelpers;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -130,10 +131,10 @@ public function findPopulatedByList(BookmarkList $list, Criteria $criteria, ?int
$parameters['time'] = $criteria->getSince();
}

$entryWhere = $this->makeWhereString($entryWhereArr);
$entryCommentWhere = $this->makeWhereString($entryCommentWhereArr);
$postWhere = $this->makeWhereString($postWhereArr);
$postCommentWhere = $this->makeWhereString($postCommentWhereArr);
$entryWhere = SqlHelpers::makeWhereString($entryWhereArr);
$entryCommentWhere = SqlHelpers::makeWhereString($entryCommentWhereArr);
$postWhere = SqlHelpers::makeWhereString($postWhereArr);
$postCommentWhere = SqlHelpers::makeWhereString($postCommentWhereArr);

$sql = "
SELECT * FROM (
Expand All @@ -158,23 +159,4 @@ public function findPopulatedByList(BookmarkList $list, Criteria $criteria, ?int

return Pagerfanta::createForCurrentPageWithMaxPerPage($adapter, $criteria->page, $perPage ?? EntryRepository::PER_PAGE);
}

private function makeWhereString(array $whereClauses): string
{
if (empty($whereClauses)) {
return '';
}

$where = 'WHERE ';
$i = 0;
foreach ($whereClauses as $whereClause) {
if ($i > 0) {
$where .= ' AND ';
}
$where .= $whereClause;
++$i;
}

return $where;
}
}
44 changes: 33 additions & 11 deletions src/Repository/EntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use App\PageView\EntryPageView;
use App\Pagination\AdapterFactory;
use App\Service\SettingsManager;
use App\Utils\SqlHelpers;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Types\Types;
Expand Down Expand Up @@ -56,6 +57,7 @@ public function __construct(
private readonly CacheInterface $cache,
private readonly AdapterFactory $adapterFactory,
private readonly SettingsManager $settingsManager,
private readonly SqlHelpers $sqlHelpers
) {
parent::__construct($registry, Entry::class);
}
Expand Down Expand Up @@ -151,6 +153,7 @@ private function addBannedHashtagClause(QueryBuilder $qb): void

private function filter(QueryBuilder $qb, EntryPageView $criteria): QueryBuilder
{
/** @var User $user */
$user = $this->security->getUser();

if (Criteria::AP_LOCAL === $criteria->federation) {
Expand Down Expand Up @@ -339,12 +342,11 @@ public function findToDelete(User $user, int $limit): array
->getResult();
}

public function findRelatedByTag(string $tag, ?int $limit = 1): array
public function findRelatedByTag(string $tag, ?int $limit = 1, ?User $user = null): array
{
$qb = $this->createQueryBuilder('e');

return $qb
->andWhere('e.visibility = :visibility')
$qb->andWhere('e.visibility = :visibility')
->andWhere('m.visibility = :visibility')
->andWhere('u.visibility = :visibility')
->andWhere('u.isDeleted = false')
Expand All @@ -360,16 +362,23 @@ public function findRelatedByTag(string $tag, ?int $limit = 1): array
'visibility' => VisibilityInterface::VISIBILITY_VISIBLE,
'tag' => $tag,
])
->setMaxResults($limit)
->getQuery()
->setMaxResults($limit);

if (null !== $user) {
$qb->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedMagazinesDql($user))))
->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedUsersDql($user))));
$qb->setParameter('user', $user);
}

return $qb->getQuery()
->getResult();
}

public function findRelatedByMagazine(string $name, ?int $limit = 1): array
public function findRelatedByMagazine(string $name, ?int $limit = 1, ?User $user = null): array
{
$qb = $this->createQueryBuilder('e');

return $qb->where('m.name LIKE :name OR m.title LIKE :title')
$qb->where('m.name LIKE :name OR m.title LIKE :title')
->andWhere('e.visibility = :visibility')
->andWhere('m.visibility = :visibility')
->andWhere('u.visibility = :visibility')
Expand All @@ -382,12 +391,19 @@ public function findRelatedByMagazine(string $name, ?int $limit = 1): array
->setParameters(
['name' => "%{$name}%", 'title' => "%{$name}%", 'visibility' => VisibilityInterface::VISIBILITY_VISIBLE]
)
->setMaxResults($limit)
->getQuery()
->setMaxResults($limit);

if (null !== $user) {
$qb->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedMagazinesDql($user))))
->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedUsersDql($user))));
$qb->setParameter('user', $user);
}

return $qb->getQuery()
->getResult();
}

public function findLast(int $limit): array
public function findLast(int $limit, ?User $user = null): array
{
$qb = $this->createQueryBuilder('e');

Expand All @@ -401,10 +417,16 @@ public function findLast(int $limit): array
$qb = $qb->andWhere('m.apId IS NULL');
}

if (null !== $user) {
$qb->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedMagazinesDql($user))))
->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedUsersDql($user))));
$qb->setParameter('user', $user);
}

return $qb->join('e.magazine', 'm')
->join('e.user', 'u')
->orderBy('e.createdAt', 'DESC')
->setParameters(['visibility' => VisibilityInterface::VISIBILITY_VISIBLE])
->setParameter('visibility', VisibilityInterface::VISIBILITY_VISIBLE)
->setMaxResults($limit)
->getQuery()
->getResult();
Expand Down
39 changes: 24 additions & 15 deletions src/Repository/MagazineRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Entity\User;
use App\PageView\MagazinePageView;
use App\Service\SettingsManager;
use App\Utils\SqlHelpers;
use App\Utils\SubscriptionSort;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\Collection;
Expand Down Expand Up @@ -49,7 +50,7 @@ class MagazineRepository extends ServiceEntityRepository
self::SORT_NEWEST,
];

public function __construct(ManagerRegistry $registry, private readonly SettingsManager $settingsManager)
public function __construct(ManagerRegistry $registry, private readonly SettingsManager $settingsManager, private readonly SqlHelpers $sqlHelpers)
{
parent::__construct($registry, Magazine::class);
}
Expand Down Expand Up @@ -478,21 +479,23 @@ public function search(string $magazine, int $page, int $perPage = self::PER_PAG
return $pagerfanta;
}

public function findRandom(): array
public function findRandom(?User $user = null): array
{
$conn = $this->getEntityManager()->getConnection();
$sql = '
SELECT id FROM magazine
';
$whereClauses = [];
$parameters = [];
if ($this->settingsManager->get('MBIN_SIDEBAR_SECTIONS_LOCAL_ONLY')) {
$sql .= 'WHERE ap_id IS NULL';
$whereClauses[] = 'm.ap_id IS NULL';
}
if (null !== $user) {
$subSql = 'SELECT * FROM magazine_block mb WHERE mb.magazine_id = m.id AND mb.user_id = :user';
$whereClauses[] = "NOT EXISTS($subSql)";
$parameters['user'] = $user->getId();
}
$sql .= '
ORDER BY random()
LIMIT 5
';
$whereString = SqlHelpers::makeWhereString($whereClauses);
$sql = "SELECT m.id FROM magazine m $whereString ORDER BY random() LIMIT 5";
$stmt = $conn->prepare($sql);
$stmt = $stmt->executeQuery();
$stmt = $stmt->executeQuery($parameters);
$ids = $stmt->fetchAllAssociative();

return $this->createQueryBuilder('m')
Expand All @@ -505,17 +508,23 @@ public function findRandom(): array
->getResult();
}

public function findRelated(string $magazine): array
public function findRelated(string $magazine, ?User $user = null): array
{
return $this->createQueryBuilder('m')
$qb = $this->createQueryBuilder('m')
->where('m.entryCount > 0 OR m.postCount > 0')
->andWhere('m.title LIKE :magazine OR m.description LIKE :magazine OR m.name LIKE :magazine')
->andWhere('m.isAdult = false')
->andWhere('m.visibility = :visibility')
->setParameter('visibility', VisibilityInterface::VISIBILITY_VISIBLE)
->setParameter('magazine', "%{$magazine}%")
->setMaxResults(5)
->getQuery()
->setMaxResults(5);

if (null !== $user) {
$qb->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedMagazinesDql($user))));
$qb->setParameter('user', $user);
}

return $qb->getQuery()
->getResult();
}

Expand Down
48 changes: 36 additions & 12 deletions src/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use App\PageView\PostPageView;
use App\Pagination\AdapterFactory;
use App\Service\SettingsManager;
use App\Utils\SqlHelpers;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Types\Types;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function __construct(
private readonly CacheInterface $cache,
private readonly AdapterFactory $adapterFactory,
private readonly SettingsManager $settingsManager,
private readonly SqlHelpers $sqlHelpers,
) {
parent::__construct($registry, Post::class);
}
Expand Down Expand Up @@ -143,6 +145,7 @@ private function addBannedHashtagClause(QueryBuilder $qb): void

private function filter(QueryBuilder $qb, Criteria $criteria): QueryBuilder
{
/** @var User|null $user */
$user = $this->security->getUser();

if (Criteria::AP_LOCAL === $criteria->federation) {
Expand All @@ -168,8 +171,8 @@ private function filter(QueryBuilder $qb, Criteria $criteria): QueryBuilder

if ($criteria->subscribed) {
$qb->andWhere(
'EXISTS (SELECT IDENTITY(ms.magazine) FROM '.MagazineSubscription::class.' ms WHERE ms.user = :user AND ms.magazine = p.magazine)
OR
'EXISTS (SELECT IDENTITY(ms.magazine) FROM '.MagazineSubscription::class.' ms WHERE ms.user = :user AND ms.magazine = p.magazine)
OR
EXISTS (SELECT IDENTITY(uf.following) FROM '.UserFollow::class.' uf WHERE uf.follower = :user AND uf.following = p.user)
OR
p.user = :user'
Expand Down Expand Up @@ -307,11 +310,11 @@ public function findToDelete(User $user, int $limit): array
->getResult();
}

public function findRelatedByTag(string $tag, ?int $limit = 1): array
public function findRelatedByTag(string $tag, ?int $limit = 1, ?User $user = null): array
{
$qb = $this->createQueryBuilder('p');

return $qb
$qb = $qb
->andWhere('p.visibility = :visibility')
->andWhere('m.visibility = :visibility')
->andWhere('u.visibility = :visibility')
Expand All @@ -328,16 +331,23 @@ public function findRelatedByTag(string $tag, ?int $limit = 1): array
'visibility' => VisibilityInterface::VISIBILITY_VISIBLE,
'name' => $tag,
])
->setMaxResults($limit)
->getQuery()
->setMaxResults($limit);

if (null !== $user) {
$qb->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedMagazinesDql($user))))
->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedUsersDql($user))));
$qb->setParameter('user', $user);
}

return $qb->getQuery()
->getResult();
}

public function findRelatedByMagazine(string $name, ?int $limit = 1): array
public function findRelatedByMagazine(string $name, ?int $limit = 1, ?User $user = null): array
{
$qb = $this->createQueryBuilder('p');

return $qb->where('m.name LIKE :name OR m.title LIKE :title')
$qb = $qb->where('m.name LIKE :name OR m.title LIKE :title')
->andWhere('p.visibility = :visibility')
->andWhere('m.visibility = :visibility')
->andWhere('u.visibility = :visibility')
Expand All @@ -349,12 +359,19 @@ public function findRelatedByMagazine(string $name, ?int $limit = 1): array
->setParameters(
['name' => "%{$name}%", 'title' => "%{$name}%", 'visibility' => VisibilityInterface::VISIBILITY_VISIBLE]
)
->setMaxResults($limit)
->getQuery()
->setMaxResults($limit);

if (null !== $user) {
$qb->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedMagazinesDql($user))))
->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedUsersDql($user))));
$qb->setParameter('user', $user);
}

return $qb->getQuery()
->getResult();
}

public function findLast(int $limit = 1): array
public function findLast(int $limit = 1, ?User $user = null): array
{
$qb = $this->createQueryBuilder('p');

Expand All @@ -365,9 +382,16 @@ public function findLast(int $limit = 1): array
$qb = $qb->andWhere('m.apId IS NULL');
}

if (null !== $user) {
$qb->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedMagazinesDql($user))))
->andWhere($qb->expr()->not($qb->expr()->exists($this->sqlHelpers->getBlockedUsersDql($user))));
$qb->setParameter('user', $user);
}

return $qb->join('p.magazine', 'm')
->join('p.user', 'u')
->orderBy('p.createdAt', 'DESC')
->setParameters(['visibility' => VisibilityInterface::VISIBILITY_VISIBLE])
->setParameter('visibility', VisibilityInterface::VISIBILITY_VISIBLE)
->setMaxResults($limit)
->getQuery()
->getResult();
Expand Down
Loading

0 comments on commit 85bd770

Please sign in to comment.