From ea523fc6ef25b15fd635c4398c8e6d29256d24e6 Mon Sep 17 00:00:00 2001
From: damencho <damencho@jitsi.org>
Date: Thu, 13 Jun 2024 10:31:07 +0300
Subject: [PATCH] feat(visitors): Fire not live connection on jicofo response
 for visitors.

---
 JitsiConnectionErrors.spec.ts                 |  4 +++-
 JitsiConnectionErrors.ts                      |  6 ++++++
 modules/xmpp/moderator.js                     | 18 +++++++++++++++++-
 types/hand-crafted/JitsiConnectionErrors.d.ts |  1 +
 4 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/JitsiConnectionErrors.spec.ts b/JitsiConnectionErrors.spec.ts
index af2e818150..b1f6bad570 100644
--- a/JitsiConnectionErrors.spec.ts
+++ b/JitsiConnectionErrors.spec.ts
@@ -5,6 +5,7 @@ import * as exported from "./JitsiConnectionErrors";
 describe( "/JitsiConnectionErrors members", () => {
     const {
         CONNECTION_DROPPED_ERROR,
+        NOT_LIVE_ERROR,
         OTHER_ERROR,
         PASSWORD_REQUIRED,
         SERVER_ERROR,
@@ -21,6 +22,7 @@ describe( "/JitsiConnectionErrors members", () => {
         expect( JitsiConnectionErrors ).toBeDefined();
 
         expect( JitsiConnectionErrors.CONNECTION_DROPPED_ERROR ).toBe( 'connection.droppedError' );
+        expect( JitsiConnectionErrors.NOT_LIVE_ERROR ).toBe( 'connection.notLiveError' );
         expect( JitsiConnectionErrors.OTHER_ERROR ).toBe( 'connection.otherError' );
         expect( JitsiConnectionErrors.PASSWORD_REQUIRED ).toBe( 'connection.passwordRequired' );
         expect( JitsiConnectionErrors.SERVER_ERROR ).toBe( 'connection.serverError' );
@@ -30,4 +32,4 @@ describe( "/JitsiConnectionErrors members", () => {
         const keys = Object.keys( others );
         expect( keys ).withContext( `Extra members: ${ keys.join( ", " ) }` ).toEqual( [] );
     } );
-} );
\ No newline at end of file
+} );
diff --git a/JitsiConnectionErrors.ts b/JitsiConnectionErrors.ts
index 7ce173534c..1a6d7122b6 100644
--- a/JitsiConnectionErrors.ts
+++ b/JitsiConnectionErrors.ts
@@ -16,6 +16,11 @@ export enum JitsiConnectionErrors {
      */
     CONNECTION_DROPPED_ERROR = 'connection.droppedError',
 
+    /**
+     * Not ready error. When the conference error is not ready according to jicofo.
+     */
+    NOT_LIVE_ERROR = 'connection.notLiveError',
+
     /**
      * Not specified errors.
      */
@@ -35,6 +40,7 @@ export enum JitsiConnectionErrors {
 
 // exported for backward compatibility
 export const CONNECTION_DROPPED_ERROR = JitsiConnectionErrors.CONNECTION_DROPPED_ERROR;
+export const NOT_LIVE_ERROR = JitsiConnectionErrors.NOT_LIVE_ERROR;
 export const OTHER_ERROR = JitsiConnectionErrors.OTHER_ERROR;
 export const PASSWORD_REQUIRED = JitsiConnectionErrors.PASSWORD_REQUIRED;
 export const SERVER_ERROR = JitsiConnectionErrors.SERVER_ERROR;
diff --git a/modules/xmpp/moderator.js b/modules/xmpp/moderator.js
index 9c94a46a81..83c27c9116 100644
--- a/modules/xmpp/moderator.js
+++ b/modules/xmpp/moderator.js
@@ -3,7 +3,8 @@ import { getLogger } from '@jitsi/logger';
 import $ from 'jquery';
 import { $iq } from 'strophe.js';
 
-import { CONNECTION_REDIRECTED } from '../../JitsiConnectionEvents';
+import { NOT_LIVE_ERROR } from '../../JitsiConnectionErrors';
+import { CONNECTION_FAILED, CONNECTION_REDIRECTED } from '../../JitsiConnectionEvents';
 import Settings from '../settings/Settings';
 import Listenable from '../util/Listenable';
 
@@ -276,6 +277,11 @@ export default class Moderator extends Listenable {
             conferenceRequest.properties.sipGatewayEnabled = 'true';
         }
 
+        // check for explicit false, all other cases is considered live
+        if ($(resultIq).find('>conference>property[name=\'live\'][value=\'false\']').length > 0) {
+            conferenceRequest.properties.live = 'false';
+        }
+
         return conferenceRequest;
     }
 
@@ -387,6 +393,16 @@ export default class Moderator extends Listenable {
         this.sipGatewayEnabled = conferenceRequest.properties.sipGatewayEnabled;
         logger.info(`Sip gateway enabled: ${this.sipGatewayEnabled}`);
 
+        if (conferenceRequest.properties.live === 'false' && this.options.preferVisitor) {
+            this.getNextTimeout(true);
+
+            logger.info('Conference is not live.');
+
+            this.xmpp.eventEmitter.emit(CONNECTION_FAILED, NOT_LIVE_ERROR);
+
+            return;
+        }
+
         if (conferenceRequest.ready) {
             // Reset the non-error timeout (because we've succeeded here).
             this.getNextTimeout(true);
diff --git a/types/hand-crafted/JitsiConnectionErrors.d.ts b/types/hand-crafted/JitsiConnectionErrors.d.ts
index 1d647e049a..69f2f4d2ae 100644
--- a/types/hand-crafted/JitsiConnectionErrors.d.ts
+++ b/types/hand-crafted/JitsiConnectionErrors.d.ts
@@ -1,5 +1,6 @@
 export enum JitsiConnectionErrors {
   CONNECTION_DROPPED_ERROR = 'connection.droppedError',
+  NOT_LIVE_ERROR = 'connection.notLiveError',
   OTHER_ERROR = 'connection.otherError',
   PASSWORD_REQUIRED = 'connection.passwordRequired',
   SERVER_ERROR = 'connection.serverError'