1
0
Fork 0
mirror of https://github.com/archtechx/laravel-tips.git synced 2025-12-12 05:14:04 +00:00

Weekly Thread command

This commit is contained in:
Samuel Štancl 2021-10-17 21:36:41 +02:00
parent faefe1c45b
commit 3fe86bed08
3 changed files with 80 additions and 5 deletions

View file

@ -0,0 +1,72 @@
<?php
namespace App\Console\Commands;
use App\Models\Thread;
use App\Models\Tip;
use App\Twitter\Tweet;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
class WeeklyThread extends Command
{
protected $signature = 'thread:add {url} {week?}';
protected $description = 'Add a weekly thread';
public function handle()
{
// 41
$week = $this->argument('week') ?? now()->week;
// https://twitter.com/archtechx/status/1448992205138444334?s=20
$url = $this->argument('url');
// 1448992205138444334
$id = $this->extractID($url);
$slug = 'weekly-thread-2021-' . $week;
Tip::where('thread_slug', $slug)->delete();
Thread::where('slug', $slug)->delete();
$tweet = Tweet::fetch($id);
/** @var Thread $thread */
$thread = Thread::create([
'slug' => $slug,
'title' => "Weekly thread #{$week} of 2021",
'tweet_id' => $id,
'author_username' => 'archtechx',
'content' => $tweet->text,
]);
while ($answer = $this->ask('Add tweet? Paste URL', 'no')) {
if (! Str::of($answer)->startsWith('http')) {
break;
}
$tweet = Tweet::fetch($this->extractID($answer));
$child = Tweet::fetch($this->extractID(
Str::of($tweet->text)->afterLast('https://')->prepend('https://')
));
$tweet->images = $child->images;
$tweet->author = $child->author;
$tip = Tip::fromTweet($tweet, $thread->slug);
$tip->content = $child->text;
$tip->save();
}
return 0;
}
protected function extractID(string $tweetURL): string
{
return (string) Str::of($tweetURL)->after('/status/')->before('/')->before('?');
}
}

View file

@ -40,12 +40,15 @@ class Thread extends Model
public static function booted() public static function booted()
{ {
static::creating(fn (self $model) => $model->created_at ??= now()); static::creating(static function (self $model) {
$model->created_at ??= now();
$model->links ??= [];
});
} }
public function tips(): HasMany public function tips(): HasMany
{ {
return $this->hasMany(Tip::class); return $this->hasMany(Tip::class, 'thread_slug');
} }
public function author(): BelongsTo public function author(): BelongsTo

View file

@ -41,7 +41,7 @@ class Tip extends Model
$table->string('slug')->unique(); $table->string('slug')->unique();
$table->string('title'); $table->string('title');
$table->string('tweet_id'); $table->string('tweet_id');
$table->foreignId('thread_slug')->nullable(); $table->string('thread_slug')->nullable();
$table->foreignId('author_username')->constrained('authors', 'username'); $table->foreignId('author_username')->constrained('authors', 'username');
$table->json('images')->default('[]'); $table->json('images')->default('[]');
$table->timestamp('created_at'); $table->timestamp('created_at');
@ -87,9 +87,9 @@ class Tip extends Model
public static function booted() public static function booted()
{ {
static::creating(function (self $model) { static::creating(static function (self $model) {
$model->created_at ??= now(); $model->created_at ??= now();
$model->slug ??= $this->defaultSlug(); $model->slug ??= $model->defaultSlug();
}); });
static::addGlobalScope('order', fn (Builder $query) => $query->orderBy('created_at', 'desc')); static::addGlobalScope('order', fn (Builder $query) => $query->orderBy('created_at', 'desc'));