From 2fd421b115132cdea97ec5ccbbab412e02659ab7 Mon Sep 17 00:00:00 2001 From: Abijeet Date: Sun, 4 Jun 2017 11:17:14 +0530 Subject: [PATCH] #47 - Adds comment level permissions to the front-end. --- app/Http/Controllers/CommentController.php | 9 ++++++++- app/Http/Controllers/PageController.php | 9 ++------- app/Repos/CommentRepo.php | 9 ++++++--- resources/assets/js/controllers.js | 19 +++++++++++++++++++ resources/assets/js/directives.js | 2 +- resources/views/comments/comments.blade.php | 4 +++- resources/views/comments/list-item.blade.php | 4 ++-- 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index 29ccdf5a7..3a267193d 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -88,6 +88,13 @@ class CommentController extends Controller $this->checkOwnablePermission('page-view', $page); $comments = $this->commentRepo->getPageComments($pageId); - return response()->json(['success' => true, 'comments'=> $comments['comments'], 'total' => $comments['total']]); + return response()->json(['success' => true, 'comments'=> $comments['comments'], + 'total' => $comments['total'], 'permissions' => [ + 'comment_create' => $this->currentUser->can('comment-create-all'), + 'comment_update_own' => $this->currentUser->can('comment-update-own'), + 'comment_update_all' => $this->currentUser->can('comment-update-all'), + 'comment_delete_all' => $this->currentUser->can('comment-delete-all'), + 'comment_delete_own' => $this->currentUser->can('comment-delete-own'), + ], 'user_id' => $this->currentUser->id]); } } diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index 736197213..9a8525c23 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -161,7 +161,7 @@ class PageController extends Controller $pageContent = $this->entityRepo->renderPage($page); $sidebarTree = $this->entityRepo->getBookChildren($page->book); $pageNav = $this->entityRepo->getPageNav($pageContent); - + Views::add($page); $this->setPageTitle($page->getShortName()); return view('pages/show', [ @@ -376,7 +376,7 @@ class PageController extends Controller $page->fill($revision->toArray()); $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()])); - + return view('pages/revision', [ 'page' => $page, 'book' => $page->book, @@ -590,9 +590,4 @@ class PageController extends Controller return redirect($page->getUrl()); } - public function getLastXComments($pageId) - { - // $this->checkOwnablePermission('page-view', $page); - } - } diff --git a/app/Repos/CommentRepo.php b/app/Repos/CommentRepo.php index 7d0c4ebd7..83847239f 100644 --- a/app/Repos/CommentRepo.php +++ b/app/Repos/CommentRepo.php @@ -43,11 +43,14 @@ class CommentRepo { $comments = $this->comment->getAllPageComments($pageId); $index = []; $totalComments = count($comments); + $finalCommentList = []; + // normalizing the response. - foreach($comments as &$comment) { - $comment = $this->normalizeComment($comment); + for ($i = 0; $i < count($comments); ++$i) { + $comment = $this->normalizeComment($comments[$i]); $parentId = $comment->parent_id; if (empty($parentId)) { + $finalCommentList[] = $comment; $index[$comment->id] = $comment; continue; } @@ -63,7 +66,7 @@ class CommentRepo { $index[$comment->id] = $comment; } return [ - 'comments' => $comments, + 'comments' => $finalCommentList, 'total' => $totalComments ]; } diff --git a/resources/assets/js/controllers.js b/resources/assets/js/controllers.js index f64d7c038..4763f9867 100644 --- a/resources/assets/js/controllers.js +++ b/resources/assets/js/controllers.js @@ -756,6 +756,7 @@ module.exports = function (ngApp, events) { // keep track of comment levels $scope.level = 1; vm.totalCommentsStr = 'Loading...'; + vm.permissions = {}; $scope.$on('evt.new-comment', function (event, comment) { // add the comment to the comment list. @@ -764,6 +765,21 @@ module.exports = function (ngApp, events) { event.preventDefault(); }); + vm.canEdit = function (comment) { + if (vm.permissions.comment_update_all) { + return true; + } + + if (vm.permissions.comment_update_own && comment.created_by.id === vm.current_user_id) { + return true; + } + return false; + } + + vm.canComment = function () { + return vm.permissions.comment_create; + } + $timeout(function() { $http.get(window.baseUrl(`/ajax/page/${$scope.pageId}/comments/`)).then(resp => { if (!resp.data || resp.data.success !== true) { @@ -772,6 +788,9 @@ module.exports = function (ngApp, events) { } vm.comments = resp.data.comments; vm.totalComments = resp.data.total; + vm.permissions = resp.data.permissions; + vm.current_user_id = resp.data.user_id; + // TODO : Fetch message from translate. if (vm.totalComments === 0) { vm.totalCommentsStr = 'No comments found.'; diff --git a/resources/assets/js/directives.js b/resources/assets/js/directives.js index 278e0f8c6..0929a9cf4 100644 --- a/resources/assets/js/directives.js +++ b/resources/assets/js/directives.js @@ -908,7 +908,7 @@ module.exports = function (ngApp, events) { } function removeDupe() { - let $existingElement = $document.find('.comments-list comment-reply'); + let $existingElement = $document.find('.comments-list comment-reply, .comments-list comment-edit'); if (!$existingElement.length) { return; } diff --git a/resources/views/comments/comments.blade.php b/resources/views/comments/comments.blade.php index 93e7ebc05..ffa75cfed 100644 --- a/resources/views/comments/comments.blade.php +++ b/resources/views/comments/comments.blade.php @@ -12,5 +12,7 @@ - @include('comments/comment-reply', ['pageId' => $pageId]) +
+ @include('comments/comment-reply', ['pageId' => $pageId]) +
\ No newline at end of file diff --git a/resources/views/comments/list-item.blade.php b/resources/views/comments/list-item.blade.php index 46af1a862..67355c586 100644 --- a/resources/views/comments/list-item.blade.php +++ b/resources/views/comments/list-item.blade.php @@ -11,8 +11,8 @@