From 3fe86bed088b71b885b00e20fc5c7627f387955f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 17 Oct 2021 21:36:41 +0200 Subject: [PATCH] Weekly Thread command --- app/Console/Commands/WeeklyThread.php | 72 +++++++++++++++++++++++++++ app/Models/Thread.php | 7 ++- app/Models/Tip.php | 6 +-- 3 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 app/Console/Commands/WeeklyThread.php diff --git a/app/Console/Commands/WeeklyThread.php b/app/Console/Commands/WeeklyThread.php new file mode 100644 index 0000000..3c1fdb2 --- /dev/null +++ b/app/Console/Commands/WeeklyThread.php @@ -0,0 +1,72 @@ +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('?'); + } +} diff --git a/app/Models/Thread.php b/app/Models/Thread.php index 2a13090..c609e44 100644 --- a/app/Models/Thread.php +++ b/app/Models/Thread.php @@ -40,12 +40,15 @@ class Thread extends Model 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 { - return $this->hasMany(Tip::class); + return $this->hasMany(Tip::class, 'thread_slug'); } public function author(): BelongsTo diff --git a/app/Models/Tip.php b/app/Models/Tip.php index d890796..4180f46 100644 --- a/app/Models/Tip.php +++ b/app/Models/Tip.php @@ -41,7 +41,7 @@ class Tip extends Model $table->string('slug')->unique(); $table->string('title'); $table->string('tweet_id'); - $table->foreignId('thread_slug')->nullable(); + $table->string('thread_slug')->nullable(); $table->foreignId('author_username')->constrained('authors', 'username'); $table->json('images')->default('[]'); $table->timestamp('created_at'); @@ -87,9 +87,9 @@ class Tip extends Model public static function booted() { - static::creating(function (self $model) { + static::creating(static function (self $model) { $model->created_at ??= now(); - $model->slug ??= $this->defaultSlug(); + $model->slug ??= $model->defaultSlug(); }); static::addGlobalScope('order', fn (Builder $query) => $query->orderBy('created_at', 'desc'));