<commit_before><?php

namespace BookStack;
use Illuminate\Support\Facades\DB;

class Comment extends Ownable
{
    protected $fillable = ['text', 'html', 'parent_id'];

    /**
     * Get the entity that this comment belongs to
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function entity()
    {
        return $this->morphTo('entity');
    }

    /**
     * Get the page that this comment is in.
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function page()
    {
        return $this->belongsTo(Page::class);
    }

    /**
     * Get the owner of this comment.
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    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 ',
                [$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;
    }
}<commit_msg>#47 Adds two attributes updated and created to display time to user.<commit_after><?php

namespace BookStack;
use Illuminate\Support\Facades\DB;

class Comment extends Ownable
{
    protected $fillable = ['text', 'html', 'parent_id'];
    protected $appends = ['created', 'updated'];
    /**
     * Get the entity that this comment belongs to
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function entity()
    {
        return $this->morphTo('entity');
    }

    /**
     * Get the page that this comment is in.
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function page()
    {
        return $this->belongsTo(Page::class);
    }

    /**
     * Get the owner of this comment.
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    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 ',
                [$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;
    }

    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;
    }
}
{'fraction_non_alphanumeric': 0.1497695852534562, 'fraction_numerical': 0.0027649769585253456, 'mean_word_length': 2.4728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '754710', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1348, 'n_words': 295}