96 lines
3.1 KiB
PHP
96 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Followee;
|
|
use App\Models\Notification;
|
|
use App\Models\User;
|
|
use App\Settings\SettingsRegistry;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Inertia\Inertia;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function follow(User $user): JsonResponse
|
|
{
|
|
abort_if($user->id === auth()->id(), 403);
|
|
|
|
$existing = Followee::where('user_id', auth()->id())
|
|
->where('followee_id', $user->id)
|
|
->first();
|
|
|
|
if ($existing) {
|
|
$existing->delete();
|
|
return response()->json(['status' => 'none']);
|
|
}
|
|
|
|
$canView = Gate::allows('viewProfileData', $user);
|
|
|
|
Followee::create([
|
|
'user_id' => auth()->id(),
|
|
'followee_id' => $user->id,
|
|
'verified' => $canView,
|
|
]);
|
|
|
|
Notification::create([
|
|
'user_id' => $user->id,
|
|
'title' => $canView ? 'New follower' : 'Follow request',
|
|
'body' => $canView
|
|
? auth()->user()->name . ' is now following you.'
|
|
: auth()->user()->name . ' wants to follow you.',
|
|
'is_achievement' => false,
|
|
'url' => $canView ? '/u/' . auth()->user()->name : '/follow-requests',
|
|
]);
|
|
|
|
return response()->json(['status' => $canView ? 'following' : 'requested']);
|
|
}
|
|
|
|
public function approveRequest(User $follower): JsonResponse
|
|
{
|
|
$followee = Followee::where('user_id', $follower->id)
|
|
->where('followee_id', auth()->id())
|
|
->pending()
|
|
->firstOrFail();
|
|
|
|
$followee->update(['verified' => true]);
|
|
|
|
Notification::create([
|
|
'user_id' => $follower->id,
|
|
'title' => 'Follow request accepted',
|
|
'body' => auth()->user()->name . ' accepted your follow request.',
|
|
'is_achievement' => false,
|
|
'url' => '/u/' . auth()->user()->name,
|
|
]);
|
|
|
|
return response()->json(['approved' => true]);
|
|
}
|
|
|
|
public function denyRequest(User $follower): JsonResponse
|
|
{
|
|
Followee::where('user_id', $follower->id)
|
|
->where('followee_id', auth()->id())
|
|
->pending()
|
|
->delete();
|
|
|
|
return response()->json(['denied' => true]);
|
|
}
|
|
|
|
public function settings(?string $category = null){
|
|
$allowedTabs = ['general', 'followers'];
|
|
$user = auth()->user();
|
|
$current = array_merge(SettingsRegistry::defaults(), $user->settings ?? []);
|
|
$fields = array_map(fn($field) => array_merge($field, [
|
|
'value' => $current[$field['key']] ?? $field['default'],
|
|
]), SettingsRegistry::schema());
|
|
|
|
return Inertia::render('UserSettings', [
|
|
'fields' => $fields,
|
|
'categories' => SettingsRegistry::categories(),
|
|
'defaultTab' => in_array($category, $allowedTabs, true) ? $category : 'general',
|
|
]);
|
|
}
|
|
}
|