Skip to content

Commit bf5705b

Browse files
committed
Refactor thread to accept topic tag
1 parent bc7679e commit bf5705b

5 files changed

Lines changed: 47 additions & 19 deletions

File tree

app/Jobs/PostToThreadsJob.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function handle(Threads $threads): void
4242
{
4343
$user = User::query()->where('threads_user_id', '9093830107340733')->firstOrFail();
4444

45-
$threads->writePost($user, $this->post->toThreads());
45+
$threadData = $this->post->toThreads();
46+
47+
$threads->writePost($user, $threadData['content'], $threadData['topic_tag']);
4648

4749
$this->post->updateQuietly(['posted_on_threads' => true]);
4850
}

app/Models/Concerns/Threadable.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,33 @@
44

55
namespace App\Models\Concerns;
66

7-
use Spatie\Tags\Tag;
8-
97
trait Threadable
108
{
119
abstract public function threadsUrl(): string;
1210

13-
public function toThreads(): string
11+
public function toThreads(): array
1412
{
15-
$tags = $this->tags
16-
->map(fn (Tag $tag) => $tag->name)
17-
->map(fn (string $tagName) => '#'.str_replace(' ', '', $tagName))
18-
->implode(' ');
13+
$primaryTag = null;
14+
$primaryTagForContent = '';
15+
16+
if ($this->tags->isNotEmpty()) {
17+
$firstName = $this->tags->first()->name;
18+
$primaryTag = str_replace([' ', '.', '&', '@', '!', '?', ',', ';', ':'], '', $firstName);
19+
if ($primaryTag === '') {
20+
$primaryTag = null;
21+
} else {
22+
$primaryTagForContent = '#'.$primaryTag;
23+
}
24+
}
1925

20-
return '🔗 '.$this->title
26+
$content = '🔗 '.$this->title
2127
.PHP_EOL."\n"
2228
.PHP_EOL.$this->threadsUrl()
23-
.PHP_EOL.$tags;
29+
.($primaryTagForContent ? PHP_EOL.$primaryTagForContent : '');
30+
31+
return [
32+
'content' => $content,
33+
'topic_tag' => $primaryTag,
34+
];
2435
}
2536
}

app/Services/Threads/Threads.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,19 @@ public function refreshToken(User $user): void
8383
/**
8484
* Write a post to Threads.
8585
*/
86-
public function writePost(User $user, string $content): void
86+
public function writePost(User $user, string $content, ?string $topicTag = null): void
8787
{
88-
$response = Http::post("{$this->base_url}/v1.0/{$user->threads_user_id}/threads", [
88+
$payload = [
8989
'media_type' => 'TEXT', // TEXT, IMAGE, VIDEO
9090
'text' => $content,
9191
'access_token' => $user->threads_access_token,
92-
]);
92+
];
93+
94+
if ($topicTag !== null) {
95+
$payload['topic_tag'] = $topicTag;
96+
}
97+
98+
$response = Http::post("{$this->base_url}/v1.0/{$user->threads_user_id}/threads", $payload);
9399

94100
if ($response->failed()) {
95101
abort(403, 'Failed to write post to Threads.');

tests/Feature/Concerns/ThreadableTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515

1616
$thread = $post->toThreads();
1717

18-
expect($thread)->toContain('🔗 Building a Laravel Package')
18+
expect($thread['content'])->toContain('🔗 Building a Laravel Package')
1919
->toContain(route('posts.show', [$post, 'utm_source' => 'threads', 'utm_medium' => 'post']))
2020
->toContain('#Laravel');
21+
22+
expect($thread['topic_tag'])->toBe('Laravel');
2123
});
2224

23-
it('formats a thread with multiple tags', function () {
25+
it('formats a thread with multiple tags - only primary in content', function () {
2426
$post = Post::factory()->create([
2527
'title' => 'Testing with Pest',
2628
]);
@@ -31,8 +33,10 @@
3133

3234
$thread = $post->toThreads();
3335

34-
expect($thread)->toContain('#PHP')
35-
->toContain('#Testing');
36+
expect($thread['content'])->toContain('#PHP')
37+
->not->toContain('#Testing');
38+
39+
expect($thread['topic_tag'])->toBe('PHP');
3640
});
3741

3842
it('formats a thread with tags containing spaces', function () {
@@ -45,7 +49,9 @@
4549

4650
$thread = $post->toThreads();
4751

48-
expect($thread)->toContain('#VueJS');
52+
expect($thread['content'])->toContain('#VueJS');
53+
54+
expect($thread['topic_tag'])->toBe('VueJS');
4955
});
5056

5157
it('formats a thread without tags', function () {
@@ -55,6 +61,8 @@
5561

5662
$thread = $post->toThreads();
5763

58-
expect($thread)->toContain('🔗 Hello World')
64+
expect($thread['content'])->toContain('🔗 Hello World')
5965
->not->toContain('#');
66+
67+
expect($thread['topic_tag'])->toBeNull();
6068
});

tests/Feature/Jobs/PostToThreadsJobTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
$job = new PostToThreadsJob($post);
3535
$job->handle(app(Threads::class));
3636

37+
Http::assertSentCount(2);
3738
expect((bool) $post->refresh()->posted_on_threads)->toBeTrue();
3839
});
3940

0 commit comments

Comments
 (0)