Skip to content

Commit 5e3c3ad

Browse files
committed
Comments: Added back-end content reference handling
Also added archived property, to be added.
1 parent add238f commit 5e3c3ad

File tree

7 files changed

+65
-3
lines changed

7 files changed

+65
-3
lines changed

app/Activity/CommentRepo.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getById(int $id): Comment
2020
/**
2121
* Create a new comment on an entity.
2222
*/
23-
public function create(Entity $entity, string $html, ?int $parent_id): Comment
23+
public function create(Entity $entity, string $html, ?int $parent_id, string $content_ref): Comment
2424
{
2525
$userId = user()->id;
2626
$comment = new Comment();
@@ -30,6 +30,7 @@ public function create(Entity $entity, string $html, ?int $parent_id): Comment
3030
$comment->updated_by = $userId;
3131
$comment->local_id = $this->getNextLocalId($entity);
3232
$comment->parent_id = $parent_id;
33+
$comment->content_ref = preg_match('/^bkmrk-(.*?):\d+:(\d*-\d*)?$/', $content_ref) === 1 ? $content_ref : '';
3334

3435
$entity->comments()->save($comment);
3536
ActivityService::add(ActivityType::COMMENT_CREATE, $comment);

app/Activity/Controllers/CommentController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function savePageComment(Request $request, int $pageId)
2626
$input = $this->validate($request, [
2727
'html' => ['required', 'string'],
2828
'parent_id' => ['nullable', 'integer'],
29+
'content_ref' => ['string'],
2930
]);
3031

3132
$page = $this->pageQueries->findVisibleById($pageId);
@@ -40,7 +41,7 @@ public function savePageComment(Request $request, int $pageId)
4041

4142
// Create a new comment.
4243
$this->checkPermission('comment-create-all');
43-
$comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null);
44+
$comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null, $input['content_ref']);
4445

4546
return view('comments.comment-branch', [
4647
'readOnly' => false,

app/Activity/Models/Comment.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @property int $entity_id
2020
* @property int $created_by
2121
* @property int $updated_by
22+
* @property string $content_ref
23+
* @property bool $archived
2224
*/
2325
class Comment extends Model implements Loggable
2426
{

database/factories/Activity/Models/CommentFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public function definition()
2727
'html' => $html,
2828
'parent_id' => null,
2929
'local_id' => 1,
30+
'content_ref' => '',
31+
'archived' => false,
3032
];
3133
}
3234
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('comments', function (Blueprint $table) {
15+
$table->string('content_ref');
16+
$table->boolean('archived')->index();
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*/
23+
public function down(): void
24+
{
25+
Schema::table('comments', function (Blueprint $table) {
26+
$table->dropColumn('content_ref');
27+
$table->dropColumn('archived');
28+
});
29+
}
30+
};

resources/js/components/page-comments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class PageComments extends Component {
9595
const reqData = {
9696
html: this.wysiwygEditor.getContent(),
9797
parent_id: this.parentId || null,
98-
content_reference: this.contentReference || '',
98+
content_ref: this.contentReference || '',
9999
};
100100

101101
window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => {

tests/Entity/CommentTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ public function test_add_comment()
3333

3434
$this->assertActivityExists(ActivityType::COMMENT_CREATE);
3535
}
36+
public function test_add_comment_stores_content_reference_only_if_format_valid()
37+
{
38+
$validityByRefs = [
39+
'bkmrk-my-title:4589284922:4-3' => true,
40+
'bkmrk-my-title:4589284922:' => true,
41+
'bkmrk-my-title:4589284922:abc' => false,
42+
'my-title:4589284922:' => false,
43+
'bkmrk-my-title-4589284922:' => false,
44+
];
45+
46+
$page = $this->entities->page();
47+
48+
foreach ($validityByRefs as $ref => $valid) {
49+
$this->asAdmin()->postJson("/comment/$page->id", [
50+
'html' => '<p>My comment</p>',
51+
'parent_id' => null,
52+
'content_ref' => $ref,
53+
]);
54+
55+
if ($valid) {
56+
$this->assertDatabaseHas('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
57+
} else {
58+
$this->assertDatabaseMissing('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
59+
}
60+
}
61+
}
3662

3763
public function test_comment_edit()
3864
{

0 commit comments

Comments
 (0)