2018-04-18 02:17:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-11-03 02:58:26 +00:00
|
|
|
use App\{
|
|
|
|
Follower,
|
|
|
|
FollowRequest,
|
|
|
|
Profile,
|
|
|
|
UserFilter
|
|
|
|
};
|
2019-02-25 05:57:22 +00:00
|
|
|
use Auth, Cache;
|
2018-04-18 02:17:30 +00:00
|
|
|
use Illuminate\Http\Request;
|
2018-11-03 02:58:26 +00:00
|
|
|
use App\Jobs\FollowPipeline\FollowPipeline;
|
2019-06-11 03:30:01 +00:00
|
|
|
use App\Util\ActivityPub\Helpers;
|
2021-10-07 09:27:13 +00:00
|
|
|
use App\Services\FollowerService;
|
2018-04-18 02:17:30 +00:00
|
|
|
|
|
|
|
class FollowerController extends Controller
|
|
|
|
{
|
2018-05-30 02:59:10 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
$this->middleware('auth');
|
2018-05-30 02:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
$this->validate($request, [
|
2019-12-05 03:24:08 +00:00
|
|
|
'item' => 'required|string',
|
|
|
|
'force' => 'nullable|boolean',
|
2018-09-02 05:20:51 +00:00
|
|
|
]);
|
2019-12-05 03:24:08 +00:00
|
|
|
$force = (bool) $request->input('force', true);
|
2019-08-23 03:22:00 +00:00
|
|
|
$item = (int) $request->input('item');
|
2019-12-05 03:24:08 +00:00
|
|
|
$url = $this->handleFollowRequest($item, $force);
|
|
|
|
if($request->wantsJson() == true) {
|
2019-09-11 05:33:51 +00:00
|
|
|
return response()->json(200);
|
|
|
|
} else {
|
2019-12-05 03:24:08 +00:00
|
|
|
return redirect($url);
|
2019-09-11 05:33:51 +00:00
|
|
|
}
|
2018-09-02 05:20:51 +00:00
|
|
|
}
|
2018-05-30 02:59:10 +00:00
|
|
|
|
2019-12-05 03:24:08 +00:00
|
|
|
protected function handleFollowRequest($item, $force)
|
2018-09-02 05:20:51 +00:00
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
$user = Auth::user()->profile;
|
2019-06-02 02:18:21 +00:00
|
|
|
|
2018-12-24 04:51:44 +00:00
|
|
|
$target = Profile::where('id', '!=', $user->id)->whereNull('status')->findOrFail($item);
|
2018-09-02 05:20:51 +00:00
|
|
|
$private = (bool) $target->is_private;
|
2019-01-12 20:46:14 +00:00
|
|
|
$remote = (bool) $target->domain;
|
2018-11-03 02:58:26 +00:00
|
|
|
$blocked = UserFilter::whereUserId($target->id)
|
|
|
|
->whereFilterType('block')
|
|
|
|
->whereFilterableId($user->id)
|
|
|
|
->whereFilterableType('App\Profile')
|
|
|
|
->exists();
|
|
|
|
|
|
|
|
if($blocked == true) {
|
2019-06-02 02:18:21 +00:00
|
|
|
abort(400, 'You cannot follow this user.');
|
2018-11-03 02:58:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 07:25:53 +00:00
|
|
|
$isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->exists();
|
2018-05-30 02:59:10 +00:00
|
|
|
|
2019-11-11 03:42:30 +00:00
|
|
|
if($private == true && $isFollowing == 0) {
|
2019-06-11 03:30:01 +00:00
|
|
|
if($user->following()->count() >= Follower::MAX_FOLLOWING) {
|
|
|
|
abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
|
|
|
|
abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
|
|
|
|
}
|
|
|
|
|
2018-09-03 04:00:31 +00:00
|
|
|
$follow = FollowRequest::firstOrCreate([
|
|
|
|
'follower_id' => $user->id,
|
|
|
|
'following_id' => $target->id
|
|
|
|
]);
|
2019-06-11 03:30:01 +00:00
|
|
|
if($remote == true && config('federation.activitypub.remoteFollow') == true) {
|
|
|
|
$this->sendFollow($user, $target);
|
2021-10-07 09:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FollowerService::add($user->id, $target->id);
|
2019-11-11 03:42:30 +00:00
|
|
|
} elseif ($private == false && $isFollowing == 0) {
|
2019-06-02 02:18:21 +00:00
|
|
|
if($user->following()->count() >= Follower::MAX_FOLLOWING) {
|
|
|
|
abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
|
|
|
|
abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
|
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
$follower = new Follower();
|
|
|
|
$follower->profile_id = $user->id;
|
|
|
|
$follower->following_id = $target->id;
|
|
|
|
$follower->save();
|
2019-08-12 08:47:20 +00:00
|
|
|
|
2019-08-12 07:25:53 +00:00
|
|
|
if($remote == true && config('federation.activitypub.remoteFollow') == true) {
|
|
|
|
$this->sendFollow($user, $target);
|
|
|
|
}
|
2021-10-07 09:27:13 +00:00
|
|
|
FollowerService::add($user->id, $target->id);
|
2018-08-28 03:07:36 +00:00
|
|
|
FollowPipeline::dispatch($follower);
|
|
|
|
} else {
|
2019-12-05 03:24:08 +00:00
|
|
|
if($force == true) {
|
|
|
|
$request = FollowRequest::whereFollowerId($user->id)->whereFollowingId($target->id)->exists();
|
|
|
|
$follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->exists();
|
|
|
|
if($remote == true && $request && !$follower) {
|
|
|
|
$this->sendFollow($user, $target);
|
|
|
|
}
|
|
|
|
if($remote == true && $follower) {
|
|
|
|
$this->sendUndoFollow($user, $target);
|
|
|
|
}
|
|
|
|
Follower::whereProfileId($user->id)
|
|
|
|
->whereFollowingId($target->id)
|
|
|
|
->delete();
|
2021-10-07 09:27:13 +00:00
|
|
|
FollowerService::remove($user->id, $target->id);
|
2019-06-11 20:34:05 +00:00
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
}
|
2019-02-25 05:57:22 +00:00
|
|
|
|
2019-03-04 05:05:58 +00:00
|
|
|
Cache::forget('profile:following:'.$target->id);
|
2019-02-25 05:57:22 +00:00
|
|
|
Cache::forget('profile:followers:'.$target->id);
|
|
|
|
Cache::forget('profile:following:'.$user->id);
|
2019-03-04 05:05:58 +00:00
|
|
|
Cache::forget('profile:followers:'.$user->id);
|
2019-04-28 23:49:24 +00:00
|
|
|
Cache::forget('api:local:exp:rec:'.$user->id);
|
2019-04-30 02:37:27 +00:00
|
|
|
Cache::forget('user:account:id:'.$target->user_id);
|
|
|
|
Cache::forget('user:account:id:'.$user->user_id);
|
2020-01-07 06:32:32 +00:00
|
|
|
Cache::forget('px:profile:followers-v1.3:'.$user->id);
|
|
|
|
Cache::forget('px:profile:followers-v1.3:'.$target->id);
|
|
|
|
Cache::forget('px:profile:following-v1.3:'.$user->id);
|
|
|
|
Cache::forget('px:profile:following-v1.3:'.$target->id);
|
2020-12-11 05:58:13 +00:00
|
|
|
Cache::forget('profile:follower_count:'.$target->id);
|
|
|
|
Cache::forget('profile:follower_count:'.$user->id);
|
|
|
|
Cache::forget('profile:following_count:'.$target->id);
|
|
|
|
Cache::forget('profile:following_count:'.$user->id);
|
2019-12-05 03:24:08 +00:00
|
|
|
|
|
|
|
return $target->url();
|
2018-05-30 02:59:10 +00:00
|
|
|
}
|
2019-06-11 03:30:01 +00:00
|
|
|
|
2019-09-25 03:20:53 +00:00
|
|
|
public function sendFollow($user, $target)
|
2019-06-11 03:30:01 +00:00
|
|
|
{
|
|
|
|
if($target->domain == null || $user->domain != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$payload = [
|
|
|
|
'@context' => 'https://www.w3.org/ns/activitystreams',
|
2019-09-25 03:20:53 +00:00
|
|
|
'id' => $user->permalink('#follow/'.$target->id),
|
2019-06-11 03:30:01 +00:00
|
|
|
'type' => 'Follow',
|
|
|
|
'actor' => $user->permalink(),
|
|
|
|
'object' => $target->permalink()
|
|
|
|
];
|
|
|
|
|
|
|
|
$inbox = $target->sharedInbox ?? $target->inbox_url;
|
|
|
|
|
|
|
|
Helpers::sendSignedObject($user, $inbox, $payload);
|
|
|
|
}
|
2019-06-11 20:34:05 +00:00
|
|
|
|
2019-09-25 03:20:53 +00:00
|
|
|
public function sendUndoFollow($user, $target)
|
2019-06-11 20:34:05 +00:00
|
|
|
{
|
|
|
|
if($target->domain == null || $user->domain != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$payload = [
|
|
|
|
'@context' => 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id' => $user->permalink('#follow/'.$target->id.'/undo'),
|
|
|
|
'type' => 'Undo',
|
|
|
|
'actor' => $user->permalink(),
|
|
|
|
'object' => [
|
|
|
|
'id' => $user->permalink('#follows/'.$target->id),
|
|
|
|
'actor' => $user->permalink(),
|
|
|
|
'object' => $target->permalink(),
|
|
|
|
'type' => 'Follow'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$inbox = $target->sharedInbox ?? $target->inbox_url;
|
|
|
|
|
|
|
|
Helpers::sendSignedObject($user, $inbox, $payload);
|
|
|
|
}
|
2018-04-18 02:17:30 +00:00
|
|
|
}
|