From 9ff3d6ed7c6c89ccd2294b57313a95747a3073e5 Mon Sep 17 00:00:00 2001 From: Carbrex <95964955+Carbrex@users.noreply.github.com> Date: Mon, 8 Jul 2024 01:06:04 +0530 Subject: [PATCH] Don't add metadata if text length exceeds 140 after encoding and don't include metadata to calculate isShouting --- modules/common/src/main/String.scala | 26 ++++++++++++++++------- ui/@types/lichess/index.d.ts | 2 +- ui/analyse/src/study/relay/chatHandler.ts | 5 ++++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/modules/common/src/main/String.scala b/modules/common/src/main/String.scala index 3dec12da8f54c..fc468ec71ab36 100644 --- a/modules/common/src/main/String.scala +++ b/modules/common/src/main/String.scala @@ -21,18 +21,28 @@ object String: try play.utils.UriEncoding.decodePath(input, "UTF-8").some catch case _: play.utils.InvalidUriEncodingException => None - def isShouting(text: String) = + def isShouting(text: String): Boolean = text.lengthIs >= 5 && { import java.lang.Character.* // true if >1/2 of the latin letters are uppercase - text.take(80).replace("O-O", "o-o").foldLeft(0) { (i, c) => - getType(c) match - case UPPERCASE_LETTER => i + 1 - case LOWERCASE_LETTER => i - 1 - case _ => i - } > 0 + if text.contains('\ue666') then + val (before, after) = text.span(_ != '\ue666') + isShouting(before) + else + text.take(80).replace("O-O", "o-o").foldLeft(0) { (i, c) => + getType(c) match + case UPPERCASE_LETTER => i + 1 + case LOWERCASE_LETTER => i - 1 + case _ => i + } > 0 } - def noShouting(str: String): String = if isShouting(str) then str.toLowerCase else str + def noShouting(str: String): String = if isShouting(str) then + // '\ue666' is a special character used to encode broadcast chat messages. See file://./../../../../ui/analyse/src/study/relay/chatHandler.ts + if str.contains('\ue666') then + val (before, after) = str.span(_ != '\ue666') + before.toLowerCase + after + else str.toLowerCase + else str val atUsernameRegex = RawHtml.atUsernameRegex val forumPostPathRegex = """(?:(?<= )|^)\b([\w-]+/[\w-]+)\b(?:(?= )|$)""".r diff --git a/ui/@types/lichess/index.d.ts b/ui/@types/lichess/index.d.ts index 100cc892330f4..2551235b672be 100644 --- a/ui/@types/lichess/index.d.ts +++ b/ui/@types/lichess/index.d.ts @@ -30,7 +30,7 @@ interface Site { clockWidget(el: HTMLElement, opts: { time: number; pause?: boolean }): void; spinnerHtml: string; asset: { - // file://./../../site/src/assets.ts + // file://./../../site/src/asset.ts baseUrl(): string; url(url: string, opts?: AssetUrlOpts): string; flairSrc(flair: Flair): string; diff --git a/ui/analyse/src/study/relay/chatHandler.ts b/ui/analyse/src/study/relay/chatHandler.ts index 63ea6adb7b29f..6d3656a068d36 100644 --- a/ui/analyse/src/study/relay/chatHandler.ts +++ b/ui/analyse/src/study/relay/chatHandler.ts @@ -12,7 +12,10 @@ export function broadcastChatHandler(ctrl: AnalyseCtrl): BroadcastChatHandler { if (ctrl.study?.relay && !ctrl.study.relay.tourShow()) { const chapterId = ctrl.study.currentChapter().id; const ply = ctrl.study.currentNode().ply; - text = text + separator + chapterId + separator + ply; + const newText = text + separator + chapterId + separator + ply; + if (newText.length <= 140) { + text = newText; + } } return text; };