From e1a7bca3eb11d6445e69638c42e6dac71dbd4448 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 4 Feb 2021 14:39:37 -0500 Subject: [PATCH] Highlight when a message crosses an SMS boundary (#1773) --- cmd/server/assets/realmadmin/_form_sms.html | 67 ++++++++++++++++++++- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/cmd/server/assets/realmadmin/_form_sms.html b/cmd/server/assets/realmadmin/_form_sms.html index 03f4d66d2..524a524ad 100644 --- a/cmd/server/assets/realmadmin/_form_sms.html +++ b/cmd/server/assets/realmadmin/_form_sms.html @@ -316,8 +316,29 @@ const $messageBubbles = $('div#message-bubbles'); const $messageBubble = $(''); + // spanBoundary returns whether the substr crosses the smsLength boundary in + // body. If it would, it returns the index where it would happen, the start + // of the substring in index, and the remainder of substr in the next + // message. + function spansBoundary(body, substr) { + let idx = body.indexOf(substr); + if (idx < 0) { + return [0, 0, 0]; + } + + let msg = Math.floor(idx / smsLength); + let start = idx % smsLength; + let end = start + substr.length; + let rem = end - smsLength; + + if (rem <= 0) { + return [0, 0, 0]; + } + + return [msg, start, rem]; + } + function buildTemplateSplits(target) { - $messageBubbles.empty(); if(!target) { return; } @@ -335,10 +356,50 @@ return; } + // Build all the bubbles. + let bubbles = []; for(let i = 0; i < parts.length; i++) { - let msg = parts[i]; - $messageBubble.clone().text(msg).appendTo($messageBubbles); + let msg = $('
').text(parts[i]).html(); + let ensLinkEscaped = $('
').text(ensLink).html(); + msg = msg.replace(ensLinkEscaped, `${ensLinkEscaped}`); + + bubbles.push($messageBubble.clone().html(msg)); + } + + // Check if any critical information spans a boundary and highlight. + let criticals = [shortCode, longCode, ensLink]; + let anyCriticalsBreak = false; + for(let i = 0; i < criticals.length; i++) { + let field = criticals[i]; + + let [msg, start, rem] = spansBoundary(val, field) + if (rem === 0) { + continue; + } + + anyCriticalsBreak = true; + + let $thisBubble = $(bubbles[msg]); + let thisText = $thisBubble.text(); + let thisSub = $('
').text(thisText.substring(start)).html(); + + let $nextBubble = $(bubbles[msg+1]); + let nextText = $nextBubble.text(); + let nextSub = $('
').text(nextText.substring(0, rem)).html(); + + $thisBubble.html(thisText.replace(thisSub, `${thisSub}`)); + $nextBubble.html(nextText.replace(nextSub, `${nextSub}`)); + } + + if (anyCriticalsBreak) { + bubbles.unshift($(`
` + + `Critical information such as links or codes could be split across ` + + `an SMS boundary! These instances are highlighted in red ` + + `below.
`)); } + + $messageBubbles.empty(); + $messageBubbles.append(bubbles); } // When the textarea changes, rebuild the splits.