diff --git a/app/Http/Controllers/DeletionController.php b/app/Http/Controllers/DeletionController.php index f8a09f0..eb9929e 100644 --- a/app/Http/Controllers/DeletionController.php +++ b/app/Http/Controllers/DeletionController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Comment; +use App\Models\Post; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -19,4 +20,12 @@ class DeletionController extends Controller $comment->delete(); return redirect("/posts/$postid"); } + + public function deletePost(Post $post) + { + $post->featured = null; + $post->save(); + $post->delete(); + return redirect()->route('posts.home'); + } } diff --git a/app/Livewire/Pages/Profile.php b/app/Livewire/Pages/Profile.php new file mode 100644 index 0000000..1b2d3d8 --- /dev/null +++ b/app/Livewire/Pages/Profile.php @@ -0,0 +1,23 @@ +user->favorites()->withType(Post::class)->count(); + $favorite_comments = $this->user->favorites()->withType(Comment::class)->count(); + + return view('livewire.pages.profile', [ + 'favorite_posts' => $favorite_posts, 'favorite_comments' => $favorite_comments + ])->title($this->user->name); + } +} diff --git a/app/Livewire/Pages/Upload.php b/app/Livewire/Pages/Upload.php index 3ad9464..70ff7ee 100644 --- a/app/Livewire/Pages/Upload.php +++ b/app/Livewire/Pages/Upload.php @@ -15,7 +15,7 @@ class Upload extends Component { use WithFileUploads; - #[Validate('extensions:jpg,jpeg,bmp,gif,png,webp,apng,mp4,wmv,mkv|mimes:jpg,jpeg,bmp,gif,png,webp,apng,mp4,wmv,mkv|max:81920')] + #[Validate('file')] public $file; #[Validate('required|in:safe,suggestive,explicit')] diff --git a/app/Livewire/Search.php b/app/Livewire/Search.php new file mode 100644 index 0000000..de56fca --- /dev/null +++ b/app/Livewire/Search.php @@ -0,0 +1,29 @@ +searchResults = (new SpatieSearch()) + ->registerModel(User::class, 'name') + ->perform($this->searchText); + } +} diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 7778d63..1ba8db3 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -5,10 +5,11 @@ namespace App\Models; use MongoDB\Laravel\Eloquent\Model; use MongoDB\Laravel\Eloquent\SoftDeletes; use MongoDB\Laravel\Relations\BelongsTo; +use Overtrue\LaravelFavorite\Traits\Favoriteable; class Comment extends Model { - use SoftDeletes; + use SoftDeletes, Favoriteable; protected $fillable = [ 'message' ]; diff --git a/app/Models/Post.php b/app/Models/Post.php index 8dac764..488e1c6 100644 --- a/app/Models/Post.php +++ b/app/Models/Post.php @@ -9,11 +9,12 @@ use MongoDB\Laravel\Eloquent\SoftDeletes; use MongoDB\Laravel\Relations\BelongsTo; use MongoDB\Laravel\Relations\BelongsToMany; use MongoDB\Laravel\Relations\HasMany; +use Overtrue\LaravelFavorite\Traits\Favoriteable; use Symfony\Component\HttpFoundation\StreamedResponse; class Post extends Model { - use SoftDeletes; + use SoftDeletes, Favoriteable; protected $fillable = [ 'rating', 'extension', 'featured' ]; diff --git a/app/Models/User.php b/app/Models/User.php index ac569a8..647af6d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,19 +2,21 @@ namespace App\Models; -// use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; use MongoDB\Laravel\Auth\User as Authenticatable; use MongoDB\Laravel\Relations\HasMany; +use Overtrue\LaravelFavorite\Traits\Favoriter; +use Spatie\Searchable\Searchable; +use Spatie\Searchable\SearchResult; -class User extends Authenticatable +class User extends Authenticatable implements Searchable { protected $connection = 'mongodb'; protected $table = 'users'; - use HasApiTokens, HasFactory, Notifiable; + use HasApiTokens, HasFactory, Notifiable, Favoriter; protected $fillable = [ 'name', @@ -43,4 +45,14 @@ class User extends Authenticatable { return $this->hasMany(Comment::class); } + + public function getSearchResult(): SearchResult + { + $url = url("/profiles/$this->id"); + return new SearchResult( + $this, + $this->name, + $url + ); + } } diff --git a/composer.json b/composer.json index c30e020..420eba9 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,10 @@ "league/flysystem-gridfs": "3.x-dev", "livewire/livewire": "^3.6", "mongodb/laravel-mongodb": "^5.4", + "overtrue/laravel-favorite": "^5.3", "predis/predis": "^3.0", - "socialiteproviders/authentik": "^5.2" + "socialiteproviders/authentik": "^5.2", + "spatie/laravel-searchable": "^1.13" }, "require-dev": { "fakerphp/faker": "^1.23", diff --git a/composer.lock b/composer.lock index fb7ea5a..4ac4ede 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "91ba0ac099738f27d18cf80cec4b7d41", + "content-hash": "00ff44f83137e0e396a391da23ecfde8", "packages": [ { "name": "brick/math", @@ -3313,6 +3313,76 @@ ], "time": "2025-05-08T08:14:37+00:00" }, + { + "name": "overtrue/laravel-favorite", + "version": "5.3.2", + "source": { + "type": "git", + "url": "https://github.com/overtrue/laravel-favorite.git", + "reference": "de63bc6f45cbdbda8f717e38e859b25e0a20995c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/overtrue/laravel-favorite/zipball/de63bc6f45cbdbda8f717e38e859b25e0a20995c", + "reference": "de63bc6f45cbdbda8f717e38e859b25e0a20995c", + "shasum": "" + }, + "require": { + "laravel/framework": "^9.0|^10.0|^11.0|^12.0", + "php": "^8.0.2" + }, + "require-dev": { + "brainmaestro/composer-git-hooks": "dev-master", + "friendsofphp/php-cs-fixer": "^3.5", + "laravel/pint": "^1.2", + "mockery/mockery": "^1.4.4", + "orchestra/testbench": "^8.0|^9.0|^10.0", + "phpunit/phpunit": "^10.0.0|^11.5.3" + }, + "type": "library", + "extra": { + "hooks": { + "pre-push": [ + "composer test" + ], + "pre-commit": [ + "composer fix-style" + ] + }, + "laravel": { + "providers": [ + "Overtrue\\LaravelFavorite\\FavoriteServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Overtrue\\LaravelFavorite\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "overtrue", + "email": "anzhengchao@gmail.com" + } + ], + "description": "User favorite features for Laravel Application.", + "support": { + "issues": "https://github.com/overtrue/laravel-favorite/issues", + "source": "https://github.com/overtrue/laravel-favorite/tree/5.3.2" + }, + "funding": [ + { + "url": "https://github.com/overtrue", + "type": "github" + } + ], + "time": "2025-03-23T04:56:07+00:00" + }, { "name": "paragonie/constant_time_encoding", "version": "v3.0.0", @@ -4505,6 +4575,72 @@ }, "time": "2025-02-24T19:33:30+00:00" }, + { + "name": "spatie/laravel-searchable", + "version": "1.13.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-searchable.git", + "reference": "7821e4c72277133cf541ea181724af64b2b972c5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-searchable/zipball/7821e4c72277133cf541ea181724af64b2b972c5", + "reference": "7821e4c72277133cf541ea181724af64b2b972c5", + "shasum": "" + }, + "require": { + "ext-pdo": "*", + "laravel/framework": "^8.78|^9.0|^10.0|^11.0|^12.0", + "php": "^7.3|^8.0" + }, + "require-dev": { + "larapack/dd": "^1.0", + "orchestra/testbench": "^6.27|^7.0|^8.0|^9.0|^10.0", + "phpunit/phpunit": "^9.3|^10.0|^11.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\Searchable\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Alex Vanderbist", + "email": "alex@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Pragmatically search through models and other sources", + "homepage": "https://github.com/spatie/laravel-searchable", + "keywords": [ + "laravel-searchable", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-searchable/issues", + "source": "https://github.com/spatie/laravel-searchable/tree/1.13.0" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + } + ], + "time": "2025-02-25T15:59:46+00:00" + }, { "name": "symfony/clock", "version": "v7.2.0", diff --git a/config/favorite.php b/config/favorite.php new file mode 100644 index 0000000..b4ed52e --- /dev/null +++ b/config/favorite.php @@ -0,0 +1,28 @@ + false, + + /* + * User tables foreign key name. + */ + 'user_foreign_key' => 'user_id', + + /* + * Table name for favorites records. + */ + 'favorites_table' => 'favorites', + + /* + * Model name for favorite record. + */ + 'favorite_model' => Overtrue\LaravelFavorite\Favorite::class, + + /* + * Model name for favoriter model. + */ + 'favoriter_model' => App\Models\User::class, +]; diff --git a/config/livewire.php b/config/livewire.php index d609f27..14d3432 100644 --- a/config/livewire.php +++ b/config/livewire.php @@ -64,13 +64,12 @@ return [ */ 'temporary_file_upload' => [ - 'disk' => 'gridfs', // Example: 'local', 's3' | Default: 'default' - 'rules' => ['required', 'file', 'max:20480'], // Example: ['file', 'mimes:png,jpg'] | Default: ['required', 'file', 'max:12288'] (12MB) + 'disk' => null, // Example: 'local', 's3' | Default: 'default' + 'rules' => ['required', 'file', 'max:12288'], // Example: ['file', 'mimes:png,jpg'] | Default: ['required', 'file', 'max:12288'] (12MB) 'directory' => null, // Example: 'tmp' | Default: 'livewire-tmp' 'middleware' => null, // Example: 'throttle:5,1' | Default: 'throttle:60,1' 'preview_mimes' => [ // Supported file types for temporary pre-signed file URLs... - 'png', 'gif', 'bmp', 'svg', 'mp4', - 'mov', 'avi', 'wmv', 'mp3', + 'png', 'gif', 'bmp', 'svg', 'jpg', 'jpeg', 'webp', ], 'max_upload_time' => 5, // Max duration (in minutes) before an upload is invalidated... diff --git a/database/migrations/2018_12_14_000000_create_favorites_table.php b/database/migrations/2018_12_14_000000_create_favorites_table.php new file mode 100644 index 0000000..4ef256d --- /dev/null +++ b/database/migrations/2018_12_14_000000_create_favorites_table.php @@ -0,0 +1,29 @@ +id(); + $table->unsignedBigInteger(config('favorite.user_foreign_key'))->index()->comment('user_id'); + $table->morphs('favoriteable'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists(config('favorite.favorites_table')); + } +}; diff --git a/resources/views/livewire/app/home.blade.php b/resources/views/livewire/app/home.blade.php index bc1d2ba..8287697 100644 --- a/resources/views/livewire/app/home.blade.php +++ b/resources/views/livewire/app/home.blade.php @@ -7,9 +7,7 @@