pixelfed/app/Http/Controllers/FollowerController.php

43 lines
1.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2018-08-28 03:07:36 +00:00
use App\Follower;
use App\Jobs\FollowPipeline\FollowPipeline;
use App\Profile;
2018-05-30 02:59:10 +00:00
use Auth;
use Illuminate\Http\Request;
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-05-30 02:59:10 +00:00
'item' => 'required|integer',
]);
2018-08-28 03:07:36 +00:00
$user = Auth::user()->profile;
$target = Profile::where('id', '!=', $user->id)->findOrFail($request->input('item'));
2018-05-30 02:59:10 +00:00
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
2018-08-28 03:07:36 +00:00
if ($isFollowing == 0) {
$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-08-28 03:07:36 +00:00
return redirect()->back();
2018-05-30 02:59:10 +00:00
}
}