Skip to content

Commit

Permalink
dev: Remove the NewsPicker service
Browse files Browse the repository at this point in the history
With the recent changes, this class became useless.
  • Loading branch information
marienfressinaud committed Oct 17, 2024
1 parent eb698ac commit b30022d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 75 deletions.
7 changes: 2 additions & 5 deletions src/controllers/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Minz\Response;
use App\auth;
use App\models;
use App\services;
use App\utils;

/**
Expand Down Expand Up @@ -78,8 +77,7 @@ public function create(Request $request): Response
]);
}

$news_picker = new services\NewsPicker($user);
$links = $news_picker->pick(max: 50);
$links = models\Link::listFromFollowedCollections($user->id, max:50);

$news = $user->news();

Expand Down Expand Up @@ -123,8 +121,7 @@ public function showAvailable(Request $request): Response
]);
}

$news_picker = new services\NewsPicker($user);
$links = $news_picker->pick(max: 2);
$links = models\Link::listFromFollowedCollections($user->id, max:2);

return Response::json(200, [
'available' => count($links) > 0,
Expand Down
32 changes: 0 additions & 32 deletions src/services/NewsPicker.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\services;
namespace App\models\dao\links;

use App\models;
use tests\factories\CollectionFactory;
Expand All @@ -10,7 +10,7 @@
use tests\factories\LinkToCollectionFactory;
use tests\factories\UserFactory;

class NewsPickerTest extends \PHPUnit\Framework\TestCase
class NewsQueriesTest extends \PHPUnit\Framework\TestCase
{
use \Minz\Tests\InitializerHelper;
use \Minz\Tests\TimeHelper;
Expand All @@ -26,14 +26,13 @@ public function setUsers(): void
$this->other_user = UserFactory::create();
}

public function testPickSelectsFromFollowed(): void
public function testListFromFollowedCollectionsSelectsFromFollowed(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
$published_at1 = \Minz\Time::ago(3, 'days');
$published_at2 = \Minz\Time::ago(1, 'days');
$news_picker = new NewsPicker($this->user);
$link1 = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => false,
Expand Down Expand Up @@ -62,7 +61,7 @@ public function testPickSelectsFromFollowed(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(2, count($links));
$this->assertSame($link2->id, $links[0]->id);
Expand All @@ -73,15 +72,14 @@ public function testPickSelectsFromFollowed(): void
$this->assertSame($collection->id, $links[1]->source_news_resource_id);
}

public function testPickSelectsHiddenLinkIfCollectionIsShared(): void
public function testListFromFollowedCollectionsSelectsHiddenLinkIfCollectionIsShared(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
/** @var int */
$days_ago = $this->fake('numberBetween', 0, 7);
$published_at = \Minz\Time::ago($days_ago, 'days');
$news_picker = new NewsPicker($this->user);
$link = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => true,
Expand All @@ -105,23 +103,22 @@ public function testPickSelectsHiddenLinkIfCollectionIsShared(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(1, count($links));
$this->assertSame($link->id, $links[0]->id);
$this->assertSame('collection', $links[0]->source_news_type);
$this->assertSame($collection->id, $links[0]->source_news_resource_id);
}

