From 951ad2ccbbc781421a8d73c5deb5c1bfde78d50a Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Fri, 2 Aug 2024 13:20:45 +0200
Subject: [PATCH] remove taketiles

---
 .../app/Http/Controllers/TilesController.php  | 88 +------------------
 metager/config/metager/taketiles.php          |  8 --
 metager/resources/js/startpage/tiles.js       | 24 +----
 3 files changed, 5 insertions(+), 115 deletions(-)
 delete mode 100644 metager/config/metager/taketiles.php

diff --git a/metager/app/Http/Controllers/TilesController.php b/metager/app/Http/Controllers/TilesController.php
index b092601b8..87676aacb 100644
--- a/metager/app/Http/Controllers/TilesController.php
+++ b/metager/app/Http/Controllers/TilesController.php
@@ -19,24 +19,6 @@ class TilesController extends Controller
 {
     const CACHE_DURATION_SECONDS = 300;
 
-    public function loadTakeTiles(\Illuminate\Http\Request $request)
-    {
-        if (!$request->filled("ckey") || !Cache::has($request->input("ckey"))) {
-            abort(404);
-        }
-        $ckey = $request->input("ckey");
-        $count = $request->input("count", 4);
-        $tiles = [];
-        $tiles = self::TAKE_TILES($ckey, $count);
-        StatisticsController::LOG_STATISTICS([
-            "e_c" => "Take Tiles",
-            "e_a" => "Load",
-            "e_n" => "Take Tiles",
-            "e_v" => sizeof($tiles),
-        ]);
-        return response()->json($tiles);
-    }
-
     /**
      * Generate Tiles for a given request
      * This includes static TIles and SUMA Tiles
@@ -86,8 +68,9 @@ class TilesController extends Controller
         }
         $tiles[] = new Tile(title: __('index.plugin'), image: "/img/svg-icons/plug-in.svg", url: $plugin_url, image_alt: "MetaGer Plugin Logo", classes: $classes, target: $target, id: "plugin-btn");
 
-        $tiles[] = new Tile(title: "Unser Trägerverein", image: "/img/tiles/sumaev.png", url: "https://suma-ev.de", image_alt: "SUMA_EV Logo");
-        //$tiles[] = new Tile(title: "Maps", image: "/img/tiles/maps.png", url: "https://maps.metager.de", image_alt: "MetaGer Maps Logo");
+        if (Localization::getLanguage() === "de")
+            $tiles[] = new Tile(title: "Unser Trägerverein", image: "/img/tiles/sumaev.png", url: "https://suma-ev.de", image_alt: "SUMA_EV Logo");
+        $tiles[] = new Tile(title: "Maps", image: "/img/tiles/maps.png", url: "https://maps.metager.de", image_alt: "MetaGer Maps Logo");
         $tiles[] = new Tile(title: __('sidebar.nav28'), image: "/img/icon-settings.svg", url: route("settings", ["focus" => app(SearchSettings::class)->fokus, "url" => url()->full()]), image_alt: "Settings Logo", image_classes: "invert-dm");
 
         return $tiles;
@@ -103,69 +86,4 @@ class TilesController extends Controller
         $tiles = [];
         return $tiles;
     }
-
-    /**
-     * Generates Tile ads from Takeads
-     * 
-     * @return array
-     */
-    private static function TAKE_TILES(string $ckey, int $count): array
-    {
-        $tiles = [];
-
-        // Check if the user has disabled tiles => Allow disabling the ads on the startpage for now aswell
-        if (!app(SearchSettings::class)->tiles_startpage)
-            return $tiles;
-        $cc = Localization::getRegion();
-        $result_cache_key = "taketiles:fetch:$ckey:$cc:$count";
-
-        $result = Cache::get($result_cache_key);
-        if ($result === null) {
-            $supported_countries = ["US", "GB", "DE", "AT", "CH", "TR"];
-            if (!config("metager.taketiles.enabled") || !in_array($cc, $supported_countries)) {
-                return $tiles;
-            }
-            if (app(Authorization::class)->canDoAuthenticatedSearch(false))
-                return $tiles;
-
-            $endpoint = config("metager.taketiles.endpoint");
-            $params = [
-                "count" => $count,
-                "deviceId" => $ckey,
-                "countryCode" => $cc
-            ];
-            $mission = [
-                "resulthash" => $result_cache_key,
-                "url" => $endpoint . "?" . http_build_query($params),
-                "useragent" => Request::useragent(),
-                "headers" => [
-                    "Content-Type" => "application/json",
-                    "Authorization" => "Bearer " . config("metager.taketiles.public_key"),
-                ],
-                "cacheDuration" => ceil(self::CACHE_DURATION_SECONDS / 60),
-                "name" => "Take Tiles",
-            ];
-            $mission = json_encode($mission);
-            Redis::rpush(\App\MetaGer::FETCHQUEUE_KEY, $mission);
-            Cache::put($ckey, "1", now()->addSeconds(self::CACHE_DURATION_SECONDS));
-            $result = Redis::blpop($result_cache_key, 0);
-            if (sizeof($result) === 2) {
-                $result = $result[1];
-            }
-        }
-
-        if ($result !== null) {
-            try {
-                $result = json_decode($result);
-                foreach ($result->data as $result_tile) {
-                    $tiles[] = new Tile(title: $result_tile->title, image: $result_tile->image, image_alt: $result_tile->title . " Image", url: $result_tile->url, advertisement: true);
-                }
-            } catch (Exception $e) {
-                Log::error($e);
-            }
-
-        }
-
-        return $tiles;
-    }
 }
