metaversedataman/laravel-befriended

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

dev-master 2023-08-13 02:09 UTC

This package is auto-updated.

Last update: 2024-09-13 04:20:08 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 而不是使用问题跟踪器。

🎉 致谢