Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions inc/Abilities/Bluesky/BlueskyDeleteAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,19 @@ public function checkPermission(): bool {
return PermissionHelper::can_manage();
}

public function execute( array $input ): array {
public function execute( array $input ): array|\WP_Error {
$auth = $this->getAuthProvider();
if ( ! $auth ) {
return array(
'success' => false,
'error' => 'Bluesky auth provider not available',
);
return new \WP_Error( 'missing_auth', 'Bluesky auth provider not available', array( 'status' => 401 ) );
}

$session = $auth->get_session();
if ( empty( $session['accessJwt'] ) ) {
return array(
'success' => false,
'error' => 'Bluesky session not available',
);
return new \WP_Error( 'missing_auth', 'Bluesky session not available', array( 'status' => 401 ) );
}

if ( empty( $input['post_uri'] ) ) {
return array(
'success' => false,
'error' => 'post_uri is required',
);
return new \WP_Error( 'missing_param', 'post_uri is required', array( 'status' => 400 ) );
}

$did = $session['did'];
Expand All @@ -109,10 +100,7 @@ public function execute( array $input ): array {
$rkey = end( $uri_parts );

if ( empty( $rkey ) ) {
return array(
'success' => false,
'error' => 'Could not extract rkey from post URI: ' . $post_uri,
);
return new \WP_Error( 'api_error', 'Could not extract rkey from post URI: ' . $post_uri, array( 'status' => 500 ) );
}

$response = wp_remote_post(
Expand All @@ -132,10 +120,7 @@ public function execute( array $input ): array {
);

if ( is_wp_error( $response ) ) {
return array(
'success' => false,
'error' => $response->get_error_message(),
);
return new \WP_Error( 'api_error', $response->get_error_message(), array( 'status' => 500 ) );
}

$status_code = wp_remote_retrieve_response_code( $response );
Expand All @@ -151,10 +136,7 @@ public function execute( array $input ): array {
}

$body = json_decode( wp_remote_retrieve_body( $response ), true );
return array(
'success' => false,
'error' => $body['error'] ?? 'Failed to delete post',
);
return new \WP_Error( 'api_error', $body['error'] ?? 'Failed to delete post', array( 'status' => 500 ) );
}

private function getAuthProvider(): ?BlueskyAuth {
Expand Down
38 changes: 10 additions & 28 deletions inc/Abilities/Bluesky/BlueskyPublishAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,36 +134,27 @@ private function registerAbilities(): void {
* @param array $input Ability input with publish parameters.
* @return array Response with post details or error.
*/
public static function execute_publish( array $input ): array {
public static function execute_publish( array $input ): array|\WP_Error {
$content = $input['content'] ?? '';
$title = $input['title'] ?? '';
$image_url = $input['image_url'] ?? '';
$source_url = $input['source_url'] ?? '';

if ( empty( $content ) ) {
return array(
'success' => false,
'error' => 'Content is required',
);
return new \WP_Error( 'missing_param', 'Content is required', array( 'status' => 400 ) );
}

$auth = new AuthAbilities();
$provider = $auth->getProvider( 'bluesky' );

if ( ! $provider || ! $provider->is_authenticated() ) {
return array(
'success' => false,
'error' => 'Bluesky not authenticated',
);
return new \WP_Error( 'missing_auth', 'Bluesky not authenticated', array( 'status' => 401 ) );
}

// Use the provider's session method which handles auth internally.
$session = $provider->get_session();
if ( is_wp_error( $session ) ) {
return array(
'success' => false,
'error' => $session->get_error_message(),
);
return new \WP_Error( 'api_error', $session->get_error_message(), array( 'status' => 500 ) );
}

$handle = $session['handle'];
Expand Down Expand Up @@ -244,10 +235,7 @@ public static function execute_publish( array $input ): array {
);

if ( is_wp_error( $response ) ) {
return array(
'success' => false,
'error' => $response->get_error_message(),
);
return new \WP_Error( 'api_error', $response->get_error_message(), array( 'status' => 500 ) );
}

$status_code = wp_remote_retrieve_response_code( $response );
Expand All @@ -271,10 +259,7 @@ public static function execute_publish( array $input ): array {
$error_msg = $data['message'];
}

return array(
'success' => false,
'error' => $error_msg,
);
return new \WP_Error( 'api_error', $error_msg, array( 'status' => 500 ) );
}

/**
Expand All @@ -283,16 +268,13 @@ public static function execute_publish( array $input ): array {
* @param array $input Ability input.
* @return array Account details or error.
*/
public static function get_account( array $input ): array {
public static function get_account( array $input ): array|\WP_Error {
$input;
$auth = new AuthAbilities();
$provider = $auth->getProvider( 'bluesky' );

if ( ! $provider || ! $provider->is_authenticated() ) {
return array(
'success' => false,
'error' => 'Bluesky not authenticated',
);
return new \WP_Error( 'missing_auth', 'Bluesky not authenticated', array( 'status' => 401 ) );
}

$details = $provider->get_account_details();
Expand Down Expand Up @@ -358,7 +340,7 @@ private static function upload_image( string $access_token, string $image_url ):
* @param string $text Post text to scan for URLs.
* @return array Array of facet objects, empty if no URLs found.
*/
private static function extract_url_facets( string $text ): array {
private static function extract_url_facets( string $text ): array|\WP_Error {
$facets = array();

// Match URLs in the text.
Expand Down Expand Up @@ -401,7 +383,7 @@ private static function extract_url_facets( string $text ): array {
* @param string $url URL to fetch OG tags from.
* @return array Associative array with 'title', 'description', 'image' keys (empty strings if not found).
*/
private static function fetch_og_tags( string $url ): array {
private static function fetch_og_tags( string $url ): array|\WP_Error {
$defaults = array(
'title' => '',
'description' => '',
Expand Down
68 changes: 16 additions & 52 deletions inc/Abilities/Bluesky/BlueskyReadAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,21 @@ public function checkPermission(): bool {
return PermissionHelper::can_manage();
}

public function execute( array $input ): array {
public function execute( array $input ): array|\WP_Error {
$action = $input['action'] ?? 'list';

$auth = $this->getAuthProvider();
if ( ! $auth ) {
return array(
'success' => false,
'error' => 'Bluesky auth provider not available',
);
return new \WP_Error( 'missing_auth', 'Bluesky auth provider not available', array( 'status' => 401 ) );
}

$session = $auth->get_session();
if ( is_wp_error( $session ) ) {
return array(
'success' => false,
'error' => 'Bluesky session creation failed: ' . $session->get_error_message(),
);
return new \WP_Error( 'api_error', 'Bluesky session creation failed: ' . $session->get_error_message(), array( 'status' => 500 ) );
}

if ( empty( $session['accessJwt'] ) ) {
return array(
'success' => false,
'error' => 'Bluesky authentication failed (no access token in session)',
);
return new \WP_Error( 'missing_auth', 'Bluesky authentication failed (no access token in session)', array( 'status' => 401 ) );
}

$pds_url = $session['pds_url'] ?? 'https://bsky.social';
Expand All @@ -130,30 +121,21 @@ public function execute( array $input ): array {

case 'get':
if ( empty( $input['post_uri'] ) ) {
return array(
'success' => false,
'error' => 'post_uri is required for the get action',
);
return new \WP_Error( 'missing_param', 'post_uri is required for the get action', array( 'status' => 400 ) );
}
return $this->getPostThread( $pds_url, $access_token, $input['post_uri'] );

case 'profile':
return $this->getProfile( $pds_url, $access_token, $did ? $did : $handle );

default:
return array(
'success' => false,
'error' => "Unknown action: {$action}. Use list, get, or profile.",
);
return new \WP_Error( 'api_error', "Unknown action: {$action}. Use list, get, or profile.", array( 'status' => 500 ) );
}
}

private function listPosts( string $pds_url, string $access_token, string $actor, array $input ): array {
private function listPosts( string $pds_url, string $access_token, string $actor, array $input ): array|\WP_Error {
if ( empty( $actor ) ) {
return array(
'success' => false,
'error' => 'Bluesky DID/handle not available. Try re-authenticating.',
);
return new \WP_Error( 'missing_auth', 'Bluesky DID/handle not available. Try re-authenticating.', array( 'status' => 401 ) );
}

$limit = min( absint( $input['limit'] ?? 25 ), 100 );
Expand All @@ -176,21 +158,15 @@ private function listPosts( string $pds_url, string $access_token, string $actor
);

if ( ! $result['success'] ) {
return array(
'success' => false,
'error' => 'Bluesky API request failed: ' . ( $result['error'] ?? 'unknown' ),
);
return new \WP_Error( 'api_error', 'Bluesky API request failed: ' . ( $result['error'] ?? 'unknown' ), array( 'status' => 500 ) );
}

$data = json_decode( $result['data'], true );
$http_code = $result['status_code'];

if ( 200 !== $http_code || isset( $data['error'] ) ) {
$error_msg = $data['message'] ?? $data['error'] ?? 'Failed to fetch Bluesky feed';
return array(
'success' => false,
'error' => $error_msg,
);
return new \WP_Error( 'api_error', $error_msg, array( 'status' => 500 ) );
}

$feed = $data['feed'] ?? array();
Expand All @@ -207,7 +183,7 @@ private function listPosts( string $pds_url, string $access_token, string $actor
);
}

private function getPostThread( string $pds_url, string $access_token, string $post_uri ): array {
private function getPostThread( string $pds_url, string $access_token, string $post_uri ): array|\WP_Error {
$params = array(
'uri' => $post_uri,
'depth' => 6,
Expand All @@ -223,21 +199,15 @@ private function getPostThread( string $pds_url, string $access_token, string $p
);

if ( ! $result['success'] ) {
return array(
'success' => false,
'error' => 'Bluesky API request failed: ' . ( $result['error'] ?? 'unknown' ),
);
return new \WP_Error( 'api_error', 'Bluesky API request failed: ' . ( $result['error'] ?? 'unknown' ), array( 'status' => 500 ) );
}

$data = json_decode( $result['data'], true );
$http_code = $result['status_code'];

if ( 200 !== $http_code || isset( $data['error'] ) ) {
$error_msg = $data['message'] ?? $data['error'] ?? 'Failed to fetch post thread';
return array(
'success' => false,
'error' => $error_msg,
);
return new \WP_Error( 'api_error', $error_msg, array( 'status' => 500 ) );
}

return array(
Expand All @@ -246,7 +216,7 @@ private function getPostThread( string $pds_url, string $access_token, string $p
);
}

private function getProfile( string $pds_url, string $access_token, string $actor ): array {
private function getProfile( string $pds_url, string $access_token, string $actor ): array|\WP_Error {
$params = array( 'actor' => $actor );

$url = $pds_url . '/xrpc/app.bsky.actor.getProfile?' . http_build_query( $params );
Expand All @@ -259,21 +229,15 @@ private function getProfile( string $pds_url, string $access_token, string $acto
);

if ( ! $result['success'] ) {
return array(
'success' => false,
'error' => 'Bluesky API request failed: ' . ( $result['error'] ?? 'unknown' ),
);
return new \WP_Error( 'api_error', 'Bluesky API request failed: ' . ( $result['error'] ?? 'unknown' ), array( 'status' => 500 ) );
}

$data = json_decode( $result['data'], true );
$http_code = $result['status_code'];

if ( 200 !== $http_code || isset( $data['error'] ) ) {
$error_msg = $data['message'] ?? $data['error'] ?? 'Failed to fetch profile';
return array(
'success' => false,
'error' => $error_msg,
);
return new \WP_Error( 'api_error', $error_msg, array( 'status' => 500 ) );
}

return array(
Expand Down
Loading
Loading