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

Fix content, add command, update readme

This commit is contained in:
Samuel Štancl 2021-08-27 18:37:33 +02:00
parent 3c3a5fcfd9
commit 69d86147d7
12 changed files with 70 additions and 35 deletions

View file

@ -0,0 +1,35 @@
<?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 AddTweet extends Command
{
protected $signature = 'tweet:add {tweet}';
protected $description = 'Add a tweet';
public function handle()
{
$id = Str::of($this->argument('tweet'))->afterLast('/')->before('?');
$this->line('Fetching tweet ID ' . $id);
$tip = Tip::fromTweet(
$tweet = Tweet::fetch($id)
);
$tip->slug = $this->ask('Slug', $tip->defaultSlug());
$tip->thread_slug = $this->choice('Thread', Thread::all()->pluck('slug', 'title')->prepend('- None - ', null)->toArray(), null);
$tip->save();
$this->info("Successfully saved tip to: content/tips/{$tip->slug}.md");
}
}

View file

@ -32,7 +32,7 @@ class Thread extends Model
$table->string('slug')->unique();
$table->string('title');
$table->string('tweet_id')->nullable();
$table->foreignId('author_username')->constrained('authors', 'username');
$table->foreignId('author_username')->nullable()->constrained('authors', 'username');
$table->text('content');
$table->json('links')->default('{}');
$table->timestamp('created_at');
@ -58,7 +58,7 @@ class Thread extends Model
if (is_numeric($this->tweet_id)) {
return "https://twitter.com/{$this->author_username}/status/{$this->tweet_id}";
}
return $this->tweet_id;
}

View file

@ -19,7 +19,7 @@ use Illuminate\Support\Str;
* @property string $content
* @property string $tweet_id
* @property string $author_username
* @property string|null $thread_id
* @property string|null $thread_slug
* @property string[] $images
* @property-read Author $author
* @property-read Thread|null $thread
@ -41,7 +41,7 @@ class Tip extends Model
$table->string('slug')->unique();
$table->string('title');
$table->string('tweet_id');
$table->foreignId('thread_slug')->constrained('threads', 'slug')->nullable();
$table->foreignId('thread_slug')->nullable();
$table->foreignId('author_username')->constrained('authors', 'username');
$table->json('images')->default('[]');
$table->timestamp('created_at');
@ -74,7 +74,7 @@ class Tip extends Model
'title' => (string) Str::of(Str::of($tweet->text)->explode("\n")->first())->rtrim('.')->replaceMatches('/^([^a-zA-Z]*)/', ''), // remove any non-ascii characters from the beginning
'content' => (string) Str::of($tweet->text)->explode("\n")->skip(1)->join("\n"),
'tweet_id' => $tweet->id,
'thread_slug' => $threadSlug ?? Thread::firstWhere('tweet_id', $tweet->threadId)->slug,
'thread_slug' => $threadSlug ?? Thread::firstWhere('tweet_id', $tweet->threadId)?->slug,
'author_username' => Author::firstOrCreate([
'username' => $tweet->author->username,
'name' => $tweet->author->name,
@ -89,7 +89,7 @@ class Tip extends Model
{
static::creating(function (self $model) {
$model->created_at ??= now();
$model->slug ??= Str::slug($model->title);
$model->slug ??= $this->defaultSlug();
});
static::addGlobalScope('order', fn (Builder $query) => $query->orderBy('created_at', 'desc'));
@ -104,4 +104,9 @@ class Tip extends Model
{
return false;
}
public function defaultSlug(): string
{
return Str::slug($this->title);
}
}