joining-open-groups

pull/1225/head
Vincent 5 years ago
parent 8484b9d3f6
commit 39b41cb222

@ -1090,7 +1090,7 @@
window.setMediaPermissions(!mediaPermissions);
};
// attempts a connection to an open group server
// Attempts a connection to an open group server
window.attemptConnection = async (serverURL, channelId) => {
let rawserverURL = serverURL
.replace(/^https?:\/\//i, '')
@ -1099,7 +1099,7 @@
const sslServerURL = `https://${rawserverURL}`;
const conversationId = `publicChat:${channelId}@${rawserverURL}`;
// quickly peak to make sure we don't already have it
// Quickly peak to make sure we don't already have it
const conversationExists = window.ConversationController.get(
conversationId
);
@ -1110,7 +1110,7 @@
});
}
// get server
// Get server
const serverAPI = await window.lokiPublicChatAPI.findOrCreateServer(
sslServerURL
);
@ -1122,13 +1122,13 @@
});
}
// create conversation
// Create conversation
const conversation = await window.ConversationController.getOrCreateAndWait(
conversationId,
'group'
);
// convert conversation to a public one
// Convert conversation to a public one
await conversation.setPublicSource(sslServerURL, channelId);
// and finally activate it

@ -28,6 +28,7 @@ import {
SessionButtonColor,
SessionButtonType,
} from './SessionButton';
import { OpenGroup } from '../../session/types';
export interface Props {
searchTerm: string;
@ -438,27 +439,24 @@ export class LeftPaneMessageSection extends React.Component<Props, State> {
}
}
private handleJoinChannelButtonClick(groupUrl: string) {
private async handleJoinChannelButtonClick(server: string) {
const { loading } = this.state;
if (loading) {
return false;
}
// longest TLD is now (20/02/06) 24 characters per https://jasontucker.blog/8945/what-is-the-longest-tld-you-can-get-for-a-domain-name
const regexURL = /(http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,24}(:[0-9]{1,5})?(\/.*)?/;
if (groupUrl.length <= 0) {
if (!OpenGroup.validate(server)) {
window.pushToast({
title: window.i18n('noServerURL'),
type: 'error',
id: 'connectToServerFail',
});
return false;
}
await OpenGroup.join(server);
if (!regexURL.test(groupUrl)) {
if (groupUrl.length <= 0) {
window.pushToast({
title: window.i18n('noServerURL'),
type: 'error',
@ -468,6 +466,13 @@ export class LeftPaneMessageSection extends React.Component<Props, State> {
return false;
}
return false;
}
MainViewController.joinChannelStateManager(this, groupUrl, () => {
this.handleToggleOverlay(undefined);
});

@ -1,5 +1,9 @@
// 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";
interface OpenGroupParams {
server: string;
channel: number;
@ -18,7 +22,11 @@ export class OpenGroup {
public readonly server: string;
public readonly channel: number;
public readonly groupId?: string;
public readonly conversationId: 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
@ -35,6 +43,14 @@ export class OpenGroup {
this.groupId = OpenGroup.getGroupId(this.server, this.channel);
}
public static validate(serverUrl: string): boolean {
if (this.serverRegex.test(serverUrl)) {
return true;
}
return false;
}
public static from(
groupId: string,
conversationId: string,
@ -66,6 +82,26 @@ export class OpenGroup {
return new OpenGroup(openGroupParams);
}
public static async join(server: string): Promise<OpenGroup | undefined> {
if (!OpenGroup.validate(server)) {
return;
}
// Make this not hard coded
const channel = 1;
const conversation = window.attemptConnection(server, channel);
const groupId = OpenGroup.getGroupId(server, channel);
return new OpenGroup({
server,
groupId,
conversation.cid,
})
return {serverInfo, connectionPromise};
}
private static getServer(groupId: string, hasSSL: boolean): string | undefined {
const isValid = this.groupIdRegex.test(groupId);
const strippedServer = isValid ? groupId.split('@')[1] : undefined;
@ -102,4 +138,6 @@ export class OpenGroup {
return `http${hasSSL ? 's' : ''}://${server}`;
}
}

5
ts/window.d.ts vendored

@ -7,12 +7,15 @@ import { LokiPublicChatFactoryInterface } from '../js/modules/loki_public_chat_a
import { LokiAppDotNetServerInterface } from '../js/modules/loki_app_dot_net_api';
import { LokiMessageInterface } from '../js/modules/loki_message_api';
import { SwarmPolling } from './session/snode_api/swarmPolling';
import { LibTextsecure } from '../libtextsecure';
import { ConversationType } from '../js/modules/data';
/*
We declare window stuff here instead of global.d.ts because we are importing other declarations.
If you import anything in global.d.ts, the type system won't work correctly.
*/
declare global {
interface Window {
CONSTANTS: any;
@ -33,7 +36,7 @@ declare global {
StubMessageAPI: any;
WebAPI: any;
Whisper: any;
attemptConnection: any;
attemptConnection: ConversationType;
clearLocalData: any;
clipboard: any;
confirmationDialog: any;

Loading…
Cancel
Save