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
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
@ -32,32 +31,46 @@ class YellowMastodonapi {
// 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");