opengroupmessage

pull/1177/head
Vincent 5 years ago
parent 6c35f3c773
commit 00122333ff

@ -15,7 +15,7 @@ export class OpenGroupMessage extends Message {
public readonly body?: string; public readonly body?: string;
public readonly attachments: Array<AttachmentPointer>; public readonly attachments: Array<AttachmentPointer>;
public readonly quote?: Quote; public readonly quote?: Quote;
public readonly preview: Array<Preview>; public readonly preview?: Array<Preview>;
constructor({ constructor({
timestamp, timestamp,

@ -13,18 +13,17 @@ export class OpenGroup {
public readonly channel: number; public readonly channel: number;
public readonly groupId?: string; public readonly groupId?: string;
public readonly conversationId: string; public readonly conversationId: string;
private readonly isValid: boolean;
constructor(params: OpenGroupParams) { 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'); throw Error('an invalid server or groupId was provided');
} }
const strippedServer = params.server.replace('https://', '');
this.server = strippedServer;
this.channel = params.channel; this.channel = params.channel;
this.conversationId = params.conversationId; this.conversationId = params.conversationId;
this.groupId = OpenGroup.getGroupId(this.server, this.channel); 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 // Returns a new instance from a groupId if it's valid
// eg. groupId = 'publicChat:1@chat.getsession.org' // eg. groupId = 'publicChat:1@chat.getsession.org'
// Valid groupId? const server = this.getServer(groupId);
if (!this.groupIdRegex.test(groupId)) { const channel = this.getChannel(groupId);
// Was groupId successfully utilized?
if (!server || !channel) {
return; return;
} }
const openGroupParams = { const openGroupParams = {
server: this.getServer(groupId), server,
channel: this.getChannel(groupId), channel,
groupId, groupId,
conversationId, conversationId,
}; } as OpenGroupParams;
if (this.validate(openGroupParams)) { if (this.serverRegex.test(server)) {
return new OpenGroup(openGroupParams); return new OpenGroup(openGroupParams);
} }
return; return;
} }
private static validate(openGroup: OpenGroupParams): boolean { private static getServer(groupId: string): string | undefined {
// Validate that all the values match by rebuilding groupId. const isValid = this.groupIdRegex.test(groupId);
const { server } = openGroup;
// Valid server?
if (!this.serverRegex.test(server)) {
return false;
}
return true;
}
private static getServer(groupId: string): string { return isValid
// groupId is already validated in constructor; no need to re-check ? groupId.split('@')[1]
return groupId.split('@')[1]; : undefined;
} }
private static getChannel(groupId: string): number { private static getChannel(groupId: string): number | undefined {
// groupId is already validated in constructor; no need to re-check const isValid = this.groupIdRegex.test(groupId);
const channelMatch = groupId.match(/^.*\:([0-9]*)\@.*$/); 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 { private static getGroupId(server: string, channel: number): string {

@ -8,20 +8,16 @@ import * as MIME from '../../../../ts/types/MIME';
import { OpenGroup } from '../../../session/types/OpenGroup'; import { OpenGroup } from '../../../session/types/OpenGroup';
describe('OpenGroupMessage', () => { describe('OpenGroupMessage', () => {
const group = { const group = new OpenGroup({
server: 'server', server: 'chat.example.server',
channel: 1, channel: 1,
conversationId: '0', conversationId: '0',
}; });
const group = new OpenGroup({
server: 'server'
})
it('can create empty message with just a timestamp and group', () => { it('can create empty message with just a timestamp and group', () => {
const message = new OpenGroupMessage({ const message = new OpenGroupMessage({
timestamp: Date.now(), timestamp: Date.now(),
group., group,
}); });
expect(message?.timestamp).to.be.approximately(Date.now(), 10); expect(message?.timestamp).to.be.approximately(Date.now(), 10);
expect(message?.group).to.deep.equal(group); expect(message?.group).to.deep.equal(group);

Loading…
Cancel
Save