ForRo/app/Services/UpdateService.php
ro 2932af0d3f show multiple instance comments
the previous update cycle only showed the latest comment from Sources
concerning a specific instance

now all available comments are pulled from denylist data to be displayed
under Description on the front end
2024-09-17 15:41:31 -06:00

155 lines
5.9 KiB
PHP

<?php
namespace App\Services;
use App\Models\Location;
use App\Repositories\LocationRepository;
use App\Repositories\SourceRepository;
use Ramsey\Uuid\Uuid;
class UpdateService
{
protected $location;
protected $source;
public function __construct(
LocationRepository $locationRepository,
SourceRepository $sourceRepository
) {
$this->location = $locationRepository;
$this->source = $sourceRepository;
}
public function data()
{
$response = $this->source->updateSourceData();
if ($response['updated'] == 'true') {
return count($response['checked']) . ' SOURCES UPDATED';
} else {
return 'NO SOURCES PRESENT';
}
}
public function list()
{
$duplicates = 0;
$fresh = 0;
$unified = [];
$sources = $this->source->getActive();
$locations = $this->location->getActiveLocations();
foreach ($sources as $source) {
//$listData = json_decode();
foreach (json_decode($source->list_data) as $item) {
$index = array_search($item->domain, array_column($unified, 'url'));
if ($index) {
//if there is a match, update the count and comment
if ($item->severity == "suspend" || $item->severity == "defederate") {
++$unified[$index]['block_count'];
array_push($unified[$index]['block_vote'], $source->url);
} else {
++$unified[$index]['silence_count'];
array_push($unified[$index]['silence_vote'], $source->url);
}
if (!is_null($item->comment) && $item->comment != ' ' && $item->comment != '') {
$unified[$index]['comment'] = $item->comment . '+' . $unified[$index]['comment'];
}
} else {
$silence = 0;
$suspend = 0;
$block_vote = [];
$silence_vote = [];
if ($item->severity == "suspend" || $item->severity == "defederate") {
++$suspend;
array_push($block_vote, $source->url);
} else {
++$silence;
array_push($silence_vote, $source->url);
}
array_push($unified, [
'name' => $item->domain,
'url' => $item->domain,
'rating' => $item->severity,
'comment' => $item->comment,
'block_count' => $suspend,
'silence_count' => $silence,
'block_vote' => $block_vote,
'silence_vote' => $silence_vote,
]);
}
}
}
//clear out all previous descriptions
foreach ($locations as $loc) {
$loc->description = ' ';
$loc->save();
}
foreach ($unified as $item) {
$location = $this->location->getLocation($item['url']);
if ($location) {
++$duplicates;
//update block count for existing item
$location->block_count = $item['block_count'];
$location->block_vote = [];
$location->block_vote = $item['block_vote'];
$location->silence_count = $item['silence_count'];
$location->silence_vote = [];
$location->silence_vote = $item['silence_vote'];
//clear descriptions
if (!is_null($item['comment']) || !$item['comment'] != " ") {
$location->description = $item['comment'];
} else {
$location->description = 'description pending';
}
$location->actions_count = $item['block_count'] + $item['silence_count'];
if (($item['block_count'] + $item['silence_count']) < 2) {
$location->active = false;
} else {
$location->active = true;
}
//replace null with empty array
if ($location->images == null) {
$location->images = [];
};
$location->save();
} else {
// make new entries for instances not present
++$fresh;
$images = [];
$rating = ($item['rating'] == 'defederate') ? 'suspend' : $item['rating'];
$status = true;
if (($item['block_count'] + $item['silence_count']) < 2) {
$status = false;
}
$new = Location::create([
'uuid' => Uuid::uuid4(),
'name' => $item['url'],
'url' => $item['url'],
'description' => ($item['comment'] != null) ? $item['comment'] : "no description",
'active' => $status,
'rating' => $rating,
'added_by' => 1,
'tags' => 'poor moderation, hate speech',
'images' => json_encode($images),
'block_count' => $item['block_count'],
'block_vote' => $item['block_vote'],
'silence_count' => $item['silence_count'],
'silence_vote' => $item['silence_vote'],
'actions_cont' => $item['block_count'] + $item['silence_count']
]);
}
}
//TODO: Send update post to TBS social account
return $duplicates . ' LOCATIONS UPDATED | ' . $fresh . ' NEW LOCATIONS CREATED';
}
}