Skip to content

Commit

Permalink
Refactor follow requests
Browse files Browse the repository at this point in the history
According to comments in PR overtrue#138

Resolves: overtrue#101
  • Loading branch information
michavie committed Apr 17, 2020
1 parent 577534c commit a805baa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ $user->followers;

### Follow Requests

If you would like to have some follow requests to need to be accepted by the user being followed, simply override the **isPrivateUser()** method in the model that uses the **Followable** trait with your custom logic:
If you would like to have some follow requests to need to be accepted by the user being followed, simply override the **needsToApproveFollowRequests()** method in the model that uses the **Followable** trait with your custom logic:

```php
public function isPrivateUser()
public function needsToApproveFollowRequests()
{
// Your custom logic here
return (bool) $this->private;
Expand Down
30 changes: 8 additions & 22 deletions src/Followable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait Followable
/**
* @return bool
*/
public function isPrivateUser()
public function needsToApproveFollowRequests()
{
return false;
}
Expand All @@ -36,7 +36,7 @@ public function isPrivateUser()
*/
public function follow($user)
{
$isPending = $user->isPrivateUser() ?: false;
$isPending = $user->needsToApproveFollowRequests() ?: false;

$this->followings()->attach($user, [
'accepted_at' => $isPending ? null : now()
Expand Down Expand Up @@ -67,10 +67,6 @@ public function toggleFollow($user)
*/
public function rejectFollowRequestFrom($user)
{
if ($user instanceof Model) {
$user = $user->getKey();
}

$this->followers()->detach($user);
}

Expand All @@ -79,10 +75,6 @@ public function rejectFollowRequestFrom($user)
*/
public function acceptFollowRequestFrom($user)
{
if ($user instanceof Model) {
$user = $user->getKey();
}

$this->followers()->updateExistingPivot($user, ['accepted_at' => now()]);
}

Expand All @@ -98,14 +90,12 @@ public function hasRequestedToFollow(Model $user): bool
/* @var \Illuminate\Database\Eloquent\Model $this */
if ($this->relationLoaded('followings')) {
return $this->followings
->filter(function ($following) {
return $following->pivot->accepted_at === null;
})
->whereNull('pivot.accepted_at')
->contains($user);
}

return $this->followings()
->whereNull('accepted_at')
->wherePivot('accepted_at', null)
->where($this->getQualifiedKeyName(), $user)
->exists();
}
Expand All @@ -124,14 +114,12 @@ public function isFollowing($user)
/* @var \Illuminate\Database\Eloquent\Model $this */
if ($this->relationLoaded('followings')) {
return $this->followings
->filter(function ($following) {
return $following->pivot->accepted_at !== null;
})
->whereNotNull('pivot.accepted_at')
->contains($user);
}

return $this->followings()
->whereNotNull('accepted_at')
->wherePivot('accepted_at', '!=', null)
->where($this->getQualifiedKeyName(), $user)
->exists();
}
Expand All @@ -150,14 +138,12 @@ public function isFollowedBy($user)
/* @var \Illuminate\Database\Eloquent\Model $this */
if ($this->relationLoaded('followers')) {
return $this->followers
->filter(function ($following) {
return $following->pivot->accepted_at !== null;
})
->whereNotNull('pivot.accepted_at')
->contains($user);
}

return $this->followers()
->whereNotNull('accepted_at')
->wherePivot('accepted_at', '!=', null)
->where($this->getQualifiedKeyName(), $user)
->exists();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class User extends Model
/**
* @return bool
*/
public function isPrivateUser()
public function needsToApproveFollowRequests()
{
return $this->private ?? false;
}
Expand Down

0 comments on commit a805baa

Please sign in to comment.