mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-28 17:02:04 +03:00
#47 - Changes the way we are handling fetching of data for the comment section.
This commit is contained in:
@ -1,12 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class Comment extends Ownable
|
||||
{
|
||||
public $sub_comments = [];
|
||||
protected $fillable = ['text', 'html', 'parent_id'];
|
||||
protected $appends = ['created', 'updated'];
|
||||
protected $appends = ['created', 'updated', 'sub_comments'];
|
||||
/**
|
||||
* Get the entity that this comment belongs to
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
||||
@ -34,24 +34,38 @@ class Comment extends Ownable
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function getCommentsByPage($pageId, $commentId, $pageNum = 0, $limit = 0) {
|
||||
|
||||
public function getPageComments($pageId) {
|
||||
$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, '
|
||||
$query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, '
|
||||
. 'comments.created_at, comments.updated_at, comments.parent_id, '
|
||||
. '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 ',
|
||||
[$pageId]);
|
||||
|
||||
if (empty($commentId)) {
|
||||
$query->whereRaw('page_id = ? AND parent_id IS NULL', [$pageId]);
|
||||
} else {
|
||||
$query->whereRaw('page_id = ? AND parent_id = ?', [$pageId, $commentId]);
|
||||
}
|
||||
. 'i.url AS avatar ');
|
||||
$query->whereRaw('page_id = ?', [$pageId]);
|
||||
$query->orderBy('created_at');
|
||||
return $query;
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
public function getAllPageComments($pageId) {
|
||||
return self::where('page_id', '=', $pageId)->with(['createdBy' => function($query) {
|
||||
$query->select('id', 'name', 'image_id');
|
||||
}, 'updatedBy' => function($query) {
|
||||
$query->select('id', 'name');
|
||||
}, 'createdBy.avatar' => function ($query) {
|
||||
$query->select('id', 'path', 'url');
|
||||
}])->get();
|
||||
}
|
||||
|
||||
public function getCommentById($commentId) {
|
||||
return self::where('id', '=', $commentId)->with(['createdBy' => function($query) {
|
||||
$query->select('id', 'name', 'image_id');
|
||||
}, 'updatedBy' => function($query) {
|
||||
$query->select('id', 'name');
|
||||
}, 'createdBy.avatar' => function ($query) {
|
||||
$query->select('id', 'path', 'url');
|
||||
}])->first();
|
||||
}
|
||||
|
||||
public function getCreatedAttribute() {
|
||||
@ -72,4 +86,8 @@ class Comment extends Ownable
|
||||
];
|
||||
return $updated;
|
||||
}
|
||||
|
||||
public function getSubCommentsAttribute() {
|
||||
return $this->sub_comments;
|
||||
}
|
||||
}
|
||||
|
@ -54,9 +54,12 @@ class CommentController extends Controller
|
||||
$respMsg = trans('entities.comment_updated');
|
||||
}
|
||||
|
||||
$comment = $this->commentRepo->getCommentById($comment->id);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => $respMsg
|
||||
'message' => $respMsg,
|
||||
'comment' => $comment
|
||||
]);
|
||||
|
||||
}
|
||||
@ -64,11 +67,10 @@ class CommentController extends Controller
|
||||
public function destroy($id) {
|
||||
$comment = $this->comment->findOrFail($id);
|
||||
$this->checkOwnablePermission('comment-delete', $comment);
|
||||
|
||||
//
|
||||
}
|
||||
|
||||
public function getCommentThread($pageId, $commentId = null) {
|
||||
|
||||
public function getPageComments($pageId) {
|
||||
try {
|
||||
$page = $this->entityRepo->getById('page', $pageId, true);
|
||||
} catch (ModelNotFoundException $e) {
|
||||
@ -85,12 +87,7 @@ class CommentController extends Controller
|
||||
|
||||
$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(['success' => true, 'comments'=> $comments, 'total' => $totalComments]);
|
||||
}
|
||||
return response()->json(['success' => true, 'comments'=> $comments]);
|
||||
$comments = $this->commentRepo->getPageComments($pageId);
|
||||
return response()->json(['success' => true, 'comments'=> $comments['comments'], 'total' => $comments['total']]);
|
||||
}
|
||||
}
|
||||
|
@ -39,13 +39,48 @@ class CommentRepo {
|
||||
return $comment;
|
||||
}
|
||||
|
||||
public function getCommentsForPage($pageId, $commentId, $count = 20) {
|
||||
// requesting parent comments
|
||||
$query = $this->comment->getCommentsByPage($pageId, $commentId);
|
||||
return $query->paginate($count);
|
||||
public function getPageComments($pageId) {
|
||||
$comments = $this->comment->getAllPageComments($pageId);
|
||||
$index = [];
|
||||
$totalComments = count($comments);
|
||||
// normalizing the response.
|
||||
foreach($comments as &$comment) {
|
||||
$comment = $this->normalizeComment($comment);
|
||||
$parentId = $comment->parent_id;
|
||||
if (empty($parentId)) {
|
||||
$index[$comment->id] = $comment;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (empty($index[$parentId])) {
|
||||
// weird condition should not happen.
|
||||
continue;
|
||||
}
|
||||
if (empty($index[$parentId]->sub_comments)) {
|
||||
$index[$parentId]->sub_comments = [];
|
||||
}
|
||||
array_push($index[$parentId]->sub_comments, $comment);
|
||||
$index[$comment->id] = $comment;
|
||||
}
|
||||
return [
|
||||
'comments' => $comments,
|
||||
'total' => $totalComments
|
||||
];
|
||||
}
|
||||
|
||||
public function getCommentCount($pageId) {
|
||||
return $this->comment->where('page_id', '=', $pageId)->count();
|
||||
public function getCommentById($commentId) {
|
||||
return $this->normalizeComment($this->comment->getCommentById($commentId));
|
||||
}
|
||||
|
||||
private function normalizeComment($comment) {
|
||||
if (empty($comment)) {
|
||||
return;
|
||||
}
|
||||
$comment->createdBy->avatar_url = $comment->createdBy->getAvatar(50);
|
||||
$comment->createdBy->profile_url = $comment->createdBy->getProfileUrl();
|
||||
if (!empty($comment->updatedBy)) {
|
||||
$comment->updatedBy->profile_url = $comment->updatedBy->getProfileUrl();
|
||||
}
|
||||
return $comment;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user