summaryrefslogtreecommitdiff
path: root/app/Http/Controllers/PresenceController.php
blob: 10e1c90a4da414d7bd4475fb77e37da76bdba44b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php

namespace App\Http\Controllers;

use App\Models\Writing;
use App\Models\Comment;
use App\Models\User;
use App\Mail\ContactMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\RateLimiter;

class PresenceController extends Controller
{
    /**
     * Public page with status + timeline
     */
    public function index()
    {
        $user = User::first();
        $posts = Writing::posts()
            ->public()
            ->orderBy('created_at', 'desc')
            ->paginate(10);

        return view('presence.index', [
            'user' => $user,
            'posts' => $posts,
        ]);
    }

    /**
     * Single post with comments
     */
    public function show($id)
    {
        $post = Writing::posts()->public()->findOrFail($id);
        $comments = $post->comments()->approved()->orderBy('created_at', 'asc')->get();

        return view('presence.show', [
            'post' => $post,
            'comments' => $comments,
        ]);
    }

    /**
     * Admin dashboard for posting/managing
     */
    public function admin()
    {
        $user = Auth::user();
        $posts = Writing::posts()
            ->where('user_id', $user->id)
            ->orderBy('created_at', 'desc')
            ->paginate(20);

        $pendingComments = Comment::where('is_approved', false)->count();
        $recentComments = Comment::orderBy('created_at', 'desc')->limit(10)->get();

        return view('presence.admin', [
            'user' => $user,
            'posts' => $posts,
            'pendingComments' => $pendingComments,
            'recentComments' => $recentComments,
        ]);
    }

    /**
     * Update current status
     */
    public function updateStatus(Request $request)
    {
        $validated = $request->validate([
            'statushtml' => 'nullable|string|max:500',
        ]);

        $user = Auth::user();
        $user->statushtml = $validated['statushtml'];
        $user->status_updated_at = now();
        $user->save();

        return redirect()->back()->with('success', 'Status updated!');
    }

    /**
     * Create new post
     */
    public function storePost(Request $request)
    {
        $validated = $request->validate([
            'title' => 'nullable|string|max:255',
            'content' => 'required|string|min:1',
            'is_public' => 'boolean',
        ]);

        Writing::create([
            'title' => $validated['title'] ?? '',
            'content' => $validated['content'],
            'user_id' => Auth::id(),
            'type' => 'post',
            'is_public' => $validated['is_public'] ?? true,
        ]);

        return redirect()->back()->with('success', 'Post created!');
    }

    /**
     * Edit post
     */
    public function updatePost(Request $request, $id)
    {
        $post = Writing::posts()->where('user_id', Auth::id())->findOrFail($id);

        $validated = $request->validate([
            'title' => 'nullable|string|max:255',
            'content' => 'required|string|min:1',
            'is_public' => 'boolean',
        ]);

        $post->update([
            'title' => $validated['title'] ?? '',
            'content' => $validated['content'],
            'is_public' => $validated['is_public'] ?? true,
        ]);

        return redirect()->back()->with('success', 'Post updated!');
    }

    /**
     * Remove post
     */
    public function deletePost($id)
    {
        $post = Writing::posts()->where('user_id', Auth::id())->findOrFail($id);
        $post->delete();

        return redirect()->back()->with('success', 'Post deleted.');
    }

    /**
     * Add comment to post
     */
    public function storeComment(Request $request, $id)
    {
        $post = Writing::posts()->public()->findOrFail($id);

        $key = 'comment:' . $request->ip();
        if (RateLimiter::tooManyAttempts($key, 5)) {
            return redirect()->back()->with('error', 'Too many comments. Please wait a moment.');
        }
        RateLimiter::hit($key, 60);

        $validated = $request->validate([
            'author_name' => 'required|string|max:100',
            'author_email' => 'required|email|max:255',
            'content' => 'required|string|min:1|max:2000',
        ]);

        Comment::create([
            'writing_id' => $post->id,
            'author_name' => $validated['author_name'],
            'author_email' => $validated['author_email'],
            'content' => $validated['content'],
            'is_approved' => true,
        ]);

        return redirect()->back()->with('success', 'Comment added!');
    }

    /**
     * Remove comment (moderation)
     */
    public function deleteComment($id)
    {
        $comment = Comment::findOrFail($id);
        $comment->delete();

        return redirect()->back()->with('success', 'Comment removed.');
    }

    /**
     * Handle contact form submission
     */
    public function contact(Request $request)
    {
        $key = 'contact:' . $request->ip();
        if (RateLimiter::tooManyAttempts($key, 3)) {
            return redirect()->back()->with('error', 'Too many messages. Please wait a few minutes.');
        }
        RateLimiter::hit($key, 300);

        $validated = $request->validate([
            'name' => 'required|string|max:100',
            'email' => 'required|email|max:255',
            'message' => 'required|string|min:10|max:5000',
        ]);

        $owner = User::first();

        if ($owner && $owner->email && config('mail.default') !== 'log') {
            Mail::to($owner->email)->send(new ContactMessage(
                $validated['name'],
                $validated['email'],
                $validated['message']
            ));
        }

        return redirect()->back()->with('success', 'Message sent! Thank you for reaching out.');
    }
}