update: forums

This commit is contained in:
Roardom
2023-05-08 00:55:22 +00:00
parent 13be87c23e
commit 8824abd9d5
35 changed files with 630 additions and 558 deletions
+137 -65
View File
@@ -25,7 +25,6 @@ use App\Achievements\UserMade700Posts;
use App\Achievements\UserMade800Posts;
use App\Achievements\UserMade900Posts;
use App\Achievements\UserMadeFirstPost;
use App\Models\Forum;
use App\Models\Post;
use App\Models\Topic;
use App\Models\User;
@@ -47,53 +46,57 @@ class PostController extends Controller
{
}
/**
* 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 reply(Request $request, int $id): \Illuminate\Http\RedirectResponse
public function store(Request $request): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
$topic = Topic::findOrFail($id);
$forum = $topic->forum;
$category = Forum::findOrFail($forum->id);
// The user has the right to create a post here?
if (! $category->getPermission()->reply_topic || ($topic->state == 'close' && ! $request->user()->group->is_modo)) {
return to_route('forums.index')
->withErrors(trans('forum.reply-topic-error'));
}
$post = new Post();
$post->content = $request->input('content');
$post->user_id = $user->id;
$post->topic_id = $topic->id;
$v = validator($post->toArray(), [
$request->validate([
'content' => 'required|min:1',
'user_id' => 'required',
'topic_id' => 'required',
'topic_id' => 'required|integer',
]);
if ($v->fails()) {
return to_route('forum_topic', ['id' => $topic->id])
->withErrors($v->errors());
}
$user = $request->user();
$post->save();
$topic = Topic::whereRelation('forumPermissions', [
['reply_topic', '=', 1],
['group_id', '=', $user->group_id]
])
->when(! $user->group->is_modo, fn ($query) => $query->where('state', '=', 'open'))
->findOrFail($request->topic_id);
$topic->last_post_user_id = $user->id;
$topic->last_post_user_username = $user->username;
$topic->num_post = Post::where('topic_id', '=', $topic->id)->count();
$topic->last_reply_at = $post->created_at;
$topic->save();
$forum = $topic->forum;
$forum->num_post = $forum->getPostCount($forum->id);
$forum->num_topic = $forum->getTopicCount($forum->id);
$forum->last_post_user_id = $user->id;
$forum->last_post_user_username = $user->username;
$forum->last_topic_id = $topic->id;
$forum->last_topic_name = $topic->name;
$forum->save();
$post = Post::create([
'content' => $request->content,
'user_id' => $user->id,
'topic_id' => $topic->id,
]);
$topic->update([
'last_post_user_id' => $user->id,
'last_post_user_username' => $user->username,
'num_post' => $topic->posts()->count(),
'last_reply_at' => $post->created_at,
]);
$forum->update([
'num_post' => $forum->posts()->count(),
'num_topic' => $forum->topics()->count(),
'last_post_user_id' => $user->id,
'last_post_user_username' => $user->username,
'last_topic_id' => $topic->id,
'last_topic_name' => $topic->name,
'updated_at' => $post->created_at,
]);
// Post To Chatbox and Notify Subscribers
$appurl = config('app.url');
@@ -111,6 +114,20 @@ class PostController extends Controller
}
$topic->notifySubscribers($user, $topic, $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
@@ -120,33 +137,28 @@ class PostController extends Controller
Notification::send($users, new NewPostTag($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);
return redirect()->to($realUrl)
->withSuccess(trans('forum.reply-topic-success'));
}
/**
* Edit Post Form.
* Edit A Post.
*/
public function postEditForm(int $id, int $postId): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
public function edit(int $id): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
$topic = Topic::findOrFail($id);
$post = Post::find($id);
$topic = $post->topic()
->whereRelation('forumPermissions', [
['show_forum', '=', 1],
['read_topic', '=', 1],
['reply_topic', '=', 1],
['group_id', '=', auth()->user()->group_id],
])
->when(! auth()->user()->group->is_modo, fn ($query) => $query->where('state', '=', 'open'))
->sole();
$forum = $topic->forum;
$category = $forum->getCategory();
$post = Post::findOrFail($postId);
$category = $forum->category;
return view('forum.post.edit', [
'topic' => $topic,
@@ -157,17 +169,35 @@ class PostController extends Controller
}
/**
* Edit A Post In A Topic.
* Update A Post.
*/
public function postEdit(Request $request, int $postId): \Illuminate\Http\RedirectResponse
public function update(Request $request, int $id): \Illuminate\Http\RedirectResponse
{
$request->validate([
'content' => 'required|min:1',
]);
$user = $request->user();
$post = Post::findOrFail($postId);
$postUrl = sprintf('forums/topics/%s?page=%s#post-%s', $post->topic->id, $post->getPageNumber(), $postId);
$post = Post::findOrFail($id);
$postUrl = sprintf('forums/topics/%s?page=%s#post-%s', $post->topic->id, $post->getPageNumber(), $id);
abort_unless($user->group->is_modo || $user->id === $post->user_id, 403);
$post->content = $request->input('content');
$post->save();
abort_unless(
$post->topic()
->whereRelation('forumPermissions', [
['reply_topic', '=', 1],
['group_id', '=', auth()->user()->group_id],
])
->when(! auth()->user()->group->is_modo, fn ($query) => $query->where('state', '=', 'open'))
->exists(),
403
);
$post->update([
'content' => $request->content,
]);
return redirect()->to($postUrl)
->withSuccess(trans('forum.edit-post-success'));
@@ -178,15 +208,57 @@ class PostController extends Controller
*
* @throws Exception
*/
public function postDelete(Request $request, int $postId): \Illuminate\Http\RedirectResponse
public function destroy(Request $request, int $id): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
$post = Post::with('topic')->findOrFail($postId);
$post = Post::with('topic')->findOrFail($id);
abort_unless($user->group->is_modo || $user->id === $post->user_id, 403);
$topic = $post->topic()->whereRelation('forumPermissions', [
['reply_topic', '=', 1],
['group_id', '=', auth()->user()->group_id],
])
->sole();
$post->delete();
return to_route('forum_topic', ['id' => $post->topic->id])
$latestPost = $topic->latestPost;
if ($latestPost === null) {
$topic->delete();
$isTopicDeleted = true;
} else {
$latestPoster = $latestPost->user;
$topic->update([
'last_post_user_id' => $latestPoster->id,
'last_post_user_username' => $latestPoster->username,
'num_post' => $topic->posts()->count(),
'last_reply_at' => $latestPost->created_at,
]);
}
$forum = $topic->forum;
$lastRepliedTopic = $forum->lastRepliedTopic;
$latestPost = $lastRepliedTopic->latestPost;
$latestPoster = $latestPost->user;
$forum->query()->update([
'num_post' => $forum->posts()->count(),
'num_topic' => $forum->topics()->count(),
'last_post_user_id' => $latestPoster->id,
'last_post_user_username' => $latestPoster->username,
'last_topic_id' => $lastRepliedTopic->id,
'last_topic_name' => $lastRepliedTopic->name,
'updated_at' => $latestPost->created_at,
]);
if ($isTopicDeleted === true) {
return to_route('forums.show', ['id' => $forum->id])
->withSuccess(trans('forum.delete-post-success'));
}
return to_route('topics.show', ['id' => $post->topic->id])
->withSuccess(trans('forum.delete-post-success'));
}
}