damus.io

damus.io website
git clone git://jb55.com/damus.io
Log | Files | Refs | README

commit 17d4f9e46822bfcaa7cc60c956da2a9d84c35a7b
parent 917fa903985426e06c884e377da522d05432d47e
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 11 Nov 2022 11:14:52 -0800

web: support content warnings

Diffstat:
Mwebv2/damus.css | 11+++++++++++
Mwebv2/damus.js | 49++++++++++++++++++++++++++++++++++++++++++++++++-
Mwebv2/index.html | 4++--
3 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/webv2/damus.css b/webv2/damus.css @@ -83,6 +83,17 @@ body { background: rgba(0,0,0,0.4); } +summary { + cursor: pointer; +} + +details { + background-color: rgba(255,255,255,0.2); + padding: 10px; + margin-right: 30px; + border-radius: 20px; +} + textarea, input { background-color: rgba(255,255,255,0.2); border: 0; diff --git a/webv2/damus.js b/webv2/damus.js @@ -45,6 +45,7 @@ function init_home_model() { events: [], chatrooms: {}, deletions: {}, + cw_open: {}, deleted: {}, profiles: {}, profile_events: {}, @@ -533,6 +534,13 @@ function redraw_events(model) { //log_debug("rendering home view") model.rendered = {} model.events_el.innerHTML = render_events(model) + setup_home_event_handlers(model.events_el) +} + +function setup_home_event_handlers(events_el) +{ + for (const el of events_el.querySelectorAll(".cw")) + el.addEventListener("toggle", toggle_content_warning.bind(null)) } function redraw_home_view(model) { @@ -1222,6 +1230,30 @@ function convert_quote_blocks(content, show_media) }, "") } +function get_content_warning(tags) +{ + for (const tag of tags) { + if (tag.length >= 1 && tag[0] === "content-warning") + return tag[1] || "" + } + + return null +} + +function toggle_content_warning(e) +{ + const el = e.target + const id = el.id.split("_")[1] + const ev = DSTATE.all_events[id] + + if (!ev) { + log_debug("could not find content-warning event", id) + return + } + + DSTATE.cw_open[id] = el.open +} + function format_content(ev, show_media) { if (ev.kind === 7) { @@ -1230,7 +1262,22 @@ function format_content(ev, show_media) return sanitize(ev.content.trim()) } - return convert_quote_blocks(ev.content.trim(), show_media) + const content = ev.content.trim() + const body = convert_quote_blocks(content, show_media) + + let cw = get_content_warning(ev.tags) + if (cw !== null) { + cw = cw === ""? "Content Warning" : `Content Warning: ${cw}` + const open = !!DSTATE.cw_open[ev.id]? "open" : "" + return ` + <details class="cw" id="cw_${ev.id}" ${open}> + <summary>${cw}</summary> + ${body} + </details> + ` + } + + return body } function sanitize(content) diff --git a/webv2/index.html b/webv2/index.html @@ -6,7 +6,7 @@ <title>Damus</title> - <link rel="stylesheet" href="damus.css?v=17"> + <link rel="stylesheet" href="damus.css?v=18"> </head> <body> <section class="header"> @@ -41,7 +41,7 @@ <script src="noble-secp256k1.js?v=1"></script> <script src="bech32.js?v=1"></script> <script src="nostr.js?v=6"></script> - <script src="damus.js?v=54"></script> + <script src="damus.js?v=55"></script> <script> // I have to delay loading to wait for nos2x const relay = setTimeout(damus_web_init, 100)