chore(*): remove duplicate cache rule + cleanup cache headers; cleanup + formatting

This commit is contained in:
Cory Dransfeldt 2025-05-17 13:25:40 -07:00
parent 425fed6ff6
commit 0e565970a5
No known key found for this signature in database
42 changed files with 223 additions and 217 deletions

View file

@ -10,19 +10,16 @@ class MastodonPostHandler extends ApiHandler
private string $mastodonAccessToken;
private string $rssFeedUrl = "https://www.coryd.dev/feeds/syndication.xml";
private string $baseUrl = "https://www.coryd.dev";
private const MASTODON_API_STATUS = "https://follow.coryd.dev/api/v1/statuses";
private Client $httpClient;
public function __construct(?Client $httpClient = null)
{
parent::__construct();
$this->ensureCliAccess();
$this->ensureCliAccess();
$this->mastodonAccessToken = getenv("MASTODON_ACCESS_TOKEN") ?: $_ENV["MASTODON_ACCESS_TOKEN"] ?? "";
$this->httpClient = $httpClient ?: new Client();
$this->validateAuthorization();
}
@ -49,6 +46,7 @@ class MastodonPostHandler extends ApiHandler
foreach (array_reverse($latestItems) as $item) {
$existing = $this->fetchFromApi("mastodon_posts", "link=eq." . urlencode($item["link"]));
if (!empty($existing)) continue;
$content = $this->truncateContent(
@ -57,7 +55,6 @@ class MastodonPostHandler extends ApiHandler
$item["link"],
500
);
$timestamp = date("Y-m-d H:i:s");
if (!$this->storeInDatabase($item["link"], $timestamp)) {
@ -66,6 +63,7 @@ class MastodonPostHandler extends ApiHandler
}
$postedUrl = $this->postToMastodon($content, $item["image"] ?? null);
if ($postedUrl) {
echo "Posted: {$postedUrl}\n";
} else {
@ -79,6 +77,7 @@ class MastodonPostHandler extends ApiHandler
private function fetchRSSFeed(string $rssFeedUrl): array
{
$rssText = file_get_contents($rssFeedUrl);
if (!$rssText) throw new \Exception("Failed to fetch RSS feed.");
$rss = new \SimpleXMLElement($rssText);
@ -106,9 +105,11 @@ class MastodonPostHandler extends ApiHandler
"headers" => $headers,
"json" => $postData
]);
if ($response->getStatusCode() >= 400) throw new \Exception("Mastodon post failed: {$response->getBody()}");
$body = json_decode($response->getBody()->getContents(), true);
return $body["url"] ?? null;
}
@ -121,9 +122,11 @@ class MastodonPostHandler extends ApiHandler
"created_at" => $timestamp
]
]);
return true;
} catch (\Exception $e) {
echo "Error storing post in DB: " . $e->getMessage() . "\n";
return false;
}
}
@ -132,9 +135,11 @@ class MastodonPostHandler extends ApiHandler
{
try {
$response = $this->fetchFromApi("mastodon_posts", "limit=1");
return is_array($response);
} catch (\Exception $e) {
echo "Database check failed: " . $e->getMessage() . "\n";
return false;
}
}
@ -158,5 +163,6 @@ try {
$handler->handlePost();
} catch (\Exception $e) {
http_response_code(500);
echo json_encode(["error" => $e->getMessage()]);
}