mirror of
https://github.com/archtechx/laravel-tips.git
synced 2025-12-12 05:14:04 +00:00
initial commit
This commit is contained in:
commit
1beb9dd34c
281 changed files with 56773 additions and 0 deletions
42
app/Models/Author.php
Normal file
42
app/Models/Author.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Orbit\Concerns\Orbital;
|
||||
|
||||
/**
|
||||
* @property string $username
|
||||
* @property string $avatar
|
||||
* @property string $name Full name.
|
||||
* @property-read string $profile_url
|
||||
*/
|
||||
class Author extends Model
|
||||
{
|
||||
use Orbital;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
public static function schema(Blueprint $table)
|
||||
{
|
||||
$table->string('username')->primary();
|
||||
$table->string('avatar');
|
||||
$table->string('name');
|
||||
}
|
||||
|
||||
public function getProfileUrlAttribute(): string
|
||||
{
|
||||
return 'https://twitter.com/' . $this->username;
|
||||
}
|
||||
|
||||
public function getKeyName()
|
||||
{
|
||||
return 'username';
|
||||
}
|
||||
|
||||
public function getIncrementing()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
57
app/Models/Thread.php
Normal file
57
app/Models/Thread.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Twitter\Tweet;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Orbit\Concerns\Orbital;
|
||||
|
||||
class Thread extends Model
|
||||
{
|
||||
use Orbital;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
public static function schema(Blueprint $table)
|
||||
{
|
||||
$table->string('slug')->unique();
|
||||
$table->string('title');
|
||||
$table->string('tweet_id')->nullable();
|
||||
$table->foreignId('author_username')->constrained('authors', 'username');
|
||||
$table->text('content');
|
||||
$table->timestamp('created_at');
|
||||
}
|
||||
|
||||
public static function booted()
|
||||
{
|
||||
static::creating(fn (self $model) => $model->created_at ??= now());
|
||||
}
|
||||
|
||||
public function tips(): HasMany
|
||||
{
|
||||
return $this->hasMany(Tip::class);
|
||||
}
|
||||
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Author::class, 'author_username', 'username');
|
||||
}
|
||||
|
||||
public function getTweetUrlAttribute(): string|null
|
||||
{
|
||||
return "https://twitter.com/{$this->author_username}/status/{$this->tweet_id}";
|
||||
}
|
||||
|
||||
public function getKeyName()
|
||||
{
|
||||
return 'slug';
|
||||
}
|
||||
|
||||
public function getIncrementing()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
103
app/Models/Tip.php
Normal file
103
app/Models/Tip.php
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Twitter\Tweet;
|
||||
use App\Twitter\TwitterImage;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Orbit\Concerns\Orbital;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @property string $slug
|
||||
* @property string $title
|
||||
* @property string $content
|
||||
* @property string $tweet_id
|
||||
* @property string $author_username
|
||||
* @property string|null $thread_id
|
||||
* @property string[] $images
|
||||
* @property-read Author $author
|
||||
* @property-read Thread $thread
|
||||
* @property-read \Illuminate\Eloquent\Collection|Image[] $images
|
||||
* @property-read string $tweet_url
|
||||
*/
|
||||
class Tip extends Model
|
||||
{
|
||||
use HasFactory, Orbital;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
public $casts = [
|
||||
'images' => 'array',
|
||||
];
|
||||
|
||||
public static function schema(Blueprint $table)
|
||||
{
|
||||
$table->string('slug')->unique();
|
||||
$table->string('title');
|
||||
$table->string('tweet_id');
|
||||
$table->foreignId('thread_slug')->constrained('threads', 'slug')->nullable();
|
||||
$table->foreignId('author_username')->constrained('authors', 'username');
|
||||
$table->json('images')->default('[]');
|
||||
$table->timestamp('created_at');
|
||||
}
|
||||
|
||||
public function thread(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Thread::class, 'thread_slug', 'slug');
|
||||
}
|
||||
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Author::class, 'author_username', 'username');
|
||||
}
|
||||
|
||||
public function getTweetUrlAttribute(): string|null
|
||||
{
|
||||
return "https://twitter.com/{$this->author_username}/status/{$this->tweet_id}";
|
||||
}
|
||||
|
||||
/** @return TwitterImage[] */
|
||||
public function images(): array
|
||||
{
|
||||
return array_map(fn (string $url) => new TwitterImage($url), $this->images);
|
||||
}
|
||||
|
||||
public static function fromTweet(Tweet $tweet, string $threadSlug = null): static
|
||||
{
|
||||
return new static([
|
||||
'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,
|
||||
'author_username' => Author::firstOrCreate([
|
||||
'username' => $tweet->author->username,
|
||||
'name' => $tweet->author->name,
|
||||
'avatar' => $tweet->author->profile_image_url,
|
||||
])->username,
|
||||
'images' => array_map(fn (TwitterImage $image) => $image->url, $tweet->images),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function booted()
|
||||
{
|
||||
static::creating(function (self $model) {
|
||||
$model->created_at ??= now();
|
||||
$model->slug ??= Str::slug($model->title);
|
||||
});
|
||||
}
|
||||
|
||||
public function getKeyName()
|
||||
{
|
||||
return 'slug';
|
||||
}
|
||||
|
||||
public function getIncrementing()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue