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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/Model/InboxItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'),
Expand Down
30 changes: 30 additions & 0 deletions src/Model/XFollowResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** The outcome of following or unfollowing an X user from a connected account. */
final class XFollowResult extends Model
{
private function __construct(
array $raw,
public readonly string $accountId,
public readonly string $userId,
public readonly bool $following,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'account_id'),
self::requiredStr($data, 'user_id'),
self::bool($data, 'following') ?? false,
);
}
}
56 changes: 56 additions & 0 deletions src/Model/XPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

use DateTimeImmutable;

/** An X post read live with a connected account's token. */
final class XPost extends Model
{
private function __construct(
array $raw,
public readonly string $id,
public readonly ?string $text,
public readonly ?string $permalink,
public readonly ?string $authorName,
public readonly ?string $authorHandle,
public readonly ?string $authorAvatarUrl,
public readonly ?int $authorFollowers,
/** Thread root id. */
public readonly ?string $conversationId,
/** The post this one replied to. */
public readonly ?string $parentExternalId,
public readonly ?int $likes,
public readonly ?int $replies,
public readonly ?int $reposts,
public readonly ?int $quotes,
public readonly ?DateTimeImmutable $platformCreatedAt,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'id'),
self::str($data, 'text'),
self::str($data, 'permalink'),
self::str($data, 'author_name'),
self::str($data, 'author_handle'),
self::str($data, 'author_avatar_url'),
self::int($data, 'author_followers'),
self::str($data, 'conversation_id'),
self::str($data, 'parent_external_id'),
self::int($data, 'likes'),
self::int($data, 'replies'),
self::int($data, 'reposts'),
self::int($data, 'quotes'),
self::date($data, 'platform_created_at'),
);
}
}
30 changes: 30 additions & 0 deletions src/Model/XSearchPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** One page of X search results; pass `nextToken` back as `next_token` for the next. */
final class XSearchPage extends Model
{
/** @param array<int, XPost> $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'),
);
}
}
16 changes: 16 additions & 0 deletions src/Resource/AccountsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
44 changes: 44 additions & 0 deletions src/Resource/InboxResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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<string, mixed> $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),
);
}
}
21 changes: 21 additions & 0 deletions tests/AccountsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
Loading
Loading