(Update) Forums System 🚀

- pull like/dislike function out of ForumController and into its own.
- refactor perm checks
- fix redirects to point to the permalink of the specific post
- ref #398
- TODO: clean routes
This commit is contained in:
HDVinnie
2019-01-11 14:00:04 -05:00
parent d3d3438c61
commit aef4d38c9e
3 changed files with 117 additions and 91 deletions
+7 -86
View File
@@ -13,7 +13,6 @@
namespace App\Http\Controllers;
use App\Like;
use App\Post;
use App\User;
use App\Forum;
@@ -325,7 +324,7 @@ class ForumController extends Controller
$user->addProgress(new UserMade800Posts(), 1);
$user->addProgress(new UserMade900Posts(), 1);
return redirect()->route('forum_topic', ['slug' => $topic->slug, 'id' => $topic->id])
return redirect($postUrl)
->with($this->toastr->success('Post Successfully Posted', 'Yay!', ['options']));
}
}
@@ -533,52 +532,38 @@ class ForumController extends Controller
* Edit A Post In A Topic.
*
* @param \Illuminate\Http\Request $request
* @param $slug
* @param $id
* @param $postId
*
* @return Illuminate\Http\RedirectResponse
*/
public function postEdit(Request $request, $slug, $id, $postId)
public function postEdit(Request $request, $postId)
{
$user = auth()->user();
$topic = Topic::findOrFail($id);
$post = Post::findOrFail($postId);
$postUrl = "forums/topic/{$post->topic->slug}.{$post->topic->id}?page={$post->getPageNumber()}#post-{$postId}";
if ($user->group->is_modo == false) {
if ($post->user_id != $user->id) {
return redirect()->route('forum_topic', ['slug' => $topic->slug, 'id' => $topic->id])
->with($this->toastr->error('You Cannot Edit This!', 'Whoops!', ['options']));
}
}
abort_unless($user->group->is_modo || $post->user_id != $user->id, 403);
$post->content = $request->input('content');
$post->save();
return redirect()->route('forum_topic', ['slug' => $topic->slug, 'id' => $topic->id])
return redirect($postUrl)
->with($this->toastr->success('Post Successfully Edited!', 'Yay!', ['options']));
}
/**
* Delete A Post.
*
* @param $slug
* @param $id
* @param $postId
*
* @return Illuminate\Http\RedirectResponse
*/
public function postDelete($slug, $id, $postId)
public function postDelete($postId)
{
$user = auth()->user();
$topic = Topic::findOrFail($id);
$post = Post::findOrFail($postId);
if ($user->group->is_modo == false) {
if ($post->user_id != $user->id) {
return redirect()->route('forum_topic', ['slug' => $topic->slug, 'id' => $topic->id])
->with($this->toastr->error('You Cannot Delete This!', 'Whoops!', ['options']));
}
}
abort_unless($user->group->is_modo || $post->user_id != $user->id, 403);
$post->delete();
return redirect()->route('forum_topic', ['slug' => $topic->slug, 'id' => $topic->id])
@@ -787,68 +772,4 @@ class ForumController extends Controller
return redirect()->route('forum_topic', ['slug' => $topic->slug, 'id' => $topic->id])
->with($this->toastr->info('Label Change Has Been Applied', 'Info', ['options']));
}
/**
* Like A Post.
*
* @param $postId
*
* @return Illuminate\Http\RedirectResponse
*/
public function likePost($postId)
{
$post = Post::findOrFail($postId);
$user = auth()->user();
$like = $user->likes()->where('post_id', '=', $post->id)->where('like', '=', 1)->first();
$dislike = $user->likes()->where('post_id', '=', $post->id)->where('dislike', '=', 1)->first();
if ($like || $dislike) {
return redirect()->route('forum_topic', ['slug' => $post->topic->slug, 'id' => $post->topic->id])
->with($this->toastr->error('You have already liked/disliked this post!', 'Bro', ['options']));
} elseif ($user->id == $post->user_id) {
return redirect()->route('forum_topic', ['slug' => $post->topic->slug, 'id' => $post->topic->id])
->with($this->toastr->error('You cannot like your own post!', 'Umm', ['options']));
} else {
$new = new Like();
$new->user_id = $user->id;
$new->post_id = $post->id;
$new->like = 1;
$new->save();
return redirect()->route('forum_topic', ['slug' => $post->topic->slug, 'id' => $post->topic->id])
->with($this->toastr->success('Like Successfully Applied!', 'Yay', ['options']));
}
}
/**
* Dislike A Post.
*
* @param $postId
*
* @return Illuminate\Http\RedirectResponse
*/
public function dislikePost($postId)
{
$post = Post::findOrFail($postId);
$user = auth()->user();
$like = $user->likes()->where('post_id', '=', $post->id)->where('like', '=', 1)->first();
$dislike = $user->likes()->where('post_id', '=', $post->id)->where('dislike', '=', 1)->first();
if ($like || $dislike) {
return redirect()->route('forum_topic', ['slug' => $post->topic->slug, 'id' => $post->topic->id])
->with($this->toastr->error('You have already liked/disliked this post!', 'Bro', ['options']));
} elseif ($user->id == $post->user_id) {
return redirect()->route('forum_topic', ['slug' => $post->topic->slug, 'id' => $post->topic->id])
->with($this->toastr->error('You cannot like your own post!', 'Umm', ['options']));
} else {
$new = new Like();
$new->user_id = $user->id;
$new->post_id = $post->id;
$new->dislike = 1;
$new->save();
return redirect()->route('forum_topic', ['slug' => $post->topic->slug, 'id' => $post->topic->id])
->with($this->toastr->success('Dislike Successfully Applied!', 'Yay', ['options']));
}
}
}
+105
View File
@@ -0,0 +1,105 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
*
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
* @author HDVinnie
*/
namespace App\Http\Controllers;
use App\Like;
use App\Post;
use Brian2694\Toastr\Toastr;
use Illuminate\Http\Request;
class LikeController extends Controller
{
/**
* @var Toastr
*/
private $toastr;
/**
* LikeController Constructor.
*
* @param Toastr $toastr
*/
public function __construct(Toastr $toastr)
{
$this->toastr = $toastr;
}
/**
* Like A Post.
*
* @param $postId
*
* @return Illuminate\Http\RedirectResponse
*/
public function store($postId)
{
$post = Post::findOrFail($postId);
$postUrl = "forums/topic/{$post->topic->slug}.{$post->topic->id}?page={$post->getPageNumber()}#post-{$postId}";
$user = auth()->user();
$like = $user->likes()->where('post_id', '=', $post->id)->where('like', '=', 1)->first();
$dislike = $user->likes()->where('post_id', '=', $post->id)->where('dislike', '=', 1)->first();
if ($like || $dislike) {
return redirect($postUrl)
->with($this->toastr->error('You have already liked/disliked this post!', 'Bro', ['options']));
} elseif ($user->id == $post->user_id) {
return redirect($postUrl)
->with($this->toastr->error('You cannot like your own post!', 'Umm', ['options']));
} else {
$new = new Like();
$new->user_id = $user->id;
$new->post_id = $post->id;
$new->like = 1;
$new->save();
return redirect($postUrl)
->with($this->toastr->success('Like Successfully Applied!', 'Yay', ['options']));
}
}
/**
* Dislike A Post.
*
* @param $postId
*
* @return Illuminate\Http\RedirectResponse
*/
public function destroy($postId)
{
$post = Post::findOrFail($postId);
$postUrl = "forums/topic/{$post->topic->slug}.{$post->topic->id}?page={$post->getPageNumber()}#post-{$postId}";
$user = auth()->user();
$like = $user->likes()->where('post_id', '=', $post->id)->where('like', '=', 1)->first();
$dislike = $user->likes()->where('post_id', '=', $post->id)->where('dislike', '=', 1)->first();
if ($like || $dislike) {
return redirect($postUrl)
->with($this->toastr->error('You have already liked/disliked this post!', 'Bro', ['options']));
} elseif ($user->id == $post->user_id) {
return redirect($postUrl)
->with($this->toastr->error('You cannot dislike your own post!', 'Umm', ['options']));
} else {
$new = new Like();
$new->user_id = $user->id;
$new->post_id = $post->id;
$new->dislike = 1;
$new->save();
return redirect($postUrl)
->with($this->toastr->success('Dislike Successfully Applied!', 'Yay', ['options']));
}
}
}
+5 -5
View File
@@ -340,10 +340,10 @@ Route::group(['middleware' => 'language'], function () {
// Open Topic
Route::get('/topic/{slug}.{id}/open', 'ForumController@openTopic')->name('forum_open');
// Edit Post
Route::get('/topic/{slug}.{id}/post-{postId}/edit', 'ForumController@postEditForm')->name('forum_post_edit_form');
Route::post('/topic/{slug}.{id}/post-{postId}/edit', 'ForumController@postEdit')->name('forum_post_edit');
Route::get('/posts/{slug}.{id}/post-{postId}/edit', 'ForumController@postEditForm')->name('forum_post_edit_form');
Route::post('/posts/{postId}/edit', 'ForumController@postEdit')->name('forum_post_edit');
// Delete Post
Route::get('/topic/{slug}.{id}/post-{postId}/delete', 'ForumController@postDelete')->name('forum_post_delete');
Route::get('/posts/{postId}/delete', 'ForumController@postDelete')->name('forum_post_delete');
// Reply To Topic
Route::post('/topic/{slug}.{id}/reply', 'ForumController@reply')->name('forum_reply');
// Edit Topic
@@ -366,8 +366,8 @@ Route::group(['middleware' => 'language'], function () {
Route::get('/topic/{slug}.{id}/implemented', 'ForumController@implementedTopic')->name('forum_implemented');
// Like - Dislike System
Route::any('/like/post/{postId}', 'ForumController@likePost')->name('like');
Route::any('/dislike/post/{postId}', 'ForumController@dislikePost')->name('dislike');
Route::any('/like/post/{postId}', 'LikeController@store')->name('like');
Route::any('/dislike/post/{postId}', 'LikeController@destroy')->name('dislike');
// Subscription System
Route::get('/subscribe/{topic}', 'SubscriptionController@subscribe')->name('subscribe');