Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions src/Entity/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,41 +572,6 @@ public function getAllSuspectPackages(): array
return $this->getEntityManager()->getConnection()->fetchAllAssociative($sql);
}

/**
* Narrows a list of package ids down to those the "too many views" heuristic could still flag:
* still present, not already suspect, still publicly viewable, new enough, and not owned by a
* vendor a moderator has verified (which blocks the flagging for good). The download half of
* the check lives in Redis, so callers have to apply SUSPECT_VIEWS_MAX_DOWNLOADS themselves.
*
* @param list<int> $ids
*
* @return list<int>
*/
public function getPackageIdsFlaggableByViews(array $ids): array
{
if (\count($ids) === 0) {
return [];
}

// a suppressing freeze 404s the package page for everyone, so no views can come in anymore,
// while a gentle freeze keeps serving it and thus keeps the counter live
$sql = 'SELECT p.id FROM package p
LEFT JOIN vendor v ON v.name = p.vendor
WHERE p.id IN (:ids)
AND p.suspect IS NULL
AND (p.frozen IS NULL OR p.frozen NOT IN (:suppressed))
AND p.createdAt >= :minCreatedAt
AND COALESCE(v.verified, 0) = 0';

$rows = $this->getEntityManager()->getConnection()->fetchFirstColumn(
$sql,
['ids' => $ids, 'suppressed' => PackageFreezeReason::suppressingValues(), 'minCreatedAt' => self::SUSPECT_VIEWS_MIN_CREATED_AT],
['ids' => ArrayParameterType::INTEGER, 'suppressed' => ArrayParameterType::STRING]
);

return array_map('intval', $rows);
}

/**
* @param list<int> $ids
*
Expand Down
29 changes: 0 additions & 29 deletions tests/Entity/PackageRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,33 +198,4 @@ public function testGetFilteredQueryBuilderExcludesSuppressedByDefault(): void
self::assertContains('vendor/spam', $withFrozen);
self::assertContains('vendor/malware', $withFrozen);
}

public function testGetPackageIdsFlaggableByViewsKeepsOnlyPackagesTheHeuristicCanStillReach(): void
{
$live = self::createPackage('vendor/live', 'https://example.org/live');
$suspect = self::createPackage('vendor/suspect', 'https://example.org/suspect');
$suspect->setSuspect('Too many views');
$old = self::createPackage('vendor/old', 'https://example.org/old');
$old->setCreatedAt(new \DateTimeImmutable('2018-01-01'));
$spam = self::createPackage('vendor/spam', 'https://example.org/spam');
$spam->freeze(PackageFreezeReason::Spam);
$temporary = self::createPackage('vendor/temporary', 'https://example.org/temporary');
$temporary->freeze(PackageFreezeReason::Temporary);
$verified = self::createPackage('verifiedvendor/pkg', 'https://example.org/verifiedvendor/pkg');
$vendor = new Vendor('verifiedvendor');
$vendor->setVerified(true);
$this->store($live, $suspect, $old, $spam, $temporary, $verified, $vendor);

$ids = $this->packageRepository->getPackageIdsFlaggableByViews([
$live->getId(), $suspect->getId(), $old->getId(), $spam->getId(), $temporary->getId(), $verified->getId(), 999999999,
]);

self::assertContains($live->getId(), $ids);
self::assertContains($temporary->getId(), $ids, 'a gentle freeze keeps serving the page, so views still come in');
self::assertNotContains($suspect->getId(), $ids);
self::assertNotContains($old->getId(), $ids);
self::assertNotContains($spam->getId(), $ids, 'a suppressing freeze 404s the page, so no more views can arrive');
self::assertNotContains($verified->getId(), $ids, 'a verified vendor can never be flagged again');
self::assertNotContains(999999999, $ids, 'a deleted package cannot be flagged either');
}
}