From a962ef22280980de74040d5bc2058642f60601b5 Mon Sep 17 00:00:00 2001 From: William Grant Date: Tue, 2 May 2023 17:26:44 +1000 Subject: [PATCH] feat: getMessageQuoteProps now loads data from conversation.quotes state and loads correctly into the Quote --- ts/models/message.ts | 48 +++++++++++++++++++++++++++++ ts/state/selectors/conversations.ts | 43 ++++++++++++++++++++------ 2 files changed, 81 insertions(+), 10 deletions(-) diff --git a/ts/models/message.ts b/ts/models/message.ts index 6fa9704c7..82fb208c1 100644 --- a/ts/models/message.ts +++ b/ts/models/message.ts @@ -636,6 +636,7 @@ export class MessageModel extends Backbone.Model { if (quote.text) { // do not show text of not found messages. // if the message was deleted better not show it's text content in the message + // TODO this will be where we show message not found. quoteProps.text = sliceQuoteText(quote.text); } @@ -1408,3 +1409,50 @@ const updatesToDispatch: Map = new M export class MessageCollection extends Backbone.Collection {} MessageCollection.prototype.model = MessageModel; + +// TODO rename and consolidate with getPropsForQuote +export function verifyQuote( + convo: ConversationModel, + msg: PropsForMessageWithoutConvoProps | undefined +) { + const referencedMessageNotFound = Boolean(msg); + const authorName = convo ? convo.getContactProfileNameOrShortenedPubKey() : null; + let isFromMe = convo ? convo.id === UserUtils.getOurPubKeyStrFromCache() : false; + + // NOTE follows PropsForQuote except the sender can be undefined + let quoteProps: any = { + authorName: authorName || 'Unknown', + isFromMe, + referencedMessageNotFound, + }; + + if (!msg) { + return quoteProps; + } + + const id = msg.id; + const author = msg.sender; + + if (convo?.isPublic() && PubKey.hasBlindedPrefix(author)) { + const room = OpenGroupData.getV2OpenGroupRoom(msg.convoId); + if (room && roomHasBlindEnabled(room)) { + const usFromCache = findCachedBlindedIdFromUnblinded( + UserUtils.getOurPubKeyStrFromCache(), + room.serverPublicKey + ); + if (usFromCache && usFromCache === author) { + isFromMe = true; + } + } + } + + const attachment = msg.attachments && msg.attachments[0]; + quoteProps = { + ...quoteProps, + sender: author, + messageId: id, + attachment: referencedMessageNotFound && attachment ? attachment : undefined, + }; + + return quoteProps; +} diff --git a/ts/state/selectors/conversations.ts b/ts/state/selectors/conversations.ts index 499dd450d..be3a5d48a 100644 --- a/ts/state/selectors/conversations.ts +++ b/ts/state/selectors/conversations.ts @@ -8,6 +8,7 @@ import { MessageModelPropsWithConvoProps, MessageModelPropsWithoutConvoProps, MessagePropsDetails, + PropsForQuote, ReduxConversationType, SortedMessageModelProps, } from '../ducks/conversations'; @@ -25,7 +26,6 @@ import { MessageContentSelectorProps } from '../../components/conversation/messa import { MessageContentWithStatusSelectorProps } from '../../components/conversation/message/message-content/MessageContentWithStatus'; import { MessageContextMenuSelectorProps } from '../../components/conversation/message/message-content/MessageContextMenu'; import { MessageLinkPreviewSelectorProps } from '../../components/conversation/message/message-content/MessageLinkPreview'; -import { MessageQuoteSelectorProps } from '../../components/conversation/message/message-content/MessageQuote'; import { MessageStatusSelectorProps } from '../../components/conversation/message/message-content/MessageStatus'; import { MessageTextSelectorProps } from '../../components/conversation/message/message-content/MessageText'; import { GenericReadableMessageSelectorProps } from '../../components/conversation/message/message-item/GenericReadableMessage'; @@ -37,6 +37,7 @@ import { ConversationTypeEnum } from '../../models/conversationAttributes'; import { MessageReactsSelectorProps } from '../../components/conversation/message/message-content/MessageReactions'; import { filter, isEmpty, pick, sortBy } from 'lodash'; +import { verifyQuote } from '../../models/message'; export const getConversations = (state: StateType): ConversationsStateType => state.conversations; @@ -967,17 +968,39 @@ export const getMessageLinkPreviewProps = createSelector(getMessagePropsByMessag return msgProps; }); -export const getMessageQuoteProps = createSelector(getMessagePropsByMessageId, (props): - | MessageQuoteSelectorProps - | undefined => { - if (!props || isEmpty(props)) { - return undefined; - } +export const getMessageQuoteProps = createSelector( + getConversations, + getMessagePropsByMessageId, + (convosProps, msgProps) => { + if (!convosProps || isEmpty(convosProps) || !msgProps || isEmpty(msgProps)) { + return undefined; + } - const msgProps: MessageQuoteSelectorProps = pick(props.propsForMessage, ['direction', 'quote']); + const direction = msgProps.propsForMessage.direction; + const quote = msgProps.propsForQuote; + if (!direction || !quote || isEmpty(quote)) { + return undefined; + } - return msgProps; -}); + const { messageId, sender } = quote; + if (!messageId || !sender) { + return undefined; + } + + let quoteProps: PropsForQuote = quote; + + const conversationId = convosProps.selectedConversation; + const sourceMessage = convosProps.quotes[`${messageId}-${sender}`].propsForMessage; + if (conversationId && sourceMessage) { + const convo = getConversationController().get(conversationId); + quoteProps = verifyQuote(convo, sourceMessage); + } + + window.log.debug(`WIP: quoteProps`, quoteProps); + + return { direction, quote: quoteProps }; + } +); export const getMessageStatusProps = createSelector(getMessagePropsByMessageId, (props): | MessageStatusSelectorProps