56 lines
1.6 KiB
PHP
56 lines
1.6 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 Inertia\Inertia;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function follow(User $user): JsonResponse
|
|
{
|
|
$existing = Followee::where('user_id', auth()->id())
|
|
->where('followee_id', $user->id)
|
|
->first();
|
|
|
|
if ($existing) {
|
|
$existing->delete();
|
|
return response()->json(['following' => false]);
|
|
}
|
|
|
|
Followee::create([
|
|
'user_id' => auth()->id(),
|
|
'followee_id' => $user->id,
|
|
]);
|
|
|
|
Notification::create([
|
|
'user_id' => $user->id,
|
|
'title' => 'New follower',
|
|
'body' => auth()->user()->name . ' is now following you.',
|
|
'is_achievement' => false,
|
|
'url' => '/u/'. auth()->user()->name,
|
|
]);
|
|
|
|
return response()->json(['following' => true]);
|
|
}
|
|
|
|
public function settings(){
|
|
$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(),
|
|
]);
|
|
}
|
|
}
|