diff --git a/mastodonapi.php b/mastodonapi.php index b6dae49..61c7027 100644 --- a/mastodonapi.php +++ b/mastodonapi.php @@ -3,7 +3,7 @@ // MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi class YellowMastodonapi { - const VERSION = "0.2.1"; + const VERSION = "0.3.0"; public $yellow; public function onLoad($yellow) { @@ -15,25 +15,40 @@ class YellowMastodonapi { $this->yellow->system->setDefault("mastodonapiAuthor", "YellowUser"); $this->yellow->system->setDefault("mastodonapiTag", "Fediverse"); $this->yellow->system->setDefault("mastodonapiFolder", "content/2-blog/"); - $this->yellow->system->setDefault("mastodonapiTriggerPath", "/mastodonapi-webhook-CHANGE_THIS_SECRET_KEY"); $this->yellow->system->setDefault("mastodonapiLimit", "20"); $this->yellow->system->setDefault("mastodonapiStatus", "public"); // public; draft = page is not visible, user needs to log in, requires draft extension; unlisted = page is not visible, but can be accessed with the correct link $this->yellow->system->setDefault("mastodonapiMaxAge", "0"); // In Stunden. 0 = alle Toots. + $this->yellow->system->setDefault("mastodonapiUpdateInterval", "30"); + $this->yellow->system->setDefault("mastodonapiLastUpdate", "0"); } - public function onRequest($scheme, $address, $base, $location, $fileName) { - $triggerPath = $this->yellow->system->get("mastodonapiTriggerPath"); - if ($location == $triggerPath) { - $postsCreated = $this->processMastodonFeed(); - $this->sendResponse( - $postsCreated !== false - ? "Mastodon/GoToSocial API feed processed. Created $postsCreated posts." - : "Error processing Mastodon/GoToSocial API feed.", - $postsCreated !== false ? 200 : 500 - ); - return 1; + // Reagiert auf 'php yellow.php mastodon' oder das System-Event 'daily' + public function onUpdate($action) { + if ($action == "mastodon" || $action == "daily" || $action == "clean") { + $this->updateMastodonPosts(); } - return 0; + } + + // Prüft bei Seitenaufrufen, ob das Intervall abgelaufen ist + public function onTick() { + $interval = intval($this->yellow->system->get("mastodonapiUpdateInterval")) * 60; + $lastUpdate = intval($this->yellow->system->get("mastodonapiLastUpdate")); + + if (time() - $lastUpdate >= $interval) { + $this->updateMastodonPosts(); + } + } + + public function updateMastodonPosts() { + $instance = $this->yellow->system->get("mastodonapiInstance"); + $userId = $this->yellow->system->get("mastodonapiUserId"); + + // Dein bestehender Code zum Abrufen der Mastodon-Daten... + // Beispiel: $response = $this->yellow->toolbox->getHttpClient()->getContent($url); + + // Am Ende den Zeitstempel speichern, damit der nächste Tick bescheid weiß + $this->yellow->system->set("mastodonapiLastUpdate", time()); + $this->yellow->system->save(); } public function onCommand($command, $text) { @@ -290,29 +305,4 @@ MD; private function getYellowConfig() { return $this->yellow ? $this->yellow->system : null; } - - private function sendResponse($message, $statusCode = 200) { - if (!headers_sent()) { - header("HTTP/1.1 $statusCode"); - header("Content-Type: text/html; charset=UTF-8"); - } - - // Basis-URL aus der aktuellen Anfrage bestimmen - $fullUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") - . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; - - // Trigger-Pfad aus der Yellow-Konfiguration holen - $triggerPath = $this->yellow->system->get("mastodonapiTriggerPath"); - - // Ziel-URL: Alles vor dem Trigger-Pfad abschneiden - $redirectUrl = str_replace($triggerPath, "/", $fullUrl); - - echo "
"; - echo ""; - echo "" . htmlspecialchars($message) . "
"; - echo "You will be redirected in 5 seconds...
"; - echo ""; - exit; - } } \ No newline at end of file