2022-07-01 00:52:03 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-04-27 22:35:35 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { BodyRangeType } from '../../types/Util';
|
2022-07-01 00:52:03 +00:00
|
|
|
import type { ForwardMessagePropsType } from '../ducks/globalModals';
|
2022-06-17 00:48:57 +00:00
|
|
|
import type { StateType } from '../reducer';
|
2022-07-01 00:52:03 +00:00
|
|
|
import * as log from '../../logging/log';
|
2022-06-17 00:48:57 +00:00
|
|
|
import { ForwardMessageModal } from '../../components/ForwardMessageModal';
|
|
|
|
import { LinkPreviewSourceType } from '../../types/LinkPreview';
|
2022-07-01 00:52:03 +00:00
|
|
|
import { ToastMessageBodyTooLong } from '../../components/ToastMessageBodyTooLong';
|
2022-07-21 16:26:11 +00:00
|
|
|
import type { GetConversationByIdType } from '../selectors/conversations';
|
|
|
|
import {
|
|
|
|
getAllComposableConversations,
|
|
|
|
getConversationSelector,
|
|
|
|
} from '../selectors/conversations';
|
2021-09-09 16:29:01 +00:00
|
|
|
import { getEmojiSkinTone } from '../selectors/items';
|
2022-06-17 00:48:57 +00:00
|
|
|
import { getIntl, getTheme, getRegionCode } from '../selectors/user';
|
|
|
|
import { getLinkPreview } from '../selectors/linkPreviews';
|
2022-07-01 00:52:03 +00:00
|
|
|
import { getMessageById } from '../../messages/getMessageById';
|
2022-06-17 00:48:57 +00:00
|
|
|
import { getPreferredBadgeSelector } from '../selectors/badges';
|
2022-07-01 00:52:03 +00:00
|
|
|
import { maybeForwardMessage } from '../../util/maybeForwardMessage';
|
|
|
|
import {
|
|
|
|
maybeGrabLinkPreview,
|
|
|
|
resetLinkPreview,
|
|
|
|
} from '../../services/LinkPreview';
|
2021-04-27 22:35:35 +00:00
|
|
|
import { selectRecentEmojis } from '../selectors/emojis';
|
2022-07-01 00:52:03 +00:00
|
|
|
import { showToast } from '../../util/showToast';
|
|
|
|
import { useActions as useEmojiActions } from '../ducks/emojis';
|
|
|
|
import { useActions as useItemsActions } from '../ducks/items';
|
|
|
|
import { useGlobalModalActions } from '../ducks/globalModals';
|
|
|
|
import { useLinkPreviewActions } from '../ducks/linkPreviews';
|
2022-07-21 16:26:11 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-04-27 22:35:35 +00:00
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
export function SmartForwardMessageModal(): JSX.Element | null {
|
|
|
|
const forwardMessageProps = useSelector<
|
|
|
|
StateType,
|
|
|
|
ForwardMessagePropsType | undefined
|
|
|
|
>(state => state.globalModals.forwardMessageProps);
|
|
|
|
const candidateConversations = useSelector(getAllComposableConversations);
|
|
|
|
const getPreferredBadge = useSelector(getPreferredBadgeSelector);
|
2022-07-21 16:26:11 +00:00
|
|
|
const getConversation = useSelector(getConversationSelector);
|
2022-07-01 00:52:03 +00:00
|
|
|
const i18n = useSelector(getIntl);
|
|
|
|
const linkPreviewForSource = useSelector(getLinkPreview);
|
|
|
|
const recentEmojis = useSelector(selectRecentEmojis);
|
|
|
|
const regionCode = useSelector(getRegionCode);
|
|
|
|
const skinTone = useSelector(getEmojiSkinTone);
|
|
|
|
const theme = useSelector(getTheme);
|
2021-04-27 22:35:35 +00:00
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
const { removeLinkPreview } = useLinkPreviewActions();
|
|
|
|
const { onUseEmoji: onPickEmoji } = useEmojiActions();
|
|
|
|
const { onSetSkinTone } = useItemsActions();
|
|
|
|
const { toggleForwardMessageModal } = useGlobalModalActions();
|
2021-04-27 22:35:35 +00:00
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
if (!forwardMessageProps) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-04-27 22:35:35 +00:00
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
const { attachments = [] } = forwardMessageProps;
|
2021-04-27 22:35:35 +00:00
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
function closeModal() {
|
|
|
|
resetLinkPreview();
|
|
|
|
toggleForwardMessageModal();
|
|
|
|
}
|
2021-04-27 22:35:35 +00:00
|
|
|
|
2022-07-21 16:26:11 +00:00
|
|
|
const cleanedBody = renderMentions(forwardMessageProps, getConversation);
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
return (
|
|
|
|
<ForwardMessageModal
|
|
|
|
attachments={attachments}
|
|
|
|
candidateConversations={candidateConversations}
|
|
|
|
doForwardMessage={async (
|
|
|
|
conversationIds,
|
|
|
|
messageBody,
|
|
|
|
includedAttachments,
|
|
|
|
linkPreview
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const message = await getMessageById(forwardMessageProps.id);
|
|
|
|
if (!message) {
|
|
|
|
throw new Error('No message found');
|
|
|
|
}
|
|
|
|
|
|
|
|
const didForwardSuccessfully = await maybeForwardMessage(
|
|
|
|
message.attributes,
|
|
|
|
conversationIds,
|
|
|
|
messageBody,
|
|
|
|
includedAttachments,
|
|
|
|
linkPreview
|
|
|
|
);
|
|
|
|
|
|
|
|
if (didForwardSuccessfully) {
|
|
|
|
closeModal();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
log.warn('doForwardMessage', err && err.stack ? err.stack : err);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
getPreferredBadge={getPreferredBadge}
|
|
|
|
hasContact={Boolean(forwardMessageProps.contact)}
|
|
|
|
i18n={i18n}
|
|
|
|
isSticker={Boolean(forwardMessageProps.isSticker)}
|
|
|
|
linkPreview={linkPreviewForSource(
|
|
|
|
LinkPreviewSourceType.ForwardMessageModal
|
|
|
|
)}
|
2022-07-21 16:26:11 +00:00
|
|
|
messageBody={cleanedBody}
|
2022-07-01 00:52:03 +00:00
|
|
|
onClose={closeModal}
|
|
|
|
onEditorStateChange={(
|
|
|
|
messageText: string,
|
|
|
|
_: Array<BodyRangeType>,
|
|
|
|
caretLocation?: number
|
|
|
|
) => {
|
|
|
|
if (!attachments.length) {
|
|
|
|
maybeGrabLinkPreview(
|
|
|
|
messageText,
|
|
|
|
LinkPreviewSourceType.ForwardMessageModal,
|
|
|
|
caretLocation
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onPickEmoji={onPickEmoji}
|
|
|
|
onSetSkinTone={onSetSkinTone}
|
|
|
|
onTextTooLong={() => showToast(ToastMessageBodyTooLong)}
|
|
|
|
recentEmojis={recentEmojis}
|
|
|
|
regionCode={regionCode}
|
|
|
|
removeLinkPreview={removeLinkPreview}
|
|
|
|
skinTone={skinTone}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|