Update mastodonapi.php

This commit is contained in:
simonpipe 2026-01-07 09:03:29 +01:00
parent 5420ec8f50
commit 50ceea433d

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.2.1"; const VERSION = "0.3.0";
public $yellow; public $yellow;
public function onLoad($yellow) { public function onLoad($yellow) {
@ -15,25 +15,40 @@ class YellowMastodonapi {
$this->yellow->system->setDefault("mastodonapiAuthor", "YellowUser"); $this->yellow->system->setDefault("mastodonapiAuthor", "YellowUser");
$this->yellow->system->setDefault("mastodonapiTag", "Fediverse"); $this->yellow->system->setDefault("mastodonapiTag", "Fediverse");
$this->yellow->system->setDefault("mastodonapiFolder", "content/2-blog/"); $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("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("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("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) { // Reagiert auf 'php yellow.php mastodon' oder das System-Event 'daily'
$triggerPath = $this->yellow->system->get("mastodonapiTriggerPath"); public function onUpdate($action) {
if ($location == $triggerPath) { if ($action == "mastodon" || $action == "daily" || $action == "clean") {
$postsCreated = $this->processMastodonFeed(); $this->updateMastodonPosts();
$this->sendResponse(
$postsCreated !== false
? "Mastodon/GoToSocial API feed processed. Created $postsCreated posts."
: "Error processing Mastodon/GoToSocial API feed.",
$postsCreated !== false ? 200 : 500
);
return 1;
} }
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) { public function onCommand($command, $text) {
@ -290,29 +305,4 @@ MD;
private function getYellowConfig() { private function getYellowConfig() {
return $this->yellow ? $this->yellow->system : null; 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 "<!DOCTYPE html><html><head>";
echo "<meta http-equiv='refresh' content='5;url={$redirectUrl}'>";
echo "<title>Mastodon/GoToSocial API</title></head><body>";
echo "<p>" . htmlspecialchars($message) . "</p>";
echo "<p>You will be redirected in 5 seconds...</p>";
echo "</body></html>";
exit;
}
} }