Skip to content

Commit

Permalink
Add get latest followers
Browse files Browse the repository at this point in the history
  • Loading branch information
timgavin committed Oct 27, 2023
1 parent f3711b8 commit ac17f83
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/LaravelFollow.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ public function getFollowers(): \Illuminate\Database\Eloquent\Collection
->get();
}

/**
* Returns the users who are following a user.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getLatestFollowers($limit = 5): \Illuminate\Database\Eloquent\Collection
{
return Follow::where('following_id', $this->id)
->with('followers')
->latest()
->limit($limit)
->get();
}

/**
* Returns IDs of the users a user is following.
*
Expand Down Expand Up @@ -172,10 +186,10 @@ public function cacheFollowing(mixed $duration = null): void
{
$duration ?? Carbon::now()->addDay();

cache()->forget('following.' . auth()->id());
cache()->forget('following.' . $this->id);

cache()->remember('following.' . auth()->id(), $duration, function () {
return auth()->user()->getFollowingIds();
cache()->remember('following.' . $this->id, $duration, function () {
return $this->getFollowingIds();
});
}

Expand All @@ -189,10 +203,10 @@ public function cacheFollowers(mixed $duration = null): void
{
$duration ?? Carbon::now()->addDay();

cache()->forget('followers.' . auth()->id());
cache()->forget('followers.' . $this->id);

cache()->remember('followers.' . auth()->id(), $duration, function () {
return auth()->user()->getFollowersIds();
cache()->remember('followers.' . $this->id, $duration, function () {
return $this->getFollowersIds();
});
}

Expand All @@ -205,7 +219,7 @@ public function cacheFollowers(mixed $duration = null): void
*/
public function getFollowingCache(): array
{
return cache()->get('following.' . auth()->id()) ?? [];
return cache()->get('following.' . $this->id) ?? [];
}

/**
Expand All @@ -217,7 +231,7 @@ public function getFollowingCache(): array
*/
public function getFollowersCache(): array
{
return cache()->get('followers.' . auth()->id()) ?? [];
return cache()->get('followers.' . $this->id) ?? [];
}

/**
Expand All @@ -227,7 +241,7 @@ public function getFollowersCache(): array
*/
public function clearFollowingCache(): void
{
cache()->forget('following.' . auth()->id());
cache()->forget('following.' . $this->id);
}

/**
Expand All @@ -237,6 +251,6 @@ public function clearFollowingCache(): void
*/
public function clearFollowersCache(): void
{
cache()->forget('followers.' . auth()->id());
cache()->forget('followers.' . $this->id);
}
}
19 changes: 19 additions & 0 deletions tests/FollowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ public function it_gets_the_users_who_are_following_a_user()
}
}

/** @test */
public function it_gets_the_latest_users_who_are_following_a_user()
{
$user1 = User::create();
$user2 = User::create();

$user2->follow($user1);

$followedBy = $user1->getLatestFollowers(1);

foreach ($followedBy as $item) {
if ($item->following->id === 1) {
$this->assertTrue(true);
} else {
$this->fail();
}
}
}

/** @test */
public function it_gets_the_ids_of_users_who_are_following_a_user()
{
Expand Down

0 comments on commit ac17f83

Please sign in to comment.