testapp: enhance webhook event handling

This commit is contained in:
Carlos Santos 2025-05-13 13:12:08 +02:00
parent 886fd5109b
commit 8993cab2e8
4 changed files with 98 additions and 20 deletions

2
.gitignore vendored
View File

@ -57,3 +57,5 @@ backend/src/typings/
frontend/webcomponent/**/openvidu-meet.**.js frontend/webcomponent/**/openvidu-meet.**.js
testapp/public/js/app.js testapp/public/js/app.js
testapp/public/js/webcomponent.js testapp/public/js/webcomponent.js
frontend/webcomponent/test-results/.last-run.json
frontend/webcomponent/test_localstorage_state.json

View File

@ -1,5 +1,13 @@
const socket = (window as any).io(); const socket = (window as any).io();
let meet: {
endMeeting: () => void;
leaveRoom: () => void;
on: (event: string, callback: (event: CustomEvent<any>) => void) => void;
};
let roomId: string | undefined;
let showAllWebhooksCheckbox: HTMLInputElement | null;
/** /**
* Add a component event to the events log * Add a component event to the events log
*/ */
@ -46,22 +54,42 @@ const clearWebhookEventsByRoom = (roomId: string): void => {
} }
}; };
const shouldShowWebhook = (event: any): boolean => {
return showAllWebhooksCheckbox?.checked || event.data.roomId === roomId;
};
const listenWebhookServerEvents = () => { const listenWebhookServerEvents = () => {
socket.on('webhookEvent', (event: any) => { socket.on('webhookEvent', (event: any) => {
console.log('Webhook received:', event); console.log('Webhook received:', event);
const isMeetingEnded = event.event === 'meetingEnded'; const webhookRoomId = event.data.roomId;
const roomId = event.data.roomId;
if (roomId) { if (webhookRoomId) {
saveWebhookEventToStorage(roomId, event); saveWebhookEventToStorage(webhookRoomId, event);
}
if (!shouldShowWebhook(event)) {
console.log('Ignoring webhook event:', event);
return;
} }
addWebhookEventElement(event); addWebhookEventElement(event);
// Clean up the previous events // Clean up the previous events
if (isMeetingEnded) clearWebhookEventsByRoom(roomId); const isMeetingEnded = event.event === 'meetingEnded';
if (isMeetingEnded) clearWebhookEventsByRoom(webhookRoomId);
}); });
}; };
const renderStoredWebhookEvents = (roomId: string) => {
const webhookLogList = document.getElementById('webhook-log-list');
if (webhookLogList) {
while (webhookLogList.firstChild) {
webhookLogList.removeChild(webhookLogList.firstChild);
}
}
const events = getWebhookEventsFromStorage(roomId);
events.forEach((event) => addWebhookEventElement(event));
};
const addWebhookEventElement = (event: any) => { const addWebhookEventElement = (event: any) => {
const webhookLogList = document.getElementById('webhook-log-list'); const webhookLogList = document.getElementById('webhook-log-list');
if (webhookLogList) { if (webhookLogList) {
@ -167,11 +195,15 @@ const listenWebComponentEvents = () => {
console.log('LEFT event received:', event); console.log('LEFT event received:', event);
addEventToLog('LEFT', JSON.stringify(event)); addEventToLog('LEFT', JSON.stringify(event));
}); });
meet.on('MEETING_ENDED', (event: CustomEvent<any>) => {
console.log('MEETING_ENDED event received:', event);
addEventToLog('MEETING_ENDED', JSON.stringify(event));
});
}; };
// Set up commands for the web component
const setUpWebComponentCommands = () => { const setUpWebComponentCommands = () => {
const meet = document.querySelector('openvidu-meet') as any;
if (!meet) { if (!meet) {
console.error('openvidu-meet component not found'); console.error('openvidu-meet component not found');
alert('openvidu-meet component not found in the DOM'); alert('openvidu-meet component not found in the DOM');
@ -189,21 +221,29 @@ const setUpWebComponentCommands = () => {
?.addEventListener('click', () => meet.leaveRoom()); ?.addEventListener('click', () => meet.leaveRoom());
// Toggle chat button click handler // Toggle chat button click handler
document // document
.getElementById('toggle-chat-btn') // .getElementById('toggle-chat-btn')
?.addEventListener('click', () => meet.toggleChat()); // ?.addEventListener('click', () => meet.toggleChat());
}; };
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const roomId = document.getElementById('room-id')?.textContent?.trim(); roomId = document.getElementById('room-id')?.textContent?.trim();
showAllWebhooksCheckbox = document.getElementById(
'show-all-webhooks'
) as HTMLInputElement;
meet = document.querySelector('openvidu-meet') as any;
if (!roomId) { if (!roomId) {
console.error('Room ID not found in the DOM'); console.error('Room ID not found in the DOM');
alert('Room ID not found in the DOM'); alert('Room ID not found in the DOM');
return; return;
} }
const events = getWebhookEventsFromStorage(roomId); renderStoredWebhookEvents(roomId);
events.forEach((event) => addWebhookEventElement(event));
listenWebhookServerEvents(); listenWebhookServerEvents();
listenWebComponentEvents(); listenWebComponentEvents();
setUpWebComponentCommands(); setUpWebComponentCommands();
showAllWebhooksCheckbox?.addEventListener('change', () =>
renderStoredWebhookEvents(roomId)
);
}); });

