diff --git a/CHANGELOG.md b/CHANGELOG.md index b193ab7..baed60e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,17 @@ All notable changes to `fopost/sdk` are documented here. - Custom conversions: `customConversions()`, `createCustomConversion()`, `customConversion()`, `updateCustomConversion()` and `deleteCustomConversion()`, with `CustomConversion` and `CustomConversionRule`. +- `$client->inbox()->repost()`, `unrepost()`, `bookmark()` and `unbookmark()` for X items, + mirroring `like()`/`unlike()` (`inbox` + `publish` scopes). `InboxItem` carries `reposted`, + `bookmarked`, `canRepost` and `canBookmark`. +- `$client->accounts()->followXUser($accountId, $userId)` and `unfollowXUser()` (numeric X user + id, `inbox` + `publish` scopes), returning an `XFollowResult`. +- `$client->inbox()->getXPost($postId, $accountId)` and `searchXPosts($q, $accountId, $options)` + (`inbox` scope): live X reads through the account's token, as `XPost` and `XSearchPage` + (`posts` plus `nextToken` to pass back as `next_token`). +- Every one of these is an X API call metered to the workspace; a spent X balance is a 402 + `platform_balance_spent`. An X account connected before this release must be reconnected for + repost, bookmark and follow, and answers 409 `reconnect_required` until then. ## 0.4.0 - 2026-10-18 diff --git a/CLAUDE.md b/CLAUDE.md index 8eb0d9c..7ac659c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -65,6 +65,7 @@ src/ TelegramConnectCode.php TelegramConnectStatus.php TelegramBotCommand.php TelegramBotCommands.php SlackChannel.php SlackMember.php SlackIdentity.php Inbox*.php (item, thread, conversation, account, platform, approval, reply, refresh and start-conversation results) + XPost.php XSearchPage.php XFollowResult.php Ad.php AdInsights.php ExternalAd.php AdConnection.php AdSource.php BoostablePost.php Audience.php AudiencesResult.php CreatedAudience.php TargetingOption.php LeadForm.php LeadFormSource.php Lead.php LeadsPage.php LeadFormDetail.php FeedLead.php LeadsFeedPage.php LeadPage.php @@ -101,7 +102,9 @@ resource is added. `scripts/sdk-gaps.mjs` in `fopost-api-collections` lists what - `inbox` (scope `inbox`) covers `/inbox`, `/inbox/posts`, `/inbox/conversations`, `unread-count`, `accounts`, `platforms`, `read`, `refresh`, `approvals` (+ `approve`/`reject`, integer ids), and - per-item `PATCH`, `reply`, `hide`, `unhide`, `DELETE`. Not wrapped: `/inbox/chat/*` (browser + per-item `PATCH`, `reply`, `hide`, `unhide`, `DELETE`, `like`/`unlike`, `repost`/`unrepost`, + `bookmark`/`unbookmark`, plus the X pass-through reads `/x/posts/{id}` and `/x/search` + (`getXPost`, `searchXPosts`, `account_id` required). Not wrapped: `/inbox/chat/*` (browser encrypted X Chat) and `/inbox/{id}/attachments/{index}` (a binary stream; the SDK has no binary download path). The read and refresh bodies are snake_case, the item `PATCH` is camelCase (`snoozedUntil`), and list `meta` is `{page, perPage, total}` — `PageMeta` reads `page` as diff --git a/README.md b/README.md index 43fba63..19329eb 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,11 @@ $subreddits = $client->accounts()->listRedditSubreddits('acc_1'); $rules = $client->accounts()->listRedditSubredditRules('acc_1', 'webdev'); $flairs = $client->accounts()->listRedditFlairs('acc_1', 'webdev'); $client->accounts()->setRedditDefaultSubreddit('acc_1', 'webdev'); + +// X: follow and unfollow by numeric X user id. Needs the `publish` scope too. An X account +// connected before this shipped answers 409 `reconnect_required` until it is reconnected. +$client->accounts()->followXUser('acc_1', '2244994945'); +$client->accounts()->unfollowXUser('acc_1', '2244994945'); ``` X communities linked to an account: @@ -365,6 +370,8 @@ $client->inbox()->delete($item->id); // also deletes our own reply // These also need the `publish` scope; the item's can* flags say where each works. $client->inbox()->like($item->id); // unlike() +$client->inbox()->repost($item->id); // unrepost(); X, where canRepost is true +$client->inbox()->bookmark($item->id); // unbookmark(); X, where canBookmark is true $client->inbox()->vote($item->id, 'down'); // 'up', 'down', or 'none' to take it back $client->inbox()->pin($item->id); // unpin() $client->inbox()->react($item->id, '❤️'); // null removes ours @@ -383,6 +390,12 @@ foreach ($client->inbox()->listApprovals($workspaceId) as $approval) { $client->inbox()->approveReply($approval->id); // or approveReply($id, 'edited text') } $client->inbox()->rejectReply($approval->id); + +// X pass-through reads, with the X account whose token does the read. Each one is an X API +// call metered to the workspace; an empty X balance is a 402 `platform_balance_spent`. +$post = $client->inbox()->getXPost('1790000000000000001', $accountId); +$results = $client->inbox()->searchXPosts('launch day', $accountId, ['max_results' => 20]); +$more = $client->inbox()->searchXPosts('launch day', $accountId, ['next_token' => $results->nextToken]); ``` ## Contacts diff --git a/src/Model/InboxItem.php b/src/Model/InboxItem.php index 1f4768c..2d12f78 100644 --- a/src/Model/InboxItem.php +++ b/src/Model/InboxItem.php @@ -43,11 +43,15 @@ private function __construct( public readonly ?bool $canHide, public readonly ?bool $canDelete, public readonly ?bool $liked, + public readonly ?bool $reposted, + public readonly ?bool $bookmarked, public readonly ?string $vote, public readonly ?bool $pinned, public readonly ?string $reaction, public readonly ?DateTimeImmutable $editedAt, public readonly ?bool $canLike, + public readonly ?bool $canRepost, + public readonly ?bool $canBookmark, public readonly ?bool $canVote, public readonly ?bool $canPin, public readonly ?bool $canEdit, @@ -95,11 +99,15 @@ public static function fromArray(mixed $data): static self::bool($data, 'can_hide'), self::bool($data, 'can_delete'), self::bool($data, 'liked'), + self::bool($data, 'reposted'), + self::bool($data, 'bookmarked'), self::str($data, 'vote'), self::bool($data, 'pinned'), self::str($data, 'reaction'), self::date($data, 'edited_at'), self::bool($data, 'can_like'), + self::bool($data, 'can_repost'), + self::bool($data, 'can_bookmark'), self::bool($data, 'can_vote'), self::bool($data, 'can_pin'), self::bool($data, 'can_edit'), diff --git a/src/Model/XFollowResult.php b/src/Model/XFollowResult.php new file mode 100644 index 0000000..f328e4f --- /dev/null +++ b/src/Model/XFollowResult.php @@ -0,0 +1,30 @@ + $posts */ + private function __construct( + array $raw, + public readonly array $posts, + public readonly ?string $nextToken, + ) { + parent::__construct($raw); + } + + public static function fromArray(mixed $data): static + { + $data = is_array($data) ? $data : []; + $meta = self::nested($data, 'meta') ?? []; + + return new self( + $data, + XPost::listFrom(self::seq($data, 'data')), + self::str($meta, 'next_token'), + ); + } +} diff --git a/src/Resource/AccountsResource.php b/src/Resource/AccountsResource.php index 93831fa..2221629 100644 --- a/src/Resource/AccountsResource.php +++ b/src/Resource/AccountsResource.php @@ -46,6 +46,7 @@ use Fopost\Sdk\Model\WebhookSubscription; use Fopost\Sdk\Model\XCommunity; use Fopost\Sdk\Model\XCommunityMatch; +use Fopost\Sdk\Model\XFollowResult; use Fopost\Sdk\Model\TikTokCreatorInfo; use Fopost\Sdk\Model\TikTokMusic; use Fopost\Sdk\Model\TikTokPlace; @@ -247,6 +248,21 @@ public function setRedditDefaultSubreddit(string $accountId, ?string $subreddit) )); } + /** Follow an X user from this account; $userId is the numeric X user id. Needs the `publish` scope. */ + public function followXUser(string $accountId, string $userId): XFollowResult + { + return XFollowResult::fromArray(self::unwrap( + $this->http->post("/accounts/{$accountId}/x/follow", ['user_id' => $userId]), + )); + } + + public function unfollowXUser(string $accountId, string $userId): XFollowResult + { + return XFollowResult::fromArray(self::unwrap( + $this->http->post("/accounts/{$accountId}/x/unfollow", ['user_id' => $userId]), + )); + } + /** * Channels the Slack app can post to in the connected workspace. * diff --git a/src/Resource/InboxResource.php b/src/Resource/InboxResource.php index 7e8dd5d..a15f734 100644 --- a/src/Resource/InboxResource.php +++ b/src/Resource/InboxResource.php @@ -17,6 +17,8 @@ use Fopost\Sdk\Model\InboxStartConversationResult; use Fopost\Sdk\Model\InboxThread; use Fopost\Sdk\Model\Page; +use Fopost\Sdk\Model\XPost; +use Fopost\Sdk\Model\XSearchPage; /** * $client->inbox(): comments, mentions, reviews and direct messages on connected accounts. @@ -248,6 +250,28 @@ public function unlike(string $itemId): InboxItem return InboxItem::fromArray(self::unwrap($this->http->post("/inbox/{$itemId}/unlike"))); } + /** Repost the item on X, where canRepost is true. Needs the `publish` scope. */ + public function repost(string $itemId): InboxItem + { + return InboxItem::fromArray(self::unwrap($this->http->post("/inbox/{$itemId}/repost"))); + } + + public function unrepost(string $itemId): InboxItem + { + return InboxItem::fromArray(self::unwrap($this->http->post("/inbox/{$itemId}/unrepost"))); + } + + /** Bookmark the item on X, where canBookmark is true. Needs the `publish` scope. */ + public function bookmark(string $itemId): InboxItem + { + return InboxItem::fromArray(self::unwrap($this->http->post("/inbox/{$itemId}/bookmark"))); + } + + public function unbookmark(string $itemId): InboxItem + { + return InboxItem::fromArray(self::unwrap($this->http->post("/inbox/{$itemId}/unbookmark"))); + } + /** * Vote up or down where the network ranks by votes (Reddit), where canVote is true. * @@ -365,4 +389,24 @@ public function rejectReply(int $approvalId): InboxApprovalDecision self::unwrap($this->http->post("/inbox/approvals/{$approvalId}/reject")), ); } + + /** One X post by id, read with the given X account's token; 404 when X has no such post. */ + public function getXPost(string $postId, string $accountId): XPost + { + return XPost::fromArray(self::unwrap($this->http->get("/x/posts/{$postId}", ['account_id' => $accountId]))); + } + + /** + * Recent X posts matching $q (1..512 characters), read with the given X account's token. + * + * $options: `max_results` (10..100) and `next_token` from the previous page. + * + * @param array $options + */ + public function searchXPosts(string $q, string $accountId, array $options = []): XSearchPage + { + return XSearchPage::fromArray( + $this->http->get('/x/search', ['q' => $q, 'account_id' => $accountId] + $options), + ); + } } diff --git a/tests/AccountsTest.php b/tests/AccountsTest.php index 23c2cde..03de155 100644 --- a/tests/AccountsTest.php +++ b/tests/AccountsTest.php @@ -460,6 +460,27 @@ public function testInstagramAndLinkedInReads(): void $this->assertStringEndsWith('(urn:li:organization:2414183)', $mentions[0]->annotation); } + public function testXFollowAndUnfollowSendTheUserId(): void + { + $followed = ['account_id' => 'a_1', 'user_id' => '2244994945']; + $this->transport->push(200, ['data' => $followed + ['following' => true]]); + $result = $this->client()->accounts()->followXUser('a_1', '2244994945'); + + $this->assertSame('POST', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/accounts/a_1/x/follow', $this->transport->last()['url']); + $this->assertSame(['user_id' => '2244994945'], $this->transport->lastJson()); + $this->assertSame('a_1', $result->accountId); + $this->assertSame('2244994945', $result->userId); + $this->assertTrue($result->following); + + $this->transport->push(200, ['data' => $followed + ['following' => false]]); + $result = $this->client()->accounts()->unfollowXUser('a_1', '2244994945'); + + $this->assertSame('https://api.fopost.com/v1/accounts/a_1/x/unfollow', $this->transport->last()['url']); + $this->assertSame(['user_id' => '2244994945'], $this->transport->lastJson()); + $this->assertFalse($result->following); + } + public function testValidateRefreshPrimaryAndHistoryReachTheAccount(): void { $this->transport->push(200, [ diff --git a/tests/InboxTest.php b/tests/InboxTest.php index 6a00f6e..f36bf46 100644 --- a/tests/InboxTest.php +++ b/tests/InboxTest.php @@ -341,4 +341,77 @@ public function testApprovalsUseIntegerIdsAndAnOptionalText(): void $this->assertSame('https://api.fopost.com/v1/inbox/approvals/42/reject', $this->transport->last()['url']); $this->assertSame('rejected', $decision->outcome); } + + public function testRepostAndBookmarkReturnTheItem(): void + { + $acted = self::item([ + 'reposted' => true, + 'bookmarked' => true, + 'canRepost' => true, + 'canBookmark' => true, + ]); + + foreach (['repost', 'unrepost', 'bookmark', 'unbookmark'] as $action) { + $this->transport->push(200, ['data' => $acted]); + $item = $this->client()->inbox()->{$action}('i_1'); + $this->assertSame('POST', $this->transport->last()['method']); + $this->assertSame("https://api.fopost.com/v1/inbox/i_1/{$action}", $this->transport->last()['url']); + $this->assertNull($this->transport->last()['body']); + $this->assertTrue($item->reposted); + $this->assertTrue($item->bookmarked); + $this->assertTrue($item->canRepost); + $this->assertTrue($item->canBookmark); + } + } + + public function testXPostLookupAndSearch(): void + { + $this->transport->push(200, ['data' => [ + 'id' => '1790000000000000001', + 'text' => 'Launch day', + 'permalink' => 'https://x.com/yourbrand/status/1790000000000000001', + 'author_name' => 'Your Brand', + 'author_handle' => 'yourbrand', + 'author_followers' => 1200, + 'conversation_id' => '1790000000000000001', + 'likes' => 4, + 'replies' => 1, + 'reposts' => 2, + 'quotes' => 0, + 'platform_created_at' => '2026-10-18T09:00:00Z', + ]]); + $post = $this->client()->inbox()->getXPost('1790000000000000001', 'a_1'); + + $this->assertSame('GET', $this->transport->last()['method']); + $this->assertSame( + 'https://api.fopost.com/v1/x/posts/1790000000000000001?account_id=a_1', + $this->transport->last()['url'], + ); + $this->assertSame('yourbrand', $post->authorHandle); + $this->assertSame(2, $post->reposts); + $this->assertSame('2026-10-18', $post->platformCreatedAt?->format('Y-m-d')); + + $this->transport->push(200, [ + 'data' => [['id' => '1790000000000000002', 'text' => 'Nice launch']], + 'meta' => ['next_token' => 'b26v89c19zqg8o3fpe'], + ]); + $page = $this->client()->inbox()->searchXPosts('launch day', 'a_1', ['max_results' => 20]); + + $this->assertSame( + 'https://api.fopost.com/v1/x/search?q=launch+day&account_id=a_1&max_results=20', + $this->transport->last()['url'], + ); + $this->assertCount(1, $page->posts); + $this->assertSame('1790000000000000002', $page->posts[0]->id); + $this->assertSame('b26v89c19zqg8o3fpe', $page->nextToken); + + $this->transport->push(200, ['data' => [], 'meta' => ['next_token' => null]]); + $page = $this->client()->inbox()->searchXPosts('launch day', 'a_1', ['next_token' => $page->nextToken]); + $this->assertSame( + 'https://api.fopost.com/v1/x/search?q=launch+day&account_id=a_1&next_token=b26v89c19zqg8o3fpe', + $this->transport->last()['url'], + ); + $this->assertSame([], $page->posts); + $this->assertNull($page->nextToken); + } }