Added Source Repository Class

Created a class to handle source data and methodology, and to offload
actions from service classes to make them easier to manage
This commit is contained in:
ro 2024-02-19 13:30:00 -06:00
parent 3edd6e5521
commit f96707f256
4 changed files with 117 additions and 94 deletions

View file

@ -3,25 +3,29 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\Source;
use App\Repositories\LocationRepository; use App\Repositories\LocationRepository;
use App\Repositories\SourceRepository;
class FrontIndexController extends Controller class FrontIndexController extends Controller
{ {
protected $locationRepository; protected $location;
protected $source;
public function __construct(LocationRepository $locationRepository) public function __construct(
{ LocationRepository $locationRepository,
$this->locationRepository = $locationRepository; SourceRepository $sourceRepository
) {
$this->location = $locationRepository;
$this->source = $sourceRepository;
} }
public function start() public function start()
{ {
return view('front.index', [ return view('front.index', [
'count' => count($this->locationRepository->getActiveLocations()), 'count' => count($this->location->getActiveLocations()),
'sources' => count(Source::where("active", true)->get()), 'sources' => count($this->source->getActive()),
'recent' => $this->locationRepository->getRecent(), 'recent' => $this->location->getRecent(),
'latest_date' => $this->locationRepository->getRecent()[0]->updated_at->format('Y M d'), 'latest_date' => $this->location->getRecent()[0]->updated_at->format('Y M d'),
'title' => "The Bad Space" 'title' => "The Bad Space"
]); ]);
} }
@ -29,19 +33,19 @@ class FrontIndexController extends Controller
public function indexSearch(Request $request) public function indexSearch(Request $request)
{ {
return view('front.index', [ return view('front.index', [
'count' => count($this->locationRepository->getActiveLocations()), 'count' => count($this->location->getActiveLocations()),
'sources' => count(Source::where("active", true)->get()), 'sources' => count($this->source->getActive()),
'recent' => $this->locationRepository->getRecent(), 'recent' => $this->location->getRecent(),
'results' => $this->locationRepository->search($request), 'results' => $this->location->search($request),
'terms' => $request->index_search, 'terms' => $request->index_search,
'latest_date' => $this->locationRepository->getRecent()[0]->updated_at->format('Y M d'), 'latest_date' => $this->location->getRecent()[0]->updated_at->format('Y M d'),
'title' => "Search Results", 'title' => "Search Results",
]); ]);
} }
public function about() public function about()
{ {
$sources = Source::where("active", true)->get(); $sources = $this->source->getActive();
return view('front.about', [ return view('front.about', [
'title' => "ABOUT", 'title' => "ABOUT",
'sources' => $sources 'sources' => $sources
@ -57,8 +61,8 @@ class FrontIndexController extends Controller
public function location(string $uuid = "1") public function location(string $uuid = "1")
{ {
$location = $this->locationRepository->getLocation($uuid); $location = $this->location->getLocation($uuid);
$sources = Source::where("active", true)->get(); $sources = $this->source->getActive();
$name = "NO LOCATION FOUND"; $name = "NO LOCATION FOUND";
if ($location) { if ($location) {
$name = $location->name; $name = $location->name;
@ -75,11 +79,11 @@ class FrontIndexController extends Controller
public function listings(int $pageNum = 1) public function listings(int $pageNum = 1)
{ {
$listing = $this->locationRepository->getPage($pageNum); $listing = $this->location->getPage($pageNum);
return view('front.listing', [ return view('front.listing', [
'title' => "Listings", 'title' => "Listings",
'sources' => count(Source::where("active", true)->get()), 'sources' => count($this->source->getActive()),
"totalPages" => $listing[1], "totalPages" => $listing[1],
"prev" => $listing[2], "prev" => $listing[2],
"next" => $listing[3], "next" => $listing[3],

View file

@ -4,9 +4,11 @@ namespace App\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use App\Repositories\LocationRepository; use App\Repositories\LocationRepository;
use App\Repositories\SourceRepository;
use App\Services\UpdateService; use App\Services\UpdateService;
use App\Services\MaintenanceService; use App\Services\MaintenanceService;
use App\Models\Location; use App\Models\Location;
use App\Models\Source;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@ -19,8 +21,15 @@ class AppServiceProvider extends ServiceProvider
return new LocationRepository(new Location()); return new LocationRepository(new Location());
}); });
$this->app->bind(SourceRepository::class, function ($app) {
return new SourceRepository(new Source());
});
$this->app->bind(UpdateService::class, function ($app) { $this->app->bind(UpdateService::class, function ($app) {
return new UpdateService(new LocationRepository(new Location())); return new UpdateService(
new LocationRepository(new Location()),
new SourceRepository(new Source())
);
}); });
$this->app->bind(MaintenanceService::class, function ($app) { $this->app->bind(MaintenanceService::class, function ($app) {

View file

@ -0,0 +1,70 @@
<?php
namespace App\Repositories;
use App\Models\Source;
use Carbon\Carbon;
use GuzzleHttp\Exception\ConnectException;
class SourceRepository
{
protected $source;
public function __construct(Source $source)
{
$this->source = $source;
}
public function getActive()
{
return $this->source::where("active", true)->get();
}
public function updateSourceData()
{
$sources = $this->getActive();
$missing = [];
$checked = [];
//checks all the sources to refresh data
foreach ($sources as $source) {
$result = [];
if ($source['type'] == 'mastodon') {
if ($source['token'] == null) {
try {
$result = \Mastodon::domain('https://' . $source['url'])
->get('/instance/domain_blocks');
array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
array_push($missing, ['source' => $source->url]);
}
} else {
try {
$result = \Mastodon::domain('https://' . $source['url'])
->token($source['token'])
->get('/instance/domain_blocks');
array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
}
}
} elseif ($source['type'] == 'custom' && $source['format'] == 'csv') {
try {
$denylist = array_map('str_getcsv', file('https://' . $source['url']));
foreach ($denylist as $item) {
array_push($result, [
'domain' => $item[0],
'severity' => $item[1],
'comment' => $item[2]]);
}
array_push($checked, ['source' => $source->url]);
} catch (Exception $e) {
array_push($missing, ['source' => $source->url]);
}
}
$source->list_data = json_encode($result);
$source->last_updated = Carbon::now();
$source->save();
}
return ['checked' => $checked, 'notchecked' => $missing];
}
}

View file

@ -4,68 +4,27 @@ namespace App\Services;
use App\Models\Location; use App\Models\Location;
use App\Repositories\LocationRepository; use App\Repositories\LocationRepository;
use App\Models\Source; use App\Repositories\SourceRepository;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Carbon\Carbon;
use GuzzleHttp\Exception\ConnectException;
class UpdateService class UpdateService
{ {
private $limit = 15; protected $location;
protected $model; protected $source;
protected $locationRepository;
public function __construct(LocationRepository $locationRepository) public function __construct(
{ LocationRepository $locationRepository,
$this->locationRepository = $locationRepository; SourceRepository $sourceRepository
) {
$this->location = $locationRepository;
$this->source = $sourceRepository;
} }
public function data() public function data()
{ {
$sources = Source::where("active", true)->get(); $response = $this->source->updateSourceData();
$missing = []; return count($response['checked']) . ' SOURCES UPDATED - ' .
$checked = []; count($response['notchecked']) . ' SOURCES NOT CHECKED';
//checks source url to make sure they valid
foreach ($sources as $source) {
$result = [];
if ($source['type'] == 'mastodon') {
if ($source['token'] == null) {
try {
$result = \Mastodon::domain('https://' . $source['url'])
->get('/instance/domain_blocks');
array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
array_push($missing, ['source' => $source->url]);
}
} else {
try {
$result = \Mastodon::domain('https://' . $source['url'])
->token($source['token'])
->get('/instance/domain_blocks');
array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
}
}
} elseif ($source['type'] == 'custom' && $source['format'] == 'csv') {
try {
$denylist = array_map('str_getcsv', file('https://' . $source['url']));
foreach ($denylist as $item) {
array_push($result, [
'domain' => $item[0],
'severity' => $item[1],
'comment' => $item[2]]);
}
array_push($checked, ['source' => $source->url]);
} catch (Exception $e) {
array_push($missing, ['source' => $source->url]);
}
}
$source->list_data = json_encode($result);
$source->last_updated = Carbon::now();
$source->save();
}
return count($checked) . ' SOURCES UPDATED - ' . count($missing) . ' SOURCES NOT CHECKED';
} }
public function list() public function list()
@ -74,7 +33,7 @@ class UpdateService
$fresh = 0; $fresh = 0;
$unified = []; $unified = [];
$sources = Source::where("active", true)->get(); $sources = $this->source->getActive();
foreach ($sources as $source) { foreach ($sources as $source) {
//$listData = json_decode(); //$listData = json_decode();
@ -116,7 +75,7 @@ class UpdateService
} }
foreach ($unified as $item) { foreach ($unified as $item) {
$location = $this->locationRepository->getLocation($item['url']); $location = $this->location->getLocation($item['url']);
if ($location) { if ($location) {
++$duplicates; ++$duplicates;
//update block count for existing item //update block count for existing item
@ -173,23 +132,4 @@ class UpdateService
//TODO: Send update post to TBS social account //TODO: Send update post to TBS social account
return $duplicates . ' LOCATIONS UPDATED | ' . $fresh . ' NEW LOCATIONS CREATED'; return $duplicates . ' LOCATIONS UPDATED | ' . $fresh . ' NEW LOCATIONS CREATED';
} }
public function urlExists($url)
{
// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);
// Validate URI
if (
filter_var($url, FILTER_VALIDATE_URL) === false || // check only for http/https schemes.
!in_array(
strtolower(parse_url($url, PHP_URL_SCHEME)),
["http", "https"],
true
)
) {
return false;
} // Check that URL exists
$file_headers = @get_headers($url);
return !(!$file_headers || $file_headers[0] === "HTTP/1.1 404 Not Found");
}
} }