From e72970fdc7e8a04e7ecc896b713310dabac0efbf Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 8 Jul 2020 12:14:46 +1000 Subject: [PATCH] load-conv-public --- _locales/en/messages.json | 5 +- .../session/LeftPaneMessageSection.tsx | 47 ++++++++++++++----- ts/session/types/OpenGroup.ts | 9 ++++ ts/state/selectors/conversations.ts | 6 +++ 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 36fc29438..bc1dfbbbc 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -2194,8 +2194,11 @@ "connectToServerFail": { "message": "Failed to connect to server. Check URL" }, + "connectingToServer": { + "message": "Connecting to server..." + }, "connectToServerSuccess": { - "message": "Successfully connected to new open group server" + "message": "Successfully connected to open group" }, "setPasswordFail": { "message": "Failed to set password" diff --git a/ts/components/session/LeftPaneMessageSection.tsx b/ts/components/session/LeftPaneMessageSection.tsx index d32f819e1..8a27576a7 100644 --- a/ts/components/session/LeftPaneMessageSection.tsx +++ b/ts/components/session/LeftPaneMessageSection.tsx @@ -59,7 +59,6 @@ interface State { loading: boolean; overlay: false | SessionComposeToType; valuePasted: string; - connectSuccess: boolean; } export class LeftPaneMessageSection extends React.Component { @@ -73,7 +72,6 @@ export class LeftPaneMessageSection extends React.Component { loading: false, overlay: false, valuePasted: '', - connectSuccess: false, }; const conversations = this.getCurrentConversations(); @@ -474,25 +472,50 @@ export class LeftPaneMessageSection extends React.Component { } // Connect to server - const successPromise = OpenGroup.join(serverUrl, () => { - this.setState({ loading: true }); + const successPromise = OpenGroup.join(serverUrl, async () => { + if (await OpenGroup.serverExists(serverUrl)) { + window.pushToast({ + title: window.i18n('connectingToServer'), + id: 'connectToServerSuccess', + type: 'success', + }); + + this.setState({loading: true}); + + // Call conversationRenderedResolve when the conversation is rendered. + let conversationRenderedResolve: any; + const conversationRenderedPromise = async () => + // tslint:disable-next-line: promise-must-complete + new Promise(resolve => { + conversationRenderedResolve = resolve; + }); + + // STOP IT AT THE SOURCE; SELECTORS/CONVERSATIONS.TS + const isRenderedInterval = setInterval(async () => { + // The conversation is shown in LeftPane when lastMessage is set. + // Notify the user when we're ready to render in LeftPane. + if ((await OpenGroup.getConversation(serverUrl))?.lastMessage) { + conversationRenderedResolve(); + window.pushToast({ + title: window.i18n('connectToServerFail'), + id: 'connectToServerFail', + type: 'error', + }); + } + }, 100); + + + + } }); successPromise.then(() => { this.handleToggleOverlay(undefined); this.setState({ loading: false, - connectSuccess: true, - }); - - window.pushToast({ - title: window.i18n('connectToServerSuccess'), - id: 'connectToServerSuccess', - type: 'success', }); }).catch(() => { this.setState({ - connectSuccess: false, loading: false, }); diff --git a/ts/session/types/OpenGroup.ts b/ts/session/types/OpenGroup.ts index 44a6cb661..f70ffb934 100644 --- a/ts/session/types/OpenGroup.ts +++ b/ts/session/types/OpenGroup.ts @@ -131,6 +131,15 @@ export class OpenGroup { return serverInfo.channels[0].conversation; } + public static async serverExists(server: string): Promise { + if (!OpenGroup.validate(server)) { + return false; + } + + const prefixedServer = this.prefixify(server); + return Boolean(await window.lokiPublicChatAPI.findOrCreateServer(prefixedServer)); + } + public static getConversationByCID(conversationId: string): any { const { ConversationController } = window; return ConversationController.get(conversationId); diff --git a/ts/state/selectors/conversations.ts b/ts/state/selectors/conversations.ts index 5b896ad6c..9ba5559c9 100644 --- a/ts/state/selectors/conversations.ts +++ b/ts/state/selectors/conversations.ts @@ -120,6 +120,12 @@ export const _getLeftPaneLists = ( }; } + const isPublic = conversation.isPublic; + console.log('[vince] isPublic:', isPublic); + console.log('[vince] isPublic:', isPublic); + console.log('[vince] isPublic:', isPublic); + console.log('[vince] isPublic:', isPublic); + // Remove all invalid conversations and conversatons of devices associated with cancelled attempted links if (!conversation.timestamp) { continue;