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

Made some changes to the comment system

Changed to be rendered server side along with page content.
Changed deletion to fully delete comments from the database.
Added 'local_id' to comments for referencing.
Updated reply system to be non-nested (Incomplete)
Made database comment format entity-agnostic to be more future proof.
Updated designs of comment sections.
This commit is contained in:
Dan Brown
2017-09-03 16:37:51 +01:00
parent e3f2bde26d
commit fea5630ea4
24 changed files with 478 additions and 731 deletions

View File

@ -1,12 +1,10 @@
<?php
namespace BookStack;
<?php namespace BookStack;
class Comment extends Ownable
{
public $sub_comments = [];
protected $fillable = ['text', 'html', 'parent_id'];
protected $appends = ['created', 'updated', 'sub_comments'];
protected $appends = ['created', 'updated'];
/**
* Get the entity that this comment belongs to
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
@ -17,80 +15,29 @@ class Comment extends Ownable
}
/**
* Get the page that this comment is in.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* Check if a comment has been updated since creation.
* @return bool
*/
public function page()
public function isUpdated()
{
return $this->belongsTo(Page::class);
return $this->updated_at->timestamp > $this->created_at->timestamp;
}
/**
* Get the owner of this comment.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* Get created date as a relative diff.
* @return mixed
*/
public function user()
public function getCreatedAttribute()
{
return $this->belongsTo(User::class);
return $this->created_at->diffForHumans();
}
/*
* Not being used, but left here because might be used in the future for performance reasons.
/**
* Get updated date as a relative diff.
* @return mixed
*/
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, comments.parent_id, '
. 'u.name AS created_by_name, u1.name AS updated_by_name, '
. 'i.url AS avatar ');
$query->whereRaw('page_id = ?', [$pageId]);
$query->orderBy('created_at');
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() {
$created = [
'day_time_str' => $this->created_at->toDayDateTimeString(),
'diff' => $this->created_at->diffForHumans()
];
return $created;
}
public function getUpdatedAttribute() {
if (empty($this->updated_at)) {
return null;
}
$updated = [
'day_time_str' => $this->updated_at->toDayDateTimeString(),
'diff' => $this->updated_at->diffForHumans()
];
return $updated;
}
public function getSubCommentsAttribute() {
return $this->sub_comments;
public function getUpdatedAttribute()
{
return $this->updated_at->diffForHumans();
}
}