Merge pull request #5646 from wallabag/fix/tagging-rule-custom-reading-speed

This commit is contained in:
Jérémy Benoist 2022-03-02 22:03:30 +01:00 committed by GitHub
commit 7def677ba1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 104 additions and 11 deletions

View file

@ -41,7 +41,7 @@ class TagAllCommand extends ContainerAwareCommand
$entries = $tagger->tagAllForUser($user);
$io->text('Persist entries... ');
$io->text('Persist ' . \count($entries) . ' entries... ');
$em = $this->getDoctrine()->getManager();
foreach ($entries as $entry) {

View file

@ -45,7 +45,7 @@ class ConfigFixtures extends Fixture implements DependentFixtureInterface
$emptyConfig = new Config($this->getReference('empty-user'));
$emptyConfig->setTheme('material');
$emptyConfig->setItemsPerPage(10);
$emptyConfig->setReadingSpeed(200);
$emptyConfig->setReadingSpeed(100);
$emptyConfig->setLanguage('en');
$emptyConfig->setPocketConsumerKey(null);
$emptyConfig->setActionMarkAsRead(0);

View file

@ -43,6 +43,20 @@ class TaggingRuleFixtures extends Fixture implements DependentFixtureInterface
$manager->persist($tr4);
$tr5 = new TaggingRule();
$tr5->setRule('readingTime <= 5');
$tr5->setTags(['shortread']);
$tr5->setConfig($this->getReference('empty-config'));
$manager->persist($tr5);
$tr6 = new TaggingRule();
$tr6->setRule('readingTime > 5');
$tr6->setTags(['longread']);
$tr6->setConfig($this->getReference('empty-config'));
$manager->persist($tr6);
$manager->flush();
}

View file

@ -35,8 +35,10 @@ class RuleBasedTagger
{
$rules = $this->getRulesForUser($entry->getUser());
$clonedEntry = $this->fixEntry($entry);
foreach ($rules as $rule) {
if (!$this->rulerz->satisfies($entry, $rule->getRule())) {
if (!$this->rulerz->satisfies($clonedEntry, $rule->getRule())) {
continue;
}
@ -61,14 +63,22 @@ class RuleBasedTagger
public function tagAllForUser(User $user)
{
$rules = $this->getRulesForUser($user);
$entries = [];
$entriesToUpdate = [];
$tagsCache = [];
foreach ($rules as $rule) {
$qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
$entries = $this->rulerz->filter($qb, $rule->getRule());
$entries = $this->entryRepository
->getBuilderForAllByUser($user->getId())
->getQuery()
->getResult();
foreach ($entries as $entry) {
$clonedEntry = $this->fixEntry($entry);
foreach ($rules as $rule) {
if (!$this->rulerz->satisfies($clonedEntry, $rule->getRule())) {
continue;
}
foreach ($entries as $entry) {
foreach ($rule->getTags() as $label) {
// avoid new tag duplicate by manually caching them
if (!isset($tagsCache[$label])) {
@ -78,11 +88,13 @@ class RuleBasedTagger
$tag = $tagsCache[$label];
$entry->addTag($tag);
$entriesToUpdate[] = $entry;
}
}
}
return $entries;
return $entriesToUpdate;
}
/**
@ -114,4 +126,15 @@ class RuleBasedTagger
{
return $user->getConfig()->getTaggingRules();
}
/**
* Update reading time on the fly to match the proper words per minute from the user.
*/
private function fixEntry(Entry $entry)
{
$clonedEntry = clone $entry;
$clonedEntry->setReadingTime($entry->getReadingTime() / $entry->getUser()->getConfig()->getReadingSpeed() * 200);
return $clonedEntry;
}
}

View file

@ -175,6 +175,42 @@ class EntryControllerTest extends WallabagCoreTestCase
$client->getContainer()->get('craue_config')->set('store_article_headers', 0);
}
/**
* @group NetworkCalls
*/
public function testPostNewOkWithTaggingRules()
{
$this->logInAs('empty');
$client = $this->getClient();
$crawler = $client->request('GET', '/new');
$this->assertSame(200, $client->getResponse()->getStatusCode());
$form = $crawler->filter('form[name=entry]')->form();
$data = [
'entry[url]' => $this->url,
];
$client->submit($form, $data);
$this->assertSame(302, $client->getResponse()->getStatusCode());
$content = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$tags = $content->getTagsLabel();
/*
* Without the custom reading speed of `empty` user, it'll be inversed
*/
$this->assertContains('longread', $tags);
$this->assertNotContains('shortread', $tags);
}
/**
* @group NetworkCalls
*/

View file

@ -202,10 +202,29 @@ class RuleBasedTaggerTest extends TestCase
->method('satisfies')
->willReturn(true);
$this->rulerz
->method('filter')
$query = $this->getMockBuilder('Doctrine\ORM\AbstractQuery')
->disableOriginalConstructor()
->getMock();
$query
->expects($this->once())
->method('getResult')
->willReturn([new Entry($user), new Entry($user)]);
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->disableOriginalConstructor()
->getMock();
$qb
->expects($this->once())
->method('getQuery')
->willReturn($query);
$this->entryRepository
->expects($this->once())
->method('getBuilderForAllByUser')
->willReturn($qb);
$entries = $this->tagger->tagAllForUser($user);
$this->assertCount(2, $entries);
@ -222,6 +241,7 @@ class RuleBasedTaggerTest extends TestCase
{
$user = new User();
$config = new Config($user);
$config->setReadingSpeed(200);
$user->setConfig($config);