diff --git a/ts/models/conversation.ts b/ts/models/conversation.ts index daf1b8f83..f99125f02 100644 --- a/ts/models/conversation.ts +++ b/ts/models/conversation.ts @@ -188,7 +188,7 @@ export class ConversationModel extends Backbone.Model { return UserUtils.isUsFromCache(this.id); } public isPublic() { - return !!(this.id && this.id.match(/^publicChat:/)); + return !!(this.id && this.id.match(OpenGroup.openGroupPrefixRegex)); } public isOpenGroupV2() { return this.get('type') === ConversationType.OPEN_GROUP; diff --git a/ts/opengroup/opengroupV1/OpenGroup.ts b/ts/opengroup/opengroupV1/OpenGroup.ts index a975a147d..e596deeb0 100644 --- a/ts/opengroup/opengroupV1/OpenGroup.ts +++ b/ts/opengroup/opengroupV1/OpenGroup.ts @@ -12,12 +12,22 @@ interface OpenGroupParams { } 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( '^((https?:\\/\\/){0,1})([\\w-]{2,}\\.){1,2}[\\w-]{2,}$' ); 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 channel: number; 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.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 */ constructor(params: OpenGroupParams) { @@ -66,7 +76,7 @@ export class OpenGroup { * Try to make a new instance of `OpenGroup`. * 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 * @returns `OpenGroup` if valid otherwise returns `undefined`. */ @@ -157,7 +167,7 @@ export class OpenGroup { } const rawServerURL = server.replace(/^https?:\/\//i, '').replace(/[/\\]+$/i, ''); 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 return ConversationController.getInstance().get(conversationId); @@ -200,7 +210,7 @@ export class OpenGroup { const prefixRegex = new RegExp('https?:\\/\\/'); 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 conversationId = `publicChat:${channelId}@${rawServerURL}`; + const conversationId = `${OpenGroup.openGroupPrefix}${channelId}@${rawServerURL}`; // Quickly peak to make sure we don't already have it const conversationExists = ConversationController.getInstance().get(conversationId); diff --git a/ts/opengroup/utils/OpenGroupUtils.ts b/ts/opengroup/utils/OpenGroupUtils.ts index 88fd5b6ca..84ec3d733 100644 --- a/ts/opengroup/utils/OpenGroupUtils.ts +++ b/ts/opengroup/utils/OpenGroupUtils.ts @@ -1,5 +1,6 @@ import { default as insecureNodeFetch } from 'node-fetch'; import { sendViaOnion } from '../../session/onions/onionSend'; +import { OpenGroup } from '../opengroupV1/OpenGroup'; /** * 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 - * @returns `publicChat:${roomId}@${serverUrl}` + * @returns `${OpenGroup.openGroupPrefix}${roomId}@${serverUrl}` */ export function getOpenGroupV2ConversationId(serverUrl: string, roomId: string) { if (roomId.length < 2) { throw new Error('Invalid roomId: too short'); } - return `publicChat:${roomId}@${serverUrl}`; + return `${OpenGroup.openGroupPrefix}${roomId}@${serverUrl}`; } + +export function isOpenGroupV2(conversationId: string) {} diff --git a/ts/receiver/receiver.ts b/ts/receiver/receiver.ts index d17f90e96..4861acae5 100644 --- a/ts/receiver/receiver.ts +++ b/ts/receiver/receiver.ts @@ -22,6 +22,7 @@ import { SignalService } from '../protobuf'; import { ConversationController } from '../session/conversations'; import { removeUnprocessed } from '../data/data'; import { ConversationType } from '../models/conversation'; +import { OpenGroup } from '../opengroup/opengroupV1/OpenGroup'; // 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); } - const isPublicVisibleMessage = group && group.id && !!group.id.match(/^publicChat:/); + const isPublicVisibleMessage = + group && group.id && !!group.id.match(OpenGroup.openGroupPrefixRegex); if (!isPublicVisibleMessage) { throw new Error('handlePublicMessage Should only be called with public message groups'); diff --git a/ts/test/session/unit/utils/Messages_test.ts b/ts/test/session/unit/utils/Messages_test.ts index 94a7a2c0f..71e05f589 100644 --- a/ts/test/session/unit/utils/Messages_test.ts +++ b/ts/test/session/unit/utils/Messages_test.ts @@ -20,6 +20,7 @@ import { ClosedGroupEncryptionPairMessage } from '../../../../session/messages/o import { ClosedGroupNameChangeMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupNameChangeMessage'; import { ClosedGroupNewMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupNewMessage'; import { ClosedGroupRemovedMembersMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupRemovedMembersMessage'; +import { OpenGroup } from '../../../../opengroup/opengroupV1/OpenGroup'; const { expect } = chai; @@ -226,12 +227,12 @@ describe('Message Utils', () => { let convos: Array; const mockValidOpenGroup = new MockConversation({ type: ConversationType.OPEN_GROUP, - id: 'publicChat:1@chat-dev.lokinet.org', + id: `${OpenGroup.openGroupPrefix}1@chat-dev.lokinet.org`, }); const mockValidOpenGroup2 = new MockConversation({ type: ConversationType.OPEN_GROUP, - id: 'publicChat:1@chat-dev2.lokinet.org', + id: `${OpenGroup.openGroupPrefix}1@chat-dev2.lokinet.org`, }); const mockValidClosedGroup = new MockConversation({ diff --git a/ts/test/test-utils/utils/message.ts b/ts/test/test-utils/utils/message.ts index 468e0d2a8..77eec9c80 100644 --- a/ts/test/test-utils/utils/message.ts +++ b/ts/test/test-utils/utils/message.ts @@ -92,7 +92,7 @@ export class MockConversation { } public isPublic() { - return this.id.match(/^publicChat:/); + return this.id.match(OpenGroup.openGroupPrefixRegex); } public isMediumGroup() {