-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: begin refactoring, add timestamps to ip table
- Loading branch information
Showing
14 changed files
with
90 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
migrations/2023_11_09_000000_add_timestamps_to_ip_info_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters