├── .styleci.yml ├── .github ├── FUNDING.yml ├── dependabot.yml ├── stale.yml └── workflows │ └── ci.yml ├── .gitignore ├── src ├── Traits │ ├── Like.php │ ├── Block.php │ ├── Follow.php │ ├── HasCustomModelClass.php │ ├── CanBeLiked.php │ ├── CanBeBlocked.php │ ├── CanLike.php │ ├── CanBlock.php │ ├── CanBeFollowed.php │ └── CanFollow.php ├── Contracts │ ├── Liking.php │ ├── Blocking.php │ ├── Following.php │ ├── Likeable.php │ ├── Blockable.php │ ├── Liker.php │ ├── Blocker.php │ ├── Followable.php │ └── Follower.php ├── Models │ ├── LikerModel.php │ ├── BlockerModel.php │ └── FollowerModel.php ├── BefriendedServiceProvider.php └── Scopes │ ├── BlockFilterable.php │ ├── LikeFilterable.php │ └── FollowFilterable.php ├── tests ├── database │ ├── factories │ │ ├── PageFactory.php │ │ ├── SimplePageFactory.php │ │ └── UserFactory.php │ └── migrations │ │ └── 2018_07_14_183253_pages.php ├── Models │ ├── SimplePage.php │ ├── Page.php │ └── User.php ├── CanFilterBlockingTest.php ├── CanFilterLikingTest.php ├── CanFilterFollowingTest.php ├── TestCase.php ├── LikingTest.php ├── BlockingTest.php ├── FollowTest.php └── FollowRequestTest.php ├── .editorconfig ├── .codecov.yml ├── phpunit.xml ├── config └── befriended.php ├── database └── migrations │ ├── 2020_01_14_171300_add_accepted_to_followers.php │ ├── 2018_07_14_183255_likers.php │ ├── 2018_07_14_183254_blockers.php │ └── 2018_07_14_183253_followers.php ├── composer.json ├── CONTRIBUTING.md ├── LICENSE └── README.md /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: rennokki 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /src/Traits/Like.php: -------------------------------------------------------------------------------- 1 | define(\Rennokki\Befriended\Test\Models\Page::class, function () { 6 | return [ 7 | 'name' => 'Page'.Str::random(5), 8 | ]; 9 | }); 10 | -------------------------------------------------------------------------------- /tests/database/factories/SimplePageFactory.php: -------------------------------------------------------------------------------- 1 | define(\Rennokki\Befriended\Test\Models\SimplePage::class, function () { 6 | return [ 7 | 'name' => 'Page'.Str::random(5), 8 | ]; 9 | }); 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | indent_style = space 8 | indent_size = 4 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.yml] 15 | indent_size = 2 16 | -------------------------------------------------------------------------------- /tests/Models/SimplePage.php: -------------------------------------------------------------------------------- 1 | define(\Rennokki\Befriended\Test\Models\User::class, function () { 6 | return [ 7 | 'name' => 'Name'.Str::random(5), 8 | 'email' => Str::random(5).'@gmail.com', 9 | 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret 10 | 'remember_token' => Str::random(10), 11 | ]; 12 | }); 13 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 29 3 | # Number of days of inactivity before a stale issue is closed 4 | daysUntilClose: 1 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - pinned 8 | - security 9 | # Label to use when marking an issue as stale 10 | staleLabel: wontfix 11 | # Comment to post when marking an issue as stale. Set to `false` to disable 12 | markComment: false 13 | # Comment to post when closing a stale issue. Set to `false` to disable 14 | closeComment: > 15 | This issue has been automatically closed because it has not had any recent activity. 😨 16 | -------------------------------------------------------------------------------- /src/Traits/HasCustomModelClass.php: -------------------------------------------------------------------------------- 1 | getMorphClass() 17 | : $this->getCurrentModelMorphClass(); 18 | } 19 | 20 | /** 21 | * Get the current model's morph class. 22 | * 23 | * @return string 24 | */ 25 | protected function getCurrentModelMorphClass() 26 | { 27 | return $this->getMorphClass(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Traits/CanBeLiked.php: -------------------------------------------------------------------------------- 1 | getModelMorphClass($model); 18 | 19 | return $this 20 | ->morphToMany($modelClass, 'likeable', 'likers', 'likeable_id', 'liker_id') 21 | ->withPivot('liker_type') 22 | ->wherePivot('liker_type', $modelClass) 23 | ->wherePivot('likeable_type', $this->getMorphClass()) 24 | ->withTimestamps(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Models/LikerModel.php: -------------------------------------------------------------------------------- 1 | morphTo(); 27 | } 28 | 29 | /** 30 | * The relationship for models that like this model. 31 | * 32 | * @return mixed 33 | */ 34 | public function liker() 35 | { 36 | return $this->morphTo(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/database/migrations/2018_07_14_183253_pages.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | 19 | $table->string('name'); 20 | 21 | $table->timestamps(); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | Schema::dropIfExists('pages'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Traits/CanBeBlocked.php: -------------------------------------------------------------------------------- 1 | getModelMorphClass($model); 18 | 19 | return $this 20 | ->morphToMany($modelClass, 'blockable', 'blockers', 'blockable_id', 'blocker_id') 21 | ->withPivot('blocker_type') 22 | ->wherePivot('blocker_type', $modelClass) 23 | ->wherePivot('blockable_type', $this->getMorphClass()) 24 | ->withTimestamps(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /config/befriended.php: -------------------------------------------------------------------------------- 1 | [ 17 | 18 | 'follower' => \Rennokki\Befriended\Models\FollowerModel::class, 19 | 20 | 'blocker' => \Rennokki\Befriended\Models\BlockerModel::class, 21 | 22 | 'liker' => \Rennokki\Befriended\Models\LikerModel::class, 23 | 24 | ], 25 | 26 | ]; 27 | -------------------------------------------------------------------------------- /src/Models/BlockerModel.php: -------------------------------------------------------------------------------- 1 | morphTo(); 27 | } 28 | 29 | /** 30 | * The relationship for models that block this model. 31 | * 32 | * @return mixed 33 | */ 34 | public function blocker() 35 | { 36 | return $this->morphTo(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Models/FollowerModel.php: -------------------------------------------------------------------------------- 1 | morphTo(); 27 | } 28 | 29 | /** 30 | * The relationship for models that follow this model. 31 | * 32 | * @return mixed 33 | */ 34 | public function follower() 35 | { 36 | return $this->morphTo(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /database/migrations/2020_01_14_171300_add_accepted_to_followers.php: -------------------------------------------------------------------------------- 1 | boolean('accepted')->default(1); 18 | }); 19 | } 20 | 21 | /** 22 | * Reverse the migrations. 23 | * 24 | * @return void 25 | */ 26 | public function down() 27 | { 28 | Schema::table('followers', function (Blueprint $table) { 29 | $table->dropColumn('accepted'); 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /database/migrations/2018_07_14_183255_likers.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | 19 | $table->integer('likeable_id'); 20 | $table->string('likeable_type'); 21 | 22 | $table->integer('liker_id')->nullable(); 23 | $table->string('liker_type')->nullable(); 24 | 25 | $table->timestamps(); 26 | }); 27 | } 28 | 29 | /** 30 | * Reverse the migrations. 31 | * 32 | * @return void 33 | */ 34 | public function down() 35 | { 36 | Schema::dropIfExists('likers'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /database/migrations/2018_07_14_183254_blockers.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | 19 | $table->integer('blockable_id'); 20 | $table->string('blockable_type'); 21 | 22 | $table->integer('blocker_id')->nullable(); 23 | $table->string('blocker_type')->nullable(); 24 | 25 | $table->timestamps(); 26 | }); 27 | } 28 | 29 | /** 30 | * Reverse the migrations. 31 | * 32 | * @return void 33 | */ 34 | public function down() 35 | { 36 | Schema::dropIfExists('blockers'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /database/migrations/2018_07_14_183253_followers.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | 19 | $table->integer('followable_id'); 20 | $table->string('followable_type'); 21 | 22 | $table->integer('follower_id')->nullable(); 23 | $table->string('follower_type')->nullable(); 24 | 25 | $table->timestamps(); 26 | }); 27 | } 28 | 29 | /** 30 | * Reverse the migrations. 31 | * 32 | * @return void 33 | */ 34 | public function down() 35 | { 36 | Schema::dropIfExists('followers'); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/Models/Page.php: -------------------------------------------------------------------------------- 1 | bob = factory(User::class)->create(); 19 | 20 | $this->alice = factory(User::class)->create(); 21 | 22 | factory(Page::class, 10)->create(); 23 | } 24 | 25 | public function test_can_filter_blockers() 26 | { 27 | $this->assertEquals( 28 | 10, Page::filterBlockingsOf($this->bob)->count() 29 | ); 30 | 31 | $this->assertEquals( 32 | 10, Page::filterBlockingsOf($this->alice)->count() 33 | ); 34 | 35 | $this->bob->block(Page::find(1)); 36 | 37 | $this->assertEquals( 38 | 9, Page::filterBlockingsOf($this->bob)->count() 39 | ); 40 | 41 | $this->assertEquals( 42 | 10, Page::filterBlockingsOf($this->alice)->count() 43 | ); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Contracts/Liker.php: -------------------------------------------------------------------------------- 1 | publishes([ 17 | __DIR__.'/../config/befriended.php' => config_path('befriended.php'), 18 | ], 'config'); 19 | 20 | $this->publishes([ 21 | __DIR__.'/../database/migrations/2018_07_14_183253_followers.php' => database_path('migrations/2018_07_14_183253_followers.php'), 22 | __DIR__.'/../database/migrations/2018_07_14_183254_blockers.php' => database_path('migrations/2018_07_14_183254_blockers.php'), 23 | __DIR__.'/../database/migrations/2018_07_14_183255_likers.php' => database_path('migrations/2018_07_14_183255_likers.php'), 24 | __DIR__.'/../database/migrations/2020_01_14_171300_add_accepted_to_followers.php' => database_path('migrations/2020_01_14_171300_add_accepted_to_followers.php'), 25 | ], 'migrations'); 26 | } 27 | 28 | /** 29 | * Register bindings in the container. 30 | * 31 | * @return void 32 | */ 33 | public function register() 34 | { 35 | // 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Scopes/BlockFilterable.php: -------------------------------------------------------------------------------- 1 | blocking($this->getMorphClass()) 25 | ->get() 26 | ->pluck($model->getKeyName()) 27 | ->toArray(); 28 | 29 | return $query->whereNotIn( 30 | $this->getKeyName(), $blockingsIds 31 | ); 32 | } 33 | 34 | /** 35 | * Alias to scopeWithoutBlockingsOf. 36 | * 37 | * @param \Illuminate\Database\Eloquent\Builder $query 38 | * @param \Illuminate\Database\Eloquent\Model $model 39 | * @return \Illuminate\Database\Eloquent\Builder 40 | */ 41 | public function scopeFilterBlockingsOf($query, $model) 42 | { 43 | return $this->scopeWithoutBlockingsOf($query, $model); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Contracts/Followable.php: -------------------------------------------------------------------------------- 1 | bob = factory(User::class)->create(); 19 | 20 | $this->alice = factory(User::class)->create(); 21 | 22 | factory(Page::class, 10)->create(); 23 | } 24 | 25 | public function test_filters_liked_pages() 26 | { 27 | $this->assertEquals( 28 | 0, Page::likedBy($this->bob)->count() 29 | ); 30 | 31 | $this->assertEquals( 32 | 0, Page::likedBy($this->alice)->count() 33 | ); 34 | 35 | $this->bob->like(Page::find(1)); 36 | 37 | $this->assertEquals( 38 | 1, Page::likedBy($this->bob)->count() 39 | ); 40 | 41 | $this->assertEquals( 42 | 0, Page::likedBy($this->alice)->count() 43 | ); 44 | } 45 | 46 | public function test_filters_non_liked_pages() 47 | { 48 | $this->assertEquals( 49 | 10, Page::filterUnlikedFor($this->bob)->count() 50 | ); 51 | 52 | $this->assertEquals( 53 | 10, Page::filterUnlikedFor($this->alice)->count() 54 | ); 55 | 56 | $this->bob->like(Page::find(1)); 57 | 58 | $this->assertEquals( 59 | 9, Page::filterUnlikedFor($this->bob)->count() 60 | ); 61 | 62 | $this->assertEquals( 63 | 10, Page::filterUnlikedFor($this->alice)->count() 64 | ); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /tests/CanFilterFollowingTest.php: -------------------------------------------------------------------------------- 1 | bob = factory(User::class)->create(); 19 | 20 | $this->alice = factory(User::class)->create(); 21 | 22 | factory(Page::class, 10)->create(); 23 | } 24 | 25 | public function test_filters_followed_pages() 26 | { 27 | $this->assertEquals( 28 | 0, Page::filterFollowingsOf($this->bob)->count() 29 | ); 30 | 31 | $this->assertEquals( 32 | 0, Page::filterFollowingsOf($this->alice)->count() 33 | ); 34 | 35 | $this->bob->follow(Page::find(1)); 36 | 37 | $this->assertEquals( 38 | 1, Page::filterFollowingsOf($this->bob)->count() 39 | ); 40 | 41 | $this->assertEquals( 42 | 0, Page::filterFollowingsOf($this->alice)->count() 43 | ); 44 | } 45 | 46 | public function test_filters_non_followed_pages() 47 | { 48 | $this->assertEquals( 49 | 10, Page::filterUnfollowingsOf($this->bob)->count() 50 | ); 51 | 52 | $this->assertEquals( 53 | 10, Page::filterUnfollowingsOf($this->alice)->count() 54 | ); 55 | 56 | $this->bob->follow(Page::find(1)); 57 | 58 | $this->assertEquals( 59 | 9, Page::filterUnfollowingsOf($this->bob)->count() 60 | ); 61 | 62 | $this->assertEquals( 63 | 10, Page::filterUnfollowingsOf($this->alice)->count() 64 | ); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - '*' 7 | tags: 8 | - '*' 9 | pull_request: 10 | branches: 11 | - '*' 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | php: 21 | - '8.0' 22 | - '8.1' 23 | laravel: 24 | - 8.* 25 | - 9.* 26 | prefer: 27 | - 'prefer-lowest' 28 | - 'prefer-stable' 29 | include: 30 | - laravel: '8.*' 31 | testbench: '6.*' 32 | - laravel: '9.*' 33 | testbench: '7.*' 34 | 35 | name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} --${{ matrix.prefer }} 36 | 37 | steps: 38 | - uses: actions/checkout@v3 39 | 40 | - name: Setup PHP 41 | uses: shivammathur/setup-php@v2 42 | with: 43 | php-version: ${{ matrix.php }} 44 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv 45 | coverage: pcov 46 | 47 | - uses: actions/cache@v3.0.5 48 | name: Cache dependencies 49 | with: 50 | path: ~/.composer/cache/files 51 | key: composer-php-${{ matrix.php }}-${{ matrix.laravel }}-${{ matrix.prefer }}-${{ hashFiles('composer.json') }} 52 | 53 | - name: Install dependencies 54 | run: | 55 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 56 | composer update --${{ matrix.prefer }} --prefer-dist --no-interaction --no-suggest 57 | 58 | - name: Run tests 59 | run: | 60 | vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml 61 | 62 | - uses: codecov/codecov-action@v3.1.0 63 | with: 64 | fail_ci_if_error: false 65 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rennokki/befriended", 3 | "description": "Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.", 4 | "keywords": [ 5 | "eloquent", 6 | "laravel", 7 | "model", 8 | "friend", 9 | "followers", 10 | "followers", 11 | "friends", 12 | "connection", 13 | "connections", 14 | "link", 15 | "social", 16 | "media", 17 | "block", 18 | "blocking", 19 | "blocker" 20 | ], 21 | "homepage": "https://github.com/renoki-co/befriended", 22 | "license": "Apache-2.0", 23 | "authors": [ 24 | { 25 | "name": "Alex Renoki", 26 | "email": "alex@renoki.org", 27 | "homepage": "https://github.com/rennokki", 28 | "role": "Developer" 29 | } 30 | ], 31 | "require": { 32 | "doctrine/dbal": "^2.10|^3.3", 33 | "illuminate/database": "^8.83|^9.0.1", 34 | "illuminate/support": "^8.83|^9.0.1" 35 | }, 36 | "require-dev": { 37 | "laravel/legacy-factories": "^1.3", 38 | "mockery/mockery": "^1.5", 39 | "orchestra/database": "^6.28|^7.0", 40 | "orchestra/testbench": "^6.28|^7.0", 41 | "orchestra/testbench-core": "^6.28|^7.0", 42 | "phpunit/phpunit": "^9.5.21" 43 | }, 44 | "autoload": { 45 | "psr-4": { 46 | "Rennokki\\Befriended\\": "src" 47 | } 48 | }, 49 | "autoload-dev": { 50 | "psr-4": { 51 | "Rennokki\\Befriended\\Test\\": "tests" 52 | } 53 | }, 54 | "scripts": { 55 | "test": "vendor/bin/phpunit" 56 | }, 57 | "extra": { 58 | "laravel": { 59 | "providers": [ 60 | "Rennokki\\Befriended\\BefriendedServiceProvider" 61 | ] 62 | } 63 | }, 64 | "config": { 65 | "sort-packages": true 66 | }, 67 | "minimum-stability": "dev" 68 | } 69 | -------------------------------------------------------------------------------- /src/Contracts/Follower.php: -------------------------------------------------------------------------------- 1 | liking($this->getMorphClass()) 25 | ->get() 26 | ->pluck($model->getKeyName()) 27 | ->toArray(); 28 | 29 | return $query->whereIn($this->getKeyName(), $likedIds); 30 | } 31 | 32 | /** 33 | * Filter only records that the model did not like. 34 | * 35 | * @param \Illuminate\Database\Eloquent\Builder $query 36 | * @param \Illuminate\Database\Eloquent\Model $model 37 | * @return \Illuminate\Database\Eloquent\Builder 38 | */ 39 | public function scopeNotLikedBy($query, $model) 40 | { 41 | if (! $model instanceof Liker && ! $model instanceof Liking) { 42 | return $query; 43 | } 44 | 45 | $likedIds = $model 46 | ->liking($this->getMorphClass()) 47 | ->get() 48 | ->pluck($model->getKeyName()) 49 | ->toArray(); 50 | 51 | return $query->whereNotIn($this->getKeyName(), $likedIds); 52 | } 53 | 54 | /** 55 | * Alias for scopeNotLikedBy. 56 | * 57 | * @param \Illuminate\Database\Eloquent\Builder $query 58 | * @param \Illuminate\Database\Eloquent\Model $model 59 | * @return \Illuminate\Database\Eloquent\Builder 60 | */ 61 | public function scopeFilterUnlikedFor($query, $model) 62 | { 63 | return $this->scopeNotLikedBy($query, $model); 64 | } 65 | 66 | /** 67 | * Alias for scopeLikedBy. 68 | * 69 | * @param \Illuminate\Database\Eloquent\Builder $query 70 | * @param \Illuminate\Database\Eloquent\Model $model 71 | * @return \Illuminate\Database\Eloquent\Builder 72 | */ 73 | public function scopeFilterLikedFor($query, $model) 74 | { 75 | return $this->scopeLikedBy($query, $model); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | resetDatabase(); 24 | 25 | $this->loadLaravelMigrations(['--database' => 'sqlite']); 26 | 27 | $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); 28 | 29 | $this->loadMigrationsFrom(__DIR__.'/database/migrations'); 30 | 31 | $this->withFactories(__DIR__.'/database/factories'); 32 | 33 | $this->artisan('migrate', ['--database' => 'sqlite']); 34 | } 35 | 36 | /** 37 | * Get the package providers for tests. 38 | * 39 | * @param mixed $app 40 | * @return array 41 | */ 42 | protected function getPackageProviders($app) 43 | { 44 | return [ 45 | \Rennokki\Befriended\BefriendedServiceProvider::class, 46 | ]; 47 | } 48 | 49 | /** 50 | * Set up the environment. 51 | * 52 | * @param mixed $app 53 | * @return void 54 | */ 55 | public function getEnvironmentSetUp($app) 56 | { 57 | $app['config']->set('database.default', 'sqlite'); 58 | $app['config']->set('database.connections.sqlite', [ 59 | 'driver' => 'sqlite', 60 | 'database' => __DIR__.'/database.sqlite', 61 | 'prefix' => '', 62 | ]); 63 | $app['config']->set('auth.providers.users.model', User::class); 64 | $app['config']->set('auth.providers.pages.model', Page::class); 65 | $app['config']->set('app.key', 'wslxrEFGWY6GfGhvN9L3wH3KSRJQQpBD'); 66 | $app['config']->set('befriended.models.follower', FollowerModel::class); 67 | $app['config']->set('befriended.models.blocker', BlockerModel::class); 68 | $app['config']->set('befriended.models.liker', LikerModel::class); 69 | } 70 | 71 | /** 72 | * Reset the database. 73 | * 74 | * @return void 75 | */ 76 | protected function resetDatabase() 77 | { 78 | file_put_contents(__DIR__.'/database.sqlite', null); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/Scopes/FollowFilterable.php: -------------------------------------------------------------------------------- 1 | following($this->getMorphClass()) 25 | ->get() 26 | ->pluck($model->getKeyName()) 27 | ->toArray(); 28 | 29 | return $query->whereIn($this->getKeyName(), $followingIds); 30 | } 31 | 32 | /** 33 | * Filter only records that the model did not follow. 34 | * 35 | * @param \Illuminate\Database\Eloquent\Builder $query 36 | * @param \Illuminate\Database\Eloquent\Model $model 37 | * @return \Illuminate\Database\Eloquent\Builder 38 | */ 39 | public function scopeUnfollowedBy($query, $model) 40 | { 41 | if (! $model instanceof Follower && ! $model instanceof Following) { 42 | return $query; 43 | } 44 | 45 | $followingIds = $model 46 | ->following($this->getMorphClass()) 47 | ->get() 48 | ->pluck($model->getKeyName()) 49 | ->toArray(); 50 | 51 | return $query->whereNotIn($this->getKeyName(), $followingIds); 52 | } 53 | 54 | /** 55 | * Alias to scopeFollowedBy. 56 | * 57 | * @param \Illuminate\Database\Eloquent\Builder $query 58 | * @param \Illuminate\Database\Eloquent\Model $model 59 | * @return \Illuminate\Database\Eloquent\Builder 60 | */ 61 | public function scopeFilterFollowingsOf($query, $model) 62 | { 63 | return $this->scopeFollowedBy($query, $model); 64 | } 65 | 66 | /** 67 | * Alias to scopeUnfollowedBy. 68 | * 69 | * @param \Illuminate\Database\Eloquent\Builder $query 70 | * @param \Illuminate\Database\Eloquent\Model $model 71 | * @return \Illuminate\Database\Eloquent\Builder 72 | */ 73 | public function scopeFilterUnfollowingsOf($query, $model) 74 | { 75 | return $this->scopeUnfollowedBy($query, $model); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Traits/CanLike.php: -------------------------------------------------------------------------------- 1 | getModelMorphClass($model); 21 | 22 | return $this 23 | ->morphToMany($modelClass, 'liker', 'likers', 'liker_id', 'likeable_id') 24 | ->withPivot('likeable_type') 25 | ->wherePivot('likeable_type', $modelClass) 26 | ->wherePivot('liker_type', $this->getMorphClass()) 27 | ->withTimestamps(); 28 | } 29 | 30 | /** 31 | * Check if the current model is liking another model. 32 | * 33 | * @param \Illuminate\Database\Eloquent\Model $model 34 | * @return bool 35 | */ 36 | public function isLiking($model): bool 37 | { 38 | if (! $model instanceof Likeable && ! $model instanceof Liking) { 39 | return false; 40 | } 41 | 42 | return ! is_null( 43 | $this 44 | ->liking((new $model)->getMorphClass()) 45 | ->find($model->getKey()) 46 | ); 47 | } 48 | 49 | /** 50 | * Check if the current model is liking another model. 51 | * 52 | * @param \Illuminate\Database\Eloquent\Model $model 53 | * @return bool 54 | */ 55 | public function likes($model): bool 56 | { 57 | return $this->isLiking($model); 58 | } 59 | 60 | /** 61 | * Like a certain model. 62 | * 63 | * @param \Illuminate\Database\Eloquent\Model $model 64 | * @return bool 65 | */ 66 | public function like($model): bool 67 | { 68 | if (! $model instanceof Likeable && ! $model instanceof Liking) { 69 | return false; 70 | } 71 | 72 | if ($this->isLiking($model)) { 73 | return false; 74 | } 75 | 76 | $this->liking()->attach($model->getKey(), [ 77 | 'likeable_type' => (new $model)->getMorphClass(), 78 | ]); 79 | 80 | return true; 81 | } 82 | 83 | /** 84 | * Unlike a certain model. 85 | * 86 | * @param \Illuminate\Database\Eloquent\Model $model 87 | * @return bool 88 | */ 89 | public function unlike($model): bool 90 | { 91 | if (! $model instanceof Likeable && ! $model instanceof Liking) { 92 | return false; 93 | } 94 | 95 | if (! $this->isLiking($model)) { 96 | return false; 97 | } 98 | 99 | return (bool) $this 100 | ->liking((new $model)->getMorphClass()) 101 | ->detach($model->getKey()); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/Traits/CanBlock.php: -------------------------------------------------------------------------------- 1 | getModelMorphClass($model); 21 | 22 | return $this 23 | ->morphToMany($modelClass, 'blocker', 'blockers', 'blocker_id', 'blockable_id') 24 | ->withPivot('blockable_type') 25 | ->wherePivot('blockable_type', $modelClass) 26 | ->wherePivot('blocker_type', $this->getMorphClass()) 27 | ->withTimestamps(); 28 | } 29 | 30 | /** 31 | * Check if the current model is blocking another model. 32 | * 33 | * @param \Illuminate\Database\Eloquent\Model $model 34 | * @return bool 35 | */ 36 | public function isBlocking($model): bool 37 | { 38 | if (! $model instanceof Blockable && ! $model instanceof Blocking) { 39 | return false; 40 | } 41 | 42 | return ! is_null( 43 | $this 44 | ->blocking((new $model)->getMorphClass()) 45 | ->find($model->getKey()) 46 | ); 47 | } 48 | 49 | /** 50 | * Check if the current model is blocking another model. 51 | * 52 | * @param \Illuminate\Database\Eloquent\Model $model 53 | * @return bool 54 | */ 55 | public function blocks($model): bool 56 | { 57 | return $this->isBlocking($model); 58 | } 59 | 60 | /** 61 | * Block a certain model. 62 | * 63 | * @param \Illuminate\Database\Eloquent\Model $mode 64 | * @return bool 65 | */ 66 | public function block($model): bool 67 | { 68 | if (! $model instanceof Blockable && ! $model instanceof Blocking) { 69 | return false; 70 | } 71 | 72 | if ($this->isBlocking($model)) { 73 | return false; 74 | } 75 | 76 | $this->blocking()->attach($model->getKey(), [ 77 | 'blockable_type' => (new $model)->getMorphClass(), 78 | ]); 79 | 80 | return true; 81 | } 82 | 83 | /** 84 | * Unblock a certain model. 85 | * 86 | * @param \Illuminate\Database\Eloquent\Model $model 87 | * @return bool 88 | */ 89 | public function unblock($model): bool 90 | { 91 | if (! $model instanceof Blockable && ! $model instanceof Blocking) { 92 | return false; 93 | } 94 | 95 | if (! $this->isBlocking($model)) { 96 | return false; 97 | } 98 | 99 | return (bool) $this 100 | ->blocking((new $model)->getMorphClass()) 101 | ->detach($model->getKey()); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /tests/LikingTest.php: -------------------------------------------------------------------------------- 1 | bob = factory(User::class)->create(); 26 | 27 | $this->alice = factory(User::class)->create(); 28 | 29 | $this->mark = factory(User::class)->create(); 30 | 31 | $this->page = factory(Page::class)->create(); 32 | 33 | $this->simplePage = factory(SimplePage::class)->create(); 34 | } 35 | 36 | public function test_liking() 37 | { 38 | $this->assertTrue( 39 | $this->bob->like($this->alice) 40 | ); 41 | 42 | $this->assertFalse( 43 | $this->bob->like($this->alice) 44 | ); 45 | 46 | $this->assertTrue( 47 | $this->bob->likes($this->alice) 48 | ); 49 | 50 | $this->assertEquals(1, $this->bob->liking()->count()); 51 | $this->assertEquals(0, $this->bob->likers()->count()); 52 | 53 | $this->assertEquals(0, $this->alice->liking()->count()); 54 | $this->assertEquals(1, $this->alice->likers()->count()); 55 | } 56 | 57 | public function test_unliking() 58 | { 59 | $this->assertFalse( 60 | $this->bob->unlike($this->alice) 61 | ); 62 | 63 | $this->bob->like($this->alice); 64 | 65 | $this->assertTrue( 66 | $this->bob->unlike($this->alice) 67 | ); 68 | 69 | $this->assertFalse( 70 | $this->bob->likes($this->alice) 71 | ); 72 | 73 | $this->assertEquals(0, $this->bob->liking()->count()); 74 | $this->assertEquals(0, $this->bob->likers()->count()); 75 | 76 | $this->assertEquals(0, $this->alice->liking()->count()); 77 | $this->assertEquals(0, $this->alice->likers()->count()); 78 | } 79 | 80 | public function test_liking_with_custom_model() 81 | { 82 | $this->assertTrue( 83 | $this->bob->like($this->page) 84 | ); 85 | 86 | $this->assertFalse( 87 | $this->bob->like($this->page) 88 | ); 89 | 90 | $this->assertTrue( 91 | $this->bob->likes($this->page) 92 | ); 93 | 94 | // Not specifying the model won't return any results. 95 | 96 | $this->assertEquals(0, $this->bob->liking()->count()); 97 | $this->assertEquals(0, $this->bob->likers()->count()); 98 | 99 | $this->assertEquals(0, $this->page->liking()->count()); 100 | $this->assertEquals(0, $this->page->likers()->count()); 101 | 102 | // Passing the model should return the values properly. 103 | 104 | $this->assertEquals(1, $this->bob->liking(Page::class)->count()); 105 | $this->assertEquals(0, $this->bob->likers(Page::class)->count()); 106 | 107 | $this->assertEquals(0, $this->page->liking(User::class)->count()); 108 | $this->assertEquals(1, $this->page->likers(User::class)->count()); 109 | } 110 | 111 | public function test_unliking_with_custom_model() 112 | { 113 | $this->assertFalse( 114 | $this->bob->unlike($this->page) 115 | ); 116 | 117 | $this->bob->like($this->page); 118 | 119 | $this->assertTrue( 120 | $this->bob->unlike($this->page) 121 | ); 122 | 123 | $this->assertFalse( 124 | $this->bob->likes($this->page) 125 | ); 126 | 127 | $this->assertEquals(0, $this->bob->liking(Page::class)->count()); 128 | $this->assertEquals(0, $this->bob->likers(Page::class)->count()); 129 | 130 | $this->assertEquals(0, $this->page->liking(User::class)->count()); 131 | $this->assertEquals(0, $this->page->likers(User::class)->count()); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /tests/BlockingTest.php: -------------------------------------------------------------------------------- 1 | bob = factory(User::class)->create(); 26 | 27 | $this->alice = factory(User::class)->create(); 28 | 29 | $this->mark = factory(User::class)->create(); 30 | 31 | $this->page = factory(Page::class)->create(); 32 | 33 | $this->simplePage = factory(SimplePage::class)->create(); 34 | } 35 | 36 | public function test_blocking() 37 | { 38 | $this->assertTrue( 39 | $this->bob->block($this->alice) 40 | ); 41 | 42 | $this->assertFalse( 43 | $this->bob->block($this->alice) 44 | ); 45 | 46 | $this->assertTrue( 47 | $this->bob->blocks($this->alice) 48 | ); 49 | 50 | $this->assertEquals(1, $this->bob->blocking()->count()); 51 | $this->assertEquals(0, $this->bob->blockers()->count()); 52 | 53 | $this->assertEquals(0, $this->alice->blocking()->count()); 54 | $this->assertEquals(1, $this->alice->blockers()->count()); 55 | } 56 | 57 | public function test_unblocking() 58 | { 59 | $this->assertFalse( 60 | $this->bob->unblock($this->alice) 61 | ); 62 | 63 | $this->bob->block($this->alice); 64 | 65 | $this->assertTrue( 66 | $this->bob->unblock($this->alice) 67 | ); 68 | 69 | $this->assertFalse( 70 | $this->bob->blocks($this->alice) 71 | ); 72 | 73 | $this->assertEquals(0, $this->bob->blocking()->count()); 74 | $this->assertEquals(0, $this->bob->blockers()->count()); 75 | 76 | $this->assertEquals(0, $this->alice->blocking()->count()); 77 | $this->assertEquals(0, $this->alice->blockers()->count()); 78 | } 79 | 80 | public function test_blocking_with_custom_model() 81 | { 82 | $this->assertTrue( 83 | $this->bob->block($this->page) 84 | ); 85 | 86 | $this->assertFalse( 87 | $this->bob->block($this->page) 88 | ); 89 | 90 | $this->assertTrue( 91 | $this->bob->blocks($this->page) 92 | ); 93 | 94 | // Not specifying the model won't return any results. 95 | 96 | $this->assertEquals(0, $this->bob->blocking()->count()); 97 | $this->assertEquals(0, $this->bob->blockers()->count()); 98 | 99 | $this->assertEquals(0, $this->page->blocking()->count()); 100 | $this->assertEquals(0, $this->page->blockers()->count()); 101 | 102 | // Passing the model should return the values properly. 103 | 104 | $this->assertEquals(1, $this->bob->blocking(Page::class)->count()); 105 | $this->assertEquals(0, $this->bob->blockers(Page::class)->count()); 106 | 107 | $this->assertEquals(0, $this->page->blocking(User::class)->count()); 108 | $this->assertEquals(1, $this->page->blockers(User::class)->count()); 109 | } 110 | 111 | public function test_unblocking_with_custom_model() 112 | { 113 | $this->assertFalse( 114 | $this->bob->unblock($this->page) 115 | ); 116 | 117 | $this->bob->block($this->page); 118 | 119 | $this->assertTrue( 120 | $this->bob->unblock($this->page) 121 | ); 122 | 123 | $this->assertFalse( 124 | $this->bob->blocks($this->page) 125 | ); 126 | 127 | $this->assertEquals(0, $this->bob->blocking(Page::class)->count()); 128 | $this->assertEquals(0, $this->bob->blockers(Page::class)->count()); 129 | 130 | $this->assertEquals(0, $this->page->blocking(User::class)->count()); 131 | $this->assertEquals(0, $this->page->blockers(User::class)->count()); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/Traits/CanBeFollowed.php: -------------------------------------------------------------------------------- 1 | getModelMorphClass($model); 21 | 22 | return $this 23 | ->morphToMany($modelClass, 'followable', 'followers', 'followable_id', 'follower_id') 24 | ->withPivot(['follower_type', 'accepted']) 25 | ->wherePivot('follower_type', $modelClass) 26 | ->wherePivot('followable_type', $this->getMorphClass()) 27 | ->wherePivot('accepted', true) 28 | ->withTimestamps(); 29 | } 30 | 31 | /** 32 | * Relationship for models that has requested to follow this model. 33 | * 34 | * @param null|\Illuminate\Database\Eloquent\Model $model 35 | * @return mixed 36 | */ 37 | public function followerRequests($model = null) 38 | { 39 | $modelClass = $this->getModelMorphClass($model); 40 | 41 | return $this 42 | ->morphToMany($modelClass, 'followable', 'followers', 'followable_id', 'follower_id') 43 | ->withPivot(['follower_type', 'accepted']) 44 | ->wherePivot('follower_type', $modelClass) 45 | ->wherePivot('followable_type', $this->getMorphClass()) 46 | ->wherePivot('accepted', false) 47 | ->withTimestamps(); 48 | } 49 | 50 | /** 51 | * Remove a follower from this model. 52 | * 53 | * @param \Illuminate\Database\Eloquent\Model $model 54 | * @return bool 55 | */ 56 | public function revokeFollower($model): bool 57 | { 58 | if (! $model instanceof Follower && ! $model instanceof Following) { 59 | return false; 60 | } 61 | 62 | return $model->unfollow($this); 63 | } 64 | 65 | /** 66 | * Check if the model has requested to follow the current model. 67 | * 68 | * @param \Illuminate\Database\Eloquent\Model $model 69 | * @return bool 70 | */ 71 | public function hasFollowRequestFrom($model): bool 72 | { 73 | if (! $model instanceof Followable && ! $model instanceof Following) { 74 | return false; 75 | } 76 | 77 | return ! is_null( 78 | $this 79 | ->followerRequests((new $model)->getMorphClass()) 80 | ->find($model->getKey()) 81 | ); 82 | } 83 | 84 | /** 85 | * Accept request from a certain model to be followed. 86 | * 87 | * @param \Illuminate\Database\Eloquent\Model $model 88 | * @return bool 89 | */ 90 | public function acceptFollowRequest($model): bool 91 | { 92 | if (! $model instanceof Followable && ! $model instanceof Following) { 93 | return false; 94 | } 95 | 96 | if (! $this->hasFollowRequestFrom($model)) { 97 | return false; 98 | } 99 | 100 | $this 101 | ->followerRequests((new $model)->getMorphClass()) 102 | ->find($model->getKey()) 103 | ->pivot 104 | ->update(['accepted' => true]); 105 | 106 | return true; 107 | } 108 | 109 | /** 110 | * Decline request from a certain model to be followed. 111 | * 112 | * @param \Illuminate\Database\Eloquent\Model $model 113 | * @return bool 114 | */ 115 | public function declineFollowRequest($model): bool 116 | { 117 | if (! $model instanceof Followable && ! $model instanceof Following) { 118 | return false; 119 | } 120 | 121 | if (! $this->hasFollowRequestFrom($model)) { 122 | return false; 123 | } 124 | 125 | return (bool) $this 126 | ->followerRequests((new $model)->getMorphClass()) 127 | ->detach($model->getKey()); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /tests/FollowTest.php: -------------------------------------------------------------------------------- 1 | bob = factory(User::class)->create(); 26 | 27 | $this->alice = factory(User::class)->create(); 28 | 29 | $this->mark = factory(User::class)->create(); 30 | 31 | $this->page = factory(Page::class)->create(); 32 | 33 | $this->simplePage = factory(SimplePage::class)->create(); 34 | } 35 | 36 | public function test_following() 37 | { 38 | $this->assertTrue( 39 | $this->bob->follow($this->alice) 40 | ); 41 | 42 | $this->assertFalse( 43 | $this->bob->follow($this->alice) 44 | ); 45 | 46 | $this->assertTrue( 47 | $this->bob->follows($this->alice) 48 | ); 49 | 50 | $this->assertEquals(1, $this->bob->following()->count()); 51 | $this->assertEquals(0, $this->bob->followers()->count()); 52 | 53 | $this->assertEquals(0, $this->alice->following()->count()); 54 | $this->assertEquals(1, $this->alice->followers()->count()); 55 | } 56 | 57 | public function test_unfollowing() 58 | { 59 | $this->assertFalse( 60 | $this->bob->unfollow($this->alice) 61 | ); 62 | 63 | $this->bob->follow($this->alice); 64 | 65 | $this->assertTrue( 66 | $this->bob->unfollow($this->alice) 67 | ); 68 | 69 | $this->assertFalse( 70 | $this->bob->follows($this->alice) 71 | ); 72 | 73 | $this->assertEquals(0, $this->bob->following()->count()); 74 | $this->assertEquals(0, $this->bob->followers()->count()); 75 | 76 | $this->assertEquals(0, $this->alice->following()->count()); 77 | $this->assertEquals(0, $this->alice->followers()->count()); 78 | } 79 | 80 | public function test_following_with_custom_model() 81 | { 82 | $this->assertTrue( 83 | $this->bob->follow($this->page) 84 | ); 85 | 86 | $this->assertFalse( 87 | $this->bob->follow($this->page) 88 | ); 89 | 90 | $this->assertTrue( 91 | $this->bob->follows($this->page) 92 | ); 93 | 94 | // Not specifying the model won't return any results. 95 | 96 | $this->assertEquals(0, $this->bob->following()->count()); 97 | $this->assertEquals(0, $this->bob->followers()->count()); 98 | 99 | $this->assertEquals(0, $this->page->following()->count()); 100 | $this->assertEquals(0, $this->page->followers()->count()); 101 | 102 | // Passing the model should return the values properly. 103 | 104 | $this->assertEquals(1, $this->bob->following(Page::class)->count()); 105 | $this->assertEquals(0, $this->bob->followers(Page::class)->count()); 106 | 107 | $this->assertEquals(0, $this->page->following(User::class)->count()); 108 | $this->assertEquals(1, $this->page->followers(User::class)->count()); 109 | } 110 | 111 | public function test_unfollowing_with_custom_model() 112 | { 113 | $this->assertFalse( 114 | $this->bob->unfollow($this->page) 115 | ); 116 | 117 | $this->bob->follow($this->page); 118 | 119 | $this->assertTrue( 120 | $this->bob->unfollow($this->page) 121 | ); 122 | 123 | $this->assertFalse( 124 | $this->bob->follows($this->page) 125 | ); 126 | 127 | $this->assertEquals(0, $this->bob->following(Page::class)->count()); 128 | $this->assertEquals(0, $this->bob->followers(Page::class)->count()); 129 | 130 | $this->assertEquals(0, $this->page->following(User::class)->count()); 131 | $this->assertEquals(0, $this->page->followers(User::class)->count()); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/Traits/CanFollow.php: -------------------------------------------------------------------------------- 1 | getModelMorphClass($model); 21 | 22 | return $this 23 | ->morphToMany($modelClass, 'follower', 'followers', 'follower_id', 'followable_id') 24 | ->withPivot(['followable_type', 'accepted']) 25 | ->wherePivot('followable_type', $modelClass) 26 | ->wherePivot('follower_type', $this->getMorphClass()) 27 | ->wherePivot('accepted', true) 28 | ->withTimestamps(); 29 | } 30 | 31 | /** 32 | * Check if the current model is following another model. 33 | * 34 | * @param \Illuminate\Database\Eloquent\Model $model 35 | * @return bool 36 | */ 37 | public function isFollowing($model): bool 38 | { 39 | if (! $model instanceof Followable && ! $model instanceof Following) { 40 | return false; 41 | } 42 | 43 | return ! is_null( 44 | $this 45 | ->following((new $model)->getMorphClass()) 46 | ->find($model->getKey()) 47 | ); 48 | } 49 | 50 | /** 51 | * Check if the current model is following another model. 52 | * 53 | * @param \Illuminate\Database\Eloquent\Model $model 54 | * @return bool 55 | */ 56 | public function follows($model): bool 57 | { 58 | return $this->isFollowing($model); 59 | } 60 | 61 | /** 62 | * Follow a certain model. 63 | * 64 | * @param \Illuminate\Database\Eloquent\Model $model 65 | * @return bool 66 | */ 67 | public function follow($model): bool 68 | { 69 | if (! $model instanceof Followable && ! $model instanceof Following) { 70 | return false; 71 | } 72 | 73 | if ($this->isFollowing($model)) { 74 | return false; 75 | } 76 | 77 | if ($this->hasFollowRequested($model)) { 78 | $this 79 | ->followRequests((new $model)->getMorphClass()) 80 | ->find($model->getKey()) 81 | ->pivot 82 | ->update(['accepted' => true]); 83 | } else { 84 | $this->following()->attach($model->getKey(), [ 85 | 'followable_type' => (new $model)->getMorphClass(), 86 | ]); 87 | } 88 | 89 | return true; 90 | } 91 | 92 | /** 93 | * Unfollow a certain model. 94 | * 95 | * @param \Illuminate\Database\Eloquent\Model $model 96 | * @return bool 97 | */ 98 | public function unfollow($model): bool 99 | { 100 | if (! $model instanceof Followable && ! $model instanceof Following) { 101 | return false; 102 | } 103 | 104 | if (! $this->isFollowing($model)) { 105 | return false; 106 | } 107 | 108 | return (bool) $this 109 | ->following((new $model)->getMorphClass()) 110 | ->detach($model->getKey()); 111 | } 112 | 113 | /** 114 | * Relationship for models that this model currently has requests for. 115 | * 116 | * @param null|\Illuminate\Database\Eloquent\Model $model 117 | * @return mixed 118 | */ 119 | public function followRequests($model = null) 120 | { 121 | $modelClass = $this->getModelMorphClass($model); 122 | 123 | return $this 124 | ->morphToMany($modelClass, 'follower', 'followers', 'follower_id', 'followable_id') 125 | ->withPivot(['followable_type', 'accepted']) 126 | ->wherePivot('followable_type', $modelClass) 127 | ->wherePivot('follower_type', $this->getMorphClass()) 128 | ->wherePivot('accepted', false) 129 | ->withTimestamps(); 130 | } 131 | 132 | /** 133 | * Check if the current model has requested to follow another model. 134 | * 135 | * @param \Illuminate\Database\Eloquent\Model $model 136 | * @return bool 137 | */ 138 | public function hasFollowRequested($model): bool 139 | { 140 | if (! $model instanceof Followable && ! $model instanceof Following) { 141 | return false; 142 | } 143 | 144 | return ! is_null( 145 | $this 146 | ->followRequests((new $model)->getMorphClass()) 147 | ->find($model->getKey()) 148 | ); 149 | } 150 | 151 | /** 152 | * Request to follow a certain model. 153 | * 154 | * @param \Illuminate\Database\Eloquent\Model $model 155 | * @return bool 156 | */ 157 | public function followRequest($model): bool 158 | { 159 | if (! $model instanceof Followable && ! $model instanceof Following) { 160 | return false; 161 | } 162 | 163 | if ($this->hasFollowRequested($model)) { 164 | return false; 165 | } 166 | 167 | if ($this->isFollowing($model)) { 168 | return false; 169 | } 170 | 171 | $this->followRequests()->attach($model->getKey(), [ 172 | 'followable_type' => (new $model)->getMorphClass(), 173 | 'accepted' => false, 174 | ]); 175 | 176 | return true; 177 | } 178 | 179 | /** 180 | * Cancel follow request a certain model. 181 | * 182 | * @param \Illuminate\Database\Eloquent\Model $model 183 | * @return bool 184 | */ 185 | public function cancelFollowRequest($model): bool 186 | { 187 | if (! $model instanceof Followable && ! $model instanceof Following) { 188 | return false; 189 | } 190 | 191 | if (! $this->hasFollowRequested($model)) { 192 | return false; 193 | } 194 | 195 | return (bool) $this 196 | ->followRequests((new $model)->getMorphClass()) 197 | ->detach($model->getKey()); 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /tests/FollowRequestTest.php: -------------------------------------------------------------------------------- 1 | bob = factory(User::class)->create(); 26 | 27 | $this->alice = factory(User::class)->create(); 28 | 29 | $this->mark = factory(User::class)->create(); 30 | 31 | $this->page = factory(Page::class)->create(); 32 | 33 | $this->simplePage = factory(SimplePage::class)->create(); 34 | } 35 | 36 | public function test_follow_request_to_user() 37 | { 38 | $this->assertTrue( 39 | $this->bob->followRequest($this->alice) 40 | ); 41 | 42 | $this->assertFalse( 43 | $this->bob->followRequest($this->alice) 44 | ); 45 | 46 | $this->assertFalse( 47 | $this->bob->follows($this->alice) 48 | ); 49 | 50 | $this->assertTrue( 51 | $this->bob->hasFollowRequested($this->alice) 52 | ); 53 | 54 | $this->assertTrue( 55 | $this->alice->hasFollowRequestFrom($this->bob) 56 | ); 57 | 58 | $this->assertEquals(1, $this->bob->followRequests()->count()); 59 | $this->assertEquals(1, $this->alice->followerRequests()->count()); 60 | } 61 | 62 | public function test_accept_follow_request() 63 | { 64 | $this->assertFalse( 65 | $this->alice->acceptFollowRequest($this->bob) 66 | ); 67 | 68 | $this->bob->followRequest($this->alice); 69 | 70 | $this->assertTrue( 71 | $this->alice->acceptFollowRequest($this->bob) 72 | ); 73 | 74 | $this->assertFalse( 75 | $this->alice->acceptFollowRequest($this->bob) 76 | ); 77 | 78 | $this->assertTrue( 79 | $this->bob->follows($this->alice) 80 | ); 81 | 82 | $this->assertEquals(0, $this->bob->followerRequests()->count()); 83 | $this->assertEquals(0, $this->alice->followRequests()->count()); 84 | } 85 | 86 | public function test_decline_follow_request() 87 | { 88 | $this->assertFalse( 89 | $this->alice->declineFollowRequest($this->bob) 90 | ); 91 | 92 | $this->bob->followRequest($this->alice); 93 | 94 | $this->assertTrue( 95 | $this->alice->declineFollowRequest($this->bob) 96 | ); 97 | 98 | $this->assertFalse( 99 | $this->alice->declineFollowRequest($this->bob) 100 | ); 101 | 102 | $this->assertFalse( 103 | $this->bob->follows($this->alice) 104 | ); 105 | 106 | $this->assertEquals(0, $this->bob->followerRequests()->count()); 107 | $this->assertEquals(0, $this->alice->followRequests()->count()); 108 | } 109 | 110 | public function test_cancel_follower_request() 111 | { 112 | $this->assertFalse( 113 | $this->bob->cancelFollowRequest($this->alice) 114 | ); 115 | 116 | $this->bob->followRequest($this->alice); 117 | 118 | $this->assertTrue( 119 | $this->bob->cancelFollowRequest($this->alice) 120 | ); 121 | 122 | $this->assertFalse( 123 | $this->bob->declineFollowRequest($this->alice) 124 | ); 125 | 126 | $this->assertFalse( 127 | $this->bob->follows($this->alice) 128 | ); 129 | 130 | $this->assertEquals(0, $this->bob->followerRequests()->count()); 131 | $this->assertEquals(0, $this->alice->followRequests()->count()); 132 | } 133 | 134 | public function test_follow_request_to_custom_model() 135 | { 136 | $this->assertTrue( 137 | $this->bob->followRequest($this->page) 138 | ); 139 | 140 | $this->assertFalse( 141 | $this->bob->followRequest($this->page) 142 | ); 143 | 144 | $this->assertFalse( 145 | $this->bob->follows($this->page) 146 | ); 147 | 148 | $this->assertTrue( 149 | $this->bob->hasFollowRequested($this->page) 150 | ); 151 | 152 | $this->assertTrue( 153 | $this->page->hasFollowRequestFrom($this->bob) 154 | ); 155 | 156 | // Not specifying the model won't return any results. 157 | 158 | $this->assertEquals(0, $this->bob->followRequests()->count()); 159 | $this->assertEquals(0, $this->page->followerRequests()->count()); 160 | 161 | // Not specifying the model won't return any results. 162 | 163 | $this->assertEquals(1, $this->bob->followRequests(Page::class)->count()); 164 | $this->assertEquals(1, $this->page->followerRequests(User::class)->count()); 165 | } 166 | 167 | public function test_accept_follow_request_from_custom_model() 168 | { 169 | $this->assertFalse( 170 | $this->page->acceptFollowRequest($this->bob) 171 | ); 172 | 173 | $this->bob->followRequest($this->page); 174 | 175 | $this->assertTrue( 176 | $this->page->acceptFollowRequest($this->bob) 177 | ); 178 | 179 | $this->assertFalse( 180 | $this->page->acceptFollowRequest($this->bob) 181 | ); 182 | 183 | $this->assertTrue( 184 | $this->bob->follows($this->page) 185 | ); 186 | 187 | $this->assertEquals(0, $this->bob->followerRequests(Page::class)->count()); 188 | $this->assertEquals(0, $this->page->followRequests(User::class)->count()); 189 | } 190 | 191 | public function test_decline_follow_request_from_custom_model() 192 | { 193 | $this->assertFalse( 194 | $this->page->declineFollowRequest($this->bob) 195 | ); 196 | 197 | $this->bob->followRequest($this->page); 198 | 199 | $this->assertTrue( 200 | $this->page->declineFollowRequest($this->bob) 201 | ); 202 | 203 | $this->assertFalse( 204 | $this->page->declineFollowRequest($this->bob) 205 | ); 206 | 207 | $this->assertFalse( 208 | $this->bob->follows($this->page) 209 | ); 210 | 211 | $this->assertEquals(0, $this->bob->followerRequests(Page::class)->count()); 212 | $this->assertEquals(0, $this->page->followRequests(User::class)->count()); 213 | } 214 | 215 | public function test_cancel_follower_request_from_custom_model() 216 | { 217 | $this->assertFalse( 218 | $this->bob->cancelFollowRequest($this->page) 219 | ); 220 | 221 | $this->bob->followRequest($this->page); 222 | 223 | $this->assertTrue( 224 | $this->bob->cancelFollowRequest($this->page) 225 | ); 226 | 227 | $this->assertFalse( 228 | $this->bob->declineFollowRequest($this->page) 229 | ); 230 | 231 | $this->assertFalse( 232 | $this->bob->follows($this->page) 233 | ); 234 | 235 | $this->assertEquals(0, $this->bob->followerRequests(Page::class)->count()); 236 | $this->assertEquals(0, $this->page->followRequests(User::class)->count()); 237 | } 238 | } 239 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright Renoki Co. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Laravel Befriended 2 | ================== 3 | 4 | ![CI](https://github.com/renoki-co/befriended/workflows/CI/badge.svg?branch=master) 5 | [![codecov](https://codecov.io/gh/renoki-co/befriended/branch/master/graph/badge.svg)](https://codecov.io/gh/renoki-co/befriended/branch/master) 6 | [![StyleCI](https://github.styleci.io/repos/141194551/shield?branch=master)](https://github.styleci.io/repos/141194551) 7 | [![Latest Stable Version](https://poser.pugx.org/rennokki/befriended/v/stable)](https://packagist.org/packages/rennokki/befriended) 8 | [![Total Downloads](https://poser.pugx.org/rennokki/befriended/downloads)](https://packagist.org/packages/rennokki/befriended) 9 | [![Monthly Downloads](https://poser.pugx.org/rennokki/befriended/d/monthly)](https://packagist.org/packages/rennokki/befriended) 10 | [![License](https://poser.pugx.org/rennokki/befriended/license)](https://packagist.org/packages/rennokki/befriended) 11 | 12 | Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models 13 | 14 | ## 🤝 Supporting 15 | 16 | **If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with [Github Sponsors](https://github.com/sponsors/rennokki). 📦** 17 | 18 | [](https://github-content.renoki.org/github-repo/21) 19 | 20 | ## 🚀 Installation 21 | 22 | Install the package: 23 | 24 | ```bash 25 | $ composer require rennokki/befriended 26 | ``` 27 | 28 | Publish the config: 29 | 30 | ```bash 31 | $ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="config" 32 | ``` 33 | 34 | Publish the migrations: 35 | 36 | ```bash 37 | $ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="migrations" 38 | ``` 39 | 40 | ## 🙌 Usage 41 | 42 | The power of example is better here. This package allows you simply to assign followers, blockings or likes without too much effort. What makes the package powerful is that you can filter queries using scopes out-of-the-box. 43 | 44 | ```php 45 | $alice = User::where('name', 'Alice')->first(); 46 | $bob = User::where('name', 'Bob')->first(); 47 | $tim = User::where('name', 'Tim')->first(); 48 | 49 | $alice->follow($bob); 50 | 51 | $alice->following()->count(); // 1 52 | $bob->followers()->count(); // 1 53 | 54 | User::followedBy($alice)->get(); // Just Bob shows up 55 | User::unfollowedBy($alice)->get(); // Tim shows up 56 | ``` 57 | 58 | ## Following 59 | 60 | To follow other models, your model should use the `CanFollow` trait and `Follower` contract. 61 | 62 | ```php 63 | use Rennokki\Befriended\Traits\CanFollow; 64 | use Rennokki\Befriended\Contracts\Follower; 65 | 66 | class User extends Model implements Follower { 67 | use CanFollow; 68 | ... 69 | } 70 | ``` 71 | 72 | The other models that can be followed should use `CanBeFollowed` trait and `Followable` contract. 73 | 74 | ```php 75 | use Rennokki\Befriended\Traits\CanBeFollowed; 76 | use Rennokki\Befriended\Contracts\Followable; 77 | 78 | class User extends Model implements Followable { 79 | use CanBeFollowed; 80 | ... 81 | } 82 | ``` 83 | 84 | If your model can both follow & be followed, you can use `Follow` trait and `Following` contract. 85 | 86 | ```php 87 | use Rennokki\Befriended\Traits\Follow; 88 | use Rennokki\Befriended\Contracts\Following; 89 | 90 | class User extends Model implements Following { 91 | use Follow; 92 | ... 93 | } 94 | ``` 95 | 96 | Let's suppose we have an `User` model which can follow and be followed. Within it, we can now check for followers or follow new users: 97 | 98 | ```php 99 | $zuck = User::where('name', 'Mark Zuckerberg')->first(); 100 | $user->follow($zuck); 101 | 102 | $user->following()->count(); // 1 103 | $zuck->followers()->count(); // 1 104 | ``` 105 | 106 | Now, let's suppose we have a `Page` model, than can only be followed: 107 | 108 | ```php 109 | use Rennokki\Befriended\Traits\CanBeFollowed; 110 | use Rennokki\Befriended\Contracts\Followable; 111 | 112 | class Page extends Model implements Followable { 113 | use CanBeFollowed; 114 | ... 115 | } 116 | ``` 117 | 118 | By default, if querying `following()` and `followers()` from the `User` instance, the relationships will return only `User` instances. If you plan to retrieve other instances, such as `Page`, you can pass the model name or model class as an argument to the relationships: 119 | 120 | ```php 121 | $zuckPage = Page::where('username', 'zuck')->first(); 122 | 123 | $user->follow($zuckPage); 124 | $user->following()->count(); // 0, because it doesn't follow any User instance 125 | $user->following(Page::class)->count(); // 1, because it follows only Zuck's page. 126 | ``` 127 | 128 | On-demand, you can check if your model follows some other model: 129 | 130 | ```php 131 | $user->isFollowing($friend); 132 | $user->follows($friend); // alias 133 | ``` 134 | 135 | Some users might want to remove followers from their list. The `Followable` trait comes with a `revokeFollower` method: 136 | 137 | ```php 138 | $friend->follow($user); 139 | 140 | $user->revokeFollower($friend); 141 | ``` 142 | 143 | **Note: Following, unfollowing or checking if following models that do not correctly implement `CanBeFollowed` and `Followable` will always return `false`.** 144 | 145 | ### Filtering followed/unfollowed models 146 | 147 | To filter followed or unfollowed models (which can be any other model) on query, your model which you will query should use the `Rennokki\Befriended\Scopes\FollowFilterable` trait. 148 | 149 | If your `User` model can only like other `Page` models, your `Page` model should use the trait mentioned. 150 | 151 | ```php 152 | $bob = User::where('username', 'john')->first(); 153 | $alice = User::where('username', 'alice')->first(); 154 | 155 | User::followedBy($bob)->get(); // You will get no results. 156 | User::unfollowedBy($bob)->get(); // You will get Alice. 157 | 158 | $bob->follow($alice); 159 | User::followedBy($bob)->get(); // Only Alice pops up. 160 | ``` 161 | 162 | ## Blocking 163 | 164 | Most of the functions are working like the follow feature, but this is helpful when your models would like to block other models. 165 | 166 | Use `CanBlock` trait and `Blocker` contract to allow the model to block other models. 167 | 168 | ```php 169 | use Rennokki\Befriended\Traits\CanBlock; 170 | use Rennokki\Befriended\Contracts\Blocker; 171 | 172 | class User extends Model implements Blocker { 173 | use CanBlock; 174 | ... 175 | } 176 | ``` 177 | 178 | Adding `CanBeBlocked` trait and `Blockable` contract sets the model able to be blocked. 179 | 180 | ```php 181 | use Rennokki\Befriended\Traits\CanBeBlocked; 182 | use Rennokki\Befriended\Contracts\Blockable; 183 | 184 | class User extends Model implements Blockable { 185 | use CanBeBlocked; 186 | ... 187 | } 188 | ``` 189 | 190 | For both, you should be using `Block` trait & `Blocking` contract: 191 | 192 | ```php 193 | use Rennokki\Befriended\Traits\Block; 194 | use Rennokki\Befriended\Contracts\Blocking; 195 | 196 | class User extends Model implements Blocking { 197 | use Block; 198 | ... 199 | } 200 | ``` 201 | 202 | Most of the methods are the same: 203 | 204 | ```php 205 | $user->block($user); 206 | $user->block($page); 207 | $user->unblock($user); 208 | 209 | $user->blocking(); // Users that this user blocks. 210 | $user->blocking(Page::class); // Pages that this user blocks. 211 | $user->blockers(); // Users that block this user. 212 | $user->blockers(Page::class); // Pages that block this user. 213 | 214 | $user->isBlocking($page); 215 | $user->blocks($page); // alias to isBlocking 216 | ``` 217 | 218 | ### Filtering blocked models 219 | 220 | Blocking scopes provided takes away from the query the models that are blocked. Useful to stop showing content when your models blocks other models. 221 | 222 | Make sure that the model that will be queried uses the `Rennokki\Befriended\Scopes\BlockFilterable` trait. 223 | 224 | ```php 225 | $bob = User::where('username', 'john')->first(); 226 | $alice = User::where('username', 'alice')->first(); 227 | 228 | User::withoutBlockingsOf($bob)->get(); // You will get Alice and Bob as results. 229 | 230 | $bob->block($alice); 231 | User::withoutBlockingsOf($bob)->get(); // You will get only Bob as result. 232 | ``` 233 | 234 | ## Liking 235 | 236 | Apply `CanLike` trait and `Liker` contract for models that can like: 237 | 238 | ```php 239 | use Rennokki\Befriended\Traits\CanLike; 240 | use Rennokki\Befriended\Contracts\Liker; 241 | 242 | class User extends Model implements Liker { 243 | use CanLike; 244 | ... 245 | } 246 | ``` 247 | 248 | `CanBeLiked` and `Likeable` trait can be used for models that can be liked: 249 | 250 | ```php 251 | use Rennokki\Befriended\Traits\CanBeLiked; 252 | use Rennokki\Befriended\Contracts\Likeable; 253 | 254 | class Page extends Model implements Likeable { 255 | use CanBeLiked; 256 | ... 257 | } 258 | ``` 259 | 260 | Planning to use both, use the `Like` trait and `Liking` contact: 261 | 262 | ```php 263 | use Rennokki\Befriended\Traits\Like; 264 | use Rennokki\Befriended\Contracts\Liking; 265 | 266 | class User extends Model implements Liking { 267 | use Like; 268 | ... 269 | } 270 | ``` 271 | 272 | As you have already got started with, these are the methods: 273 | 274 | ```php 275 | $user->like($user); 276 | $user->like($page); 277 | $user->unlike($page); 278 | 279 | $user->liking(); // Users that this user likes. 280 | $user->liking(Page::class); // Pages that this user likes. 281 | $user->likers(); // Users that like this user. 282 | $user->likers(Page::class); // Pages that like this user. 283 | 284 | $user->isLiking($page); 285 | $user->likes($page); // alias to isLiking 286 | ``` 287 | 288 | ### Filtering liked content 289 | 290 | Filtering liked content can make showing content easier. For example, showing in the news feed posts that weren't liked by an user can be helpful. 291 | 292 | The model you're querying from must use the `Rennokki\Befriended\Scopes\LikeFilterable` trait. 293 | 294 | Let's suppose there are 10 pages in the database. 295 | 296 | ```php 297 | $bob = User::where('username', 'john')->first(); 298 | $page = Page::find(1); 299 | 300 | Page::notLikedBy($bob)->get(); // You will get 10 results. 301 | 302 | $bob->like($page); 303 | Page::notLikedBy($bob)->get(); // You will get only 9 results. 304 | Page::likedBy($bob)->get(); // You will get one result, the $page 305 | ``` 306 | 307 | ## Follow requests 308 | 309 | This is similar to the way Instagram allows you to request follow of a private profile. 310 | 311 | To follow other models, your model should use the `CanFollow` trait and `Follower` contract. 312 | 313 | ```php 314 | use Rennokki\Befriended\Traits\CanFollow; 315 | use Rennokki\Befriended\Contracts\Follower; 316 | 317 | class User extends Model implements Follower { 318 | use CanFollow; 319 | ... 320 | } 321 | ``` 322 | 323 | The other models that can be followed should use `CanBeFollowed` trait and `Followable` contract. 324 | 325 | ```php 326 | use Rennokki\Befriended\Traits\CanBeFollowed; 327 | use Rennokki\Befriended\Contracts\Followable; 328 | 329 | class User extends Model implements Followable { 330 | use CanBeFollowed; 331 | ... 332 | } 333 | ``` 334 | 335 | If your model can both follow & be followed, you can use `Follow` trait and `Following` contract. 336 | 337 | ```php 338 | use Rennokki\Befriended\Traits\Follow; 339 | use Rennokki\Befriended\Contracts\Following; 340 | 341 | class User extends Model implements Following { 342 | use Follow; 343 | ... 344 | } 345 | ``` 346 | 347 | Let's suppose we have an `User` model which can follow and be followed. Within it, we can now check for follower requests or request to follow a users: 348 | 349 | ```php 350 | $zuck = User::where('name', 'Mark Zuckerberg')->first(); 351 | $user->followRequest($zuck); 352 | 353 | $user->followRequests()->count(); // 1 354 | $zuck->followerRequests()->count(); // 1 355 | $user->follows($zuck); // false 356 | $zuck->acceptFollowRequest($user); // true 357 | $user->follows($zuck); // true 358 | ``` 359 | 360 | Now, let's suppose we have a `Page` model, than can only be followed: 361 | 362 | ```php 363 | use Rennokki\Befriended\Traits\CanBeFollowed; 364 | use Rennokki\Befriended\Contracts\Followable; 365 | 366 | class Page extends Model implements Followable { 367 | use CanBeFollowed; 368 | ... 369 | } 370 | ``` 371 | 372 | You can then request or cancel the follow requests: 373 | 374 | ```php 375 | $user->followRequest($zuck); 376 | $user->cancelFollowRequest($zuck); 377 | ``` 378 | 379 | The one being followed can accept or decline the requests: 380 | 381 | ```php 382 | $zuck->acceptFollowRequest($user); 383 | $zuck->declineFollowRequest($user); 384 | ``` 385 | 386 | By default, if querying `followRequests()` and `followerRequests()` from the `User` instance, the relationships will return only `User` instances. 387 | 388 | If you plan to retrieve other instances, such as `Page`, you can pass the model name or model class as an argument to the relationships: 389 | 390 | ```php 391 | $zuckPage = Page::where('username', 'zuck')->first(); 392 | 393 | $user->followRequest($zuckPage); 394 | $user->followRequests()->count(); // 0, because it does not have any requests from any User instance 395 | $user->followerRequests(Page::class)->count(); // 1, because it has a follow request for Zuck's page. 396 | ``` 397 | 398 | **Note: Requesting, accepting, declining or checking if following models that do not correctly implement `CanBeFollowed` and `Followable` will always return `false`.** 399 | 400 | ## 🐛 Testing 401 | 402 | ``` bash 403 | vendor/bin/phpunit 404 | ``` 405 | 406 | ## 🤝 Contributing 407 | 408 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 409 | 410 | ## 🔒 Security 411 | 412 | If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker. 413 | 414 | ## 🎉 Credits 415 | 416 | - [Alex Renoki](https://github.com/rennokki) 417 | - [All Contributors](../../contributors) 418 | --------------------------------------------------------------------------------