add constant openGroupPrefix: publicChat: and use it

pull/1576/head
Audric Ackermann 5 years ago
parent 70572e1315
commit e3e1296788
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -188,7 +188,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
return UserUtils.isUsFromCache(this.id); return UserUtils.isUsFromCache(this.id);
} }
public isPublic() { public isPublic() {
return !!(this.id && this.id.match(/^publicChat:/)); return !!(this.id && this.id.match(OpenGroup.openGroupPrefixRegex));
} }
public isOpenGroupV2() { public isOpenGroupV2() {
return this.get('type') === ConversationType.OPEN_GROUP; return this.get('type') === ConversationType.OPEN_GROUP;

@ -12,12 +12,22 @@ interface OpenGroupParams {
} }
export class OpenGroup { export class OpenGroup {
/**
* Just a constant to have less `publicChat:` everywhere
* Note: It does already have the ':' included
*/
public static readonly openGroupPrefix = 'publicChat:';
/**
* Just a regex to match a public chat (i.e. a string starting with publicChat:)
*/
public static readonly openGroupPrefixRegex = new RegExp(`/^${OpenGroup.openGroupPrefix}/`);
private static readonly serverRegex = new RegExp( private static readonly serverRegex = new RegExp(
'^((https?:\\/\\/){0,1})([\\w-]{2,}\\.){1,2}[\\w-]{2,}$' '^((https?:\\/\\/){0,1})([\\w-]{2,}\\.){1,2}[\\w-]{2,}$'
); );
private static readonly groupIdRegex = new RegExp( private static readonly groupIdRegex = new RegExp(
'^publicChat:[0-9]*@([\\w-]{2,}.){1,2}[\\w-]{2,}$' `^${OpenGroup.openGroupPrefix}:[0-9]*@([\\w-]{2,}.){1,2}[\\w-]{2,}$`
); );
public readonly server: string; public readonly server: string;
public readonly channel: number; public readonly channel: number;
public readonly groupId?: string; public readonly groupId?: string;
@ -29,7 +39,7 @@ export class OpenGroup {
* *
* @param params.server The server URL. `https` will be prepended if `http` or `https` is not explicitly set * @param params.server The server URL. `https` will be prepended if `http` or `https` is not explicitly set
* @param params.channel The server channel * @param params.channel The server channel
* @param params.groupId The string corresponding to the server. Eg. `publicChat:1@chat.getsession.org` * @param params.groupId The string corresponding to the server. Eg. `${OpenGroup.openGroupPrefix}1@chat.getsession.org`
* @param params.conversationId The conversation ID for the backbone model * @param params.conversationId The conversation ID for the backbone model
*/ */
constructor(params: OpenGroupParams) { constructor(params: OpenGroupParams) {
@ -66,7 +76,7 @@ export class OpenGroup {
* Try to make a new instance of `OpenGroup`. * Try to make a new instance of `OpenGroup`.
* This does NOT respect `ConversationController` and does not guarentee the conversation's existence. * This does NOT respect `ConversationController` and does not guarentee the conversation's existence.
* *
* @param groupId The string corresponding to the server. Eg. `publicChat:1@chat.getsession.org` * @param groupId The string corresponding to the server. Eg. `${OpenGroup.openGroupPrefix}1@chat.getsession.org`
* @param conversationId The conversation ID for the backbone model * @param conversationId The conversation ID for the backbone model
* @returns `OpenGroup` if valid otherwise returns `undefined`. * @returns `OpenGroup` if valid otherwise returns `undefined`.
*/ */
@ -157,7 +167,7 @@ export class OpenGroup {
} }
const rawServerURL = server.replace(/^https?:\/\//i, '').replace(/[/\\]+$/i, ''); const rawServerURL = server.replace(/^https?:\/\//i, '').replace(/[/\\]+$/i, '');
const channelId = 1; const channelId = 1;
const conversationId = `publicChat:${channelId}@${rawServerURL}`; const conversationId = `${OpenGroup.openGroupPrefix}${channelId}@${rawServerURL}`;
// Quickly peak to make sure we don't already have it // Quickly peak to make sure we don't already have it
return ConversationController.getInstance().get(conversationId); return ConversationController.getInstance().get(conversationId);
@ -200,7 +210,7 @@ export class OpenGroup {
const prefixRegex = new RegExp('https?:\\/\\/'); const prefixRegex = new RegExp('https?:\\/\\/');
const strippedServer = server.replace(prefixRegex, ''); const strippedServer = server.replace(prefixRegex, '');
return `publicChat:${channel}@${strippedServer}`; return `${OpenGroup.openGroupPrefix}${channel}@${strippedServer}`;
} }
/** /**
@ -242,7 +252,7 @@ export class OpenGroup {
const rawServerURL = serverUrl.replace(/^https?:\/\//i, '').replace(/[/\\]+$/i, ''); const rawServerURL = serverUrl.replace(/^https?:\/\//i, '').replace(/[/\\]+$/i, '');
const conversationId = `publicChat:${channelId}@${rawServerURL}`; const conversationId = `${OpenGroup.openGroupPrefix}${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 = ConversationController.getInstance().get(conversationId); const conversationExists = ConversationController.getInstance().get(conversationId);

@ -1,5 +1,6 @@
import { default as insecureNodeFetch } from 'node-fetch'; import { default as insecureNodeFetch } from 'node-fetch';
import { sendViaOnion } from '../../session/onions/onionSend'; import { sendViaOnion } from '../../session/onions/onionSend';
import { OpenGroup } from '../opengroupV1/OpenGroup';
/** /**
* Tries to establish a connection with the specified open group url. * Tries to establish a connection with the specified open group url.
@ -94,11 +95,13 @@ export function prefixify(server: string, hasSSL: boolean = true): string {
/** /**
* No sql access. Just how our open groupv2 url looks like * No sql access. Just how our open groupv2 url looks like
* @returns `publicChat:${roomId}@${serverUrl}` * @returns `${OpenGroup.openGroupPrefix}${roomId}@${serverUrl}`
*/ */
export function getOpenGroupV2ConversationId(serverUrl: string, roomId: string) { export function getOpenGroupV2ConversationId(serverUrl: string, roomId: string) {
if (roomId.length < 2) { if (roomId.length < 2) {
throw new Error('Invalid roomId: too short'); throw new Error('Invalid roomId: too short');
} }
return `publicChat:${roomId}@${serverUrl}`; return `${OpenGroup.openGroupPrefix}${roomId}@${serverUrl}`;
} }
export function isOpenGroupV2(conversationId: string) {}

@ -22,6 +22,7 @@ import { SignalService } from '../protobuf';
import { ConversationController } from '../session/conversations'; import { ConversationController } from '../session/conversations';
import { removeUnprocessed } from '../data/data'; import { removeUnprocessed } from '../data/data';
import { ConversationType } from '../models/conversation'; import { ConversationType } from '../models/conversation';
import { OpenGroup } from '../opengroup/opengroupV1/OpenGroup';
// TODO: check if some of these exports no longer needed // TODO: check if some of these exports no longer needed
@ -272,7 +273,8 @@ export async function handlePublicMessage(messageData: any) {
await updateProfile(conversation, profile, profileKey); await updateProfile(conversation, profile, profileKey);
} }
const isPublicVisibleMessage = group && group.id && !!group.id.match(/^publicChat:/); const isPublicVisibleMessage =
group && group.id && !!group.id.match(OpenGroup.openGroupPrefixRegex);
if (!isPublicVisibleMessage) { if (!isPublicVisibleMessage) {
throw new Error('handlePublicMessage Should only be called with public message groups'); throw new Error('handlePublicMessage Should only be called with public message groups');

@ -20,6 +20,7 @@ import { ClosedGroupEncryptionPairMessage } from '../../../../session/messages/o
import { ClosedGroupNameChangeMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupNameChangeMessage'; import { ClosedGroupNameChangeMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupNameChangeMessage';
import { ClosedGroupNewMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupNewMessage'; import { ClosedGroupNewMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupNewMessage';
import { ClosedGroupRemovedMembersMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupRemovedMembersMessage'; import { ClosedGroupRemovedMembersMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupRemovedMembersMessage';
import { OpenGroup } from '../../../../opengroup/opengroupV1/OpenGroup';
const { expect } = chai; const { expect } = chai;
@ -226,12 +227,12 @@ describe('Message Utils', () => {
let convos: Array<ConversationModel>; let convos: Array<ConversationModel>;
const mockValidOpenGroup = new MockConversation({ const mockValidOpenGroup = new MockConversation({
type: ConversationType.OPEN_GROUP, type: ConversationType.OPEN_GROUP,
id: 'publicChat:1@chat-dev.lokinet.org', id: `${OpenGroup.openGroupPrefix}1@chat-dev.lokinet.org`,
}); });
const mockValidOpenGroup2 = new MockConversation({ const mockValidOpenGroup2 = new MockConversation({
type: ConversationType.OPEN_GROUP, type: ConversationType.OPEN_GROUP,
id: 'publicChat:1@chat-dev2.lokinet.org', id: `${OpenGroup.openGroupPrefix}1@chat-dev2.lokinet.org`,
}); });
const mockValidClosedGroup = new MockConversation({ const mockValidClosedGroup = new MockConversation({

@ -92,7 +92,7 @@ export class MockConversation {
} }
public isPublic() { public isPublic() {
return this.id.match(/^publicChat:/); return this.id.match(OpenGroup.openGroupPrefixRegex);
} }
public isMediumGroup() { public isMediumGroup() {

Loading…
Cancel
Save