Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML column on comments for storing tiptap editor data #2091

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
CK_EDITOR = "CK_EDITOR"
QUILL_EDITOR = "QUILL_EDITOR"
TEXT = "TEXT"
HTML = "HTML"

RH_COMMENT_CONTENT_TYPES = (
(CK_EDITOR, CK_EDITOR),
(QUILL_EDITOR, QUILL_EDITOR),
(TEXT, TEXT),
(HTML, HTML),
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ class Migration(migrations.Migration):
("INNER_CONTENT_COMMENT", "INNER_CONTENT_COMMENT"),
],
default="GENERIC_COMMENT",
max_length=144,
max_length=255,
),
),
migrations.AddField(
model_name="rhcommentmodel",
name="html",
field=models.TextField(blank=True, null=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.1.4 on 2025-02-02 20:05

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("researchhub_comment", "0019_alter_rhcommentmodel_comment_type_and_more"),
]

operations = [
migrations.AddField(
model_name="rhcommentmodel",
name="html",
field=models.TextField(
blank=True,
help_text="HTML representation of the comment content",
null=True,
),
),
]
34 changes: 28 additions & 6 deletions src/researchhub_comment/related_models/rh_comment_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from discussion.reaction_models import AbstractGenericReactionModel
from purchase.models import Purchase
from researchhub_comment.constants.rh_comment_content_types import (
HTML,
QUILL_EDITOR,
RH_COMMENT_CONTENT_TYPES,
)
Expand Down Expand Up @@ -57,6 +58,11 @@ class RhCommentModel(
default=QUILL_EDITOR,
max_length=144,
)
html = TextField(
blank=True,
null=True,
help_text="HTML representation of the comment content",
)
is_accepted_answer = BooleanField(null=True)
parent = ForeignKey(
"self",
Expand Down Expand Up @@ -167,9 +173,11 @@ def get_total_children_count(self):
return total_count

def update_comment_content(self):
celery_create_comment_content_src.apply_async(
(self.id, self.comment_content_json), countdown=2
)
# Only create content source for QUILL format
if self.comment_content_type != HTML:
celery_create_comment_content_src.apply_async(
(self.id, self.comment_content_json), countdown=2
)

def _update_related_discussion_count(self, amount):
from citation.models import CitationEntry
Expand Down Expand Up @@ -207,11 +215,25 @@ def decrement_discussion_count(self):
def create_from_data(cls, data):
from researchhub_comment.serializers import RhCommentSerializer

content_format = data.get("content_format", QUILL_EDITOR)

if content_format == QUILL_EDITOR:
# For QUILL format, content is already in comment_content_json
data["html"] = None
else:
# For HTML format, use comment_content directly
data["html"] = data.get("comment_content")
data["comment_content_json"] = None

rh_comment_serializer = RhCommentSerializer(data=data)
rh_comment_serializer.is_valid(raise_exception=True)
rh_comment = rh_comment_serializer.save()
celery_create_comment_content_src.apply_async(
(rh_comment.id, data.get("comment_content_json")), countdown=2
)

if content_format == QUILL_EDITOR:
# Only create content source for QUILL format
celery_create_comment_content_src.apply_async(
(rh_comment.id, rh_comment.comment_content_json), countdown=2
)

rh_comment.increment_discussion_count()
return rh_comment, rh_comment_serializer.data
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"context_title",
"created_date",
"created_by",
"html",
"updated_by",
"id",
"is_accepted_answer",
Expand Down
31 changes: 31 additions & 0 deletions src/researchhub_comment/tests/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,37 @@ def test_filter_by_bounties(self):
self.assertEqual(regular_res.status_code, 200)
self.assertEqual(regular_res.data["count"], 4)

def test_html_content_format(self):
"""Test that HTML format comments store content in html field and not in comment_content_json"""
html_content = "<p>This is a test HTML comment</p>"
res = self._create_paper_comment(
self.paper.id,
self.user_1,
content_format="HTML",
comment_content=html_content,
)

self.assertEqual(res.status_code, 200)
comment_data = res.data

# Verify html field is set and comment_content_json is null
self.assertEqual(comment_data["html"], html_content)
self.assertIsNone(comment_data["comment_content_json"])

def test_quill_content_format(self):
"""Test that QUILL_EDITOR format comments store content in comment_content_json field and not in html"""
quill_content = {"ops": [{"insert": "This is a test Quill comment"}]}
res = self._create_paper_comment(
self.paper.id, self.user_1, comment_content_json=quill_content
)

self.assertEqual(res.status_code, 200)
comment_data = res.data

# Verify comment_content_json is set and html is null
self.assertEqual(comment_data["comment_content_json"], quill_content)
self.assertIsNone(comment_data["html"])

def test_comment_mentions(self):
creator = self.user_1
recipient = self.user_2
Expand Down
Loading