Comments: Added back-end content reference handling

Also added archived property, to be added.
This commit is contained in:
Dan Brown 2025-04-18 21:13:49 +01:00
parent add238fe9f
commit 5e3c3ad634
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
7 changed files with 65 additions and 3 deletions

View File

@ -20,7 +20,7 @@ class CommentRepo
/** /**
* Create a new comment on an entity. * Create a new comment on an entity.
*/ */
public function create(Entity $entity, string $html, ?int $parent_id): Comment public function create(Entity $entity, string $html, ?int $parent_id, string $content_ref): Comment
{ {
$userId = user()->id; $userId = user()->id;
$comment = new Comment(); $comment = new Comment();
@ -30,6 +30,7 @@ class CommentRepo
$comment->updated_by = $userId; $comment->updated_by = $userId;
$comment->local_id = $this->getNextLocalId($entity); $comment->local_id = $this->getNextLocalId($entity);
$comment->parent_id = $parent_id; $comment->parent_id = $parent_id;
$comment->content_ref = preg_match('/^bkmrk-(.*?):\d+:(\d*-\d*)?$/', $content_ref) === 1 ? $content_ref : '';
$entity->comments()->save($comment); $entity->comments()->save($comment);
ActivityService::add(ActivityType::COMMENT_CREATE, $comment); ActivityService::add(ActivityType::COMMENT_CREATE, $comment);

View File

@ -26,6 +26,7 @@ class CommentController extends Controller
$input = $this->validate($request, [ $input = $this->validate($request, [
'html' => ['required', 'string'], 'html' => ['required', 'string'],
'parent_id' => ['nullable', 'integer'], 'parent_id' => ['nullable', 'integer'],
'content_ref' => ['string'],
]); ]);
$page = $this->pageQueries->findVisibleById($pageId); $page = $this->pageQueries->findVisibleById($pageId);
@ -40,7 +41,7 @@ class CommentController extends Controller
// Create a new comment. // Create a new comment.
$this->checkPermission('comment-create-all'); $this->checkPermission('comment-create-all');
$comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null); $comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null, $input['content_ref']);
return view('comments.comment-branch', [ return view('comments.comment-branch', [
'readOnly' => false, 'readOnly' => false,

View File

@ -19,6 +19,8 @@ use Illuminate\Database\Eloquent\Relations\MorphTo;
* @property int $entity_id * @property int $entity_id
* @property int $created_by * @property int $created_by
* @property int $updated_by * @property int $updated_by
* @property string $content_ref
* @property bool $archived
*/ */
class Comment extends Model implements Loggable class Comment extends Model implements Loggable
{ {

View File

@ -27,6 +27,8 @@ class CommentFactory extends Factory
'html' => $html, 'html' => $html,
'parent_id' => null, 'parent_id' => null,
'local_id' => 1, 'local_id' => 1,
'content_ref' => '',
'archived' => false,
]; ];
} }
} }

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('comments', function (Blueprint $table) {
$table->string('content_ref');
$table->boolean('archived')->index();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('comments', function (Blueprint $table) {
$table->dropColumn('content_ref');
$table->dropColumn('archived');
});
}
};

View File

@ -95,7 +95,7 @@ export class PageComments extends Component {
const reqData = { const reqData = {
html: this.wysiwygEditor.getContent(), html: this.wysiwygEditor.getContent(),
parent_id: this.parentId || null, parent_id: this.parentId || null,
content_reference: this.contentReference || '', content_ref: this.contentReference || '',
}; };
window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => { window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => {

View File

@ -33,6 +33,32 @@ class CommentTest extends TestCase
$this->assertActivityExists(ActivityType::COMMENT_CREATE); $this->assertActivityExists(ActivityType::COMMENT_CREATE);
} }
public function test_add_comment_stores_content_reference_only_if_format_valid()
{
$validityByRefs = [
'bkmrk-my-title:4589284922:4-3' => true,
'bkmrk-my-title:4589284922:' => true,
'bkmrk-my-title:4589284922:abc' => false,
'my-title:4589284922:' => false,
'bkmrk-my-title-4589284922:' => false,
];
$page = $this->entities->page();
foreach ($validityByRefs as $ref => $valid) {
$this->asAdmin()->postJson("/comment/$page->id", [
'html' => '<p>My comment</p>',
'parent_id' => null,
'content_ref' => $ref,
]);
if ($valid) {
$this->assertDatabaseHas('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
} else {
$this->assertDatabaseMissing('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
}
}
}
public function test_comment_edit() public function test_comment_edit()
{ {