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
pull/2757/head
William Grant 2 years ago
parent 9b110df1aa
commit e1a6f8e3fc

@ -60,7 +60,7 @@ import { FixedBaseEmoji } from '../../../types/Reaction';
export interface ReplyingToMessageProps { export interface ReplyingToMessageProps {
convoId: string; convoId: string;
id: string; // this is the message timestamp id: string; // this is the quoted message timestamp
author: string; author: string;
timestamp: number; timestamp: number;
text?: string; text?: string;

@ -2,7 +2,6 @@ import React, { useCallback } from 'react';
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
import _, { isEmpty } from 'lodash'; import _, { isEmpty } from 'lodash';
import { MessageModelType, MessageRenderingProps } from '../../../../models/messageType'; import { MessageModelType, MessageRenderingProps } from '../../../../models/messageType';
import { PubKey } from '../../../../session/types';
import { openConversationToSpecificMessage } from '../../../../state/ducks/conversations'; import { openConversationToSpecificMessage } from '../../../../state/ducks/conversations';
import { import {
getMessageQuoteProps, getMessageQuoteProps,
@ -39,12 +38,10 @@ export const MessageQuote = (props: Props) => {
} }
const quoteNotFound = Boolean( 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 quoteText = quote?.text || null;
const shortenedPubkey = quote?.sender ? PubKey.shorten(quote?.sender) : undefined;
const displayedPubkey = String(quote?.authorProfileName ? shortenedPubkey : quote?.sender);
const onQuoteClick = useCallback( const onQuoteClick = useCallback(
async (event: React.MouseEvent<HTMLDivElement>) => { async (event: React.MouseEvent<HTMLDivElement>) => {
@ -70,7 +67,7 @@ export const MessageQuote = (props: Props) => {
} else { } else {
void openConversationToSpecificMessage({ void openConversationToSpecificMessage({
conversationKey: String(quote.convoId), conversationKey: String(quote.convoId),
messageIdToNavigateTo: String(quote.messageId), messageIdToNavigateTo: String(quote.id),
shouldHighlightMessage: true, shouldHighlightMessage: true,
}); });
} }
@ -84,11 +81,10 @@ export const MessageQuote = (props: Props) => {
text={quoteText} text={quoteText}
attachment={quote?.attachment} attachment={quote?.attachment}
isIncoming={direction === 'incoming'} isIncoming={direction === 'incoming'}
sender={displayedPubkey} author={quote.author}
authorProfileName={quote?.authorProfileName}
authorName={quote?.authorName} authorName={quote?.authorName}
referencedMessageNotFound={quoteNotFound} referencedMessageNotFound={quoteNotFound}
isFromMe={quote?.isFromMe || false} isFromMe={Boolean(quote.isFromMe)}
/> />
); );
}; };

@ -48,7 +48,7 @@ const StyledQuoteTextContent = styled.div`
export type QuoteProps = { export type QuoteProps = {
attachment?: QuotedAttachmentType; attachment?: QuotedAttachmentType;
sender: string; author: string;
authorProfileName?: string; authorProfileName?: string;
authorName?: string; authorName?: string;
isFromMe: boolean; isFromMe: boolean;
@ -97,13 +97,11 @@ export const Quote = (props: QuoteProps) => {
/> />
<StyledQuoteTextContent> <StyledQuoteTextContent>
<QuoteAuthor <QuoteAuthor
sender={props.sender} author={props.author}
authorName={props.authorName} authorName={props.authorName}
authorProfileName={props.authorProfileName}
isFromMe={props.isFromMe} isFromMe={props.isFromMe}
isIncoming={isIncoming} isIncoming={isIncoming}
showPubkeyForAuthor={isPublic} showPubkeyForAuthor={isPublic}
referencedMessageNotFound={referencedMessageNotFound}
/> />
<QuoteText <QuoteText
isIncoming={isIncoming} isIncoming={isIncoming}

@ -21,30 +21,15 @@ const StyledQuoteAuthor = styled.div<{ isIncoming: boolean }>`
} }
`; `;
type QuoteAuthorProps = Pick< type QuoteAuthorProps = Pick<QuoteProps, 'author' | 'authorName' | 'isFromMe' | 'isIncoming'> & {
QuoteProps, showPubkeyForAuthor: boolean;
| 'authorName'
| 'authorProfileName'
| 'isFromMe'
| 'isIncoming'
| 'referencedMessageNotFound'
| 'sender'
> & {
showPubkeyForAuthor?: boolean;
}; };
export const QuoteAuthor = (props: QuoteAuthorProps) => { export const QuoteAuthor = (props: QuoteAuthorProps) => {
const { const { author, authorName, isFromMe, isIncoming, showPubkeyForAuthor } = props;
authorProfileName, debugger;
authorName,
isFromMe,
isIncoming,
referencedMessageNotFound,
sender,
showPubkeyForAuthor,
} = props;
if (referencedMessageNotFound) { if (author === '' || authorName === '') {
return null; return null;
} }
@ -54,11 +39,10 @@ export const QuoteAuthor = (props: QuoteAuthorProps) => {
window.i18n('you') window.i18n('you')
) : ( ) : (
<ContactName <ContactName
pubkey={PubKey.shorten(sender)} pubkey={PubKey.shorten(author)}
name={authorName} name={authorName}
profileName={authorProfileName}
compact={true} compact={true}
shouldShowPubkey={Boolean(showPubkeyForAuthor)} shouldShowPubkey={showPubkeyForAuthor}
/> />
)} )}
</StyledQuoteAuthor> </StyledQuoteAuthor>

@ -43,6 +43,7 @@ import {
PropsForGroupUpdateLeft, PropsForGroupUpdateLeft,
PropsForGroupUpdateName, PropsForGroupUpdateName,
PropsForMessageWithoutConvoProps, PropsForMessageWithoutConvoProps,
PropsForQuote,
} from '../state/ducks/conversations'; } from '../state/ducks/conversations';
import { import {
VisibleMessage, VisibleMessage,
@ -87,7 +88,6 @@ import { LinkPreviews } from '../util/linkPreviews';
import { roomHasBlindEnabled } from '../session/apis/open_group_api/sogsv3/sogsV3Capabilities'; import { roomHasBlindEnabled } from '../session/apis/open_group_api/sogsv3/sogsV3Capabilities';
import { getNowWithNetworkOffset } from '../session/apis/snode_api/SNodeAPI'; import { getNowWithNetworkOffset } from '../session/apis/snode_api/SNodeAPI';
import { import {
findCachedBlindedIdFromUnblinded,
getUsBlindedInThatServer, getUsBlindedInThatServer,
isUsAnySogsFromCache, isUsAnySogsFromCache,
} from '../session/apis/open_group_api/sogsv3/knownBlindedkeys'; } from '../session/apis/open_group_api/sogsv3/knownBlindedkeys';
@ -459,7 +459,7 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
} }
// tslint:disable-next-line: cyclomatic-complexity // tslint:disable-next-line: cyclomatic-complexity
public getPropsForMessage(options: any = {}): PropsForMessageWithoutConvoProps { public getPropsForMessage(): PropsForMessageWithoutConvoProps {
const sender = this.getSource(); const sender = this.getSource();
const expirationLength = this.get('expireTimer') * 1000; const expirationLength = this.get('expireTimer') * 1000;
const expireTimerStart = this.get('expirationStartTimestamp'); const expireTimerStart = this.get('expirationStartTimestamp');
@ -520,7 +520,7 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
if (reacts && Object.keys(reacts).length) { if (reacts && Object.keys(reacts).length) {
props.reacts = reacts; props.reacts = reacts;
} }
const quote = this.getPropsForQuote(options); const quote = this.getPropsForQuote();
if (quote) { if (quote) {
props.quote = quote; props.quote = quote;
} }
@ -566,72 +566,8 @@ export class MessageModel extends Backbone.Model<MessageAttributes> {
return this.get('reacts') || null; return this.get('reacts') || null;
} }
public getPropsForQuote(_options: any = {}) { public getPropsForQuote(): PropsForQuote | null {
const quote = this.get('quote'); return this.get('quote') || null;
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 getPropsForAttachment(attachment: AttachmentTypeWithPath): PropsForAttachment | null { public getPropsForAttachment(attachment: AttachmentTypeWithPath): PropsForAttachment | null {

@ -168,10 +168,9 @@ export type PropsForQuote = {
text?: string; text?: string;
attachment?: QuotedAttachmentType; attachment?: QuotedAttachmentType;
isFromMe?: boolean; isFromMe?: boolean;
sender: string; author: string;
authorProfileName?: string;
authorName?: string; authorName?: string;
messageId?: string; id?: string; // this is the quoted message timestamp
referencedMessageNotFound?: boolean; referencedMessageNotFound?: boolean;
convoId?: string; convoId?: string;
}; };

@ -38,11 +38,12 @@ import { ConversationTypeEnum } from '../../models/conversationAttributes';
import { MessageReactsSelectorProps } from '../../components/conversation/message/message-content/MessageReactions'; import { MessageReactsSelectorProps } from '../../components/conversation/message/message-content/MessageReactions';
import { filter, isEmpty, pick, sortBy } from 'lodash'; import { filter, isEmpty, pick, sortBy } from 'lodash';
import { findAndFormatContact, processQuoteAttachment } from '../../models/message'; import { processQuoteAttachment } from '../../models/message';
import { PubKey } from '../../session/types'; import { PubKey } from '../../session/types';
import { OpenGroupData } from '../../data/opengroups'; import { OpenGroupData } from '../../data/opengroups';
import { roomHasBlindEnabled } from '../../session/apis/open_group_api/sogsv3/sogsV3Capabilities'; import { roomHasBlindEnabled } from '../../session/apis/open_group_api/sogsv3/sogsV3Capabilities';
import { findCachedBlindedIdFromUnblinded } from '../../session/apis/open_group_api/sogsv3/knownBlindedkeys'; import { findCachedBlindedIdFromUnblinded } from '../../session/apis/open_group_api/sogsv3/knownBlindedkeys';
import { MessageModelType } from '../../models/messageType';
export const getConversations = (state: StateType): ConversationsStateType => state.conversations; export const getConversations = (state: StateType): ConversationsStateType => state.conversations;
@ -984,45 +985,45 @@ export const getMessageLinkPreviewProps = createSelector(getMessagePropsByMessag
export const getMessageQuoteProps = createSelector( export const getMessageQuoteProps = createSelector(
getConversationQuotes, getConversationQuotes,
getMessagePropsByMessageId, getMessagePropsByMessageId,
(quotesProps, msgProps) => { (quotesProps, msgModel): { direction: MessageModelType; quote: PropsForQuote } | undefined => {
if (!msgProps || isEmpty(msgProps)) { if (!msgModel || isEmpty(msgModel)) {
return undefined; return undefined;
} }
const direction = msgProps.propsForMessage.direction; const msgProps = msgModel.propsForMessage;
if (!msgProps.propsForQuote || isEmpty(msgProps.propsForQuote)) { const direction = msgProps.direction;
if (!msgProps.quote || isEmpty(msgProps.quote)) {
return undefined; return undefined;
} }
const { messageId, sender } = msgProps.propsForQuote; const { id, author, authorName: _authorName } = msgProps.quote;
if (!messageId || !sender) { if (!id || !author) {
return undefined; return undefined;
} }
// NOTE: if the message is not found, we still want to render the quote // NOTE: if the message is not found, we still want to render the quote
if (!quotesProps || isEmpty(quotesProps)) { 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) { if (!sourceMessage) {
return { direction, quote: { sender, referencedMessageNotFound: true } }; return { direction, quote: { author, referencedMessageNotFound: true } };
} }
const sourceMsgProps = sourceMessage.propsForMessage; const sourceMsgProps = sourceMessage.propsForMessage;
if (!sourceMsgProps || sourceMsgProps.isDeleted) { if (!sourceMsgProps || sourceMsgProps.isDeleted) {
return { direction, quote: { sender, referencedMessageNotFound: true } }; return { direction, quote: { author, referencedMessageNotFound: true } };
} }
const convo = getConversationController().get(sourceMsgProps.convoId); const convo = getConversationController().get(sourceMsgProps.convoId);
if (!convo) { 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]; 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)) { if (convo.isPublic() && PubKey.hasBlindedPrefix(sourceMsgProps.sender)) {
const room = OpenGroupData.getV2OpenGroupRoom(sourceMsgProps.convoId); 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 = { const quote: PropsForQuote = {
text: sourceMsgProps.text, text: sourceMsgProps.text,
attachment: attachment ? processQuoteAttachment(attachment) : undefined, attachment: attachment ? processQuoteAttachment(attachment) : undefined,
isFromMe, isFromMe,
sender: sourceMsgProps.sender, author: sourceMsgProps.sender,
authorName, authorName,
messageId: sourceMsgProps.id, id: sourceMsgProps.id,
referencedMessageNotFound: false, referencedMessageNotFound: false,
convoId: convo.id, convoId: convo.id,
}; };

Loading…
Cancel
Save