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

⚗️(backend) Text to yjs base64 #344

Open
wants to merge 1 commit into
base: feature/back-read-yjs-doc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion src/backend/core/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pytest

from core.utils import email_invitation, yjs_base64_to_text
from core.utils import email_invitation, text_to_yjs_base64, yjs_base64_to_text

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -111,3 +111,8 @@ def test_yjs_base64_to_text():
)

assert yjs_base64_to_text(base64_string) == "Hello world"


def test_text_to_yjs_base64():
base64_string = text_to_yjs_base64("Hello world")
assert yjs_base64_to_text(base64_string) == "Hello world"
20 changes: 20 additions & 0 deletions src/backend/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,23 @@ def yjs_base64_to_text(base64_string):

soup = BeautifulSoup(blocknote_structure, "html.parser")
return soup.get_text(separator=" ").strip()


def text_to_yjs_base64(text: str) -> str:
"""Convert plain text to a base64-encoded Yjs document"""
doc = Y.YDoc()

# Insert the paragraph text into the document
with doc.begin_transaction() as txn:
xml_fragment = doc.get_xml_element('document-store')

xml_element = xml_fragment.push_xml_element(txn, 'paragraph')

xml_text = xml_element.push_xml_text(txn)
xml_text.push(txn, text)

# Encode the document as a Uint8Array
update = Y.encode_state_as_update(doc)

# Encode the result to base64
return base64.b64encode(update).decode('utf-8')
21 changes: 9 additions & 12 deletions src/backend/demo/management/commands/create_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from faker import Faker

from core import models
from core import models, utils

from demo import defaults

Expand Down Expand Up @@ -127,17 +127,14 @@ def create_demo(stdout):

with Timeit(stdout, "Creating documents"):
for _ in range(defaults.NB_OBJECTS["docs"]):
queue.push(
models.Document(
title=fake.sentence(nb_words=4),
link_reach=models.LinkReachChoices.AUTHENTICATED
if random_true_with_probability(0.5)
else random.choice(models.LinkReachChoices.values),
)
)

queue.flush()

models.Document(
title=fake.sentence(nb_words=4),
content=utils.text_to_yjs_base64(fake.text()),
link_reach=models.LinkReachChoices.AUTHENTICATED
if random_true_with_probability(0.5)
else random.choice(models.LinkReachChoices.values),
).save()

with Timeit(stdout, "Creating docs accesses"):
docs_ids = list(models.Document.objects.values_list("id", flat=True))
users_ids = list(models.User.objects.values_list("id", flat=True))
Expand Down