mastodonapi.php aktualisiert

This commit is contained in:
simonpipe 2025-08-13 16:46:23 +02:00
parent 0fe5607616
commit 60341a408c

View file

@ -1,8 +1,4 @@
<?php
// MastodonAPI & GoToSocial extension
// Kompatibel mit Mastodon-Servern & GoToSocial-Instanzen
// https://codeberg.org/simonpipe/yellow-mastodonapi (angepasst)
class YellowMastodonAPI {
const VERSION = "0.6";
public $yellow;
@ -27,7 +23,7 @@ class YellowMastodonAPI {
$this->sendResponse(
$postsCreated !== false
? "Mastodon/GoToSocial API feed processed. Created $postsCreated posts."
: "Error processing Mastodon/GoToSocial API feed. Check logs for details.",
: "Error processing Mastodon/GoToSocial API feed.",
$postsCreated !== false ? 200 : 500
);
return 1;
@ -50,7 +46,7 @@ class YellowMastodonAPI {
private function processMastodonFeed() {
$instanceUrl = $this->yellow->system->get("mastodonapiInstanceUrl");
$accountId = $this->yellow->system->get("mastodonapiAccountId");
$accessToken = $this->yellow->system->get("mastodonapiAccessToken"); // optional
$accessToken = $this->yellow->system->get("mastodonapiAccessToken");
$hashtag = ltrim($this->yellow->system->get("mastodonapiHashtag"), '#');
$defaultAuthor = $this->yellow->system->get("mastodonapiAuthor");
$defaultTag = $this->yellow->system->get("mastodonapiTag");
@ -58,7 +54,6 @@ class YellowMastodonAPI {
$limit = intval($this->yellow->system->get("mastodonapiLimit"));
if (empty($instanceUrl) || empty($accountId)) {
error_log("ERROR: MastodonAPI: Missing configuration values.");
return false;
}
@ -69,7 +64,6 @@ class YellowMastodonAPI {
$headers[] = "Authorization: Bearer " . $accessToken;
}
// Daten via cURL abrufen (funktioniert bei Mastodon & GoToSocial)
$ch = curl_init($apiUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
@ -81,17 +75,14 @@ class YellowMastodonAPI {
]);
$json = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($json === false || $httpCode < 200 || $httpCode >= 300) {
error_log("ERROR: Could not fetch API data from $apiUrl (HTTP $httpCode, Error: $curlError)");
return false;
}
$posts = json_decode($json, true);
if (!is_array($posts)) {
error_log("ERROR: MastodonAPI: Invalid JSON returned by API.");
return false;
}
@ -99,7 +90,6 @@ class YellowMastodonAPI {
$postsCreated = 0;
foreach ($posts as $post) {
// Keine Boosts / Antworten
if (!empty($post['reblog']) || (!empty($post['reblogged']) && $post['reblogged'] === true)) continue;
if (!is_null($post['in_reply_to_id'])) continue;
@ -164,7 +154,6 @@ class YellowMastodonAPI {
?? ($media['meta']['small']['url'] ?? null);
if (empty($imageUrl)) {
error_log("ERROR: No usable image URL found in media attachment: " . json_encode($media));
return false;
}
@ -190,11 +179,9 @@ class YellowMastodonAPI {
]);
$imgData = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($imgData === false || $httpCode < 200 || $httpCode >= 300) {
error_log("ERROR: Could not download image from $imageUrl (HTTP $httpCode, Error: $curlError)");
return false;
}
@ -202,8 +189,6 @@ class YellowMastodonAPI {
return $fileName;
}
// Rest der Helper-Methoden wie bei dir (generateTitle, generateTitleSlug, generateMarkdown, getYellowConfig, sendResponse) ...
private function generateTitle($content) {
$text = strip_tags($content);
$text = preg_replace('/https?:\/\/[^\s]+|#\w+/u', '', $text);
@ -227,12 +212,10 @@ class YellowMastodonAPI {
$content = strip_tags($html);
$content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5, 'UTF-8');
// Hashtag-Block extrahieren
$hashtagString = '';
if (preg_match('/(\s*(#\w+))+$/u', $content, $matches)) {
$content = substr($content, 0, -strlen($matches[0]));
if (preg_match_all('/#(\w+)/u', $matches[0], $tags)) {
// Ersetzt '#' durch '&num;' für die Ausgabe
$safeHashtags = array_map(function($tag) {
return str_replace('#', '&num;', $tag);
}, $tags[0]);
@ -250,7 +233,6 @@ class YellowMastodonAPI {
$featuredImage = '';
$featuredImageAlt = '';
if (!empty($images)) {
$featuredImage = "Image: " . $images[0] . "\n";
if (isset($alts[0]) && !empty($alts[0])) {
@ -260,7 +242,6 @@ class YellowMastodonAPI {
$htmlSafeAuthorName = str_replace('@', '&#64;', $origAuthor);
// Baut den Blockquote-String robust und schrittweise zusammen
$blockquote = "> \"$content\"";
if (!empty($hashtagString)) {
$blockquote .= "\n>\n> " . $hashtagString;