Skip to content

Commit

Permalink
centralize ip retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 10, 2023
1 parent 1d8207e commit f147e4e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 43 deletions.
14 changes: 2 additions & 12 deletions src/Api/GeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,15 @@ public function get(string $ip)
/**
* @param array $ips
*
* @return ServiceResponse[]|void
* @return ServiceResponse[]
*/
public function getBatch(array $ips)
{
return $this->getService()->getBatch($ips);
}

public function getSaved(string $ip)
public function getSaved(string $ip): ?IPInfo
{
$response = $this->checkErrors();

if ($response) {
$ipInfo = new IPInfo();
$ipInfo->address = $ip;
$ipInfo->fill($response->toJSON());

return $ipInfo;
}

return IPInfo::where('address', $ip)->first();
}

Expand Down
34 changes: 6 additions & 28 deletions src/Jobs/RetrieveIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
namespace FoF\GeoIP\Jobs;

use Flarum\Queue\AbstractJob;
use FoF\GeoIP\Api\GeoIP;
use FoF\GeoIP\Command\FetchIPInfo;
use FoF\GeoIP\Model\IPInfo;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Arr;

Expand All @@ -29,7 +30,7 @@ public function __construct(protected string $ip)
self::$queued[] = $ip;
}

public function handle(GeoIP $geoIP, Repository $cache): void
public function handle(Repository $cache, Dispatcher $bus): void
{
$ip = $this->ip;
$cacheKey = "fof-geoip.retrieving.$ip";
Expand All @@ -44,33 +45,10 @@ public function handle(GeoIP $geoIP, Repository $cache): void
static::$retrieving[] = $ip;
$cache->add($cacheKey, true, 60 * 60);

$response = $geoIP->get($ip);
/** @var IPInfo $ipInfo */
$ipInfo = $bus->dispatch(new FetchIPInfo($ip));

if ($response) {
$ipInfo = IPInfo::where('address', $ip)->first();

if (!$ipInfo) {
$ipInfo = new IPInfo();
$ipInfo->address = $ip;
$ipInfo->fill($response->toJson());

// If response is fake, it means an error occurred that was logged to the admin dashboard.
// We don't want to save fake responses.
if (!$response->fake) {
$ipInfo->save();
}
}

if (!$response->fake) {
// If using sync queue driver, this will be immediately available.
// If using another driver (eg. redis), it will remember this IP has been retrieved until the process ends.
static::$retrieved[$ip] = $ipInfo;
}
}

// Only remove from cache if we didn't get a fake response
if (!$response || !$response->fake) {
// Remove from retrieving list and cache
if ($ipInfo->exists) {
static::$retrieving = array_diff(static::$retrieving, [$ip]);
$cache->forget("fof-geoip.retrieving.$ip");
}
Expand Down
6 changes: 3 additions & 3 deletions src/Repositories/GeoIPRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public function __construct(protected GeoIP $geoIP, protected Queue $queue)
/**
* @param string|null $ip
*
* @return IPInfo|void
* @return IPInfo|null
*/
public function get($ip)
public function get(?string $ip): ?IPInfo
{
if (!$ip) {
return;
return null;
}

return $this->geoIP->getSaved($ip);
Expand Down

0 comments on commit f147e4e

Please sign in to comment.