From 6a62437c3e33c5eb7f90c7fb27b6c27745e1cd0e Mon Sep 17 00:00:00 2001 From: Warrick Corfe-Tan Date: Fri, 12 Nov 2021 13:29:35 +1100 Subject: [PATCH] Blocking, accepting on click and accepting on msg send working across clients. --- app/sql.js | 4 ---- ts/components/ConversationListItem.tsx | 7 ++++--- ts/components/session/LeftPaneMessageSection.tsx | 16 +++++++++++++++- ts/models/conversation.ts | 5 +++++ ts/receiver/configMessage.ts | 1 + ts/receiver/contentMessage.ts | 1 - ts/receiver/dataMessage.ts | 9 +++++++-- ts/receiver/queuedJob.ts | 3 +++ ts/receiver/receiver.ts | 9 ++++----- ts/session/utils/syncUtils.ts | 6 ++++-- 10 files changed, 43 insertions(+), 18 deletions(-) diff --git a/app/sql.js b/app/sql.js index 819cc6a4c..76ae257a7 100644 --- a/app/sql.js +++ b/app/sql.js @@ -1622,10 +1622,6 @@ function updateConversation(data) { profileName, } = data; - // TODO: msgreq - remove - console.log({ usrData: data }); - console.log('usrData@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'); - globalInstance .prepare( `UPDATE ${CONVERSATIONS_TABLE} SET diff --git a/ts/components/ConversationListItem.tsx b/ts/components/ConversationListItem.tsx index 6af908935..9decf0223 100644 --- a/ts/components/ConversationListItem.tsx +++ b/ts/components/ConversationListItem.tsx @@ -30,7 +30,7 @@ import { ConversationNotificationSettingType } from '../models/conversation'; import { Flex } from './basic/Flex'; import { SessionButton, SessionButtonColor } from './session/SessionButton'; import { getConversationById } from '../data/data'; -import { syncConfigurationIfNeeded } from '../session/utils/syncUtils'; +import { forceSyncConfigurationNowIfNeeded } from '../session/utils/syncUtils'; import { BlockedNumberController } from '../util'; // tslint:disable-next-line: no-empty-interface @@ -294,7 +294,7 @@ const ConversationListItem = (props: Props) => { window?.log?.error('Unable to find conversation to be blocked.'); } await BlockedNumberController.block(convoToBlock?.id); - await syncConfigurationIfNeeded(true); + await forceSyncConfigurationNowIfNeeded(); }; /** @@ -306,7 +306,8 @@ const ConversationListItem = (props: Props) => { console.warn({ convoAfterSetIsApproved: conversationToApprove }); // TODO: Send sync message to other devices. Using config message - await syncConfigurationIfNeeded(true); + // await syncConfigurationIfNeeded(true); + await forceSyncConfigurationNowIfNeeded(); }; return ( diff --git a/ts/components/session/LeftPaneMessageSection.tsx b/ts/components/session/LeftPaneMessageSection.tsx index 9fa401bdc..68a3b818d 100644 --- a/ts/components/session/LeftPaneMessageSection.tsx +++ b/ts/components/session/LeftPaneMessageSection.tsx @@ -30,6 +30,7 @@ import { clearSearch, search, updateSearchTerm } from '../../state/ducks/search' import _ from 'lodash'; import { MessageRequestsBanner } from './MessageRequestsBanner'; import { BlockedNumberController } from '../../util'; +import { forceSyncConfigurationNowIfNeeded } from '../../session/utils/syncUtils'; export interface Props { searchTerm: string; @@ -305,7 +306,20 @@ export class LeftPaneMessageSection extends React.Component { }} onButtonClick={async () => { // TODO: msgrequest iterate all convos and block - console.warn('Test'); + // iterate all conversations and set all to approve then + const allConversations = getConversationController().getConversations(); + let syncRequired = false; + + _.forEach(allConversations, convo => { + if (convo.isApproved() !== true) { + convo.setIsApproved(true); + syncRequired = true; + } + }); + if (syncRequired) { + // syncConfigurationIfNeeded(true); + await forceSyncConfigurationNowIfNeeded(); + } }} searchTerm={searchTerm} searchResults={searchResults} diff --git a/ts/models/conversation.ts b/ts/models/conversation.ts index 6cce5e326..b747c2bd2 100644 --- a/ts/models/conversation.ts +++ b/ts/models/conversation.ts @@ -1161,6 +1161,11 @@ export class ConversationModel extends Backbone.Model { public async addSingleMessage(messageAttributes: MessageAttributesOptionals, setToExpire = true) { const model = new MessageModel(messageAttributes); + const isMe = messageAttributes.source === UserUtils.getOurPubKeyStrFromCache(); + if (isMe) { + await this.setIsApproved(true); + } + // no need to trigger a UI update now, we trigger a messageAdded just below const messageId = await model.commit(false); model.set({ id: messageId }); diff --git a/ts/receiver/configMessage.ts b/ts/receiver/configMessage.ts index e0b0fbb3a..a8d7f0ec6 100644 --- a/ts/receiver/configMessage.ts +++ b/ts/receiver/configMessage.ts @@ -152,6 +152,7 @@ export async function handleConfigurationMessage( envelope: EnvelopePlus, configurationMessage: SignalService.ConfigurationMessage ): Promise { + window?.log?.info('Handling configuration message'); const ourPubkey = UserUtils.getOurPubKeyStrFromCache(); if (!ourPubkey) { return; diff --git a/ts/receiver/contentMessage.ts b/ts/receiver/contentMessage.ts index aec1ebba4..f5422bc33 100644 --- a/ts/receiver/contentMessage.ts +++ b/ts/receiver/contentMessage.ts @@ -377,7 +377,6 @@ export async function innerHandleContentMessage( } if (content.configurationMessage) { // this one can be quite long (downloads profilePictures and everything, is do not block) - console.warn('@@config message received. contentmessage.ts'); void handleConfigurationMessage( envelope, content.configurationMessage as SignalService.ConfigurationMessage diff --git a/ts/receiver/dataMessage.ts b/ts/receiver/dataMessage.ts index aff80bf61..751ffe2db 100644 --- a/ts/receiver/dataMessage.ts +++ b/ts/receiver/dataMessage.ts @@ -32,11 +32,14 @@ export async function updateProfileOneAtATime( } const oneAtaTimeStr = `updateProfileOneAtATime:${conversation.id}`; return allowOnlyOneAtATime(oneAtaTimeStr, async () => { - return updateProfile(conversation, profile, profileKey); + return createOrUpdateProfile(conversation, profile, profileKey); }); } -async function updateProfile( +/** + * Creates a new profile from the profile provided. Creates the profile if it doesn't exist. + */ +async function createOrUpdateProfile( conversation: ConversationModel, profile: SignalService.DataMessage.ILokiProfile, profileKey?: Uint8Array | null // was any @@ -400,6 +403,7 @@ export async function isMessageDuplicate({ return false; } const filteredResult = [result].filter((m: any) => m.attributes.body === message.body); + console.warn({ filteredResult }); return filteredResult.some(m => isDuplicate(m, message, source)); } catch (error) { window?.log?.error('isMessageDuplicate error:', Errors.toLogFormat(error)); @@ -420,6 +424,7 @@ export const isDuplicate = ( Math.abs(m.attributes.sent_at - testedMessage.timestamp) <= PUBLICCHAT_MIN_TIME_BETWEEN_DUPLICATE_MESSAGES; + debugger; return sameUsername && sameText && timestampsSimilar; }; diff --git a/ts/receiver/queuedJob.ts b/ts/receiver/queuedJob.ts index c0a35b5d4..6c185b5d8 100644 --- a/ts/receiver/queuedJob.ts +++ b/ts/receiver/queuedJob.ts @@ -313,6 +313,9 @@ async function handleRegularMessage( if (type === 'outgoing') { handleSyncedReceipts(message, conversation); + + // TODO: Can we assume sync receipts are always from linked device outgoings? + if (dataMessage.body !== 'unapprove') conversation.setIsApproved(true); } const conversationActiveAt = conversation.get('active_at'); diff --git a/ts/receiver/receiver.ts b/ts/receiver/receiver.ts index 601a4f1fd..6a2a342fd 100644 --- a/ts/receiver/receiver.ts +++ b/ts/receiver/receiver.ts @@ -161,12 +161,11 @@ export function handleRequest(body: any, options: ReqOptions, messageHash: strin incomingMessagePromises.push(promise); } -// tslint:enable:cyclomatic-complexity max-func-body-length */ - -// *********************************************************************** -// *********************************************************************** -// *********************************************************************** +// tslint:enable:cyclomatic-complexity max-func-body-length */ +/** + * Used in background.js + */ export async function queueAllCached() { const items = await getAllFromCache(); items.forEach(async item => { diff --git a/ts/session/utils/syncUtils.ts b/ts/session/utils/syncUtils.ts index 43db448c5..52ca3e15a 100644 --- a/ts/session/utils/syncUtils.ts +++ b/ts/session/utils/syncUtils.ts @@ -70,8 +70,9 @@ export const syncConfigurationIfNeeded = async (force: boolean = false) => { }; export const forceSyncConfigurationNowIfNeeded = async (waitForMessageSent = false) => - new Promise(resolve => { - const allConvos = getConversationController().getConversations(); + new Promise(async resolve => { + // const allConvos = getConversationController().getConversations(); + const allConvos = (await getAllConversations()).models; // if we hang for more than 10sec, force resolve this promise. setTimeout(() => { @@ -88,6 +89,7 @@ export const forceSyncConfigurationNowIfNeeded = async (waitForMessageSent = fal resolve(true); } : undefined; + console.warn({configMessage}); void getMessageQueue().sendSyncMessage(configMessage, callback as any); // either we resolve from the callback if we need to wait for it, // or we don't want to wait, we resolve it here.