Skip to content

Commit

Permalink
chore: begin refactoring, add timestamps to ip table
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 9, 2023
1 parent ab9765f commit 5bd1a95
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 60 deletions.
2 changes: 2 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Flarum\Post\Post;
use Flarum\Settings\Event\Saving;
use FoF\GeoIP\Api\GeoIP;
use FoF\GeoIP\Api\Serializer\IPInfoSerializer;
use FoF\GeoIP\Model\IPInfo;
use FoF\GeoIP\Repositories\GeoIPRepository;

return [
Expand Down
2 changes: 0 additions & 2 deletions js/src/forum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ export { default as extend } from './extend';
app.initializers.add('fof/geoip', () => {
extendPostMeta();
extendBanIPModal();

// This cannot be enabled until https://github.com/flarum/framework/pull/3888 is merged and released
extendAccessTokensList();
});
8 changes: 8 additions & 0 deletions js/src/forum/models/IPInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ export default class IPInfo extends Model {
error() {
return Model.attribute<string>('error').call(this);
}

createdAt() {
return Model.attribute('createdAt', Model.transformDate).call(this);
}

updatedAt() {
return Model.attribute('updatedAt', Model.transformDate).call(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Flarum\Database\Migration;

return Migration::addColumns('ip_info', [
'created_at' => ['timestamp', 'nullable' => false, 'useCurrent' => true],
'updated_at' => ['datetime', 'nullable' => true],
]);
59 changes: 8 additions & 51 deletions src/Api/Controller/ShowIpInfoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
use Flarum\Api\Controller\AbstractShowController;
use Flarum\Http\RequestUtil;
use FoF\GeoIP\Api\GeoIP;
use FoF\GeoIP\IPInfo;
use FoF\GeoIP\IPInfoSerializer;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use FoF\GeoIP\Api\Serializer\IPInfoSerializer;
use FoF\GeoIP\Command\FetchIPInfo;
use FoF\GeoIP\Model\IPInfo;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
Expand All @@ -25,7 +26,7 @@ class ShowIpInfoController extends AbstractShowController
{
public $serializer = IPInfoSerializer::class;

public function __construct(protected GeoIP $geoIP)
public function __construct(protected GeoIP $geoIP, protected Dispatcher $bus)
{
}

Expand All @@ -39,55 +40,11 @@ public function __construct(protected GeoIP $geoIP)
*/
public function data(ServerRequestInterface $request, Document $document): IPInfo
{
RequestUtil::getActor($request)->assertRegistered();
$actor = RequestUtil::getActor($request);
$actor->assertRegistered();

$ip = urldecode(Arr::get($request->getQueryParams(), 'ip'));

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

if (!$ipInfo) {
$ipInfo = $this->lookup($ip);
$this->saveIpInfo($ip, $ipInfo);
}

return $ipInfo;
}

/**
* Lookup IP information using the GeoIP service.
*
* @param string $ip
*
* @throws ModelNotFoundException
*
* @return array
*/
protected function lookup(string $ip): array
{
$response = $this->geoIP->get($ip);

if (!$response || $response->fake) {
throw new ModelNotFoundException("Unable to fetch IP information for IP: {$ip}");
}

return $response->toJson();
}

/**
* Save the IP information to the database.
*
* @param string $ip
* @param array $data
*
* @return IPInfo
*/
protected function saveIpInfo(string $ip, array $data): IPInfo
{
$ipInfo = new IPInfo();
$ipInfo->address = $ip;
$ipInfo->fill($data);
$ipInfo->save();

return $ipInfo;
return $this->bus->dispatch(new FetchIPInfo($ip, $actor));
}
}
2 changes: 1 addition & 1 deletion src/Api/GeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Carbon\Carbon;
use Flarum\Settings\SettingsRepositoryInterface;
use FoF\GeoIP\IPInfo;
use FoF\GeoIP\Model\IPInfo;
use FoF\GeoIP\Traits\HandlesGeoIPErrors;

class GeoIP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
* file that was distributed with this source code.
*/

namespace FoF\GeoIP;
namespace FoF\GeoIP\Api\Serializer;

use Flarum\Api\Serializer\AbstractSerializer;
use FoF\GeoIP\Model\IPInfo;
use InvalidArgumentException;

class IPInfoSerializer extends AbstractSerializer
Expand Down Expand Up @@ -39,6 +40,8 @@ protected function getDefaultAttributes($ip): array
'threatLevel' => $ip->threat_level,
'threatType' => $ip->threat_types,
'error' => $ip->error,
'createdAt' => $this->formatDate($ip->created_at),
'updatedAt' => $this->formatDate($ip->updated_at),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Api/Services/BaseGeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace FoF\GeoIP\Api\Services;

use Flarum\Settings\SettingsRepositoryInterface;
use FoF\GeoIP\Api\ServiceInterface;
use FoF\GeoIP\Api\ServiceResponse;
use FoF\GeoIP\Concerns\ServiceInterface;
use GuzzleHttp\Client;
use Psr\Log\LoggerInterface;

Expand Down
14 changes: 14 additions & 0 deletions src/Command/FetchIPInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace FoF\GeoIP\Command;

use Flarum\User\User;

class FetchIPInfo
{
public function __construct(
public string $ip,
public ?User $actor = null,
public bool $refresh = false
) {}
}
35 changes: 35 additions & 0 deletions src/Command/FetchIPInfoHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace FoF\GeoIP\Command;

use FoF\GeoIP\Api\GeoIP;
use FoF\GeoIP\Model\IPInfo;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class FetchIPInfoHandler
{
public function __construct(
protected GeoIP $geoip
) {}

public function handle(FetchIPInfo $command): IPInfo
{
$ipInfo = IPInfo::query()->firstOrNew(['address' => $command->ip]);

if (!$ipInfo->exists || $command->refresh) {
$response = $this->geoip->get($command->ip);

if (!$response || $response->fake) {
throw new ModelNotFoundException("Unable to fetch IP information for IP: {$command->ip}");
}

if ($response) {
$ipInfo->address = $command->ip;
$ipInfo->fill($response->toJSON());
$ipInfo->save();
}
}

return $ipInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace FoF\GeoIP\Api;
namespace FoF\GeoIP\Concerns;

interface ServiceInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/RetrieveIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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

Expand Down
7 changes: 6 additions & 1 deletion src/IPInfo.php → src/Model/IPInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* file that was distributed with this source code.
*/

namespace FoF\GeoIP;
namespace FoF\GeoIP\Model;

use Carbon\Carbon;
use Flarum\Database\AbstractModel;

/**
Expand All @@ -22,6 +23,8 @@
* @property string|null $threat_level
* @property string|null $threat_types
* @property string|null $error
* @property Carbon $created_at
* @property Carbon $updated_at
*/
class IPInfo extends AbstractModel
{
Expand All @@ -33,4 +36,6 @@ class IPInfo extends AbstractModel
'threat_level', 'threat_types',
'error',
];

public $timestamps = true;
}
2 changes: 1 addition & 1 deletion src/Repositories/GeoIPRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use Flarum\Post\Post;
use FoF\GeoIP\Api\GeoIP;
use FoF\GeoIP\IPInfo;
use FoF\GeoIP\Jobs\RetrieveIP;
use FoF\GeoIP\Model\IPInfo;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Support\Arr;

Expand Down

0 comments on commit 5bd1a95

Please sign in to comment.