Skip to content

Commit

Permalink
Scope to filter unliked content (#15)
Browse files Browse the repository at this point in the history
* Adding filter for unliked models.
  • Loading branch information
rennokki authored Jul 21, 2018
1 parent cdff7c2 commit e2d0cf6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ $user->follows($friend); // alias

### Filtering followed/unfollowed models
You can filter your queries using scopes provided by this package to filter followed models, if you plan, for example, to create a news feed, or if your user wants to find new people, you can filter the unfollowed models.
Make sure your model uses the `Rennokki\Befriended\Scopes\CanFilterFollowingModels` trait for filtering following models and/or `Rennokki\Befriended\Scopes\CanFilterUnfollowedModels` for filtering unfollowed models.

```php
$bob = User::where('username', 'john')->first();
$alice = User::where('username', 'alice')->first();
Expand Down Expand Up @@ -171,6 +173,8 @@ $user->blocks($page); // alias to isBlocking

### Filtering blocked models
Blocking scopes provided takes away from the query the models that are blocked. Useful to stop showing content when someone blocks people.
Make sure your model uses the `Rennokki\Befriended\Scopes\CanFilterBlockedModels` trait.

```php
$bob = User::where('username', 'john')->first();
$alice = User::where('username', 'alice')->first();
Expand Down Expand Up @@ -229,3 +233,18 @@ $user->likers(Page::class); // Pages that like this user.
$user->isLiking($page);
$user->likes($page); // alias to isLiking
```

### Filtering liked content
Filtering liked content can make showing content easier. For example, showing in the news feed posts that weren't liked by an user can be helpful.
The model you're querying from must use the `Rennokki\Befriended\Scopes\CanFilterUnlikedModels` trait.

Let's suppose there are 10 pages in the database.
```php
$bob = User::where('username', 'john')->first();
$page = Page::find(1);

Page::filterUnlikedFor($bob)->get(); // You will get 10 results.

$bob->like($page);
Page::filterUnlikedFor($bob)->get(); // You will get only 9 results.
```
13 changes: 13 additions & 0 deletions src/Scopes/CanFilterUnlikedModels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rennokki\Befriended\Scopes;

trait CanFilterUnlikedModels
{
public function scopeFilterUnlikedFor($query, $model)
{
$likedIds = collect($model->liking($this->getMorphClass())->get()->toArray())->pluck($model->getKeyName())->all();

return $query->whereNotIn($this->getKeyName(), $likedIds);
}
}
35 changes: 35 additions & 0 deletions tests/CanFilterLikingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rennokki\Befriended\Test;

use Rennokki\Befriended\Test\Models\Page;

class CanFilterLikingTest extends TestCase
{
protected $user;
protected $user2;
protected $user3;
protected $page;

public function setUp()
{
parent::setUp();

$this->user = factory(\Rennokki\Befriended\Test\Models\User::class)->create();
$this->user2 = factory(\Rennokki\Befriended\Test\Models\User::class)->create();

factory(\Rennokki\Befriended\Test\Models\Page::class, 10)->create();
}

public function testCanFilterNonLikes()
{
$this->assertEquals(Page::filterUnlikedFor($this->user)->count(), 10);
$this->assertEquals(Page::filterUnlikedFor($this->user2)->count(), 10);

$this->user->like(Page::find(1));
$this->user->like(Page::find(2));

$this->assertEquals(Page::filterUnlikedFor($this->user)->count(), 8);
$this->assertEquals(Page::filterUnlikedFor($this->user2)->count(), 10);
}
}
3 changes: 2 additions & 1 deletion tests/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
use Rennokki\Befriended\Traits\CanBeBlocked;
use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Scopes\CanFilterBlockedModels;
use Rennokki\Befriended\Scopes\CanFilterUnlikedModels;
use Rennokki\Befriended\Scopes\CanFilterFollowingModels;
use Rennokki\Befriended\Scopes\CanFilterUnfollowedModels;

class Page extends Model implements Following, Blocking, Liking
{
use CanFollow, CanBeFollowed, CanBlock, CanBeBlocked, CanLike, CanBeLiked, CanFilterFollowingModels, CanFilterBlockedModels,
CanFilterUnfollowedModels;
CanFilterUnfollowedModels, CanFilterUnlikedModels;

protected $fillable = [
'name',
Expand Down
3 changes: 2 additions & 1 deletion tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
use Rennokki\Befriended\Traits\CanBeBlocked;
use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Scopes\CanFilterBlockedModels;
use Rennokki\Befriended\Scopes\CanFilterUnlikedModels;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Rennokki\Befriended\Scopes\CanFilterFollowingModels;
use Rennokki\Befriended\Scopes\CanFilterUnfollowedModels;

class User extends Authenticatable implements Following, Blocking, Liking
{
use CanFollow, CanBeFollowed, CanBlock, CanBeBlocked, CanLike, CanBeLiked, CanFilterFollowingModels, CanFilterBlockedModels,
CanFilterUnfollowedModels;
CanFilterUnfollowedModels, CanFilterUnlikedModels;

protected $fillable = [
'name', 'email', 'password',
Expand Down

0 comments on commit e2d0cf6

Please sign in to comment.