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
|
|
|
|
};
|
2018-05-30 02:59:10 +00:00
|
|
|
use Auth;
|
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;
|
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, [
|
2018-09-02 05:20:51 +00:00
|
|
|
'item' => 'required|integer',
|
|
|
|
]);
|
|
|
|
$item = $request->input('item');
|
|
|
|
$this->handleFollowRequest($item);
|
2018-11-27 08:42:31 +00:00
|
|
|
if($request->wantsJson()) {
|
|
|
|
return response()->json([
|
|
|
|
200
|
|
|
|
], 200);
|
|
|
|
}
|
2018-09-02 05:20:51 +00:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
2018-05-30 02:59:10 +00:00
|
|
|
|
2018-09-02 05:20:51 +00:00
|
|
|
protected function handleFollowRequest($item)
|
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
$user = Auth::user()->profile;
|
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) {
|
|
|
|
return redirect()->back()->with('error', 'You cannot follow this user.');
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
$isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->count();
|
2018-05-30 02:59:10 +00:00
|
|
|
|
2019-01-12 20:46:14 +00:00
|
|
|
if($private == true && $isFollowing == 0 || $remote == true) {
|
2018-09-03 04:00:31 +00:00
|
|
|
$follow = FollowRequest::firstOrCreate([
|
|
|
|
'follower_id' => $user->id,
|
|
|
|
'following_id' => $target->id
|
|
|
|
]);
|
2018-09-02 05:20:51 +00:00
|
|
|
} elseif ($isFollowing == 0) {
|
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();
|
|
|
|
$follower->delete();
|
|
|
|
}
|
2018-05-30 02:59:10 +00:00
|
|
|
}
|
2018-04-18 02:17:30 +00:00
|
|
|
}
|