getting opengroup conversation

pull/1225/head
Vincent 5 years ago
parent ab966e600a
commit f6ae5386e9

@ -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"

@ -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) {

@ -439,45 +439,79 @@ export class LeftPaneMessageSection extends React.Component<Props, State> {
}
}
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(

@ -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<boolean> {
public static async getConversation(server: string): Promise<any> {
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}`;
}
}

Loading…
Cancel
Save