16c068b4b0
Add media capability reporting to MokoJoomCrossServiceInterface. Each plugin now returns its supported media types: - image, video, gif, document (per platform capability) - Empty array for text-only services (Nostr, Ntfy, ConvertKit) Enables the dispatcher to skip media attachments for text-only services and choose appropriate media types per platform. Authored-by: Moko Consulting Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
134 lines
4.5 KiB
PHP
134 lines
4.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @package MokoJoomCross
|
|
* @subpackage plg_mokojoomcross_hashnode
|
|
* @author Moko Consulting <hello@mokoconsulting.tech>
|
|
* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved.
|
|
* @license GNU General Public License version 3 or later; see LICENSE
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*/
|
|
|
|
namespace Joomla\Plugin\MokoJoomCross\Hashnode\Extension;
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
use Joomla\CMS\Plugin\CMSPlugin;
|
|
use Joomla\Component\MokoJoomCross\Administrator\Service\MokoJoomCrossServiceInterface;
|
|
use Joomla\Event\SubscriberInterface;
|
|
|
|
/**
|
|
* Hashnode service plugin for MokoJoomCross.
|
|
*
|
|
* Uses the Hashnode GraphQL API at https://gql.hashnode.com.
|
|
*/
|
|
class HashnodeService extends CMSPlugin implements SubscriberInterface, MokoJoomCrossServiceInterface
|
|
{
|
|
public static function getSubscribedEvents(): array
|
|
{
|
|
return ['onMokoJoomCrossGetServices' => 'onMokoJoomCrossGetServices'];
|
|
}
|
|
|
|
public function onMokoJoomCrossGetServices(&$services): void
|
|
{
|
|
$services[] = $this;
|
|
}
|
|
|
|
public function getServiceType(): string { return 'hashnode'; }
|
|
public function getServiceName(): string { return 'Hashnode'; }
|
|
public function getMaxLength(): int { return 0; }
|
|
public function supportsMedia(): bool { return true; }
|
|
|
|
public function publish(string $message, array $media, array $credentials, array $params): array
|
|
{
|
|
$token = $credentials['token'] ?? '';
|
|
$publicationId = $credentials['publication_id'] ?? '';
|
|
|
|
if (empty($token) || empty($publicationId)) {
|
|
return ['success' => false, 'platform_post_id' => '', 'response' => ['error' => 'Missing API token or publication ID.']];
|
|
}
|
|
|
|
$title = mb_substr(strip_tags($message), 0, 150);
|
|
|
|
$query = 'mutation PublishPost($input: PublishPostInput!) { publishPost(input: $input) { post { id url } } }';
|
|
$variables = [
|
|
'input' => [
|
|
'title' => $title,
|
|
'contentMarkdown' => $message,
|
|
'publicationId' => $publicationId,
|
|
'tags' => [],
|
|
],
|
|
];
|
|
|
|
$payload = json_encode(['query' => $query, 'variables' => $variables]);
|
|
|
|
$ch = curl_init('https://gql.hashnode.com');
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $payload,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Authorization: ' . $token,
|
|
'Content-Type: application/json',
|
|
],
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
$data = json_decode($response, true) ?: [];
|
|
|
|
$postId = $data['data']['publishPost']['post']['id'] ?? '';
|
|
|
|
if ($httpCode >= 200 && $httpCode < 300 && !empty($postId)) {
|
|
return ['success' => true, 'platform_post_id' => $postId, 'response' => $data];
|
|
}
|
|
|
|
return ['success' => false, 'platform_post_id' => '', 'response' => $data];
|
|
}
|
|
|
|
public function validateCredentials(array $credentials): array
|
|
{
|
|
$token = $credentials['token'] ?? '';
|
|
$publicationId = $credentials['publication_id'] ?? '';
|
|
|
|
if (empty($token) || empty($publicationId)) {
|
|
return ['valid' => false, 'message' => 'API token and publication ID are required.', 'account_name' => ''];
|
|
}
|
|
|
|
$query = '{ me { username name } }';
|
|
$payload = json_encode(['query' => $query]);
|
|
|
|
$ch = curl_init('https://gql.hashnode.com');
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $payload,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Authorization: ' . $token,
|
|
'Content-Type: application/json',
|
|
],
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 10,
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$data = json_decode($response, true) ?: [];
|
|
$name = $data['data']['me']['name'] ?? $data['data']['me']['username'] ?? '';
|
|
|
|
if (!empty($name)) {
|
|
return ['valid' => true, 'message' => 'Connected', 'account_name' => $name];
|
|
}
|
|
|
|
return ['valid' => false, 'message' => 'Failed to verify token.', 'account_name' => ''];
|
|
}
|
|
|
|
public function getSupportedMediaTypes(): array
|
|
{
|
|
return ['image'];
|
|
}
|
|
}
|