1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2025-03-04 10:39:08 +00:00
pixelfed/app/Http/Controllers/FollowerController.php

146 lines
4.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2018-11-02 20:58:26 -06:00
use App\{
Follower,
FollowRequest,
Profile,
UserFilter
};
2019-02-24 22:57:22 -07:00
use Auth, Cache;
use Illuminate\Http\Request;
2018-11-02 20:58:26 -06:00
use App\Jobs\FollowPipeline\FollowPipeline;
use App\Util\ActivityPub\Helpers;
class FollowerController extends Controller
{
2018-05-29 20:59:10 -06:00
public function __construct()
{
2018-08-28 03:07:36 +00:00
$this->middleware('auth');
2018-05-29 20:59:10 -06:00
}
public function store(Request $request)
{
2018-08-28 03:07:36 +00:00
$this->validate($request, [
'item' => 'required|integer',
]);
$item = $request->input('item');
$this->handleFollowRequest($item);
2018-11-27 01:42:31 -07:00
if($request->wantsJson()) {
return response()->json([
200
], 200);
}
return redirect()->back();
}
2018-05-29 20:59:10 -06:00
protected function handleFollowRequest($item)
{
2018-08-28 03:07:36 +00:00
$user = Auth::user()->profile;
2019-06-01 20:18:21 -06:00
2018-12-23 21:51:44 -07:00
$target = Profile::where('id', '!=', $user->id)->whereNull('status')->findOrFail($item);
$private = (bool) $target->is_private;
2019-01-12 13:46:14 -07:00
$remote = (bool) $target->domain;
2018-11-02 20:58:26 -06:00
$blocked = UserFilter::whereUserId($target->id)
->whereFilterType('block')
->whereFilterableId($user->id)
->whereFilterableType('App\Profile')
->exists();
if($blocked == true) {
2019-06-01 20:18:21 -06:00
abort(400, 'You cannot follow this user.');
2018-11-02 20:58:26 -06:00
}
2018-08-28 03:07:36 +00:00
$isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->count();
2018-05-29 20:59:10 -06:00
2019-01-12 13:46:14 -07:00
if($private == true && $isFollowing == 0 || $remote == true) {
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-02 22:00:31 -06:00
$follow = FollowRequest::firstOrCreate([
'follower_id' => $user->id,
'following_id' => $target->id
]);
if($remote == true && config('federation.activitypub.remoteFollow') == true) {
$this->sendFollow($user, $target);
}
} elseif ($isFollowing == 0) {
2019-06-01 20:18:21 -06: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();
FollowPipeline::dispatch($follower);
} else {
$follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->firstOrFail();
if($remote == true) {
$this->sendUndoFollow($user, $target);
}
2018-08-28 03:07:36 +00:00
$follower->delete();
}
2019-02-24 22:57:22 -07:00
2019-03-03 22:05:58 -07:00
Cache::forget('profile:following:'.$target->id);
2019-02-24 22:57:22 -07:00
Cache::forget('profile:followers:'.$target->id);
Cache::forget('profile:following:'.$user->id);
2019-03-03 22:05:58 -07:00
Cache::forget('profile:followers:'.$user->id);
2019-04-28 17:49:24 -06:00
Cache::forget('api:local:exp:rec:'.$user->id);
Cache::forget('user:account:id:'.$target->user_id);
Cache::forget('user:account:id:'.$user->user_id);
2018-05-29 20:59:10 -06:00
}
protected function sendFollow($user, $target)
{
if($target->domain == null || $user->domain != null) {
return;
}
$payload = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Follow',
'actor' => $user->permalink(),
'object' => $target->permalink()
];
$inbox = $target->sharedInbox ?? $target->inbox_url;
Helpers::sendSignedObject($user, $inbox, $payload);
}
protected function sendUndoFollow($user, $target)
{
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);
}
}