Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions backend/data/blooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Bloom:


def add_bloom(*, sender: User, content: str) -> Bloom:
if len(content) > 280:
raise ValueError("Bloom must not exceed 280 characters")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you changed the character limit, you'd need to change it in two places (and could easily forget one) - can you think how to avoid this?

The same applies in your JavaScript. Often we need to duplicate things for different languages, but within one language we should be able to avoid this duplication :)


hashtags = [word[1:] for word in content.split(" ") if word.startswith("#")]

now = datetime.datetime.now(tz=datetime.UTC)
Expand Down
26 changes: 22 additions & 4 deletions front-end/components/bloom.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
* "sender": username,
* "content": "string from textarea",
* "sent_timestamp": "datetime as ISO 8601 formatted string"}

*/
const createBloom = (template, bloom) => {
if (!bloom) return;

const MAX_BLOOM_LENGTH = 280;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still three occurrences of 280 in this file?


const bloomFrag = document.getElementById(template).content.cloneNode(true);
const bloomParser = new DOMParser();

Expand All @@ -21,14 +23,30 @@ const createBloom = (template, bloom) => {
const bloomTimeLink = bloomFrag.querySelector("a:has(> [data-time])");
const bloomContent = bloomFrag.querySelector("[data-content]");

const isTooLong = bloom.content && bloom.content.length > MAX_BLOOM_LENGTH;

// Highlight invalid blooms (UI only)
if (isTooLong) {
bloomArticle.style.border = "2px solid red";
bloomArticle.style.backgroundColor = "#fff5f5";
bloomArticle.title = "This bloom exceeds 280 characters (invalid)";
}

bloomArticle.setAttribute("data-bloom-id", bloom.id);
bloomUsername.setAttribute("href", `/profile/${bloom.sender}`);
bloomUsername.textContent = bloom.sender;
bloomTime.textContent = _formatTimestamp(bloom.sent_timestamp);
bloomTimeLink.setAttribute("href", `/bloom/${bloom.id}`);

const displayContent = isTooLong
? `[INVALID BLOOM - exceeds 280 characters]\n\n${bloom.content}`
: bloom.content;

bloomContent.replaceChildren(
...bloomParser.parseFromString(_formatHashtags(bloom.content), "text/html")
.body.childNodes
...bloomParser.parseFromString(
_formatHashtags(displayContent),
"text/html"
).body.childNodes
);

return bloomFrag;
Expand Down Expand Up @@ -84,4 +102,4 @@ function _formatTimestamp(timestamp) {
}
}

export {createBloom};
export { createBloom };