Users can now control which in-app and email notifications they receive from their Settings tab on their channel page. Bell (in-app) preferences: - New comment on my video (default: on) - Reply to my comment (default: on) - Comment liked (default: on) - Video liked (default: on) - New subscriber (default: on) - New video from channels I follow (default: on) - New post from channels I follow (default: on) - New user registration — super admins only (default: on) Email preferences (same set plus): - Comment liked (default: off — too noisy) - Video liked (default: off — too noisy) - New post from channels I follow (default: off) - My video finished processing (default: on) - Weekly activity digest every Monday (default: on) - New user registration — super admins only (default: on) Implementation: - Migration: notification_preferences JSON column on users table - User::notificationPref($key) helper with typed defaults - All existing notification classes updated to check prefs in via() - 4 new notification classes: NewSubscriberNotification, VideoLikedNotification, NewPostNotification, WeeklyDigestNotification - 8 new email views matching existing dark theme - SendWeeklyDigest artisan command, scheduled every Monday 09:00 - NewSubscriberNotification wired into UserController::toggleSubscribe - VideoLikedNotification wired into UserController::toggleLike - NewPostNotification wired into PostController::store (to all subscribers) - Bell renderer updated for new_subscriber, video_like, new_post types - Preferences saved via AJAX (POST /settings/notifications) — instant toggle with automatic revert on failure Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
170 lines
5.9 KiB
PHP
170 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Post;
|
|
use App\Models\PostImage;
|
|
use App\Models\PostVideo;
|
|
use App\Models\User;
|
|
use App\Notifications\NewPostNotification;
|
|
use App\Services\NasSyncService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class PostController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
}
|
|
|
|
public function store(Request $request, User $user)
|
|
{
|
|
if (Auth::id() !== $user->id) {
|
|
abort(403);
|
|
}
|
|
|
|
$request->validate([
|
|
'body' => 'nullable|string|max:2000',
|
|
'images' => 'nullable|array|max:10',
|
|
'images.*' => 'image|mimes:jpg,jpeg,png,webp,gif|max:8192',
|
|
'video_ids' => 'nullable|array|max:10',
|
|
'video_ids.*' => 'exists:videos,id',
|
|
'video_id' => 'nullable|exists:videos,id',
|
|
'image' => 'nullable|image|mimes:jpg,jpeg,png,webp,gif|max:8192',
|
|
]);
|
|
|
|
$hasImages = $request->hasFile('images');
|
|
$hasVideoIds = $request->filled('video_ids');
|
|
$hasLegacyImg = $request->hasFile('image');
|
|
$hasLegacyVid = $request->filled('video_id');
|
|
|
|
if (! $request->body && ! $hasImages && ! $hasLegacyImg && ! $hasVideoIds && ! $hasLegacyVid) {
|
|
return back()->withErrors(['body' => 'Post cannot be empty.']);
|
|
}
|
|
|
|
// Create post first — we need the ID as the folder name
|
|
$post = Post::create([
|
|
'user_id' => $user->id,
|
|
'body' => $request->body,
|
|
'video_id' => $request->video_id ?? null,
|
|
]);
|
|
|
|
$nas = app(NasSyncService::class);
|
|
$nasMode = $nas->isEnabled();
|
|
$postDir = $nas->resolvePostDir($post); // "users/{slug}/posts/{id}"
|
|
|
|
if ($hasImages || $hasLegacyImg) {
|
|
if ($nasMode) {
|
|
// ── NAS primary: upload directly from PHP temp files ──────────
|
|
$nas->mkdirp($postDir);
|
|
|
|
if ($hasLegacyImg) {
|
|
$file = $request->file('image');
|
|
$ext = $file->getClientOriginalExtension() ?: 'jpg';
|
|
$nasPath = "{$postDir}/0.{$ext}";
|
|
$nas->putFile($file->getRealPath(), $nasPath);
|
|
$post->update(['image' => $nasPath]);
|
|
}
|
|
|
|
if ($hasImages) {
|
|
foreach ($request->file('images') as $idx => $file) {
|
|
$ext = $file->getClientOriginalExtension() ?: 'jpg';
|
|
$nasPath = "{$postDir}/" . ($idx + 1) . ".{$ext}";
|
|
$nas->putFile($file->getRealPath(), $nasPath);
|
|
PostImage::create([
|
|
'post_id' => $post->id,
|
|
'filename' => $nasPath,
|
|
'sort_order' => $idx,
|
|
]);
|
|
}
|
|
}
|
|
} else {
|
|
// ── Local storage: save inside the user's posts directory ─────
|
|
$localDir = storage_path('app/' . $postDir);
|
|
@mkdir($localDir, 0755, true);
|
|
|
|
if ($hasLegacyImg) {
|
|
$ext = $request->file('image')->getClientOriginalExtension() ?: 'jpg';
|
|
$filename = "0.{$ext}";
|
|
$request->file('image')->move($localDir, $filename);
|
|
$post->update(['image' => "{$postDir}/{$filename}"]);
|
|
}
|
|
|
|
if ($hasImages) {
|
|
foreach ($request->file('images') as $idx => $file) {
|
|
$ext = $file->getClientOriginalExtension() ?: 'jpg';
|
|
$filename = ($idx + 1) . ".{$ext}";
|
|
$file->move($localDir, $filename);
|
|
PostImage::create([
|
|
'post_id' => $post->id,
|
|
'filename' => "{$postDir}/{$filename}",
|
|
'sort_order' => $idx,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($hasVideoIds) {
|
|
foreach ($request->input('video_ids') as $idx => $videoId) {
|
|
PostVideo::create([
|
|
'post_id' => $post->id,
|
|
'video_id' => $videoId,
|
|
'sort_order' => $idx,
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Notify subscribers of new post
|
|
$author = $user->fresh();
|
|
$author->subscribers()->each(function (User $subscriber) use ($post, $author) {
|
|
try { $subscriber->notify(new NewPostNotification($post, $author)); } catch (\Throwable) {}
|
|
});
|
|
|
|
return back()->with('toast_success', 'Post shared!');
|
|
}
|
|
|
|
public function destroy(Post $post)
|
|
{
|
|
if (Auth::id() !== $post->user_id && ! Auth::user()->isAdmin()) {
|
|
abort(403);
|
|
}
|
|
|
|
$post->loadMissing('postImages');
|
|
$nas = app(NasSyncService::class);
|
|
|
|
if ($nas->isEnabled()) {
|
|
try {
|
|
$nas->deleteNasPost($post);
|
|
} catch (\Throwable) {}
|
|
}
|
|
|
|
// Always clean up local copies (handles both legacy flat and new structured format)
|
|
$nas->deleteLocalPostImages($post);
|
|
|
|
$post->delete();
|
|
|
|
return back()->with('toast_success', 'Post deleted.');
|
|
}
|
|
|
|
public function react(Post $post)
|
|
{
|
|
$user = Auth::user();
|
|
$existing = $post->reactions()->where('user_id', $user->id)->first();
|
|
|
|
if ($existing) {
|
|
$existing->delete();
|
|
$liked = false;
|
|
} else {
|
|
$post->reactions()->create(['user_id' => $user->id, 'type' => 'like']);
|
|
$liked = true;
|
|
}
|
|
|
|
return response()->json([
|
|
'liked' => $liked,
|
|
'count' => $post->reactions()->count(),
|
|
]);
|
|
}
|
|
}
|