Skip to content

Commit 1a539df

Browse files
committed
Added Fixed #167
1 parent 23740fc commit 1a539df

File tree

7 files changed

+98
-11
lines changed

7 files changed

+98
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ composer.lock
66
.phpunit.result.cache
77
cghooks.lock
88

9+
.php-cs-fixer.cache

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ $user1->areFollowingEachOther($user2);
7676

7777
```php
7878
$user->followings;
79+
$user->approvedFollowings;
80+
$user->notApprovedFollowings;
7981
```
8082

8183
#### Get followers:
8284

8385
```php
8486
$user->followers;
87+
$user->approvedFollowers;
88+
$user->notApprovedFollowers;
8589
```
8690

8791
### Follow Requests
@@ -101,22 +105,31 @@ public function needsToApproveFollowRequests()
101105
```php
102106
// followings count
103107
$user->followings()->count();
108+
$user->approvedFollowings()->count();
109+
$user->notApprovedFollowings()->count();
104110

105111
// with query where
106112
$user->followings()->where('gender', 'female')->count();
107113

108114
// followers count
109115
$user->followers()->count();
116+
$user->approvedFollowers()->count();
117+
$user->notApprovedFollowers()->count();
110118
```
111119

112120
List with `*_count` attribute:
113121

114122
```php
115123
$users = User::withCount(['followings', 'followers'])->get();
124+
// or
125+
$users = User::withCount(['approvedFollowings', 'approvedFollowers'])->get();
116126

117127
foreach($users as $user) {
118128
// $user->followings_count;
119129
// $user->followers_count;
130+
// or
131+
// $user->approved_followings_count;
132+
// $user->approved_followers_count;
120133
}
121134
```
122135

src/Followable.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace Overtrue\LaravelFollow;
44

55
use Illuminate\Contracts\Pagination\Paginator;
6+
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Pagination\CursorPaginator;
79
use Illuminate\Pagination\LengthAwarePaginator;
8-
use Illuminate\Support\Collection;
9-
use Overtrue\LaravelFavorite\Traits\Favoriteable;
10+
use Illuminate\Support\Enumerable;
11+
use Illuminate\Support\LazyCollection;
1012

