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 {
convoId: string;
id: string; // this is the message timestamp
id: string; // this is the quoted message timestamp
author: string;
timestamp: number;
text?: string;

@ -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<HTMLDivElement>) => {
@ -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)}
/>
);
};

@ -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) => {
/>
<StyledQuoteTextContent>
<QuoteAuthor
sender={props.sender}
author={props.author}
authorName={props.authorName}
authorProfileName={props.authorProfileName}
isFromMe={props.isFromMe}
isIncoming={isIncoming}
showPubkeyForAuthor={isPublic}
referencedMessageNotFound={referencedMessageNotFound}
/>
<QuoteText
isIncoming={isIncoming}

@ -21,30 +21,15 @@ const StyledQuoteAuthor = styled.div<{ isIncoming: boolean }>`
}
`;
type QuoteAuthorProps = Pick<
QuoteProps,
| 'authorName'
| 'authorProfileName'
| 'isFromMe'
| 'isIncoming'
| 'referencedMessageNotFound'
| 'sender'
> & {
showPubkeyForAuthor?: boolean;
type QuoteAuthorProps = Pick<QuoteProps, 'author' | 'authorName' | 'isFromMe' | 'isIncoming'> & {
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')
) : (
<ContactName
pubkey={PubKey.shorten(sender)}
pubkey={PubKey.shorten(author)}
name={authorName}
profileName={authorProfileName}
compact={true}
shouldShowPubkey={Boolean(showPubkeyForAuthor)}
shouldShowPubkey={showPubkeyForAuthor}
/>
)}
</StyledQuoteAuthor>

@ -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<MessageAttributes> {
}
// 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<MessageAttributes> {
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<MessageAttributes> {
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 {

@ -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;
};

@ -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,
};

Loading…
Cancel
Save