rennokki/befriended

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

资助包维护!
rennokki

4.1.0 2022-02-10 19:27 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联系,而不是使用问题跟踪器。

🎉 致谢