1113
/**
1214
* @property \Illuminate\Database\Eloquent\Collection $followings
@@ -29,6 +31,7 @@ public function needsToApproveFollowRequests(): bool
2931
*/
3032
public function follow($user): array
3133
{
34+
/** @var \Illuminate\Database\Eloquent\Model|\Overtrue\LaravelFollow\Followable $user */
3235
$isPending = $user->needsToApproveFollowRequests() ?: false;
3336

3437
$this->followings()->attach($user, [
@@ -80,7 +83,6 @@ public function hasRequestedToFollow($user): bool
8083
$user = $user->getKey();
8184
}
8285

83-
/* @var \Illuminate\Database\Eloquent\Model $this */
8486
if ($this->relationLoaded('followings')) {
8587
return $this->followings
8688
->where('pivot.accepted_at', '===', null)
@@ -102,7 +104,6 @@ public function isFollowing($user): bool
102104
$user = $user->getKey();
103105
}
104106

105-
/* @var \Illuminate\Database\Eloquent\Model $this */
106107
if ($this->relationLoaded('followings')) {
107108
return $this->followings
108109
->where('pivot.accepted_at', '!==', null)
@@ -124,7 +125,6 @@ public function isFollowedBy($user): bool
124125
$user = $user->getKey();
125126
}
126127

127-
/* @var \Illuminate\Database\Eloquent\Model $this */
128128
if ($this->relationLoaded('followers')) {
129129
return $this->followers
130130
->where('pivot.accepted_at', '!==', null)
@@ -163,7 +163,6 @@ public function scopeOrderByFollowersCountAsc($query)
163163

164164
public function followers(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
165165
{
166-
/* @var \Illuminate\Database\Eloquent\Model $this */
167166
return $this->belongsToMany(
168167
__CLASS__,
169168
\config('follow.relation_table', 'user_follower'),
@@ -174,7 +173,6 @@ public function followers(): \Illuminate\Database\Eloquent\Relations\BelongsToMa
174173

175174
public function followings(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
176175
{
177-
/* @var \Illuminate\Database\Eloquent\Model $this */
178176
return $this->belongsToMany(
179177
__CLASS__,
180178
\config('follow.relation_table', 'user_follower'),
@@ -183,6 +181,26 @@ public function followings(): \Illuminate\Database\Eloquent\Relations\BelongsToM
183181
)->withPivot('accepted_at')->withTimestamps()->using(UserFollower::class);
184182
}
185183

184+
public function scopeApprovedFollowers(Builder $query): \Illuminate\Database\Eloquent\Relations\BelongsToMany
185+
{
186+
return $this->followers()->wherePivotNotNull('accepted_at');
187+
}
188+
189+
public function scopeNotApprovedFollowers(Builder $query): \Illuminate\Database\Eloquent\Relations\BelongsToMany
190+
{
191+
return $this->followers()->wherePivotNull('accepted_at');
192+
}
193+
194+
public function scopeApprovedFollowings(Builder $query): \Illuminate\Database\Eloquent\Relations\BelongsToMany
195+
{
196+
return $this->followings()->wherePivotNotNull('accepted_at');
197+
}
198+
199+
public function scopeNotApprovedFollowings(Builder $query): \Illuminate\Database\Eloquent\Relations\BelongsToMany
200+
{
201+
return $this->followings()->wherePivotNull('accepted_at');
202+
}
203+
186204
public function attachFollowStatus($followables, callable $resolver = null)
187205
{
188206
$returnFirst = false;
@@ -196,14 +214,18 @@ public function attachFollowStatus($followables, callable $resolver = null)
196214
$followables = $followables->getCollection();
197215
break;
198216
case $followables instanceof Paginator:
217+
case $followables instanceof CursorPaginator:
199218
$followables = \collect($followables->items());
200219
break;
220+
case $followables instanceof LazyCollection:
221+
$followables = \collect(\iterator_to_array($followables->getIterator()));
222+
break;
201223
case \is_array($followables):
202224
$followables = \collect($followables);
203225
break;
204226
}
205227

206-
\abort_if(!($followables instanceof Collection), 422, 'Invalid $followables type.');
228+
\abort_if(!($followables instanceof Enumerable), 422, 'Invalid $followables type.');
207229

208230
$followed = UserFollower::where('follower_id', $this->getKey())->get();
209231

src/UserFollower.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Overtrue\LaravelFollow;
44

5+
use Illuminate\Database\Eloquent\Builder;
56
use Illuminate\Database\Eloquent\Relations\Pivot;
67
use Overtrue\LaravelFollow\Events\Followed;
78
use Overtrue\LaravelFollow\Events\Unfollowed;

tests/FeatureTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
namespace Tests;
44

55
use Illuminate\Support\Carbon;
6-
use Illuminate\Support\Facades\DB;
76
use Illuminate\Support\Facades\Event;
87
use Overtrue\LaravelFollow\Events\Followed;
98
use Overtrue\LaravelFollow\Events\Unfollowed;
10-
use Overtrue\LaravelFollow\UserFollower;
119

1210
class FeatureTest extends TestCase
1311
{
@@ -214,6 +212,33 @@ function () use ($user1, $users) {
214212
$this->assertTrue($users[2]->has_followed);
215213
$this->assertTrue($users[3]->has_followed);
216214

215+
// paginator
216+
$users = User::paginate();
217+
$user1->attachFollowStatus($users);
218+
219+
$users = $users->toArray()['data'];
220+
$this->assertFalse($users[0]['has_followed']);
221+
$this->assertTrue($users[1]['has_followed']);
222+
$this->assertTrue($users[2]['has_followed']);
223+
$this->assertTrue($users[3]['has_followed']);
224+
225+
// cursor paginator
226+
$users = User::cursorPaginate();
227+
$user1->attachFollowStatus($users);
228+
229+
$users = $users->toArray()['data'];
230+
$this->assertFalse($users[0]['has_followed']);
231+
$this->assertTrue($users[1]['has_followed']);
232+
$this->assertTrue($users[2]['has_followed']);
233+
$this->assertTrue($users[3]['has_followed']);
234+
235+
// cursor
236+
$users = User::cursor();
237+
$users = $user1->attachFollowStatus($users)->toArray();
238+
$this->assertFalse($users[0]['has_followed']);
239+
$this->assertTrue($users[1]['has_followed']);
240+
$this->assertTrue($users[2]['has_followed']);
241+
$this->assertTrue($users[3]['has_followed']);
217242

218243
// with custom resolver
219244
$users = \collect(['creator' => $user2], ['creator' => $user3], ['creator' => $user4]);

tests/PrivacyTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,29 @@ public function test_following_private_user_sets_request_pending_with_eager_load
6464
$this->assertTrue($user1->hasRequestedToFollow($user2));
6565
$this->assertFalse($user2->isFollowedBy($user1));
6666
}
67+
68+
public function test_approved_scopes()
69+
{
70+
$user1 = User::create(['name' => 'user1']);
71+
$user2 = User::create(['name' => 'user2', 'private' => true]);
72+
73+
$user1->follow($user2);
74+
75+
$this->assertCount(1, $user1->followings()->get());
76+
$this->assertCount(0, $user1->approvedFollowings()->get());
77+
$this->assertCount(1, $user1->notApprovedFollowings()->get());
78+
79+
$this->assertCount(1, $user2->followers()->get());
80+
$this->assertCount(0, $user2->approvedFollowers()->get());
81+
$this->assertCount(1, $user2->notApprovedFollowers()->get());
82+
83+
$user2->acceptFollowRequestFrom($user1);
84+
85+
$this->assertCount(1, $user1->followings()->get());
86+
$this->assertCount(1, $user1->approvedFollowings()->get());
87+
$this->assertCount(0, $user1->notApprovedFollowings()->get());
88+
$this->assertCount(1, $user2->followers()->get());
89+
$this->assertCount(1, $user2->approvedFollowers()->get());
90+
$this->assertCount(0, $user2->notApprovedFollowers()->get());
91+
}
6792
}

tests/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class User extends Model
1818
/**
1919
* @return bool
2020
*/
21-
public function needsToApproveFollowRequests()
21+
public function needsToApproveFollowRequests(): bool
2222
{
2323
return $this->private ?? false;
2424
}

0 commit comments

Comments
 (0)