2020-10-13 16:46:46 +02:00
|
|
|
import Axios from "axios";
|
|
|
|
import {API_URL} from "../Enum/EnvironmentVariable";
|
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
export class Room {
|
2020-10-13 16:46:46 +02:00
|
|
|
public readonly id: string;
|
|
|
|
public readonly isPublic: boolean;
|
|
|
|
private mapUrl: string|undefined;
|
2020-10-13 18:44:50 +02:00
|
|
|
private instance: string|undefined;
|
2020-10-13 16:46:46 +02:00
|
|
|
|
2020-10-13 17:08:24 +02:00
|
|
|
constructor(id: string) {
|
2020-10-13 16:46:46 +02:00
|
|
|
if (id.startsWith('/')) {
|
|
|
|
id = id.substr(1);
|
|
|
|
}
|
|
|
|
this.id = id;
|
|
|
|
if (id.startsWith('_/')) {
|
|
|
|
this.isPublic = true;
|
|
|
|
} else if (id.startsWith('@/')) {
|
|
|
|
this.isPublic = false;
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid room ID');
|
|
|
|
}
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
2020-10-13 16:46:46 +02:00
|
|
|
|
|
|
|
public async getMapUrl(): Promise<string> {
|
2020-10-13 17:30:53 +02:00
|
|
|
return new Promise<string>((resolve, reject) => {
|
2020-10-13 16:46:46 +02:00
|
|
|
if (this.mapUrl !== undefined) {
|
|
|
|
resolve(this.mapUrl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isPublic) {
|
2020-10-13 17:30:53 +02:00
|
|
|
const match = /_\/[^/]+\/(.+)/.exec(this.id);
|
2020-10-13 16:46:46 +02:00
|
|
|
if (!match) throw new Error('Could not extract url from "'+this.id+'"');
|
|
|
|
this.mapUrl = window.location.protocol+'//'+match[1];
|
|
|
|
resolve(this.mapUrl);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// We have a private ID, we need to query the map URL from the server.
|
|
|
|
const urlParts = this.parsePrivateUrl(this.id);
|
|
|
|
|
2020-10-13 17:30:53 +02:00
|
|
|
Axios.get(`${API_URL}/map`, {
|
2020-10-13 16:46:46 +02:00
|
|
|
params: urlParts
|
2020-10-13 17:30:53 +02:00
|
|
|
}).then(({data}) => {
|
|
|
|
console.log('Map ', this.id, ' resolves to URL ', data.mapUrl);
|
|
|
|
resolve(data.mapUrl);
|
|
|
|
return;
|
2020-10-13 16:46:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-13 18:44:50 +02:00
|
|
|
/**
|
|
|
|
* Instance name is:
|
|
|
|
* - In a public URL: the second part of the URL ( _/[instance]/map.json)
|
|
|
|
* - In a private URL: [organizationId/worldId]
|
|
|
|
*/
|
|
|
|
public getInstance(): string {
|
|
|
|
if (this.instance !== undefined) {
|
|
|
|
return this.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isPublic) {
|
|
|
|
const match = /_\/([^/]+)\/.+/.exec(this.id);
|
|
|
|
if (!match) throw new Error('Could not extract instance from "'+this.id+'"');
|
|
|
|
this.instance = match[1];
|
|
|
|
return this.instance;
|
|
|
|
} else {
|
2020-10-14 10:37:00 +02:00
|
|
|
const match = /@\/([^/]+)\/([^/]+)\/.+/.exec(this.id);
|
2020-10-13 18:44:50 +02:00
|
|
|
if (!match) throw new Error('Could not extract instance from "'+this.id+'"');
|
|
|
|
this.instance = match[1]+'/'+match[2];
|
|
|
|
return this.instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 16:46:46 +02:00
|
|
|
private parsePrivateUrl(url: string): { organizationSlug: string, worldSlug: string, roomSlug?: string } {
|
2020-10-13 17:30:53 +02:00
|
|
|
const regex = /@\/([^/]+)\/([^/]+)(?:\/([^/]*))?/gm;
|
2020-10-13 16:46:46 +02:00
|
|
|
const match = regex.exec(url);
|
|
|
|
if (!match) {
|
|
|
|
throw new Error('Invalid URL '+url);
|
|
|
|
}
|
2020-10-13 17:30:53 +02:00
|
|
|
const results: { organizationSlug: string, worldSlug: string, roomSlug?: string } = {
|
2020-10-13 16:46:46 +02:00
|
|
|
organizationSlug: match[1],
|
|
|
|
worldSlug: match[2],
|
|
|
|
}
|
|
|
|
if (match[3] !== undefined) {
|
|
|
|
results.roomSlug = match[3];
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|