View File

@ -84,6 +84,30 @@
</button> </button>
</form> </form>
</li> </li>
{{! ONLY RECORDINGS LINK }}
<li>
<form action="/join-room" method="post">
<input
type="hidden"
name="roomUrl"
value="{{ publisherRoomUrl + '?viewRecordings=true' }}"
/>
<input
type="hidden"
name="participantRole"
value="publisher"
/>
<input
type="hidden"
name="roomId"
value="{{ roomId }}"
/>
<button type="submit" id="join-as-publisher" class="dropdown-item">
View Recordings
</button>
</form>
</li>
</ul> </ul>
</div> </div>
</li> </li>

View File

@ -19,12 +19,13 @@
<div id="control-panel" class="d-flex flex-column"> <div id="control-panel" class="d-flex flex-column">
<span> <span>
<span id="participant-role" class="h5"> <span id="participant-role" class="h5">
{{#isModerator}}<span class="badge bg-success">Moderator</span {{#isModerator}}
>{{/isModerator}} <span class="badge bg-success">Moderator</span>
{{^isModerator {{/isModerator}}
}}<span class="badge bg-secondary">Participant</span {{^isModerator}}
>{{/isModerator}} <span class="badge bg-secondary">Participant</span>
{{/isModerator}}
</span> </span>
<span id="room-id" class="text-muted"> <span id="room-id" class="text-muted">
{{ roomId }} {{ roomId }}
@ -41,9 +42,9 @@
<button id="leave-room-btn" class="btn btn-warning"> <button id="leave-room-btn" class="btn btn-warning">
Leave Room Leave Room
</button> </button>
<button id="toggle-chat-btn" class="btn btn-success"> <!-- <button id="toggle-chat-btn" class="btn btn-success">
Toggle Chat Toggle Chat
</button> </button> -->
</div> </div>
<!-- Events --> <!-- Events -->
@ -57,6 +58,17 @@
<!-- Webhooks Log --> <!-- Webhooks Log -->
<div class="section"> <div class="section">
<h5 class="title">Webhook</h5> <h5 class="title">Webhook</h5>
<div class="form-check mb-2">
<input
class="form-check-input"
type="checkbox"
value=""
id="show-all-webhooks"
/>
<label class="form-check-label" for="show-all-webhooks">
Show all
</label>
</div>
<ul id="webhook-log-list" class="log-list"> <ul id="webhook-log-list" class="log-list">
<!-- Webhooks will be added dynamically here --> <!-- Webhooks will be added dynamically here -->
</ul> </ul>