public function testPickSelectsFromPrivateCollectionIfShared(): void
public function testListFromFollowedCollectionsSelectsFromPrivateCollectionIfShared(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
/** @var int */
$days_ago = $this->fake('numberBetween', 0, 7);
$published_at = \Minz\Time::ago($days_ago, 'days');
$news_picker = new NewsPicker($this->user);
$link = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => false,
Expand All @@ -145,15 +142,15 @@ public function testPickSelectsFromPrivateCollectionIfShared(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(1, count($links));
$this->assertSame($link->id, $links[0]->id);
$this->assertSame('collection', $links[0]->source_news_type);
$this->assertSame($collection->id, $links[0]->source_news_resource_id);
}

public function testPickRespectsFromFollowedIfOldLinksButWithTimeFilterAll(): void
public function testListFromFollowedCollectionsRespectsFromFollowedIfOldLinksButWithTimeFilterAll(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
Expand All @@ -165,7 +162,6 @@ public function testPickRespectsFromFollowedIfOldLinksButWithTimeFilterAll(): vo
// when the user started to follow the collection
$delta_followed_days = $this->fake('numberBetween', 0, 7);
$followed_at = \Minz\Time::ago($days_ago - $delta_followed_days, 'days');
$news_picker = new NewsPicker($this->user);
$link = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => false,
Expand All @@ -187,23 +183,22 @@ public function testPickRespectsFromFollowedIfOldLinksButWithTimeFilterAll(): vo
'time_filter' => 'all',
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(1, count($links));
$this->assertSame($link->id, $links[0]->id);
$this->assertSame('collection', $links[0]->source_news_type);
$this->assertSame($collection->id, $links[0]->source_news_resource_id);
}

public function testPickDoesNotPickFromFollowedIfTooOld(): void
public function testListFromFollowedCollectionsDoesNotPickFromFollowedIfTooOld(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
/** @var int */
$days_ago = $this->fake('numberBetween', 8, 180);
$published_at = \Minz\Time::ago($days_ago, 'days');
$news_picker = new NewsPicker($this->user);
$link = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => false,
Expand All @@ -223,20 +218,19 @@ public function testPickDoesNotPickFromFollowedIfTooOld(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(0, count($links));
}

public function testPickDoesNotSelectFromFollowedIfTooOldWithTimeFilterStrict(): void
public function testListFromFollowedCollectionsDoesNotSelectFromFollowedIfTooOldWithTimeFilterStrict(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
/** @var int */
$hours_ago = $this->fake('numberBetween', 25, 72);
$published_at = \Minz\Time::ago($hours_ago, 'hours');
$news_picker = new NewsPicker($this->user);
$link = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => false,
Expand All @@ -257,20 +251,19 @@ public function testPickDoesNotSelectFromFollowedIfTooOldWithTimeFilterStrict():
'time_filter' => 'strict',
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(0, count($links));
}

public function testPickDoesNotSelectFromFollowedIfLinkIsHidden(): void
public function testListFromFollowedCollectionsDoesNotSelectFromFollowedIfLinkIsHidden(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
/** @var int */
$days_ago = $this->fake('numberBetween', 0, 7);
$published_at = \Minz\Time::ago($days_ago, 'days');
$news_picker = new NewsPicker($this->user);
$link = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => true,
Expand All @@ -290,20 +283,19 @@ public function testPickDoesNotSelectFromFollowedIfLinkIsHidden(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(0, count($links));
}

public function testPickDoesNotSelectFromFollowedIfCollectionIsPrivate(): void
public function testListFromFollowedCollectionsDoesNotSelectFromFollowedIfCollectionIsPrivate(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
/** @var int */
$days_ago = $this->fake('numberBetween', 0, 7);
$published_at = \Minz\Time::ago($days_ago, 'days');
$news_picker = new NewsPicker($this->user);
$link = LinkFactory::create([
'user_id' => $this->other_user->id,
'is_hidden' => false,
Expand All @@ -323,20 +315,19 @@ public function testPickDoesNotSelectFromFollowedIfCollectionIsPrivate(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(0, count($links));
}

public function testPickDoesNotSelectFromFollowedIfUrlInBookmarks(): void
public function testListFromFollowedCollectionsDoesNotSelectFromFollowedIfUrlInBookmarks(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
/** @var int */
$days_ago = $this->fake('numberBetween', 0, 7);
$published_at = \Minz\Time::ago($days_ago, 'days');
$news_picker = new NewsPicker($this->user);
$bookmarks = $this->user->bookmarks();
/** @var string */
$url = $this->fake('url');
Expand Down Expand Up @@ -368,20 +359,19 @@ public function testPickDoesNotSelectFromFollowedIfUrlInBookmarks(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(0, count($links));
}

public function testPickDoesNotSelectFromFollowedIfUrlInReadList(): void
public function testListFromFollowedCollectionsDoesNotSelectFromFollowedIfUrlInReadList(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
/** @var int */
$days_ago = $this->fake('numberBetween', 0, 7);
$published_at = \Minz\Time::ago($days_ago, 'days');
$news_picker = new NewsPicker($this->user);
$read_list = $this->user->readList();
/** @var string */
$url = $this->fake('url');
Expand Down Expand Up @@ -413,20 +403,19 @@ public function testPickDoesNotSelectFromFollowedIfUrlInReadList(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(0, count($links));
}

public function testPickDoesNotSelectFromFollowedIfUrlInNeverList(): void
public function testListFromFollowedCollectionsDoesNotSelectFromFollowedIfUrlInNeverList(): void
{
/** @var \DateTimeImmutable */
$now = $this->fake('dateTime');
$this->freeze($now);
/** @var int */
$days_ago = $this->fake('numberBetween', 0, 7);
$published_at = \Minz\Time::ago($days_ago, 'days');
$news_picker = new NewsPicker($this->user);
$never_list = $this->user->neverList();
/** @var string */
$url = $this->fake('url');
Expand Down Expand Up @@ -458,12 +447,12 @@ public function testPickDoesNotSelectFromFollowedIfUrlInNeverList(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(0, count($links));
}

public function testPickDoesNotSelectFromFollowedIfLinkIsOwned(): void
public function testListFromFollowedCollectionsDoesNotSelectFromFollowedIfLinkIsOwned(): void
{
// This is a very particular use case where the user got write access
// to a collection while he was following it (or followed it
Expand All @@ -476,7 +465,6 @@ public function testPickDoesNotSelectFromFollowedIfLinkIsOwned(): void
/** @var int */
$days_ago = $this->fake('numberBetween', 0, 7);
$published_at = \Minz\Time::ago($days_ago, 'days');
$news_picker = new NewsPicker($this->user);
$link = LinkFactory::create([
'user_id' => $this->user->id,
'is_hidden' => false,
Expand All @@ -496,7 +484,7 @@ public function testPickDoesNotSelectFromFollowedIfLinkIsOwned(): void
'collection_id' => $collection->id,
]);

$links = $news_picker->pick();
$links = models\Link::listFromFollowedCollections($this->user->id, max: 50);

$this->assertSame(0, count($links));
}
Expand Down

0 comments on commit b30022d

Please sign in to comment.