Updated blocklist retrival process, template edits

The process for updating blocklists per source wasn't effecient, so it
has been rewritten to process each source individually before moving on,
relieving some processing load on the server and ensuring every source
comes back with data, even in the event it doesn't grab it the first
time.

also removed the recent list from the index page as the recently updated
list doesn't reflect what's been the last to get updated and changed the
theme color to match the current palette.
This commit is contained in:
ro 2024-09-12 19:20:13 -06:00
parent d3d1d56680
commit 43e0004ac5
5 changed files with 87 additions and 64 deletions

View file

@ -9,10 +9,16 @@ use GuzzleHttp\Exception\ConnectException;
class SourceRepository class SourceRepository
{ {
protected $source; protected $source;
protected $missing;
protected $updated;
protected $sources;
public function __construct(Source $source) public function __construct(Source $source)
{ {
$this->source = $source; $this->source = $source;
$this->missing = [];
$this->updated = [];
$this->sources = $source::where("active", true)->get();
} }
public function getActive() public function getActive()
@ -20,51 +26,85 @@ class SourceRepository
return $this->source::where("active", true)->get(); return $this->source::where("active", true)->get();
} }
public function updateSourceData() public function updateSourceData($index = 0)
{ {
$sources = $this->getActive(); //$sources = $this->getActive();
$missing = [];
$checked = [];
//checks all the sources to refresh data //checks all the sources to refresh data
foreach ($sources as $source) { $count = count($this->sources);
$result = []; if ($count == 0) {
if ($source['type'] == 'mastodon') { return [
if ($source['token'] == null) { 'checked' => $this->updated,
try { 'notchecked' => $this->missing,
$result = \Mastodon::domain('https://' . $source['url']) 'count' => $count,
->get('/instance/domain_blocks'); 'updated' => 'false',
array_push($checked, ['source' => $source->url]); ];
} catch (ConnectException $e) { } else {
array_push($missing, ['source' => $source->url]); //check index
} if ($index <= $count - 1) {
$source = $this->sources[$index];
$result = $this->getMastoBlocklist($source);
if (count($result) > 0) {
$source->list_data = json_encode($result);
$source->last_updated = Carbon::now();
$source->save();
array_push($this->updated, ['source' => $source->url]);
$index++;
$result = $this->updateSourceData($index);
} else { } else {
try { //if empty run the same index again
$result = \Mastodon::domain('https://' . $source['url']) array_push($this->missing, ['source' => $source->url]);
->token($source['token']) $result = $this->updateSourceData($index);
->get('/instance/domain_blocks');
array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
}
} }
} elseif ($source['type'] == 'custom' && $source['format'] == 'csv') { } else {
//continue
}
return [
'checked' => $this->updated,
'notchecked' => $this->missing,
'count' => $count,
'updated' => 'true',
];
}
}
private function getMastoBlocklist($source)
{
$result = [];
if ($source['type'] == 'mastodon') {
if ($source['token'] == null) {
try { try {
$denylist = array_map('str_getcsv', file('https://' . $source['url'])); $result = \Mastodon::domain('https://' . $source['url'])
foreach ($denylist as $item) { ->get('/instance/domain_blocks');
array_push($result, [ //array_push($checked, ['source' => $source->url]);
'domain' => $item[0], } catch (ConnectException $e) {
'severity' => $item[1], //dd($source);
'comment' => $item[2]]); //array_push($missing, ['source' => $source->url]);
} }
array_push($checked, ['source' => $source->url]); } else {
} catch (Exception $e) { try {
array_push($missing, ['source' => $source->url]); $result = \Mastodon::domain('https://' . $source['url'])
->token($source['token'])
->get('/instance/domain_blocks');
//array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
//array_push($missing, ['source' => $source->url]);
//dd($source);
} }
} }
} elseif ($source['type'] == 'custom' && $source['format'] == 'csv') {
$source->list_data = json_encode($result); try {
$source->last_updated = Carbon::now(); $denylist = array_map('str_getcsv', file('https://' . $source['url']));
$source->save(); 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]);
}
} }
return ['checked' => $checked, 'notchecked' => $missing]; return $result;
} }
} }

View file

@ -23,8 +23,11 @@ class UpdateService
public function data() public function data()
{ {
$response = $this->source->updateSourceData(); $response = $this->source->updateSourceData();
return count($response['checked']) . ' SOURCES UPDATED - ' . if ($response['updated'] == 'true') {
count($response['notchecked']) . ' SOURCES NOT CHECKED'; return count($response['checked']) . ' SOURCES UPDATED';
} else {
return 'NO SOURCES PRESENT';
}
} }
public function list() public function list()

View file

@ -199,9 +199,8 @@ footer {
padding: 10px; padding: 10px;
gap: 10px; gap: 10px;
height: auto; height: auto;
width: 80%; width: auto;
margin: 20px auto; margin: 20px auto;
max-width: 1000px;
position: relative; position: relative;
} }

View file

@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="theme-color" content="#d66365" /> <meta name="theme-color" content="#c3639e" />
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title> <title>
@yield('title') @yield('title')

View file

@ -38,27 +38,8 @@
@endisset @endisset
<section class="index-meta"> <section class="index-meta">
<article> <article>
<h2>Recent Updates</h2>
@foreach($recent as $item)
<a class="list-link" role="listitem" href="/location/{{$item->uuid}}">
@php
$rating = floor(($item->actions_count / $sources)*100);
@endphp
<span class="item-rating">{{$rating}}%</span>
<label class="item-name">{{$item->name}}</label>
<div class="item-silence">
<img class="item-icon" src="/assets/images/global/status-silence.svg" title="silenced" />
{{$item->silence_count}}
</div>
<div class="item-block">
<img class="item-icon" src="/assets/images/global/status-suspend.svg" title="suspended" />
{{$item->block_count}}
</div>
</a>
@endforeach
<h2>Info</h2>
<div class="index-meta"> <div class="index-meta">
<label>Locations Tracked</label> <label>Active Locations Tracked</label>
<label>{{$count}}</label> <label>{{$count}}</label>
<label>Total Sources</label> <label>Total Sources</label>
<label>{{$sources}}</label> <label>{{$sources}}</label>