forked from HDInnovations/UNIT3D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostController.php
More file actions
282 lines (236 loc) · 9.77 KB
/
PostController.php
File metadata and controls
282 lines (236 loc) · 9.77 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
declare(strict_types=1);
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Controllers;
use App\Achievements\UserMade100Posts;
use App\Achievements\UserMade200Posts;
use App\Achievements\UserMade25Posts;
use App\Achievements\UserMade300Posts;
use App\Achievements\UserMade400Posts;
use App\Achievements\UserMade500Posts;
use App\Achievements\UserMade50Posts;
use App\Achievements\UserMade600Posts;
use App\Achievements\UserMade700Posts;
use App\Achievements\UserMade800Posts;
use App\Achievements\UserMade900Posts;
use App\Achievements\UserMadeFirstPost;
use App\Models\Post;
use App\Models\Topic;
use App\Models\User;
use App\Notifications\NewPost;
use App\Notifications\NewPostTag;
use App\Repositories\ChatRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
use Exception;
/**
* @see \Tests\Todo\Feature\Http\Controllers\PostControllerTest
*/
class PostController extends Controller
{
/**
* PostController Constructor.
*/
public function __construct(private readonly ChatRepository $chatRepository)
{
}
/**
* Posts Index.
*/
public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
return view('forum.post.index');
}
/**
* Store A New Post To A Topic.
*/
public function store(Request $request): \Illuminate\Http\RedirectResponse
{
$request->validate([
'content' => 'required|min:1',
'topic_id' => 'required|integer',
'anon' => 'sometimes|boolean',
]);
$user = $request->user();
$topic = Topic::authorized(canReplyTopic: true)->findOrFail($request->integer('topic_id'));
$forum = $topic->forum;
$post = Post::query()->create([
'content' => $request->input('content'),
'anon' => $request->boolean('anon'),
'user_id' => $user->id,
'topic_id' => $topic->id,
]);
$topic->update([
'last_post_id' => $post->id,
'last_post_user_id' => $user->id,
'num_post' => $topic->posts()->count(),
'last_post_created_at' => $post->created_at,
]);
$forum->update([
'num_post' => $forum->posts()->count(),
'num_topic' => $forum->topics()->count(),
'last_post_user_id' => $user->id,
'last_post_id' => $post->id,
'last_topic_id' => $topic->id,
'last_post_created_at' => $post->created_at,
]);
// Post To Chatbox and Notify Subscribers
$postUrl = route('topics.permalink', ['topicId' => $topic->id, 'postId' => $post->id]);
$realUrl = route('topics.permalink', ['topicId' => $topic->id, 'postId' => $post->id], false);
$profileUrl = href_profile($user);
if (config('other.staff-forum-notify') && ($forum->id == config('other.staff-forum-id') || $forum->forum_category_id == config('other.staff-forum-id'))) {
$staffers = User::query()
->where('id', '!=', $user->id)
->whereRelation('forumPermissions', [
['read_topic', '=', 1],
['forum_id', '=', $forum->id],
])
->get();
foreach ($staffers as $staffer) {
$staffer->notify(new NewPost('staff', $user, $post));
}
} else {
$isChatboxPrivy = $forum->permissions()
->where('read_topic', '=', true)
->whereRelation('group', 'slug', '=', 'user')
->exists();
if ($isChatboxPrivy) {
if ($post->anon) {
$this->chatRepository->systemMessage(\sprintf('An anonymous user has left a reply on topic [url=%s]%s[/url]', $postUrl, $topic->name));
} else {
$this->chatRepository->systemMessage(\sprintf('[url=%s]%s[/url] has left a reply on topic [url=%s]%s[/url]', $profileUrl, $user->username, $postUrl, $topic->name));
}
}
$topicStarter = $topic->user;
// Notify All Subscribers Of New Reply
$topicStarter->notify(new NewPost('topic', $user, $post));
$subscribers = User::query()
->where('id', '!=', $user->id)
->whereRelation('subscriptions', 'topic_id', '=', $topic->id)
->whereRelation('forumPermissions', [
['read_topic', '=', 1],
['forum_id', '=', $topic->forum_id],
])
->where(
fn ($query) => $query
->whereRelation('notification', 'show_subscription_topic', '=', true)
->orDoesntHave('notification')
)
->get();
foreach ($subscribers as $subscriber) {
$subscriber->notify(new NewPost('subscription', $user, $post));
}
// Achievements
$user->unlock(new UserMadeFirstPost());
$user->addProgress(new UserMade25Posts(), 1);
$user->addProgress(new UserMade50Posts(), 1);
$user->addProgress(new UserMade100Posts(), 1);
$user->addProgress(new UserMade200Posts(), 1);
$user->addProgress(new UserMade300Posts(), 1);
$user->addProgress(new UserMade400Posts(), 1);
$user->addProgress(new UserMade500Posts(), 1);
$user->addProgress(new UserMade600Posts(), 1);
$user->addProgress(new UserMade700Posts(), 1);
$user->addProgress(new UserMade800Posts(), 1);
$user->addProgress(new UserMade900Posts(), 1);
}
// User Tagged Notification
preg_match_all('/@([\w\-]+)/', (string) $post->content, $matches);
$users = User::query()->whereIn('username', $matches[1])->where('id', '!=', $user->id)->get();
Notification::send($users, new NewPostTag($post));
return redirect()->to($realUrl)
->with('success', trans('forum.reply-topic-success'));
}
/**
* Edit A Post.
*/
public function edit(Request $request, int $id): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
$user = $request->user();
$post = Post::query()->find($id);
$topic = Topic::query()->whereKey($post->topic_id)->authorized(canReadTopic: true, canReplyTopic: true)->sole();
abort_unless($user->group->is_modo || $user->id === $post->user_id, 403);
$forum = $topic->forum;
$category = $forum->category;
return view('forum.post.edit', [
'topic' => $topic,
'forum' => $forum,
'post' => $post,
'category' => $category,
]);
}
/**
* Update A Post.
*/
public function update(Request $request, int $id): \Illuminate\Http\RedirectResponse
{
$request->validate([
'content' => 'required|min:1',
]);
$user = $request->user();
$post = Post::query()->findOrFail($id);
$postUrl = route('topics.permalink', ['topicId' => $post->topic->id, 'postId' => $id], false);
abort_unless($user->group->is_modo || $user->id === $post->user_id, 403);
abort_unless($post->topic()->authorized(canReplyTopic: true)->exists(), 403);
$post->update([
'content' => $request->input('content'),
]);
return redirect()->to($postUrl)
->with('success', trans('forum.edit-post-success'));
}
/**
* Delete A Post.
*
* @throws Exception
*/
public function destroy(Request $request, int $id): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
$post = Post::query()->with('topic')->findOrFail($id);
abort_unless($user->group->is_modo || $user->id === $post->user_id, 403);
$topic = Topic::query()->whereKey($post->topic_id)->authorized(canReplyTopic: true)->sole();
$post->delete();
$latestPost = $topic->latestPostSlow;
$isTopicDeleted = false;
if ($latestPost === null) {
$topic->delete();
$isTopicDeleted = true;
} else {
$latestPoster = $latestPost->user;
$topic->update([
'last_post_id' => $latestPost->id,
'last_post_user_id' => $latestPoster->id,
'num_post' => $topic->posts()->count(),
'last_post_created_at' => $latestPost->created_at,
]);
}
$forum = $topic->forum;
$lastRepliedTopic = $forum->lastRepliedTopicSlow;
$latestPost = $lastRepliedTopic->latestPostSlow;
$latestPoster = $latestPost->user;
$forum->update([
'num_post' => $forum->posts()->count(),
'num_topic' => $forum->topics()->count(),
'last_post_id' => $latestPost->id,
'last_post_user_id' => $latestPoster->id,
'last_topic_id' => $lastRepliedTopic->id,
'last_post_created_at' => $latestPost->created_at,
]);
if ($isTopicDeleted === true) {
return to_route('forums.show', ['id' => $forum->id])
->with('success', trans('forum.delete-post-success'));
}
return to_route('topics.show', ['id' => $post->topic->id])
->with('success', trans('forum.delete-post-success'));
}
}