Skip to content

Commit

Permalink
fix waypoint link reloads room Hubs-Foundation#3149
Browse files Browse the repository at this point in the history
  • Loading branch information
yakyouk committed May 20, 2021
1 parent 565e944 commit 86fd89e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/components/open-media-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ AFRAME.registerComponent("open-media-button", {
if (visible) {
let label = "open link";
if (!this.data.onlyOpenLink) {
let hubId;
if (await isLocalHubsAvatarUrl(src)) {
label = "use avatar";
} else if ((await isLocalHubsSceneUrl(src)) && mayChangeScene) {
label = "use scene";
} else if (await isHubsRoomUrl(src)) {
const url = new URL(this.src);
if (url.hash && window.location.pathname === url.pathname) {
} else if ((hubId = await isHubsRoomUrl(src))) {
const url = new URL(src);
if (url.hash && APP.hub.hub_id === hubId) {
label = "go to";
} else {
label = "visit room";
Expand All @@ -43,6 +44,7 @@ AFRAME.registerComponent("open-media-button", {

const exitImmersive = async () => await handleExitTo2DInterstitial(false, () => {}, true);

let hubId;
if (this.data.onlyOpenLink) {
await exitImmersive();
window.open(this.src);
Expand All @@ -52,9 +54,14 @@ AFRAME.registerComponent("open-media-button", {
this.el.sceneEl.emit("avatar_updated");
} else if ((await isLocalHubsSceneUrl(this.src)) && mayChangeScene) {
this.el.sceneEl.emit("scene_media_selected", this.src);
} else if (await isHubsRoomUrl(this.src)) {
await exitImmersive();
location.href = this.src;
} else if ((hubId = await isHubsRoomUrl(this.src))) {
const url = new URL(this.src);
if (url.hash && APP.hub.hub_id === hubId) {
location.hash = url.hash;
} else {
await exitImmersive();
location.href = this.src;
}
} else {
await exitImmersive();
window.open(this.src);
Expand Down
11 changes: 7 additions & 4 deletions src/utils/media-url-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ async function isHubsServer(url) {
return isHubsServer;
}

const hubsSceneRegex = /https?:\/\/[^/]+\/scenes\/(\w+)\/?\S*/;
const hubsAvatarRegex = /https?:\/\/[^/]+\/avatars\/(?<id>\w+)\/?\S*/;
const hubsRoomRegex = /(https?:\/\/)?[^/]+\/([a-zA-Z0-9]{7})\/?\S*/;
const hubsSceneRegex = /https?:\/\/[^/]+\/scenes\/[a-zA-Z0-9]{7}(?:\/|$)/;
const hubsAvatarRegex = /https?:\/\/[^/]+\/avatars\/(?<id>[a-zA-Z0-9]{7})(?:\/|$)/;
const hubsRoomRegex = /(https?:\/\/)?[^/]+\/(?<id>[a-zA-Z0-9]{7})(?:\/|$)/;

export const isLocalHubsUrl = async url =>
(await isHubsServer(url)) && new URL(url).origin === document.location.origin;
Expand All @@ -194,7 +194,10 @@ export const isHubsAvatarUrl = async url => (await isHubsServer(url)) && hubsAva
export const isLocalHubsAvatarUrl = async url => (await isHubsAvatarUrl(url)) && (await isLocalHubsUrl(url));

export const isHubsRoomUrl = async url =>
(await isHubsServer(url)) && !(await isHubsAvatarUrl(url)) && !(await isHubsSceneUrl(url)) && hubsRoomRegex.test(url);
(await isHubsServer(url)) &&
!(await isHubsAvatarUrl(url)) &&
!(await isHubsSceneUrl(url)) &&
url.match(hubsRoomRegex)?.groups.id;

export const isHubsDestinationUrl = async url =>
(await isHubsServer(url)) && ((await isHubsSceneUrl(url)) || (await isHubsRoomUrl(url)));
Expand Down

0 comments on commit 86fd89e

Please sign in to comment.