From d1531b832d6ee804f60fe66570ad3cae300d95be Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 8 Mar 2023 19:56:24 -0600 Subject: [PATCH] Truncate Nostr pubkeys in reply mentions --- CHANGELOG.md | 1 + app/soapbox/components/status-reply-mentions.tsx | 3 ++- .../features/compose/components/reply-mentions.tsx | 12 +++++++++--- app/soapbox/utils/nostr.ts | 6 ++++++ 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 app/soapbox/utils/nostr.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a9ac41b44..b75cf6d1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added ### Changed +- Posts: truncate Nostr pubkeys in reply mentions. ### Fixed - Posts: fixed emojis being cut off in reactions modal. diff --git a/app/soapbox/components/status-reply-mentions.tsx b/app/soapbox/components/status-reply-mentions.tsx index 7bd44495c..61f2f2969 100644 --- a/app/soapbox/components/status-reply-mentions.tsx +++ b/app/soapbox/components/status-reply-mentions.tsx @@ -6,6 +6,7 @@ import { openModal } from 'soapbox/actions/modals'; import HoverRefWrapper from 'soapbox/components/hover-ref-wrapper'; import HoverStatusWrapper from 'soapbox/components/hover-status-wrapper'; import { useAppDispatch } from 'soapbox/hooks'; +import { isPubkey } from 'soapbox/utils/nostr'; import type { Account, Status } from 'soapbox/types/entities'; @@ -56,7 +57,7 @@ const StatusReplyMentions: React.FC = ({ status, hoverable className='reply-mentions__account' onClick={(e) => e.stopPropagation()} > - @{account.username} + @{isPubkey(account.username) ? account.username.slice(0, 8) : account.username} ); diff --git a/app/soapbox/features/compose/components/reply-mentions.tsx b/app/soapbox/features/compose/components/reply-mentions.tsx index baa8f8d04..511950c8e 100644 --- a/app/soapbox/features/compose/components/reply-mentions.tsx +++ b/app/soapbox/features/compose/components/reply-mentions.tsx @@ -6,6 +6,7 @@ import { openModal } from 'soapbox/actions/modals'; import { useAppSelector, useCompose, useFeatures } from 'soapbox/hooks'; import { statusToMentionsAccountIdsArray } from 'soapbox/reducers/compose'; import { makeGetStatus } from 'soapbox/selectors'; +import { isPubkey } from 'soapbox/utils/nostr'; import type { Status as StatusEntity } from 'soapbox/types/entities'; @@ -52,9 +53,14 @@ const ReplyMentions: React.FC = ({ composeId }) => { ); } - const accounts = to.slice(0, 2).map((acct: string) => ( - @{acct.split('@')[0]} - )).toArray(); + const accounts = to.slice(0, 2).map((acct: string) => { + const username = acct.split('@')[0]; + return ( + + @{isPubkey(username) ? username.slice(0, 8) : username} + + ); + }).toArray(); if (to.size > 2) { accounts.push( diff --git a/app/soapbox/utils/nostr.ts b/app/soapbox/utils/nostr.ts new file mode 100644 index 000000000..461677636 --- /dev/null +++ b/app/soapbox/utils/nostr.ts @@ -0,0 +1,6 @@ +/** Check whether the given input is a valid Nostr hexidecimal pubkey. */ +const isPubkey = (value: string) => /^[0-9a-f]{64}$/i.test(value); + +export { + isPubkey, +}; \ No newline at end of file