diff --git a/mastodonapi.php b/mastodonapi.php index 7c5b756..be2da58 100644 --- a/mastodonapi.php +++ b/mastodonapi.php @@ -3,7 +3,7 @@ // MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi class YellowMastodonapi { - const VERSION = "0.3.0"; + const VERSION = "0.3.1"; public $yellow; public function onLoad($yellow) { @@ -19,7 +19,6 @@ class YellowMastodonapi { $this->yellow->system->setDefault("mastodonapiStatus", "public"); $this->yellow->system->setDefault("mastodonapiMaxAge", "0"); $this->yellow->system->setDefault("mastodonapiUpdateInterval", "30"); - $this->yellow->system->setDefault("mastodonapiLastUpdate", "0"); } // Reagiert auf System-Events und manuelle Aufrufe @@ -29,35 +28,49 @@ class YellowMastodonapi { } } - // Ermöglicht 'php yellow.php mastodonapi' im Terminal +// Ermöglicht 'php yellow.php mastodonapi' im Terminal public function onCommand($command, $text) { if ($command == "mastodonapi") { - // Wir leiten den Befehl einfach an onUpdate weiter - $this->onUpdate("mastodon"); - return 0; // Erfolg + echo "Mastodon Import gestartet...\n"; + $count = $this->updateMastodonPosts(); + + if ($count === false) { + echo "Fehler: API nicht erreichbar oder falsche ID.\n"; + return 500; + } + + echo "Erfolg: $count neue Posts verarbeitet.\n"; + return 0; } return 0; } - // Der "Lazy Cron" für Nutzer ohne echten Cronjob + // Hilfsfunktion für den Pfad zur Status-Datei + private function getStatusFile() { + return $this->yellow->system->get("coreWorkerDirectory")."mastodonapi.status"; + } + + // Die einzige Version dieser Funktion behalten: + public function updateMastodonPosts() { + $postsCreated = $this->processMastodonFeed(); + + if ($postsCreated !== false) { + file_put_contents($this->getStatusFile(), time()); + } + return $postsCreated; + } + public function onTick() { - $interval = intval($this->yellow->system->get("mastodonapiUpdateInterval")) * 60; - $lastUpdate = intval($this->yellow->system->get("mastodonapiLastUpdate")); + $interval = intval($this->yellow->system->get("mastodonapiUpdateInterval")) 60; + $statusFile = $this->getStatusFile(); + + $lastUpdate = file_exists($statusFile) ? intval(file_get_contents($statusFile)) : 0; if (time() - $lastUpdate >= $interval) { $this->updateMastodonPosts(); } } - public function updateMastodonPosts() { - $success = $this->processMastodonFeed(); - - if ($success !== false) { - $this->yellow->system->set("mastodonapiLastUpdate", time()); - $this->yellow->system->save(); - } - } - private function processMastodonFeed() { $instanceUrl = $this->yellow->system->get("mastodonapiInstanceUrl"); $accountId = $this->yellow->system->get("mastodonapiAccountId");