diff --git a/ts/session/messages/outgoing/OpenGroupMessage.ts b/ts/session/messages/outgoing/OpenGroupMessage.ts index 516b26fff..c670a5e68 100644 --- a/ts/session/messages/outgoing/OpenGroupMessage.ts +++ b/ts/session/messages/outgoing/OpenGroupMessage.ts @@ -15,7 +15,7 @@ export class OpenGroupMessage extends Message { public readonly body?: string; public readonly attachments: Array; public readonly quote?: Quote; - public readonly preview: Array; + public readonly preview?: Array; constructor({ timestamp, diff --git a/ts/session/types/OpenGroup.ts b/ts/session/types/OpenGroup.ts index 85cf03f11..60a1d1acd 100644 --- a/ts/session/types/OpenGroup.ts +++ b/ts/session/types/OpenGroup.ts @@ -13,18 +13,17 @@ export class OpenGroup { public readonly channel: number; public readonly groupId?: string; public readonly conversationId: string; - private readonly isValid: boolean; constructor(params: OpenGroupParams) { - this.isValid = OpenGroup.validate(params); + const strippedServer = params.server.replace('https://', ''); + this.server = strippedServer; - if (!this.isValid) { + // Validate server format + const isValid = OpenGroup.serverRegex.test(this.server); + if (!isValid) { throw Error('an invalid server or groupId was provided'); } - const strippedServer = params.server.replace('https://', ''); - - this.server = strippedServer; this.channel = params.channel; this.conversationId = params.conversationId; this.groupId = OpenGroup.getGroupId(this.server, this.channel); @@ -34,47 +33,43 @@ export class OpenGroup { // Returns a new instance from a groupId if it's valid // eg. groupId = 'publicChat:1@chat.getsession.org' - // Valid groupId? - if (!this.groupIdRegex.test(groupId)) { + const server = this.getServer(groupId); + const channel = this.getChannel(groupId); + + // Was groupId successfully utilized? + if (!server || !channel) { return; } const openGroupParams = { - server: this.getServer(groupId), - channel: this.getChannel(groupId), + server, + channel, groupId, conversationId, - }; + } as OpenGroupParams; - if (this.validate(openGroupParams)) { + if (this.serverRegex.test(server)) { return new OpenGroup(openGroupParams); } return; } - private static validate(openGroup: OpenGroupParams): boolean { - // Validate that all the values match by rebuilding groupId. - const { server } = openGroup; - - // Valid server? - if (!this.serverRegex.test(server)) { - return false; - } - - return true; - } + private static getServer(groupId: string): string | undefined { + const isValid = this.groupIdRegex.test(groupId); - private static getServer(groupId: string): string { - // groupId is already validated in constructor; no need to re-check - return groupId.split('@')[1]; + return isValid + ? groupId.split('@')[1] + : undefined; } - private static getChannel(groupId: string): number { - // groupId is already validated in constructor; no need to re-check + private static getChannel(groupId: string): number | undefined { + const isValid = this.groupIdRegex.test(groupId); const channelMatch = groupId.match(/^.*\:([0-9]*)\@.*$/); - return channelMatch ? Number(channelMatch[1]) : 1; + return channelMatch && isValid + ? Number(channelMatch[1]) + : undefined; } private static getGroupId(server: string, channel: number): string { diff --git a/ts/test/session/messages/OpenGroupMessage_test.ts b/ts/test/session/messages/OpenGroupMessage_test.ts index 44fe696e4..5b4f84b9e 100644 --- a/ts/test/session/messages/OpenGroupMessage_test.ts +++ b/ts/test/session/messages/OpenGroupMessage_test.ts @@ -8,20 +8,16 @@ import * as MIME from '../../../../ts/types/MIME'; import { OpenGroup } from '../../../session/types/OpenGroup'; describe('OpenGroupMessage', () => { - const group = { - server: 'server', + const group = new OpenGroup({ + server: 'chat.example.server', channel: 1, conversationId: '0', - }; - - const group = new OpenGroup({ - server: 'server' - }) + }); it('can create empty message with just a timestamp and group', () => { const message = new OpenGroupMessage({ timestamp: Date.now(), - group., + group, }); expect(message?.timestamp).to.be.approximately(Date.now(), 10); expect(message?.group).to.deep.equal(group);