From f6ae5386e91e216f679e969f3ee28e3ef9cd3362 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 8 Jul 2020 10:59:20 +1000 Subject: [PATCH] getting opengroup conversation --- _locales/en/messages.json | 6 +- js/modules/loki_public_chat_api.js | 2 - .../session/LeftPaneMessageSection.tsx | 60 +++++++++++++++---- ts/session/types/OpenGroup.ts | 42 +++++++++---- 4 files changed, 82 insertions(+), 28 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 522bc76f4..3f2e59ed6 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -2180,7 +2180,7 @@ "message": "You have removed your password." }, "publicChatExists": { - "message": "You are already connected to this public channel" + "message": "You are already connected to this open group" }, "connectToServerFail": { "message": "Failed to connect to server. Check URL" @@ -2239,6 +2239,10 @@ "message": "Invalid Pubkey Format", "description": "Error string shown when user types an invalid pubkey format" }, + "attemptedConnectionTimeout": { + "message": "Connection to open group timed out", + "description": "Shown in toast when attempted connection to OpenGroup times out" + }, "lnsMappingNotFound": { "message": "There is no LNS mapping associated with this name", "description": "Shown in toast if user enters an unknown LNS name" diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index bbe64b8f6..e37c01f3c 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -7,8 +7,6 @@ const nodeFetch = require('node-fetch'); const validOpenGroupServer = async serverUrl => { // test to make sure it's online (and maybe has a valid SSL cert) try { - console.log('[vince] loki_public_chat_api --> serverUrl:', serverUrl); - const url = new URL(serverUrl); if (window.lokiFeatureFlags.useFileOnionRequests) { diff --git a/ts/components/session/LeftPaneMessageSection.tsx b/ts/components/session/LeftPaneMessageSection.tsx index 951f0d487..4c2d690ee 100644 --- a/ts/components/session/LeftPaneMessageSection.tsx +++ b/ts/components/session/LeftPaneMessageSection.tsx @@ -439,45 +439,79 @@ export class LeftPaneMessageSection extends React.Component { } } - private async handleJoinChannelButtonClick(server: string) { + private async handleJoinChannelButtonClick(serverUrl: string) { const { loading } = this.state; if (loading) { - return false; + return; } - if (!OpenGroup.validate(server)) { + // Server URL entered? + if (serverUrl.length === 0) { window.pushToast({ title: window.i18n('noServerURL'), type: 'error', id: 'connectToServerFail', }); - await OpenGroup.join(server); - + return; + } - if (groupUrl.length <= 0) { + // Server URL valid? + if (!OpenGroup.validate(serverUrl)) { window.pushToast({ title: window.i18n('noServerURL'), - type: 'error', id: 'connectToServerFail', + type: 'error', }); - return false; + return; } + // Already connected? + if (Boolean(await OpenGroup.getConversation(serverUrl))) { + window.pushToast({ + title: window.i18n('publicChatExists'), + id: 'publicChatExists', + type: 'error', + }); - return false; + return; } - + const successPromise = OpenGroup.join(serverUrl); + const timeoutPromise = new Promise((_resolve, reject) => + // tslint:disable-next-line: no-unnecessary-callback-wrapper no-void-expression + setTimeout(() => reject(), window.CONSTANTS.MAX_CONNECTION_DURATION) + ); - MainViewController.joinChannelStateManager(this, groupUrl, () => { + // Connect to server with timeout. + this.setState({ loading: true }); + await Promise.race([successPromise, timeoutPromise]).then(() => { this.handleToggleOverlay(undefined); - }); + this.setState({ + loading: false, + connectSuccess: true, + }); - return true; + window.pushToast({ + title: window.i18n('connectToServerSuccess'), + id: 'connectToServerSuccess', + type: 'success', + }); + }).catch(() => { + this.setState({ + connectSuccess: false, + loading: false, + }); + + window.pushToast({ + title: window.i18n('attemptedConnectionTimeout'), + id: 'attemptedConnectionTimeout', + type: 'error', + }); + }); } private async onCreateClosedGroup( diff --git a/ts/session/types/OpenGroup.ts b/ts/session/types/OpenGroup.ts index 15d836acf..4b679bf18 100644 --- a/ts/session/types/OpenGroup.ts +++ b/ts/session/types/OpenGroup.ts @@ -1,8 +1,6 @@ // This is the Open Group equivalent to the PubKey type. -import LokiPublicChatFactoryAPI from "../../../js/modules/loki_public_chat_api"; -import { UserUtil } from "../../util"; -import { ConversationType } from "../../receiver/common"; +import { LokiPublicChatFactoryInterface } from "../../../js/modules/loki_public_chat_api"; interface OpenGroupParams { server: string; @@ -24,10 +22,6 @@ export class OpenGroup { public readonly groupId?: string; public readonly conversationId: string; // eg. c12 - // The following are set on join() - not required - public connected?: boolean; - public conversation?: ConversationType; - constructor(params: OpenGroupParams) { // https will be prepended unless explicitly http this.server = OpenGroup.prefixify(params.server.toLowerCase()); @@ -92,6 +86,20 @@ export class OpenGroup { const channel = 1; let conversation; let conversationId; + + // Return OpenGroup if we're already connected + conversation = await OpenGroup.getConversation(prefixedServer); + if (conversation) { + conversationId = conversation?.cid; + if (conversationId) { + return new OpenGroup({ + server: prefixedServer, + channel: 1, + conversationId, + }); + } + } + try { conversation = await window.attemptConnection(prefixedServer, channel); conversationId = conversation?.cid; @@ -108,12 +116,24 @@ export class OpenGroup { }); } - public static async isConnected(server: string): Promise { + public static async getConversation(server: string): Promise { if (!OpenGroup.validate(server)) { - return false; + return; + } + + const prefixedServer = this.prefixify(server); + const serverInfo = await window.lokiPublicChatAPI.findOrCreateServer(prefixedServer) as any; + + if (!serverInfo?.channels?.length) { + return; } - return Boolean(window.lokiPublicChatAPI.findOrCreateServer(server)); + return serverInfo.channels[0].conversation; + } + + public static getConversationByCID(conversationId: string): any { + const { ConversationController } = window; + return ConversationController.get(conversationId); } private static getServer(groupId: string, hasSSL: boolean): string | undefined { @@ -152,6 +172,4 @@ export class OpenGroup { return `http${hasSSL ? 's' : ''}://${server}`; } - - }