From 3e468ccd3c78a2cd90d651f697ce69a9ad43acbe Mon Sep 17 00:00:00 2001 From: Ali Hesari Date: Tue, 10 Nov 2026 22:26:31 +0100 Subject: [PATCH] feat(posts): accept default accounts on bulk import Refs fopost/fopost#1219 --- CHANGELOG.md | 3 +++ src/Resource/PostsResource.php | 29 ++++++++++++++++++++++++----- tests/PostsTest.php | 7 ++++++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb6f893..b2371a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ All notable changes to `fopost/sdk` are documented here. ### Added +- `validateBulkImport()` and `commitBulkImport()` take an optional `$accounts` list (account + ids or platform names), sent comma-separated and applied to every CSV row whose own + `accounts` cell is empty. - `$client->inbox()->resolve($workspaceId, $type, $accountId = null, $before = null)` resolves every unanswered item of one type, optionally only on some accounts or only items dated before `$before`, and returns how many changed. Nothing is sent to a platform. diff --git a/src/Resource/PostsResource.php b/src/Resource/PostsResource.php index 0dc75a9..0ed921d 100644 --- a/src/Resource/PostsResource.php +++ b/src/Resource/PostsResource.php @@ -338,13 +338,17 @@ public function unpublish(string $postId): UnpublishResult * Check a CSV of posts row by row without creating anything. * * @param string|array{filename: string, contents: string, mimeType?: string} $file A path or an in-memory file. + * @param array $accounts Ids or platform names for rows whose accounts cell is empty. */ - public function validateBulkImport(string $workspaceId, string|array $file): BulkImportValidation - { + public function validateBulkImport( + string $workspaceId, + string|array $file, + array $accounts = [], + ): BulkImportValidation { $body = $this->http->requestMultipart( 'POST', '/posts/bulk-import/validate', - ['workspace_id' => $workspaceId], + self::bulkImportFields($workspaceId, $accounts), self::normalizeFiles([$file]), 'file', ); @@ -356,13 +360,14 @@ public function validateBulkImport(string $workspaceId, string|array $file): Bul * Create every valid row of the CSV as a post. Needs the `publish` scope as well. * * @param string|array{filename: string, contents: string, mimeType?: string} $file A path or an in-memory file. + * @param array $accounts Ids or platform names for rows whose accounts cell is empty. */ - public function commitBulkImport(string $workspaceId, string|array $file): BulkImportCommit + public function commitBulkImport(string $workspaceId, string|array $file, array $accounts = []): BulkImportCommit { $body = $this->http->requestMultipart( 'POST', '/posts/bulk-import/commit', - ['workspace_id' => $workspaceId], + self::bulkImportFields($workspaceId, $accounts), self::normalizeFiles([$file]), 'file', ); @@ -370,6 +375,20 @@ public function commitBulkImport(string $workspaceId, string|array $file): BulkI return BulkImportCommit::fromArray(self::unwrap($body)); } + /** + * @param array $accounts + * @return array + */ + private static function bulkImportFields(string $workspaceId, array $accounts): array + { + $fields = ['workspace_id' => $workspaceId]; + if ($accounts !== []) { + $fields['accounts'] = implode(',', $accounts); + } + + return $fields; + } + /** Delete every post a bulk import created. */ public function rollbackBulkImport(string $batchId): BulkImportRollback { diff --git a/tests/PostsTest.php b/tests/PostsTest.php index 6c0a0bb..693d4f0 100644 --- a/tests/PostsTest.php +++ b/tests/PostsTest.php @@ -239,7 +239,7 @@ public function testBulkImportSendsTheCsvAsMultipart(): void $csv = ['filename' => 'posts.csv', 'contents' => "text\nHi\n", 'mimeType' => 'text/csv']; $check = $this->client()->posts()->validateBulkImport('w_1', $csv); - $commit = $this->client()->posts()->commitBulkImport('w_1', $csv); + $commit = $this->client()->posts()->commitBulkImport('w_1', $csv, ['twitter', 'linkedin']); $rollback = $this->client()->posts()->rollbackBulkImport('b_1'); $first = $this->transport->requests[0]; @@ -248,10 +248,15 @@ public function testBulkImportSendsTheCsvAsMultipart(): void $this->assertStringStartsWith('multipart/form-data; boundary=', $first['headers']['Content-Type']); $this->assertStringContainsString('name="workspace_id"' . "\r\n\r\nw_1", (string) $first['body']); $this->assertStringContainsString('name="file"; filename="posts.csv"', (string) $first['body']); + $this->assertStringNotContainsString('name="accounts"', (string) $first['body']); $this->assertSame(1, $check->invalidRows); $this->assertSame(['unknown account'], $check->rows[0]->errors); $this->assertSame('https://api.fopost.com/v1/posts/bulk-import/commit', $this->transport->requests[1]['url']); + $this->assertStringContainsString( + 'name="accounts"' . "\r\n\r\ntwitter,linkedin", + (string) $this->transport->requests[1]['body'], + ); $this->assertSame('b_1', $commit->batchId); $this->assertSame('2026-03-01', $commit->posts[0]->scheduleAt?->format('Y-m-d'));