1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-28 17:02:04 +03:00

#47 Implements the reply and edit functionality for comments.

This commit is contained in:
Abijeet
2017-05-16 00:40:14 +05:30
parent 4f231d1bf0
commit 03e5d61798
10 changed files with 149 additions and 95 deletions

View File

@ -5,8 +5,8 @@ use Illuminate\Support\Facades\DB;
class Comment extends Ownable
{
protected $fillable = ['text', 'html'];
protected $fillable = ['text', 'html', 'parent_id'];
/**
* Get the entity that this comment belongs to
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
@ -15,7 +15,7 @@ class Comment extends Ownable
{
return $this->morphTo('entity');
}
/**
* Get the page that this comment is in.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
@ -24,32 +24,32 @@ class Comment extends Ownable
{
return $this->belongsTo(Page::class);
}
/**
* Get the owner of this comment.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
public function user()
{
return $this->belongsTo(User::class);
}
public function getCommentsByPage($pageId, $commentId, $pageNum = 0, $limit = 0) {
public function getCommentsByPage($pageId, $commentId, $pageNum = 0, $limit = 0) {
$query = static::newQuery();
$query->join('users AS u', 'comments.created_by', '=', 'u.id');
$query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id');
$query->leftJoin('images AS i', 'i.id', '=', 'u.image_id');
$query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, comments.created_at, comments.updated_at, '
. 'u.name AS created_by_name, u1.name AS updated_by_name, '
. '(SELECT count(c.id) FROM bookstack.comments c WHERE c.parent_id = comments.id AND page_id = ?) AS cnt_sub_comments, i.url AS avatar ',
. '(SELECT count(c.id) FROM bookstack.comments c WHERE c.parent_id = comments.id AND page_id = ?) AS cnt_sub_comments, i.url AS avatar ',
[$pageId]);
if (empty($commentId)) {
$query->whereRaw('page_id = ? AND parent_id IS NULL', [$pageId]);
} else {
$query->whereRaw('page_id = ? AND parent_id = ?', [$pageId, $commentId]);
}
}
$query->orderBy('created_at');
return $query;
}

View File

@ -2,18 +2,19 @@
use BookStack\Repos\CommentRepo;
use BookStack\Repos\EntityRepo;
use BookStack\Comment;
use Illuminate\Http\Request;
use Views;
// delete -checkOwnablePermission \
class CommentController extends Controller
{
protected $entityRepo;
public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo)
public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo, Comment $comment)
{
$this->entityRepo = $entityRepo;
$this->commentRepo = $commentRepo;
$this->comment = $comment;
parent::__construct();
}
@ -43,10 +44,10 @@ class CommentController extends Controller
// create a new comment.
$this->checkPermission('comment-create-all');
$comment = $this->commentRepo->create($page, $request->only(['text', 'html', 'parent_id']));
$respMsg = trans('entities.comment_created');
$respMsg = trans('entities.comment_created');
} else {
// update existing comment
// get comment by ID and check if this user has permission to update.
// get comment by ID and check if this user has permission to update.
$comment = $this->comment->findOrFail($commentId);
$this->checkOwnablePermission('comment-update', $comment);
$this->commentRepo->update($comment, $request->all());
@ -59,7 +60,7 @@ class CommentController extends Controller
]);
}
public function destroy($id) {
$comment = $this->comment->findOrFail($id);
$this->checkOwnablePermission('comment-delete', $comment);
@ -67,13 +68,13 @@ class CommentController extends Controller
//
}
public function getComments($pageId, $commentId = null) {
public function getCommentThread($pageId, $commentId = null) {
try {
$page = $this->entityRepo->getById('page', $pageId, true);
} catch (ModelNotFoundException $e) {
return response('Not found', 404);
}
if($page->draft) {
// cannot add comments to drafts.
return response()->json([
@ -81,15 +82,15 @@ class CommentController extends Controller
'message' => trans('errors.no_comments_for_draft'),
], 400);
}
$this->checkOwnablePermission('page-view', $page);
$comments = $this->commentRepo->getCommentsForPage($pageId, $commentId);
if (empty($commentId)) {
// requesting for parent level comments, send the total count as well.
$totalComments = $this->commentRepo->getCommentCount($pageId);
return response()->json(array('success' => true, 'comments'=> $comments, 'total' => $totalComments));
return response()->json(['success' => true, 'comments'=> $comments, 'total' => $totalComments]);
}
return response()->json(array('success' => true, 'comments'=> $comments));
return response()->json(['success' => true, 'comments'=> $comments]);
}
}

View File

@ -10,7 +10,7 @@ use BookStack\Page;
class CommentRepo {
/**
*
* @var Comment $comment
* @var Comment $comment
*/
protected $comment;
@ -25,7 +25,7 @@ class CommentRepo {
$comment->fill($data);
// new comment
$comment->page_id = $page->id;
$comment->created_by = $userId;
$comment->created_by = $userId;
$comment->save();
return $comment;
}
@ -37,13 +37,13 @@ class CommentRepo {
$comment->save();
return $comment;
}
public function getCommentsForPage($pageId, $commentId, $count = 20) {
public function getCommentsForPage($pageId, $commentId, $count = 20) {
// requesting parent comments
$query = $this->comment->getCommentsByPage($pageId, $commentId);
return $query->paginate($count);
return $query->paginate($count);
}
public function getCommentCount($pageId) {
return $this->comment->where('page_id', '=', $pageId)->count();
}