Upload files to "JS"
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
// Kronic Media bridge: playback is owned by media.kronickrusaders.ca.
|
||||
// Chat only broadcasts room events and hosts the external media window.
|
||||
window.Kronick = window.Kronick || {};
|
||||
|
||||
Kronick.normalizePublicMediaUrl = function(raw) {
|
||||
if (typeof raw !== 'string' || raw.length > 4096) return '';
|
||||
try {
|
||||
const url = new URL(raw, Kronick.config.MEDIA_ORIGIN);
|
||||
return ['https:', 'http:'].includes(url.protocol) ? url.href : '';
|
||||
} catch { return ''; }
|
||||
};
|
||||
|
||||
Kronick.notifyMediaFrame = function(message) {
|
||||
const frame = document.getElementById('musicFrame');
|
||||
if (!frame?.contentWindow) return;
|
||||
frame.contentWindow.postMessage(message || {
|
||||
type: 'KRONICK_CHAT_CONTEXT',
|
||||
room: Kronick.state.currentRoomName || '',
|
||||
connected: Boolean(Kronick.state.room?.localParticipant),
|
||||
user: Kronick.account?.user?.username || ''
|
||||
}, Kronick.config.MEDIA_ORIGIN);
|
||||
};
|
||||
|
||||
// Set when a shared item arrives before the external iframe is ready.
|
||||
Kronick.pendingMediaShare = null;
|
||||
Kronick.flushMediaShare = function() {
|
||||
if (!Kronick.pendingMediaShare) return;
|
||||
Kronick.notifyMediaFrame({
|
||||
type: 'KRONICK_ROOM_MEDIA_OPEN',
|
||||
room: Kronick.state.currentRoomName || '',
|
||||
media: Kronick.pendingMediaShare
|
||||
});
|
||||
};
|
||||
|
||||
Kronick.sendMediaToRoom = async function(data) {
|
||||
const room = Kronick.state.room;
|
||||
if (!room?.localParticipant) {
|
||||
alert('Join a Kronic chat room before sharing media.');
|
||||
return;
|
||||
}
|
||||
// The media app should send a canonical item URL (or a media ID in a future API).
|
||||
const url = Kronick.normalizePublicMediaUrl(data?.url);
|
||||
if (!url) {
|
||||
console.warn('Media share requires a valid URL.', data);
|
||||
return;
|
||||
}
|
||||
const mediaType = String(data.mediaType || 'video').slice(0, 24);
|
||||
const message = {
|
||||
type: 'media',
|
||||
mediaType, url,
|
||||
title: String(data.title || '').slice(0, 300),
|
||||
thumbnail: Kronick.normalizePublicMediaUrl(data.thumbnail || '')
|
||||
};
|
||||
try {
|
||||
await room.localParticipant.publishData(
|
||||
new TextEncoder().encode(JSON.stringify(message)),
|
||||
{reliable: true, topic: Kronick.config.DATA_TOPIC_MEDIA}
|
||||
);
|
||||
Kronick.handleRoomMedia(message);
|
||||
} catch(error) {
|
||||
console.error('Failed to share media to room:', error);
|
||||
}
|
||||
};
|
||||
|
||||
Kronick.handleRoomMedia = function(message) {
|
||||
if (!message || typeof message !== 'object') return;
|
||||
if (message.type === 'media-stop') {
|
||||
Kronick.pendingMediaShare = null;
|
||||
Kronick.notifyMediaFrame({type: 'KRONICK_ROOM_MEDIA_STOP'});
|
||||
return;
|
||||
}
|
||||
if (message.type !== 'media') return;
|
||||
const url = Kronick.normalizePublicMediaUrl(message.url);
|
||||
if (!url) return;
|
||||
if (String(message.mediaType).toLowerCase() === 'gif') {
|
||||
Kronick.showSharedGif(url);
|
||||
return;
|
||||
}
|
||||
Kronick.pendingMediaShare = {
|
||||
url,
|
||||
mediaType: String(message.mediaType || 'video').slice(0, 24),
|
||||
title: String(message.title || '').slice(0, 300),
|
||||
thumbnail: Kronick.normalizePublicMediaUrl(message.thumbnail || '')
|
||||
};
|
||||
Kronick.openMusicBrowser();
|
||||
Kronick.flushMediaShare();
|
||||
};
|
||||
|
||||
// A local close should not end playback for other room members.
|
||||
Kronick.closeSharedMedia = function(sendStop = false) {
|
||||
Kronick.closeMusicBrowser();
|
||||
if (!sendStop) return;
|
||||
const room = Kronick.state.room;
|
||||
if (!room?.localParticipant) return;
|
||||
room.localParticipant.publishData(
|
||||
new TextEncoder().encode(JSON.stringify({type: 'media-stop'})),
|
||||
{reliable: true, topic: Kronick.config.DATA_TOPIC_MEDIA}
|
||||
).catch(console.error);
|
||||
};
|
||||
Kronick.stopSharedMediaForRoom = function() {
|
||||
Kronick.closeSharedMedia(true);
|
||||
};
|
||||
|
||||
Kronick.showSharedGif = function(url) {
|
||||
const gif = document.getElementById('sharedGif');
|
||||
if (!gif) return;
|
||||
gif.style.display = 'none';
|
||||
gif.onload = () => { gif.style.display = 'block'; };
|
||||
gif.onerror = () => { gif.style.display = 'none'; };
|
||||
gif.src = url;
|
||||
clearTimeout(Kronick.state.sharedGifTimeout);
|
||||
Kronick.state.sharedGifTimeout = setTimeout(() => {
|
||||
gif.style.display = 'none';
|
||||
gif.removeAttribute('src');
|
||||
}, 8000);
|
||||
};
|
||||
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
const frame = document.getElementById('musicFrame');
|
||||
frame?.addEventListener('load', () => {
|
||||
Kronick.notifyMediaFrame();
|
||||
Kronick.flushMediaShare();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user