temian/laravel-befriended

Eloquent Befriended 提供类似社交媒体的功能,如关注、屏蔽以及根据关注或屏蔽的模型过滤内容。

dev-main 2023-04-21 18:12 UTC

This package is auto-updated.

Last update: 2024-09-12 06:42:18 UTC


README

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Eloquent Befriended 提供类似社交媒体的功能,如关注、屏蔽以及根据关注或屏蔽的模型过滤内容

🤝 支持

如果你在生产应用、演示、爱好项目、学校项目等中使用 Renoki Co. 的一个或多个开源包,请通过 Github Sponsors 支持我们的工作。 📦

🚀 安装

安装包

$ composer require rennokki/befriended

发布配置

$ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="config"

发布迁移

$ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="migrations"

🙌 使用

示例的力量在这里更为明显。此包允许你轻松地分配关注者、屏蔽或点赞,而不需要太多努力。使此包强大的是,你可以使用开箱即用的作用域来过滤查询。

$alice = User::where('name', 'Alice')->first();
$bob = User::where('name', 'Bob')->first();
$tim = User::where('name', 'Tim')->first();

$alice->follow($bob);

$alice->following()->count(); // 1
$bob->followers()->count(); // 1

User::followedBy($alice)->get(); // Just Bob shows up
User::unfollowedBy($alice)->get(); // Tim shows up

关注

要关注其他模型,你的模型应使用 CanFollow 特性和 Follower 合约。

use Rennokki\Befriended\Traits\CanFollow;
use Rennokki\Befriended\Contracts\Follower;

class User extends Model implements Follower {
    use CanFollow;
    ...
}

可以被关注的其他模型应使用 CanBeFollowed 特性和 Followable 合约。

use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class User extends Model implements Followable {
    use CanBeFollowed;
    ...
}

如果你的模型既可以关注又可以被关注,你可以使用 Follow 特性和 Following 合约。

use Rennokki\Befriended\Traits\Follow;
use Rennokki\Befriended\Contracts\Following;

class User extends Model implements Following {
    use Follow;
    ...
}

假设我们有一个 User 模型,它可以关注并可以被关注。在它里面,我们现在可以检查关注者或关注新用户

$zuck = User::where('name', 'Mark Zuckerberg')->first();
$user->follow($zuck);

$user->following()->count(); // 1
$zuck->followers()->count(); // 1

现在,假设我们有一个 Page 模型,它只能被关注

use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class Page extends Model implements Followable {
    use CanBeFollowed;
    ...
}

默认情况下,如果从 User 实例查询 following()followers(),关系将仅返回 User 实例。如果你计划检索其他实例,例如 Page,你可以将模型名称或模型类作为参数传递给关系

$zuckPage = Page::where('username', 'zuck')->first();

$user->follow($zuckPage);
$user->following()->count(); // 0, because it doesn't follow any User instance
$user->following(Page::class)->count(); // 1, because it follows only Zuck's page.

按需,你可以检查你的模型是否关注了其他模型

$user->isFollowing($friend);
$user->follows($friend); // alias

一些用户可能想要从他们的列表中删除关注者。带有 Followable 特性的 revokeFollower 方法

$friend->follow($user);

$user->revokeFollower($friend);

注意:关注、取消关注或检查未正确实现 CanBeFollowedFollowable 的模型时,总会返回 false

过滤关注/取消关注的模型

要在查询中过滤关注或取消关注的模型(可以是任何其他模型),你将要查询的模型应使用 Rennokki\Befriended\Scopes\FollowFilterable 特性。

如果你的 User 模型只能喜欢其他 Page 模型,那么你的 Page 模型应使用上述提到的特性。

$bob = User::where('username', 'john')->first();
$alice = User::where('username', 'alice')->first();

User::followedBy($bob)->get(); // You will get no results.
User::unfollowedBy($bob)->get(); // You will get Alice.

$bob->follow($alice);
User::followedBy($bob)->get(); // Only Alice pops up.

屏蔽

大部分功能的工作方式与关注功能类似,但在你的模型需要屏蔽其他模型时,这很有帮助。

使用 CanBlock 特性和 Blocker 合约来允许模型屏蔽其他模型。

use Rennokki\Befriended\Traits\CanBlock;
use Rennokki\Befriended\Contracts\Blocker;

class User extends Model implements Blocker {
    use CanBlock;
    ...
}

添加 CanBeBlocked 特性和 Blockable 合约设置模型能够被屏蔽。

use Rennokki\Befriended\Traits\CanBeBlocked;
use Rennokki\Befriended\Contracts\Blockable;

class User extends Model implements Blockable {
    use CanBeBlocked;
    ...
}

对于这两个,你应该使用 Block 特性和 Blocking 合约

use Rennokki\Befriended\Traits\Block;
use Rennokki\Befriended\Contracts\Blocking;

class User extends Model implements Blocking {
    use Block;
    ...
}

大部分方法都是相同的

$user->block($user);
$user->block($page);
$user->unblock($user);

