From 245531faf6c6ce537567b42e891fb3ca010ccc49 Mon Sep 17 00:00:00 2001 From: Ro Date: Mon, 23 Jan 2023 19:59:51 -0800 Subject: [PATCH] Added support of fedifence CSV and pagination Populated the DB with entries from a fedifence export provided by @Oliphant (https://codeberg.org/oliphant/blocklists). As such also updated the appropriate templates with pagination to be able to peruse through the location directory. Also added an edit link on the location template front end to make finding and updating instance info easy. --- .gitignore | 2 +- public/assets/css/front/listing.css | 13 ++- src/Controller/Routes/Back/Locations.php | 34 +++++- src/Controller/Routes/Front/Index.php | 21 +++- src/Service/HandleImports.php | 134 +++++++++++++++++++++++ src/Service/HandleLocations.php | 2 +- templates/back/locations.twig | 23 ++-- templates/forms/bulk-add-location.twig | 8 ++ templates/front/listing.twig | 13 ++- templates/front/location.twig | 5 + 10 files changed, 234 insertions(+), 21 deletions(-) create mode 100644 src/Service/HandleImports.php diff --git a/.gitignore b/.gitignore index dc8ca91..e5b5e5e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ - +/files ###> symfony/framework-bundle ### .env.local /.env.local.php diff --git a/public/assets/css/front/listing.css b/public/assets/css/front/listing.css index 81423e0..9539106 100644 --- a/public/assets/css/front/listing.css +++ b/public/assets/css/front/listing.css @@ -9,8 +9,19 @@ section[role="listings"] { section[role="listings"] a { color: var(--highlight); - font-size: 2.5em; + font-size: 2em; font-weight: bold; border: 0; display: block; } + +section[role="listings"] a label { + color: var(--secondary); + font-size: 0.3em; + text-decoration: underline; + font-family: var(--mono-type); +} + +section[role="listings"] a:hover { + color: var(--white); +} diff --git a/src/Controller/Routes/Back/Locations.php b/src/Controller/Routes/Back/Locations.php index 1d2fb1b..23539c7 100644 --- a/src/Controller/Routes/Back/Locations.php +++ b/src/Controller/Routes/Back/Locations.php @@ -12,6 +12,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\RequestStack; use Doctrine\Persistence\ManagerRegistry; use App\Service\HandleLocations; +use App\Service\HandleImports; use App\Service\Auth; use App\Service\FileUploader; use App\Service\Render; @@ -45,8 +46,27 @@ class Locations extends AbstractController $member = $session->get("member"); $list = $locations->getLocationsPage($pageNum); - //$search = $connection->fetchAllAssociative("SELECT * FROM searchlocations('agenda')"); - return $render->page(["list" => $list, "mode" => "index"], "Bad Space | Locations", "back/locations.twig"); + $next = $pageNum + 1; + if ($next > $list["total"]) { + $next = 1; + } + + $prev = $pageNum - 1; + + if ($prev <= 0) { + $prev = $list["total"]; + } + return $render->page( + [ + "list" => $list, + "mode" => "index", + "curentPage" => $pageNum, + "nextPage" => $next, + "prevPage" => $prev + ], + "Bad Space | Locations", + "back/locations.twig" + ); } else { return $render->page([], "The Bad Space | Den", "back/index.twig"); } @@ -164,6 +184,7 @@ class Locations extends AbstractController Request $request, Auth $auth, HandleLocations $locations, + HandleImports $imports, ManagerRegistry $doctrine, FileUploader $uploader, Render $render @@ -181,7 +202,7 @@ class Locations extends AbstractController $token = $request->get("token"); $entityManager = $doctrine->getManager(); $notice = ''; - + $type = $request->get("input_type"); if (!$this->isCsrfTokenValid("upload", $token)) { $logger->info("CSRF failure"); return new Response( @@ -217,7 +238,12 @@ class Locations extends AbstractController ]); } //if it's cool, send it to be processed - $response = $locations->addMultipleLocations($file, $result["id"]); + if ($type == "tbs" || $type == "") { + $response = $locations->addMultipleLocations($file, $result["id"]); + } else { + $response = $imports->importLocations($file, $result["id"]); + } + if ($response["status"]) { $notice = "New locations added! Take a break."; return $render->page( diff --git a/src/Controller/Routes/Front/Index.php b/src/Controller/Routes/Front/Index.php index b5c15c8..c5b69ae 100644 --- a/src/Controller/Routes/Front/Index.php +++ b/src/Controller/Routes/Front/Index.php @@ -73,9 +73,26 @@ class Index extends AbstractController Auth $auth, Render $render, HandleLocations $locations, - string $pageNum + int $pageNum = 1 ): Response { $list = $locations->getLocationsPage($pageNum, "true"); - return $render->page(["list" => $list, "page" => $pageNum], "About The Bad Space", "front/listing.twig"); + + $next = $pageNum + 1; + if ($next > $list["total"]) { + $next = 1; + } + + $prev = $pageNum - 1; + + if ($prev <= 0) { + $prev = $list["total"]; + } + + return $render->page([ + "list" => $list, + "currentPage" => $pageNum, + "nextPage" => $next, + "prevPage" => $prev + ], "About The Bad Space", "front/listing.twig"); } } diff --git a/src/Service/HandleImports.php b/src/Service/HandleImports.php new file mode 100644 index 0000000..622780f --- /dev/null +++ b/src/Service/HandleImports.php @@ -0,0 +1,134 @@ +connection = $connection; + $this->entityManager = $entityManager; + $this->session = $requestStack->getSession(); + } + + /** + * Add new Locations to db from fedifence file + * provided by Oliphant + * + * @param string $path location of file + * @param int $memberID member adding locations + * @return Object + */ + public function importLocations($file, $memberId) + { + //read csv + $csv = Reader::createFromPath($file, "r"); + $csv->setHeaderOffset(0); + $records = $csv->getRecords(); + $recordCount = count($csv); + $duplicates = 0; + $errorMessage = null; + + //extract data row by row + + //TODO: set name to lowercase for comparison + foreach ($records as $offset => $row) { + $url = $row["domain"]; + //$images = $row["Images"]; + $desc = $row["public_comment"]; + $ratings = $row["severity"]; + + //check to see if location already exists + $list = $this->entityManager->getRepository(Location::class); + $entry = $list->findOneBy(["url" => trim($url)]); + if ($entry) { + ++$duplicates; + } else { + $errorMessage = null; + $location = new Location(); + + $location->setName($url); + $location->setUrl($url); + $location->setTags("bigotry, hate speech, poor moderation"); + if ($ratings == "suspend") { + $location->setRating("defederate"); + } else { + $location->setRating("silence"); + } + + //set defaults + $location->setUuid(Uuid::v4()); + if ($desc == "") { + $location->setDescription("needs description"); + $location->setActive(false); + } else { + $location->setActive(true); + $location->setDescription($desc); + } + + $location->setCreatedAt(new \DateTimeImmutable()); + $location->setUpdatedAt(new \DateTimeImmutable()); + $location->setAddedBy($memberId); + + $this->entityManager->persist($location); + } + } + + try { + $this->entityManager->flush(); + } catch (PDOException $error) { + $errorMessage = $error->getMessage(); + } catch (DBALException $error) { + $errorMessage = $error->getMessage(); + } catch (ORMException $error) { + $errorMessage = $error->getMessage(); + } catch (Exception $error) { + $errorMessage = $error->getMessage(); + } catch (SyntaxErrorException $e) { + $errorMessage = $error->getMessage(); + } + + if ($duplicates > 0) { + $message = $duplicates . " of " . $recordCount . " location entries were duplicates"; + } else { + $message = "Locations Added. Nice Job!"; + } + // return result status + if ($errorMessage == null) { + return $response = [ + "status" => true, + "message" => $message, + ]; + } else { + return $response = ["status" => false, "message" => $errorMessage]; + } + } +} diff --git a/src/Service/HandleLocations.php b/src/Service/HandleLocations.php index 964a647..f0dd5f1 100644 --- a/src/Service/HandleLocations.php +++ b/src/Service/HandleLocations.php @@ -27,7 +27,7 @@ class HandleLocations private $session; private $entityManager; private $conn; - private $limit = 4; + private $limit = 9; public function __construct( Connection $connection, diff --git a/templates/back/locations.twig b/templates/back/locations.twig index e2ade17..432a904 100644 --- a/templates/back/locations.twig +++ b/templates/back/locations.twig @@ -30,13 +30,20 @@ Add Multiple Locations

Bad Spaces

- {% for location in options.list.locations %} - ID:{{ location.id }} - + Page + {{ options.curentPage }} + of + {{ options.list.total }}
+ {% for location in options.list.locations %} + ID:{{ location.id }} +
+ {{ location.name }}
+ {% endfor %} + Previous + Next + {% endif %} - {{ location.name }}
- {% endfor %} - {% endif %} - - {% endblock %} + + + {% endblock %} diff --git a/templates/forms/bulk-add-location.twig b/templates/forms/bulk-add-location.twig index a58e426..b712f6f 100644 --- a/templates/forms/bulk-add-location.twig +++ b/templates/forms/bulk-add-location.twig @@ -3,6 +3,14 @@

+
+ +
diff --git a/templates/front/listing.twig b/templates/front/listing.twig index eafc676..c31e129 100644 --- a/templates/front/listing.twig +++ b/templates/front/listing.twig @@ -7,12 +7,17 @@

The Bad Space Listings

Page - {{ options.page }}

+ {{ options.currentPage }} + of + {{ options.list.total }} {% for location in options.list.locations %} - ID:{{ location.id }} - - {{ location.name }}
+ + + {{ location.name }} +
{% endfor %} + Previous + Next
{% endblock %} diff --git a/templates/front/location.twig b/templates/front/location.twig index 6fafadc..b2d1780 100644 --- a/templates/front/location.twig +++ b/templates/front/location.twig @@ -17,5 +17,10 @@
TAGS: {{ options.location.tags }} +
+ {% if loggedIn %} + EDIT + {{ options.location.name }} + {% endif %} {% endblock %}