736 lines
16 KiB
JavaScript
736 lines
16 KiB
JavaScript
// message-router.js
|
|
|
|
window.Kronick =
|
|
window.Kronick || {};
|
|
|
|
|
|
/*
|
|
* Central message router for the music, Giphy and
|
|
* chat iframes.
|
|
*/
|
|
Kronick.handleWindowMessage =
|
|
function handleWindowMessage(event) {
|
|
if (
|
|
event.origin
|
|
!== Kronick.config.APP_ORIGIN
|
|
) {
|
|
console.warn(
|
|
'Rejected cross-origin '
|
|
+ 'window message:',
|
|
event.origin
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
const data =
|
|
event.data;
|
|
|
|
if (
|
|
!data
|
|
|| typeof data !== 'object'
|
|
) {
|
|
return;
|
|
}
|
|
|
|
const musicFrame =
|
|
document.getElementById(
|
|
'musicFrame'
|
|
);
|
|
|
|
const giphyFrame =
|
|
document.getElementById(
|
|
'giphyFrame'
|
|
);
|
|
|
|
const chatFrame =
|
|
document.getElementById(
|
|
'chatFrame'
|
|
);
|
|
|
|
const fromMusicFrame =
|
|
Boolean(
|
|
musicFrame?.contentWindow
|
|
&& event.source
|
|
=== musicFrame.contentWindow
|
|
);
|
|
|
|
const fromGiphyFrame =
|
|
Boolean(
|
|
giphyFrame?.contentWindow
|
|
&& event.source
|
|
=== giphyFrame.contentWindow
|
|
);
|
|
|
|
const fromChatFrame =
|
|
Boolean(
|
|
chatFrame?.contentWindow
|
|
&& event.source
|
|
=== chatFrame.contentWindow
|
|
);
|
|
|
|
|
|
switch (data.type) {
|
|
case 'KRONICK_CHAT_HISTORY_REQUEST':
|
|
if (!fromChatFrame) {
|
|
return;
|
|
}
|
|
|
|
Kronick.sendChatHistoryToFrame();
|
|
|
|
return;
|
|
|
|
|
|
case 'KRONICK_CHAT_NEW_MESSAGE':
|
|
if (!fromChatFrame) {
|
|
return;
|
|
}
|
|
|
|
Kronick
|
|
.handleIncomingChatNotification(
|
|
data.message
|
|
);
|
|
|
|
return;
|
|
|
|
|
|
case 'KRONICK_MUSIC_CLOSE':
|
|
if (!fromMusicFrame) {
|
|
return;
|
|
}
|
|
|
|
Kronick.closeMusicBrowser();
|
|
|
|
return;
|
|
|
|
|
|
case 'KRONICK_GIPHY_CLOSE':
|
|
if (!fromGiphyFrame) {
|
|
return;
|
|
}
|
|
|
|
Kronick.closeGiphyBrowser();
|
|
|
|
return;
|
|
|
|
|
|
case 'KRONICK_CHAT_GIF_PICK':
|
|
if (!fromChatFrame) {
|
|
return;
|
|
}
|
|
|
|
Kronick.openGiphyBrowser(
|
|
'chat'
|
|
);
|
|
|
|
return;
|
|
|
|
|
|
case 'KRONICK_GIPHY_SELECTED':
|
|
if (
|
|
!fromGiphyFrame
|
|
) {
|
|
console.warn(
|
|
'Rejected GIPHY selection: '
|
|
+ 'message did not come from '
|
|
+ 'the GIPHY frame.'
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
{
|
|
const gifUrl =
|
|
Kronick.getGiphyMessageUrl(
|
|
data
|
|
);
|
|
|
|
if (!gifUrl) {
|
|
console.warn(
|
|
'Rejected GIPHY selection: '
|
|
+ 'no usable GIF URL.',
|
|
data
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
if (
|
|
Kronick.state
|
|
.giphyTargetMode
|
|
=== 'chat'
|
|
) {
|
|
chatFrame?.contentWindow
|
|
?.postMessage(
|
|
{
|
|
type:
|
|
'CHAT_INSERT_AND_SEND_URL',
|
|
|
|
url:
|
|
gifUrl
|
|
},
|
|
Kronick.config
|
|
.APP_ORIGIN
|
|
);
|
|
|
|
Kronick.state
|
|
.giphyTargetMode =
|
|
'room';
|
|
|
|
Kronick.closeGiphyBrowser();
|
|
|
|
return;
|
|
}
|
|
|
|
if (
|
|
typeof Kronick.sendMediaToRoom
|
|
!== 'function'
|
|
) {
|
|
console.error(
|
|
'Kronick.sendMediaToRoom '
|
|
+ 'is not available.'
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
Kronick.sendMediaToRoom({
|
|
mediaType:
|
|
'gif',
|
|
|
|
url:
|
|
gifUrl,
|
|
|
|
title:
|
|
data.title || 'GIF'
|
|
});
|
|
|
|
Kronick.closeGiphyBrowser();
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
case 'KRONICK_GIPHY_AVATAR_SET':
|
|
if (!fromGiphyFrame) {
|
|
console.warn(
|
|
'Rejected GIPHY avatar: '
|
|
+ 'message did not come from '
|
|
+ 'the GIPHY frame.'
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
{
|
|
const avatarUrl =
|
|
Kronick.getGiphyMessageUrl(
|
|
data
|
|
);
|
|
|
|
if (!avatarUrl) {
|
|
console.warn(
|
|
'Rejected GIPHY avatar: '
|
|
+ 'no usable GIF URL.',
|
|
data
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
console.log(
|
|
'GIPHY AVATAR RECEIVED:',
|
|
{
|
|
url:
|
|
avatarUrl,
|
|
|
|
title:
|
|
data.title
|
|
|| data.gif?.title
|
|
|| ''
|
|
}
|
|
);
|
|
|
|
Kronick
|
|
.setLocalAvatar(
|
|
avatarUrl
|
|
)
|
|
.catch(
|
|
err => {
|
|
console.error(
|
|
'Could not set '
|
|
+ 'GIPHY avatar:',
|
|
err
|
|
);
|
|
}
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
case 'KRONICK_MEDIA_SHARE':
|
|
if (
|
|
!fromMusicFrame
|
|
|| typeof data.url
|
|
!== 'string'
|
|
|| !data.url
|
|
) {
|
|
console.warn(
|
|
'Rejected media share:',
|
|
{
|
|
fromMusicFrame,
|
|
url:
|
|
data.url
|
|
}
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
console.log(
|
|
'MEDIA SHARE RECEIVED '
|
|
+ 'FROM MUSIC BROWSER:',
|
|
{
|
|
mediaType:
|
|
data.mediaType,
|
|
|
|
url:
|
|
data.url,
|
|
|
|
title:
|
|
data.title,
|
|
|
|
thumbnail:
|
|
data.thumbnail
|
|
}
|
|
);
|
|
|
|
/*
|
|
* Do NOT close the music browser here.
|
|
*
|
|
* Keeping it open lets the user continue
|
|
* searching, adding tracks to the queue,
|
|
* and selecting additional media while the
|
|
* shared player is active.
|
|
*/
|
|
|
|
if (
|
|
typeof Kronick.sendMediaToRoom
|
|
!== 'function'
|
|
) {
|
|
console.error(
|
|
'Kronick.sendMediaToRoom '
|
|
+ 'is not available. '
|
|
+ 'Check media-controller.js '
|
|
+ 'for syntax/load errors.'
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* Send the media selection to LiveKit
|
|
* and play it locally.
|
|
*/
|
|
Kronick.sendMediaToRoom(
|
|
data
|
|
);
|
|
|
|
/*
|
|
* Bring the shared media window above
|
|
* the other floating tool windows.
|
|
*/
|
|
requestAnimationFrame(
|
|
() => {
|
|
const sharedMediaWrap =
|
|
document.getElementById(
|
|
'sharedMediaWrap'
|
|
);
|
|
|
|
if (!sharedMediaWrap) {
|
|
console.warn(
|
|
'sharedMediaWrap '
|
|
+ 'was not found.'
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
sharedMediaWrap.style.display =
|
|
'flex';
|
|
|
|
if (
|
|
typeof Kronick
|
|
.bringToolWindowToFront
|
|
=== 'function'
|
|
) {
|
|
Kronick
|
|
.bringToolWindowToFront(
|
|
sharedMediaWrap
|
|
);
|
|
}
|
|
|
|
if (
|
|
typeof Kronick
|
|
.keepWindowInViewport
|
|
=== 'function'
|
|
) {
|
|
Kronick
|
|
.keepWindowInViewport(
|
|
sharedMediaWrap
|
|
);
|
|
}
|
|
}
|
|
);
|
|
|
|
return;
|
|
|
|
|
|
case 'KRONICK_CHAT_SEND':
|
|
if (
|
|
!fromChatFrame
|
|
|| !data.payload
|
|
|| typeof data.payload
|
|
!== 'object'
|
|
) {
|
|
return;
|
|
}
|
|
|
|
Kronick.sendChatToRoom(
|
|
data.payload
|
|
);
|
|
|
|
return;
|
|
|
|
|
|
default:
|
|
console.warn(
|
|
'Unhandled window message:',
|
|
data.type
|
|
);
|
|
}
|
|
};
|
|
|
|
|
|
/*
|
|
* Extract a usable GIF URL from a GIPHY iframe message.
|
|
*
|
|
* Current giphy.js sends both data.url and data.gif.url.
|
|
* Prefer the nested GIF URL first so this remains compatible
|
|
* if the outer message structure changes again later.
|
|
*/
|
|
Kronick.getGiphyMessageUrl =
|
|
function getGiphyMessageUrl(data) {
|
|
const candidates = [
|
|
data?.gif?.url,
|
|
data?.url,
|
|
data?.gif?.thumb
|
|
];
|
|
|
|
for (
|
|
const candidate
|
|
of candidates
|
|
) {
|
|
if (
|
|
typeof candidate
|
|
!== 'string'
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
const url =
|
|
candidate.trim();
|
|
|
|
if (!url) {
|
|
continue;
|
|
}
|
|
|
|
/*
|
|
* The avatar must be an actual image/media URL,
|
|
* not a GIPHY landing page.
|
|
*/
|
|
try {
|
|
const parsed =
|
|
new URL(
|
|
url,
|
|
window.location.origin
|
|
);
|
|
|
|
if (
|
|
parsed.hostname
|
|
=== 'giphy.com'
|
|
|| parsed.hostname
|
|
=== 'www.giphy.com'
|
|
) {
|
|
console.warn(
|
|
'Ignoring GIPHY page URL '
|
|
+ 'instead of media URL:',
|
|
url
|
|
);
|
|
|
|
continue;
|
|
}
|
|
|
|
return parsed.href;
|
|
|
|
} catch (
|
|
error
|
|
) {
|
|
console.warn(
|
|
'Ignoring invalid GIPHY URL:',
|
|
{
|
|
url,
|
|
error:
|
|
error?.message
|
|
|| String(error)
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
return '';
|
|
};
|
|
|
|
|
|
/*
|
|
* Set the selected Giphy image as the local avatar.
|
|
*
|
|
* This updates:
|
|
* 1. the avatar input
|
|
* 2. localStorage
|
|
* 3. LiveKit participant metadata
|
|
* 4. the local participant tile immediately
|
|
*
|
|
* The GIPHY browser is only closed after the local update
|
|
* has been attempted.
|
|
*/
|
|
Kronick.setLocalAvatar =
|
|
async function setLocalAvatar(url) {
|
|
if (
|
|
typeof url !== 'string'
|
|
|| !url.trim()
|
|
) {
|
|
throw new Error(
|
|
'Avatar URL is empty.'
|
|
);
|
|
}
|
|
|
|
const avatarUrl =
|
|
url.trim();
|
|
|
|
/*
|
|
* Reject a normal giphy.com landing page. Those URLs
|
|
* cannot be used directly as an <img> avatar.
|
|
*/
|
|
try {
|
|
const parsed =
|
|
new URL(
|
|
avatarUrl,
|
|
window.location.origin
|
|
);
|
|
|
|
if (
|
|
parsed.hostname
|
|
=== 'giphy.com'
|
|
|| parsed.hostname
|
|
=== 'www.giphy.com'
|
|
) {
|
|
throw new Error(
|
|
'Received a GIPHY page URL '
|
|
+ 'instead of an image URL.'
|
|
);
|
|
}
|
|
} catch (
|
|
error
|
|
) {
|
|
console.error(
|
|
'Invalid avatar URL:',
|
|
{
|
|
url:
|
|
avatarUrl,
|
|
|
|
error:
|
|
error?.message
|
|
|| String(error)
|
|
}
|
|
);
|
|
|
|
throw error;
|
|
}
|
|
|
|
|
|
const avatarInput =
|
|
document.getElementById(
|
|
'avatarUrl'
|
|
);
|
|
|
|
if (avatarInput) {
|
|
avatarInput.value =
|
|
avatarUrl;
|
|
|
|
/*
|
|
* Notify any code listening to the avatar field.
|
|
*/
|
|
avatarInput.dispatchEvent(
|
|
new Event(
|
|
'input',
|
|
{
|
|
bubbles:
|
|
true
|
|
}
|
|
)
|
|
);
|
|
|
|
avatarInput.dispatchEvent(
|
|
new Event(
|
|
'change',
|
|
{
|
|
bubbles:
|
|
true
|
|
}
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
localStorage.setItem(
|
|
'lastAvatarUrl',
|
|
avatarUrl
|
|
);
|
|
|
|
|
|
const room =
|
|
Kronick.state?.room;
|
|
|
|
const participant =
|
|
room?.localParticipant;
|
|
|
|
|
|
if (!participant) {
|
|
console.log(
|
|
'Avatar saved locally. '
|
|
+ 'No active LiveKit participant '
|
|
+ 'is connected yet.',
|
|
avatarUrl
|
|
);
|
|
|
|
Kronick.closeGiphyBrowser();
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
let metaObj = {};
|
|
|
|
try {
|
|
metaObj =
|
|
participant.metadata
|
|
? JSON.parse(
|
|
participant.metadata
|
|
)
|
|
: {};
|
|
|
|
if (
|
|
!metaObj
|
|
|| typeof metaObj
|
|
!== 'object'
|
|
|| Array.isArray(
|
|
metaObj
|
|
)
|
|
) {
|
|
metaObj = {};
|
|
}
|
|
|
|
} catch (
|
|
error
|
|
) {
|
|
console.warn(
|
|
'Could not parse existing '
|
|
+ 'participant metadata. '
|
|
+ 'Starting with a new object.',
|
|
{
|
|
metadata:
|
|
participant.metadata,
|
|
|
|
error:
|
|
error?.message
|
|
|| String(error)
|
|
}
|
|
);
|
|
|
|
metaObj = {};
|
|
}
|
|
|
|
|
|
metaObj.avatar =
|
|
avatarUrl;
|
|
|
|
const newMetadata =
|
|
JSON.stringify(
|
|
metaObj
|
|
);
|
|
|
|
|
|
console.log(
|
|
'UPDATING LOCAL AVATAR METADATA:',
|
|
{
|
|
identity:
|
|
participant.identity,
|
|
|
|
avatar:
|
|
avatarUrl,
|
|
|
|
metadata:
|
|
newMetadata
|
|
}
|
|
);
|
|
|
|
|
|
/*
|
|
* LiveKit publishes the new metadata to the room.
|
|
*/
|
|
await participant
|
|
.setMetadata(
|
|
newMetadata
|
|
);
|
|
|
|
|
|
console.log(
|
|
'LOCAL AVATAR METADATA UPDATED:',
|
|
{
|
|
identity:
|
|
participant.identity,
|
|
|
|
avatar:
|
|
avatarUrl,
|
|
|
|
metadata:
|
|
participant.metadata
|
|
}
|
|
);
|
|
|
|
|
|
/*
|
|
* Do not wait for ParticipantMetadataChanged to redraw
|
|
* our own tile. Update it immediately as well.
|
|
*/
|
|
if (
|
|
typeof Kronick
|
|
.updateParticipantAvatar
|
|
=== 'function'
|
|
) {
|
|
Kronick.updateParticipantAvatar(
|
|
participant
|
|
);
|
|
} else {
|
|
console.warn(
|
|
'Kronick.updateParticipantAvatar '
|
|
+ 'is not available.'
|
|
);
|
|
}
|
|
|
|
|
|
Kronick.closeGiphyBrowser();
|
|
};
|