Update mastodonapi.php

This commit is contained in:
simonpipe 2026-01-07 09:46:49 +01:00
parent 3b902a118b
commit cf294020e8

View file

@ -3,7 +3,7 @@
// MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi // MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi
class YellowMastodonapi { class YellowMastodonapi {
const VERSION = "0.3.0"; const VERSION = "0.3.1";
public $yellow; public $yellow;
public function onLoad($yellow) { public function onLoad($yellow) {
@ -19,7 +19,6 @@ class YellowMastodonapi {
$this->yellow->system->setDefault("mastodonapiStatus", "public"); $this->yellow->system->setDefault("mastodonapiStatus", "public");
$this->yellow->system->setDefault("mastodonapiMaxAge", "0"); $this->yellow->system->setDefault("mastodonapiMaxAge", "0");
$this->yellow->system->setDefault("mastodonapiUpdateInterval", "30"); $this->yellow->system->setDefault("mastodonapiUpdateInterval", "30");
$this->yellow->system->setDefault("mastodonapiLastUpdate", "0");
} }
// Reagiert auf System-Events und manuelle Aufrufe // 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) { public function onCommand($command, $text) {
if ($command == "mastodonapi") { if ($command == "mastodonapi") {
// Wir leiten den Befehl einfach an onUpdate weiter echo "Mastodon Import gestartet...\n";
$this->onUpdate("mastodon"); $count = $this->updateMastodonPosts();
return 0; // Erfolg
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; 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() { public function onTick() {
$interval = intval($this->yellow->system->get("mastodonapiUpdateInterval")) * 60; $interval = intval($this->yellow->system->get("mastodonapiUpdateInterval")) 60;
$lastUpdate = intval($this->yellow->system->get("mastodonapiLastUpdate")); $statusFile = $this->getStatusFile();
$lastUpdate = file_exists($statusFile) ? intval(file_get_contents($statusFile)) : 0;
if (time() - $lastUpdate >= $interval) { if (time() - $lastUpdate >= $interval) {
$this->updateMastodonPosts(); $this->updateMastodonPosts();
} }
} }
public function updateMastodonPosts() {
$success = $this->processMastodonFeed();
if ($success !== false) {
$this->yellow->system->set("mastodonapiLastUpdate", time());
$this->yellow->system->save();
}
}
private function processMastodonFeed() { private function processMastodonFeed() {
$instanceUrl = $this->yellow->system->get("mastodonapiInstanceUrl"); $instanceUrl = $this->yellow->system->get("mastodonapiInstanceUrl");
$accountId = $this->yellow->system->get("mastodonapiAccountId"); $accountId = $this->yellow->system->get("mastodonapiAccountId");