diff --git a/metager/config/metager/taketiles.php b/metager/config/metager/taketiles.php
deleted file mode 100644
index 827872660..000000000
--- a/metager/config/metager/taketiles.php
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-return [
-    "enabled" => env("TAKE_TILES_ENABLED", false),
-    "secret" => env("TAKE_TILES_SECRET", "12345"),
-    "endpoint" => env("TAKE_TILES_ENDPOINT", ""),
-    "public_key" => env("TAKE_TILES_PUBLIC_KEY", ""),
-
-];
\ No newline at end of file
diff --git a/metager/resources/js/startpage/tiles.js b/metager/resources/js/startpage/tiles.js
index bbe8c0f4f..803634d6c 100644
--- a/metager/resources/js/startpage/tiles.js
+++ b/metager/resources/js/startpage/tiles.js
@@ -2,29 +2,9 @@ import { statistics } from "../statistics";
 
 (async () => {
     let tile_container = document.querySelector("#tiles");
-    let tile_count = tile_container.querySelectorAll("a").length;
-
     let advertisements = [];
-    let fetch_timeout = null;
-    fetchAdvertisements().then(() => udpateInterface());
+    udpateInterface();
 
-    async function fetchAdvertisements() {
-        let desired_tile_count = calculateDesiredTileCount();
-        let regular_tile_count = getRegularTileCount();
-        if (advertisements.length >= desired_tile_count - regular_tile_count) return;
-        let update_url = document.querySelector("meta[name=tiles-update-url]").content;
-        update_url += "&count=" + (desired_tile_count - tile_count);
-        return new Promise((resolve, reject) => {
-            if (fetch_timeout != null) return resolve();
-            fetch_timeout = setTimeout(() => {
-                fetch(update_url).then(response => response.json()).then(response => {
-                    advertisements = response;
-                    fetch_timeout = null;
-                    resolve();
-                }).catch(e => reject(e));
-            }, 500);
-        });
-    }
     function udpateInterface() {
         let desired_tile_count = calculateDesiredTileCount();
         let regular_tile_count = getRegularTileCount();
@@ -87,7 +67,7 @@ import { statistics } from "../statistics";
         return desired_tile_count;
     }
     window.addEventListener("resize", e => {
-        fetchAdvertisements().then(() => udpateInterface());
+        udpateInterface();
     })
 })();
 
-- 
GitLab