From e32f20d8bc04cec3abe52f1d4db84f163bbd8010 Mon Sep 17 00:00:00 2001 From: warrickct Date: Fri, 26 Nov 2021 13:20:03 +1100 Subject: [PATCH] PR changes --- ts/components/LeftPane.tsx | 4 +- .../session/LeftPaneMessageSection.tsx | 13 ++- .../session/SessionClosableOverlay.tsx | 18 ++-- ts/interactions/conversationInteractions.ts | 11 ++- ts/models/conversation.ts | 2 +- ts/receiver/callMessage.ts | 2 +- ts/receiver/configMessage.ts | 12 +-- ts/state/selectors/conversations.ts | 82 ++++++------------- .../unit/selectors/conversations_test.ts | 10 +-- 9 files changed, 66 insertions(+), 88 deletions(-) diff --git a/ts/components/LeftPane.tsx b/ts/components/LeftPane.tsx index 19ca70545..194f00a29 100644 --- a/ts/components/LeftPane.tsx +++ b/ts/components/LeftPane.tsx @@ -11,6 +11,7 @@ import { useSelector } from 'react-redux'; import { getLeftPaneLists } from '../state/selectors/conversations'; import { getQuery, getSearchResults, isSearching } from '../state/selectors/search'; import { SectionType } from '../state/ducks/section'; +import { getIsMessageRequestsEnabled } from '../state/selectors/userConfig'; // from https://github.com/bvaughn/react-virtualized/blob/fb3484ed5dcc41bffae8eab029126c0fb8f7abc0/source/List/types.js#L5 export type RowRendererParamsType = { @@ -29,14 +30,15 @@ const InnerLeftPaneMessageSection = () => { const searchResults = showSearch ? useSelector(getSearchResults) : undefined; const lists = showSearch ? undefined : useSelector(getLeftPaneLists); + const messageRequestsEnabled = useSelector(getIsMessageRequestsEnabled); - // tslint:disable: use-simple-attributes return ( ); }; diff --git a/ts/components/session/LeftPaneMessageSection.tsx b/ts/components/session/LeftPaneMessageSection.tsx index 4c2c56b00..6c3746d4b 100644 --- a/ts/components/session/LeftPaneMessageSection.tsx +++ b/ts/components/session/LeftPaneMessageSection.tsx @@ -38,6 +38,8 @@ export interface Props { contacts: Array; conversations?: Array; searchResults?: SearchResultsProps; + + messageRequestsEnabled?: boolean; } export enum SessionComposeToType { @@ -84,7 +86,7 @@ export class LeftPaneMessageSection extends React.Component { const conversation = conversations[index]; if (!conversation) { - window?.log?.info('No conversation found at index'); + throw new Error('renderRow: conversations selector returned element containing falsy value.'); return null; } @@ -222,12 +224,9 @@ export class LeftPaneMessageSection extends React.Component { * @returns void */ private async handleBlockAllRequestsClick() { - let messageRequestsEnabled = false; - if (window?.inboxStore?.getState()) { - messageRequestsEnabled = - window.inboxStore?.getState().userConfig.messageRequests === true && - window.lokiFeatureFlags?.useMessageRequests === true; - } + const messageRequestsEnabled = + this.props.messageRequestsEnabled && window?.lokiFeatureFlags?.useMessageRequests; + if (!messageRequestsEnabled) { return; } diff --git a/ts/components/session/SessionClosableOverlay.tsx b/ts/components/session/SessionClosableOverlay.tsx index 14d6efd72..967dc1b77 100644 --- a/ts/components/session/SessionClosableOverlay.tsx +++ b/ts/components/session/SessionClosableOverlay.tsx @@ -11,10 +11,7 @@ import { SessionJoinableRooms } from './SessionJoinableDefaultRooms'; import { SpacerLG, SpacerMD } from '../basic/Text'; import { useSelector } from 'react-redux'; import { getConversationRequests } from '../../state/selectors/conversations'; -import { - ConversationListItemProps, - MemoConversationListItemWithDetails, -} from '../ConversationListItem'; +import { MemoConversationListItemWithDetails } from '../ConversationListItem'; export enum SessionClosableOverlayType { Message = 'message', @@ -299,13 +296,14 @@ const MessageRequestList = () => { return (
{conversationRequests.map(conversation => { - return ; + return ( + + ); })}
); }; - -const MessageRequestListItem = (props: { conversation: ConversationListItemProps }) => { - const { conversation } = props; - return ; -}; diff --git a/ts/interactions/conversationInteractions.ts b/ts/interactions/conversationInteractions.ts index 247cb3537..fad5af236 100644 --- a/ts/interactions/conversationInteractions.ts +++ b/ts/interactions/conversationInteractions.ts @@ -122,8 +122,17 @@ export async function unblockConvoById(conversationId: string) { */ export const approveConversation = async (conversationId: string) => { const conversationToApprove = await getConversationById(conversationId); + + if (!conversationToApprove || conversationToApprove.isApproved()) { + window?.log?.info('Conversation is already approved.'); + return; + } + await conversationToApprove?.setIsApproved(true); - await forceSyncConfigurationNowIfNeeded(); + + if (conversationToApprove?.isApproved() === true) { + await forceSyncConfigurationNowIfNeeded(); + } }; export async function showUpdateGroupNameByConvoId(conversationId: string) { diff --git a/ts/models/conversation.ts b/ts/models/conversation.ts index 507b0f1d5..7a95a9979 100644 --- a/ts/models/conversation.ts +++ b/ts/models/conversation.ts @@ -738,7 +738,7 @@ export class ConversationModel extends Backbone.Model { !this.isApproved() && (this.isPrivate() || this.isMediumGroup() || this.isClosedGroup()); if (updateApprovalNeeded) { await this.setIsApproved(true); - await forceSyncConfigurationNowIfNeeded(); + void forceSyncConfigurationNowIfNeeded(); } if (this.isOpenGroupV2()) { diff --git a/ts/receiver/callMessage.ts b/ts/receiver/callMessage.ts index b1f387262..cbf0d4891 100644 --- a/ts/receiver/callMessage.ts +++ b/ts/receiver/callMessage.ts @@ -2,7 +2,7 @@ import _ from 'lodash'; import { SignalService } from '../protobuf'; import { TTL_DEFAULT } from '../session/constants'; import { SNodeAPI } from '../session/snode_api'; -import { CallManager } from '../session/utils'; +import { CallManager, UserUtils } from '../session/utils'; import { removeFromCache } from './cache'; import { EnvelopePlus } from './types'; diff --git a/ts/receiver/configMessage.ts b/ts/receiver/configMessage.ts index b091343b3..e396d76a1 100644 --- a/ts/receiver/configMessage.ts +++ b/ts/receiver/configMessage.ts @@ -54,11 +54,6 @@ async function handleGroupsAndContactsFromConfigMessage( envelope: EnvelopePlus, configMessage: SignalService.ConfigurationMessage ) { - await createOrUpdateItem({ - id: 'hasSyncedInitialConfigurationItem', - value: true, - }); - const didWeHandleAConfigurationMessageAlready = (await getItemById(hasSyncedInitialConfigurationItem))?.value || false; if (didWeHandleAConfigurationMessageAlready) { @@ -71,6 +66,11 @@ async function handleGroupsAndContactsFromConfigMessage( return; } + await createOrUpdateItem({ + id: 'hasSyncedInitialConfigurationItem', + value: true, + }); + const numberClosedGroup = configMessage.closedGroups?.length || 0; window?.log?.info( @@ -152,7 +152,7 @@ const handleContactReceived = async ( } } - await updateProfileOneAtATime(contactConvo, profile, contactReceived.profileKey); + void updateProfileOneAtATime(contactConvo, profile, contactReceived.profileKey); } catch (e) { window?.log?.warn('failed to handle a new closed group from configuration message'); } diff --git a/ts/state/selectors/conversations.ts b/ts/state/selectors/conversations.ts index d508f1fbf..07927f2fd 100644 --- a/ts/state/selectors/conversations.ts +++ b/ts/state/selectors/conversations.ts @@ -331,54 +331,21 @@ export const getConversationComparator = createSelector(getIntl, _getConversatio // export only because we use it in some of our tests // tslint:disable-next-line: cyclomatic-complexity export const _getLeftPaneLists = ( - lookup: ConversationLookupType, - comparator: (left: ReduxConversationType, right: ReduxConversationType) => number, - isMessageRequestEnabled?: boolean, - selectedConversation?: string + sortedConversations: Array, + isMessageRequestEnabled?: boolean ): { conversations: Array; contacts: Array; unreadCount: number; } => { - const values = Object.values(lookup); - const sorted = values.sort(comparator); - const conversations: Array = []; const directConversations: Array = []; let unreadCount = 0; - for (let conversation of sorted) { - if (selectedConversation === conversation.id) { - conversation = { - ...conversation, - isSelected: true, - }; - } - const isBlocked = - BlockedNumberController.isBlocked(conversation.id) || - BlockedNumberController.isGroupBlocked(conversation.id); - - if (isBlocked) { - conversation = { - ...conversation, - isBlocked: true, - }; - } - + for (const conversation of sortedConversations) { const excludeUnapproved = isMessageRequestEnabled && window.lokiFeatureFlags?.useMessageRequests; - // Add Open Group to list as soon as the name has been set - if (conversation.isPublic && (!conversation.name || conversation.name === 'Unknown group')) { - continue; - } - - // Remove all invalid conversations and conversatons of devices associated - // with cancelled attempted links - if (!conversation.isPublic && !conversation.activeAt) { - continue; - } - if (conversation.activeAt !== undefined && conversation.type === ConversationTypeEnum.PRIVATE) { directConversations.push(conversation); } @@ -407,7 +374,7 @@ export const _getLeftPaneLists = ( }; }; -export const _getConversationRequests = ( +export const _getSortedConversations = ( lookup: ConversationLookupType, comparator: (left: ReduxConversationType, right: ReduxConversationType) => number, selectedConversation?: string @@ -415,7 +382,7 @@ export const _getConversationRequests = ( const values = Object.values(lookup); const sorted = values.sort(comparator); - const conversationRequests: Array = []; + const sortedConversations: Array = []; for (let conversation of sorted) { if (selectedConversation === conversation.id) { @@ -436,14 +403,6 @@ export const _getConversationRequests = ( }; } - let messageRequestsEnabled = false; - - if (window?.inboxStore?.getState()) { - messageRequestsEnabled = - window.inboxStore?.getState().userConfig.messageRequests === true && - window.lokiFeatureFlags?.useMessageRequests === true; - } - // Add Open Group to list as soon as the name has been set if (conversation.isPublic && (!conversation.name || conversation.name === 'Unknown group')) { continue; @@ -455,28 +414,39 @@ export const _getConversationRequests = ( continue; } - if (messageRequestsEnabled && !conversation.isApproved && !conversation.isBlocked) { - // dont increase unread counter, don't push to convo list. - conversationRequests.push(conversation); - continue; - } + sortedConversations.push(conversation); } - return conversationRequests; + return sortedConversations; }; -export const getConversationRequests = createSelector( +export const getSortedConversations = createSelector( getConversationLookup, getConversationComparator, getSelectedConversationKey, + _getSortedConversations +); + +export const _getConversationRequests = ( + sortedConversations: Array, + isMessageRequestEnabled?: boolean +): Array => { + const pushToMessageRequests = + isMessageRequestEnabled && window.lokiFeatureFlags?.useMessageRequests; + return _.filter(sortedConversations, conversation => { + return pushToMessageRequests && !conversation.isApproved && !conversation.isBlocked; + }); +}; + +export const getConversationRequests = createSelector( + getSortedConversations, + getIsMessageRequestsEnabled, _getConversationRequests ); export const getLeftPaneLists = createSelector( - getConversationLookup, - getConversationComparator, + getSortedConversations, getIsMessageRequestsEnabled, - getSelectedConversationKey, _getLeftPaneLists ); diff --git a/ts/test/session/unit/selectors/conversations_test.ts b/ts/test/session/unit/selectors/conversations_test.ts index 30e5bfb89..d3b8c9677 100644 --- a/ts/test/session/unit/selectors/conversations_test.ts +++ b/ts/test/session/unit/selectors/conversations_test.ts @@ -4,11 +4,11 @@ import { ConversationTypeEnum } from '../../../../models/conversation'; import { ConversationLookupType } from '../../../../state/ducks/conversations'; import { _getConversationComparator, - _getLeftPaneLists, + _getSortedConversations, } from '../../../../state/selectors/conversations'; describe('state/selectors/conversations', () => { - describe('#getLeftPaneList', () => { + describe('#getSortedConversationsList', () => { // tslint:disable-next-line: max-func-body-length it('sorts conversations based on timestamp then by intl-friendly title', () => { const i18n = (key: string) => key; @@ -160,7 +160,7 @@ describe('state/selectors/conversations', () => { }, }; const comparator = _getConversationComparator(i18n); - const { conversations } = _getLeftPaneLists(data, comparator); + const conversations = _getSortedConversations(data, comparator); assert.strictEqual(conversations[0].name, 'First!'); assert.strictEqual(conversations[1].name, 'Á'); @@ -169,7 +169,7 @@ describe('state/selectors/conversations', () => { }); }); - describe('#getLeftPaneListWithPinned', () => { + describe('#getSortedConversationsWithPinned', () => { // tslint:disable-next-line: max-func-body-length it('sorts conversations based on pin, timestamp then by intl-friendly title', () => { const i18n = (key: string) => key; @@ -325,7 +325,7 @@ describe('state/selectors/conversations', () => { }, }; const comparator = _getConversationComparator(i18n); - const { conversations } = _getLeftPaneLists(data, comparator); + const conversations = _getSortedConversations(data, comparator); assert.strictEqual(conversations[0].name, 'Á'); assert.strictEqual(conversations[1].name, 'C');