$user->blocking(); // Users that this user blocks.
$user->blocking(Page::class); // Pages that this user blocks.
$user->blockers(); // Users that block this user.
$user->blockers(Page::class); // Pages that block this user.

$user->isBlocking($page);
$user->blocks($page); // alias to isBlocking

过滤屏蔽的模型

屏蔽作用域从查询中移除了被屏蔽的模型。当你的模型屏蔽其他模型时,这很有用,可以停止显示内容。

确保将要被查询的模型使用了 Rennokki\Befriended\Scopes\BlockFilterable 特性。

$bob = User::where('username', 'john')->first();
$alice = User::where('username', 'alice')->first();

User::withoutBlockingsOf($bob)->get(); // You will get Alice and Bob as results.

$bob->block($alice);
User::withoutBlockingsOf($bob)->get(); // You will get only Bob as result.

点赞

为可以点赞的模型应用 CanLike 特性和 Liker 合约

use Rennokki\Befriended\Traits\CanLike;
use Rennokki\Befriended\Contracts\Liker;

class User extends Model implements Liker {
    use CanLike;
    ...
}

CanBeLikedLikeable 特性可以用于可以被点赞的模型

use Rennokki\Befriended\Traits\CanBeLiked;
use Rennokki\Befriended\Contracts\Likeable;

class Page extends Model implements Likeable {
    use CanBeLiked;
    ...
}

计划同时使用两者,请使用 Like 特性和 Liking 合约

use Rennokki\Befriended\Traits\Like;
use Rennokki\Befriended\Contracts\Liking;

class User extends Model implements Liking {
    use Like;
    ...
}

就像你已经开始的那样,这些是方法

$user->like($user);
$user->like($page);
$user->unlike($page);

$user->liking(); // Users that this user likes.
$user->liking(Page::class); // Pages that this user likes.
$user->likers(); // Users that like this user.
$user->likers(Page::class); // Pages that like this user.

$user->isLiking($page);
$user->likes($page); // alias to isLiking

内容过滤

内容过滤可以使展示内容变得更简单。例如,显示新闻源中用户未点赞的帖子可能很有帮助。

您查询的模型必须使用 Rennokki\Befriended\Scopes\LikeFilterable 特性。

假设数据库中有10页。

$bob = User::where('username', 'john')->first();
$page = Page::find(1);

Page::notLikedBy($bob)->get(); // You will get 10 results.

$bob->like($page);
Page::notLikedBy($bob)->get(); // You will get only 9 results.
Page::likedBy($bob)->get(); // You will get one result, the $page

关注请求

这类似于Instagram允许你向私人资料请求关注的方式。

要关注其他模型,你的模型应使用 CanFollow 特性和 Follower 合约。

use Rennokki\Befriended\Traits\CanFollow;
use Rennokki\Befriended\Contracts\Follower;

class User extends Model implements Follower {
    use CanFollow;
    ...
}

可以被关注的其他模型应使用 CanBeFollowed 特性和 Followable 合约。

use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class User extends Model implements Followable {
    use CanBeFollowed;
    ...
}

如果你的模型既可以关注又可以被关注,你可以使用 Follow 特性和 Following 合约。

use Rennokki\Befriended\Traits\Follow;
use Rennokki\Befriended\Contracts\Following;

class User extends Model implements Following {
    use Follow;
    ...
}

假设我们有一个 User 模型,它可以关注他人,也可以被他人关注。在其中,我们现在可以检查关注请求或向用户发送关注请求

$zuck = User::where('name', 'Mark Zuckerberg')->first();
$user->followRequest($zuck);

$user->followRequests()->count(); // 1
$zuck->followerRequests()->count(); // 1
$user->follows($zuck); // false
$zuck->acceptFollowRequest($user); // true
$user->follows($zuck); // true

现在,假设我们有一个 Page 模型,它只能被关注

use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class Page extends Model implements Followable {
    use CanBeFollowed;
    ...
}

然后你可以请求或取消关注请求

$user->followRequest($zuck);
$user->cancelFollowRequest($zuck);

被关注者可以接受或拒绝请求

$zuck->acceptFollowRequest($user);
$zuck->declineFollowRequest($user);

默认情况下,从 User 实例查询 followRequests()followerRequests() 时,关系将仅返回 User 实例。

如果你计划检索其他实例,例如 Page,你可以将模型名称或模型类作为参数传递给关系

$zuckPage = Page::where('username', 'zuck')->first();

$user->followRequest($zuckPage);
$user->followRequests()->count(); // 0, because it does not have any requests from any User instance
$user->followerRequests(Page::class)->count(); // 1, because it has a follow request for Zuck's page.

注意:如果请求、接受、拒绝或检查未正确实现 CanBeFollowedFollowable 的关注模型,将始终返回 false

🐛 测试

vendor/bin/phpunit

🤝 贡献

请参阅 CONTRIBUTING 了解详细信息。

🔒 安全性

如果你发现任何与安全性相关的问题,请通过电子邮件发送至 alex@renoki.org,而不是使用问题跟踪器。

🎉 致谢