mirror of
https://koodu.h-i.works/projects/thebadspace
synced 2025-06-25 16:04:37 -05:00
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:
70
app/Repositories/SourceRepository.php
Normal file
70
app/Repositories/SourceRepository.php
Normal 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];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user