From e1a6f8e3fc6a00bdcd20c2bbd65f150bfb462646 Mon Sep 17 00:00:00 2001 From: William Grant Date: Mon, 15 May 2023 17:43:58 +1000 Subject: [PATCH] feat: cleaned up getPropsForQuote and rely on getMessageQuoteProps selector for creating quote object authorName is broken for some reason and we will need to fix it --- .../composition/CompositionBox.tsx | 2 +- .../message/message-content/MessageQuote.tsx | 12 +-- .../message/message-content/quote/Quote.tsx | 6 +- .../message-content/quote/QuoteAuthor.tsx | 30 ++------ ts/models/message.ts | 74 ++----------------- ts/state/ducks/conversations.ts | 5 +- ts/state/selectors/conversations.ts | 39 +++++----- 7 files changed, 43 insertions(+), 125 deletions(-) diff --git a/ts/components/conversation/composition/CompositionBox.tsx b/ts/components/conversation/composition/CompositionBox.tsx index 91373c867..9254c4bfa 100644 --- a/ts/components/conversation/composition/CompositionBox.tsx +++ b/ts/components/conversation/composition/CompositionBox.tsx @@ -60,7 +60,7 @@ import { FixedBaseEmoji } from '../../../types/Reaction'; export interface ReplyingToMessageProps { convoId: string; - id: string; // this is the message timestamp + id: string; // this is the quoted message timestamp author: string; timestamp: number; text?: string; diff --git a/ts/components/conversation/message/message-content/MessageQuote.tsx b/ts/components/conversation/message/message-content/MessageQuote.tsx index 559cf73e4..eaf3c8386 100644 --- a/ts/components/conversation/message/message-content/MessageQuote.tsx +++ b/ts/components/conversation/message/message-content/MessageQuote.tsx @@ -2,7 +2,6 @@ import React, { useCallback } from 'react'; import { useSelector } from 'react-redux'; import _, { isEmpty } from 'lodash'; import { MessageModelType, MessageRenderingProps } from '../../../../models/messageType'; -import { PubKey } from '../../../../session/types'; import { openConversationToSpecificMessage } from '../../../../state/ducks/conversations'; import { getMessageQuoteProps, @@ -39,12 +38,10 @@ export const MessageQuote = (props: Props) => { } const quoteNotFound = Boolean( - !quote?.sender || !quote.messageId || !quote.convoId || quote.referencedMessageNotFound + !quote?.author || !quote.id || !quote.convoId || quote.referencedMessageNotFound ); const quoteText = quote?.text || null; - const shortenedPubkey = quote?.sender ? PubKey.shorten(quote?.sender) : undefined; - const displayedPubkey = String(quote?.authorProfileName ? shortenedPubkey : quote?.sender); const onQuoteClick = useCallback( async (event: React.MouseEvent) => { @@ -70,7 +67,7 @@ export const MessageQuote = (props: Props) => { } else { void openConversationToSpecificMessage({ conversationKey: String(quote.convoId), - messageIdToNavigateTo: String(quote.messageId), + messageIdToNavigateTo: String(quote.id), shouldHighlightMessage: true, }); } @@ -84,11 +81,10 @@ export const MessageQuote = (props: Props) => { text={quoteText} attachment={quote?.attachment} isIncoming={direction === 'incoming'} - sender={displayedPubkey} - authorProfileName={quote?.authorProfileName} + author={quote.author} authorName={quote?.authorName} referencedMessageNotFound={quoteNotFound} - isFromMe={quote?.isFromMe || false} + isFromMe={Boolean(quote.isFromMe)} /> ); }; diff --git a/ts/components/conversation/message/message-content/quote/Quote.tsx b/ts/components/conversation/message/message-content/quote/Quote.tsx index f28ee40db..e0d3e1ff9 100644 --- a/ts/components/conversation/message/message-content/quote/Quote.tsx +++ b/ts/components/conversation/message/message-content/quote/Quote.tsx @@ -48,7 +48,7 @@ const StyledQuoteTextContent = styled.div` export type QuoteProps = { attachment?: QuotedAttachmentType; - sender: string; + author: string; authorProfileName?: string; authorName?: string; isFromMe: boolean; @@ -97,13 +97,11 @@ export const Quote = (props: QuoteProps) => { /> ` } `; -type QuoteAuthorProps = Pick< - QuoteProps, - | 'authorName' - | 'authorProfileName' - | 'isFromMe' - | 'isIncoming' - | 'referencedMessageNotFound' - | 'sender' -> & { - showPubkeyForAuthor?: boolean; +type QuoteAuthorProps = Pick & { + showPubkeyForAuthor: boolean; }; export const QuoteAuthor = (props: QuoteAuthorProps) => { - const { - authorProfileName, - authorName, - isFromMe, - isIncoming, - referencedMessageNotFound, - sender, - showPubkeyForAuthor, - } = props; + const { author, authorName, isFromMe, isIncoming, showPubkeyForAuthor } = props; + debugger; - if (referencedMessageNotFound) { + if (author === '' || authorName === '') { return null; } @@ -54,11 +39,10 @@ export const QuoteAuthor = (props: QuoteAuthorProps) => { window.i18n('you') ) : ( )} diff --git a/ts/models/message.ts b/ts/models/message.ts index 3adf923c3..a915973b4 100644 --- a/ts/models/message.ts +++ b/ts/models/message.ts @@ -43,6 +43,7 @@ import { PropsForGroupUpdateLeft, PropsForGroupUpdateName, PropsForMessageWithoutConvoProps, + PropsForQuote, } from '../state/ducks/conversations'; import { VisibleMessage, @@ -87,7 +88,6 @@ import { LinkPreviews } from '../util/linkPreviews'; import { roomHasBlindEnabled } from '../session/apis/open_group_api/sogsv3/sogsV3Capabilities'; import { getNowWithNetworkOffset } from '../session/apis/snode_api/SNodeAPI'; import { - findCachedBlindedIdFromUnblinded, getUsBlindedInThatServer, isUsAnySogsFromCache, } from '../session/apis/open_group_api/sogsv3/knownBlindedkeys'; @@ -459,7 +459,7 @@ export class MessageModel extends Backbone.Model { } // tslint:disable-next-line: cyclomatic-complexity - public getPropsForMessage(options: any = {}): PropsForMessageWithoutConvoProps { + public getPropsForMessage(): PropsForMessageWithoutConvoProps { const sender = this.getSource(); const expirationLength = this.get('expireTimer') * 1000; const expireTimerStart = this.get('expirationStartTimestamp'); @@ -520,7 +520,7 @@ export class MessageModel extends Backbone.Model { if (reacts && Object.keys(reacts).length) { props.reacts = reacts; } - const quote = this.getPropsForQuote(options); + const quote = this.getPropsForQuote(); if (quote) { props.quote = quote; } @@ -566,72 +566,8 @@ export class MessageModel extends Backbone.Model { return this.get('reacts') || null; } - public getPropsForQuote(_options: any = {}) { - const quote = this.get('quote'); - - if (!quote) { - return null; - } - - const { author, id, referencedMessageNotFound } = quote; - const contact: ConversationModel = author && getConversationController().get(author); - - const authorName = - contact && contact.isPrivate() ? contact.getContactProfileNameOrShortenedPubKey() : null; - - let isFromMe = contact ? contact.id === UserUtils.getOurPubKeyStrFromCache() : false; - - if (contact?.isPublic() && PubKey.hasBlindedPrefix(author)) { - const room = OpenGroupData.getV2OpenGroupRoom(this.get('conversationId')); - if (room && roomHasBlindEnabled(room)) { - const usFromCache = findCachedBlindedIdFromUnblinded( - UserUtils.getOurPubKeyStrFromCache(), - room.serverPublicKey - ); - if (usFromCache && usFromCache === author) { - isFromMe = true; - } - } - } - - const firstAttachment = quote.attachments && quote.attachments[0]; - const quoteProps: { - referencedMessageNotFound?: boolean; - sender: string; - messageId: string; - authorName: string; - text?: string; - attachment?: any; - isFromMe?: boolean; - } = { - sender: author, - messageId: id, - authorName: authorName || window.i18n('unknown'), - }; - - if (referencedMessageNotFound) { - quoteProps.referencedMessageNotFound = true; - } - - if (!referencedMessageNotFound) { - 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); - } - - const quoteAttachment = firstAttachment ? processQuoteAttachment(firstAttachment) : undefined; - if (quoteAttachment) { - // only set attachment if referencedMessageNotFound is false and we have one - quoteProps.attachment = quoteAttachment; - } - } - if (isFromMe) { - quoteProps.isFromMe = true; - } - - return quoteProps; + public getPropsForQuote(): PropsForQuote | null { + return this.get('quote') || null; } public getPropsForAttachment(attachment: AttachmentTypeWithPath): PropsForAttachment | null { diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts index 816f4aae3..6d72f3c3f 100644 --- a/ts/state/ducks/conversations.ts +++ b/ts/state/ducks/conversations.ts @@ -168,10 +168,9 @@ export type PropsForQuote = { text?: string; attachment?: QuotedAttachmentType; isFromMe?: boolean; - sender: string; - authorProfileName?: string; + author: string; authorName?: string; - messageId?: string; + id?: string; // this is the quoted message timestamp referencedMessageNotFound?: boolean; convoId?: string; }; diff --git a/ts/state/selectors/conversations.ts b/ts/state/selectors/conversations.ts index b5c1c7602..9592f5cf5 100644 --- a/ts/state/selectors/conversations.ts +++ b/ts/state/selectors/conversations.ts @@ -38,11 +38,12 @@ import { ConversationTypeEnum } from '../../models/conversationAttributes'; import { MessageReactsSelectorProps } from '../../components/conversation/message/message-content/MessageReactions'; import { filter, isEmpty, pick, sortBy } from 'lodash'; -import { findAndFormatContact, processQuoteAttachment } from '../../models/message'; +import { processQuoteAttachment } from '../../models/message'; import { PubKey } from '../../session/types'; import { OpenGroupData } from '../../data/opengroups'; import { roomHasBlindEnabled } from '../../session/apis/open_group_api/sogsv3/sogsV3Capabilities'; import { findCachedBlindedIdFromUnblinded } from '../../session/apis/open_group_api/sogsv3/knownBlindedkeys'; +import { MessageModelType } from '../../models/messageType'; export const getConversations = (state: StateType): ConversationsStateType => state.conversations; @@ -984,45 +985,45 @@ export const getMessageLinkPreviewProps = createSelector(getMessagePropsByMessag export const getMessageQuoteProps = createSelector( getConversationQuotes, getMessagePropsByMessageId, - (quotesProps, msgProps) => { - if (!msgProps || isEmpty(msgProps)) { + (quotesProps, msgModel): { direction: MessageModelType; quote: PropsForQuote } | undefined => { + if (!msgModel || isEmpty(msgModel)) { return undefined; } - const direction = msgProps.propsForMessage.direction; - if (!msgProps.propsForQuote || isEmpty(msgProps.propsForQuote)) { + const msgProps = msgModel.propsForMessage; + const direction = msgProps.direction; + + if (!msgProps.quote || isEmpty(msgProps.quote)) { return undefined; } - const { messageId, sender } = msgProps.propsForQuote; - if (!messageId || !sender) { + const { id, author, authorName: _authorName } = msgProps.quote; + if (!id || !author) { return undefined; } // NOTE: if the message is not found, we still want to render the quote if (!quotesProps || isEmpty(quotesProps)) { - return { direction, quote: { sender, referencedMessageNotFound: true } }; + return { direction, quote: { author, referencedMessageNotFound: true } }; } - const sourceMessage = quotesProps[`${messageId}-${sender}`]; + const sourceMessage = quotesProps[`${id}-${author}`]; if (!sourceMessage) { - return { direction, quote: { sender, referencedMessageNotFound: true } }; + return { direction, quote: { author, referencedMessageNotFound: true } }; } const sourceMsgProps = sourceMessage.propsForMessage; if (!sourceMsgProps || sourceMsgProps.isDeleted) { - return { direction, quote: { sender, referencedMessageNotFound: true } }; + return { direction, quote: { author, referencedMessageNotFound: true } }; } const convo = getConversationController().get(sourceMsgProps.convoId); if (!convo) { - return { direction, quote: { sender, referencedMessageNotFound: true } }; + return { direction, quote: { author, referencedMessageNotFound: true } }; } - const contact = findAndFormatContact(sourceMsgProps.sender); - const authorName = contact?.profileName || contact?.name || window.i18n('unknown'); const attachment = sourceMsgProps.attachments && sourceMsgProps.attachments[0]; - let isFromMe = convo ? convo.id === UserUtils.getOurPubKeyStrFromCache() : false; + let isFromMe = convo ? UserUtils.isUsFromCache(author) : false; if (convo.isPublic() && PubKey.hasBlindedPrefix(sourceMsgProps.sender)) { const room = OpenGroupData.getV2OpenGroupRoom(sourceMsgProps.convoId); @@ -1037,13 +1038,17 @@ export const getMessageQuoteProps = createSelector( } } + const authorName = isFromMe + ? window.i18n('you') + : convo.getNicknameOrRealUsernameOrPlaceholder(); + const quote: PropsForQuote = { text: sourceMsgProps.text, attachment: attachment ? processQuoteAttachment(attachment) : undefined, isFromMe, - sender: sourceMsgProps.sender, + author: sourceMsgProps.sender, authorName, - messageId: sourceMsgProps.id, + id: sourceMsgProps.id, referencedMessageNotFound: false, convoId: convo.id, };