ForRo/app/Repositories/SourceRepository.php

71 lines
2.4 KiB
PHP
Raw Normal View History

<?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];
}
}