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 attachments: Array<AttachmentPointer>;
public readonly quote?: Quote;
public readonly preview: Array<Preview>;
public readonly preview?: Array<Preview>;
constructor({
timestamp,

@ -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 {

@ -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);

Loading…
Cancel
Save