Skip to content

Commit

Permalink
Replace mentions with text when forwarding
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Nonnenberg <scott@signal.org>
  • Loading branch information
automated-signal and scottnonnenberg-signal committed Jul 21, 2022
1 parent ccc7acf commit b99d401
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
3 changes: 2 additions & 1 deletion ts/state/selectors/message.ts
Expand Up @@ -38,6 +38,7 @@ import type { EmbeddedContactType } from '../../types/EmbeddedContact';
import { embeddedContactSelector } from '../../types/EmbeddedContact';
import type { AssertProps, BodyRangesType } from '../../types/Util';
import type { LinkPreviewType } from '../../types/message/LinkPreviews';
import { getMentionsRegex } from '../../types/Message';
import { CallMode } from '../../types/Calling';
import { SignalService as Proto } from '../../protobuf';
import type { AttachmentType } from '../../types/Attachment';
Expand Down Expand Up @@ -705,7 +706,7 @@ function getTextAttachment(
}

export function cleanBodyForDirectionCheck(text: string): string {
const MENTIONS_REGEX = /\uFFFC/g;
const MENTIONS_REGEX = getMentionsRegex();
const EMOJI_REGEX = emojiRegex();
const initial = text.replace(MENTIONS_REGEX, '').replace(EMOJI_REGEX, '');

Expand Down
34 changes: 32 additions & 2 deletions ts/state/smart/ForwardMessageModal.tsx
Expand Up @@ -10,7 +10,11 @@ import * as log from '../../logging/log';
import { ForwardMessageModal } from '../../components/ForwardMessageModal';
import { LinkPreviewSourceType } from '../../types/LinkPreview';
import { ToastMessageBodyTooLong } from '../../components/ToastMessageBodyTooLong';
import { getAllComposableConversations } from '../selectors/conversations';
import type { GetConversationByIdType } from '../selectors/conversations';
import {
getAllComposableConversations,
getConversationSelector,
} from '../selectors/conversations';
import { getEmojiSkinTone } from '../selectors/items';
import { getIntl, getTheme, getRegionCode } from '../selectors/user';
import { getLinkPreview } from '../selectors/linkPreviews';
Expand All @@ -27,6 +31,29 @@ import { useActions as useEmojiActions } from '../ducks/emojis';
import { useActions as useItemsActions } from '../ducks/items';
import { useGlobalModalActions } from '../ducks/globalModals';
import { useLinkPreviewActions } from '../ducks/linkPreviews';
import { processBodyRanges } from '../selectors/message';
import { getTextWithMentions } from '../../util/getTextWithMentions';

function renderMentions(
message: ForwardMessagePropsType,
conversationSelector: GetConversationByIdType
): string | undefined {
const { text } = message;

if (!text) {
return text;
}

const bodyRanges = processBodyRanges(message, {
conversationSelector,
});

if (bodyRanges && bodyRanges.length) {
return getTextWithMentions(bodyRanges, text);
}

return text;
}

export function SmartForwardMessageModal(): JSX.Element | null {
const forwardMessageProps = useSelector<
Expand All @@ -35,6 +62,7 @@ export function SmartForwardMessageModal(): JSX.Element | null {
>(state => state.globalModals.forwardMessageProps);
const candidateConversations = useSelector(getAllComposableConversations);
const getPreferredBadge = useSelector(getPreferredBadgeSelector);
const getConversation = useSelector(getConversationSelector);
const i18n = useSelector(getIntl);
const linkPreviewForSource = useSelector(getLinkPreview);
const recentEmojis = useSelector(selectRecentEmojis);
Expand All @@ -58,6 +86,8 @@ export function SmartForwardMessageModal(): JSX.Element | null {
toggleForwardMessageModal();
}

const cleanedBody = renderMentions(forwardMessageProps, getConversation);

return (
<ForwardMessageModal
attachments={attachments}
Expand Down Expand Up @@ -96,7 +126,7 @@ export function SmartForwardMessageModal(): JSX.Element | null {
linkPreview={linkPreviewForSource(
LinkPreviewSourceType.ForwardMessageModal
)}
messageBody={forwardMessageProps.text}
messageBody={cleanedBody}
onClose={closeModal}
onEditorStateChange={(
messageText: string,
Expand Down
4 changes: 4 additions & 0 deletions ts/types/Message.ts
Expand Up @@ -7,6 +7,10 @@ import type { AttachmentType } from './Attachment';
import type { EmbeddedContactType } from './EmbeddedContact';
import type { IndexableBoolean, IndexablePresence } from './IndexedDB';

export function getMentionsRegex(): RegExp {
return /\uFFFC/g;
}

export type Message = (
| UserMessage
| VerifiedChangeMessage
Expand Down
2 changes: 1 addition & 1 deletion ts/types/VisualAttachment.ts
Expand Up @@ -127,7 +127,7 @@ export function makeVideoScreenshot({
strictAssert(context, 'Failed to get canvas context');
context.drawImage(video, 0, 0, canvas.width, canvas.height);

video.addEventListener('loadeddata', seek);
video.removeEventListener('loadeddata', seek);
video.removeEventListener('seeked', capture);

try {
Expand Down

0 comments on commit b99d401

Please sign in to comment.