A platform for community discussion. Free, open, simple.
FIX: Zoom webinar livestream not joining on countdown end sometimes (#42391)
Martin Brennan7b2a2d5fde34
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/lib/zoom-component-view-dom.js b/plugins/discourse-calendar/assets/javascripts/discourse/lib/zoom-component-view-dom.jsindex 75e4f5339cd..6b7445131d2 100644--- a/plugins/discourse-calendar/assets/javascripts/discourse/lib/zoom-component-view-dom.js+++ b/plugins/discourse-calendar/assets/javascripts/discourse/lib/zoom-component-view-dom.js@@ -10,12 +10,18 @@ const MIN_VIDEO_HEIGHT = 135;const MAX_VIDEO_HEIGHT = 810;export function computeZoomViewSize(root) {+ // Zoom is configured with this size once, at init, and only ever corrects it+ // if a later sync gets through. Falling back to the minimum because the root+ // happened to measure zero locks the component view into a 240px video for+ // the rest of the session, so measure the container it sits in instead.+ const measuredWidth =+ root?.getBoundingClientRect().width ||+ root?.parentElement?.getBoundingClientRect().width ||+ 0;+const width = Math.max(MIN_VIDEO_WIDTH,- Math.min(- MAX_VIDEO_WIDTH,- Math.floor(root?.getBoundingClientRect().width || 0)- )+ Math.min(MAX_VIDEO_WIDTH, Math.floor(measuredWidth)));const height = Math.max(diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/lib/zoom-meeting-session.js b/plugins/discourse-calendar/assets/javascripts/discourse/lib/zoom-meeting-session.jsindex 0f8b2d463b5..f6e47b4bbe5 100644--- a/plugins/discourse-calendar/assets/javascripts/discourse/lib/zoom-meeting-session.js+++ b/plugins/discourse-calendar/assets/javascripts/discourse/lib/zoom-meeting-session.js@@ -1,6 +1,7 @@import { tracked } from "@glimmer/tracking";import { setOwner } from "@ember/owner";import { isEmpty } from "@ember/utils";+import { bind } from "discourse/lib/decorators";import { i18n } from "discourse-i18n";import fetchZoomJoinPayload from "./fetch-zoom-join-payload";import { loadZoomMeetingSdkEmbedded } from "./load-zoom-meeting-sdk";@@ -41,6 +42,8 @@ export default class ZoomMeetingSession {this.canJoin = canJoin;this.onBeforeJoinAttempt = onBeforeJoinAttempt;this.onJoined = onJoined;++ document.addEventListener("visibilitychange", this.onVisibilityChange);}teardown() {@@ -48,6 +51,7 @@ export default class ZoomMeetingSession {clearInterval(this.retryTimer);cancelAnimationFrame(this.layoutFrame);cancelAnimationFrame(this.videoSyncFrame);+ document.removeEventListener("visibilitychange", this.onVisibilityChange);this.zoomClient?.leaveMeeting?.();}@@ -126,13 +130,37 @@ export default class ZoomMeetingSession {} finally {this.isJoining = false;- this.layoutFrame = window.requestAnimationFrame(() =>- syncZoomLayout(this.element)- );- this.videoSyncFrame = window.requestAnimationFrame(() =>- this.syncVideoSize()- );+ this.scheduleViewSync();+ }+ }++ // Both the frame callbacks below and the resize observer the modifier+ // attaches are suspended while the document is hidden, so a join that lands+ // in a background tab leaves Zoom rendering at whatever size it was last+ // told about. `onVisibilityChange` picks the work back up on return.+ scheduleViewSync() {+ if (document.hidden) {+ return;+ }++ cancelAnimationFrame(this.layoutFrame);+ cancelAnimationFrame(this.videoSyncFrame);++ this.layoutFrame = window.requestAnimationFrame(() =>+ syncZoomLayout(this.element)+ );+ this.videoSyncFrame = window.requestAnimationFrame(() =>+ this.syncVideoSize()+ );+ }++ @bind+ onVisibilityChange() {+ if (document.hidden || this.#tornDown || !this.zoomClient) {+ return;}++ this.scheduleViewSync();}async performJoin() {diff --git a/plugins/discourse-calendar/assets/stylesheets/common/livestream.scss b/plugins/discourse-calendar/assets/stylesheets/common/livestream.scssindex 50b08a8940b..09958e14d95 100644--- a/plugins/discourse-calendar/assets/stylesheets/common/livestream.scss+++ b/plugins/discourse-calendar/assets/stylesheets/common/livestream.scss@@ -70,8 +70,14 @@ body.chat-enabled.livestream-topic.confirmed-event-assistance {}&__frame {+ // The frame has to lay out as soon as a join starts, not once Zoom mounts:+ // the component view is initialized with a video size measured from this+ // element, and a collapsed element measures zero.+ display: none;+// Zoom sets `display` inline on this element when it mounts, which beats- // anything declared here. Only `justify-content` and `padding-top` apply.+ // anything declared here. Only `justify-content` and `padding-top` apply+ // from then on.//// Until the meeting is joined the widget is a small dialog (spinner,// "meeting has not started"), so it is centred. Once joined it fills thediff --git a/plugins/discourse-calendar/assets/stylesheets/desktop/livestream.scss b/plugins/discourse-calendar/assets/stylesheets/desktop/livestream.scssindex c7963a5228b..4b437569189 100644--- a/plugins/discourse-calendar/assets/stylesheets/desktop/livestream.scss+++ b/plugins/discourse-calendar/assets/stylesheets/desktop/livestream.scss@@ -84,7 +84,6 @@border-radius: 8px;overflow: hidden;background: var(--secondary);- display: none;}}}diff --git a/plugins/discourse-calendar/test/javascripts/lib/zoom-component-view-dom-test.js b/plugins/discourse-calendar/test/javascripts/lib/zoom-component-view-dom-test.jsnew file mode 100644index 00000000000..645f5533b2c--- /dev/null+++ b/plugins/discourse-calendar/test/javascripts/lib/zoom-component-view-dom-test.js@@ -0,0 +1,48 @@+import { module, test } from "qunit";+import { computeZoomViewSize } from "discourse/plugins/discourse-calendar/discourse/lib/zoom-component-view-dom";++function elementOfWidth(width) {+ return { getBoundingClientRect: () => ({ width }) };+}++module("Unit | Lib | zoom-component-view-dom", function () {+ test("computeZoomViewSize measures the root", function (assert) {+ assert.deepEqual(computeZoomViewSize(elementOfWidth(754)), {+ width: 754,+ height: 424,+ });+ });++ test("computeZoomViewSize falls back to the container when the root is collapsed", function (assert) {+ const root = {+ ...elementOfWidth(0),+ parentElement: elementOfWidth(754),+ };++ assert.deepEqual(+ computeZoomViewSize(root),+ { width: 754, height: 424 },+ "a root that has not been laid out yet does not pin Zoom to the minimum"+ );+ });++ test("computeZoomViewSize clamps to the supported range", function (assert) {+ assert.deepEqual(+ computeZoomViewSize(elementOfWidth(120)),+ { width: 240, height: 135 },+ "below the minimum"+ );++ assert.deepEqual(+ computeZoomViewSize(elementOfWidth(4000)),+ { width: 1440, height: 810 },+ "above the maximum"+ );++ assert.deepEqual(+ computeZoomViewSize(null),+ { width: 240, height: 135 },+ "without an element to measure"+ );+ });+});diff --git a/plugins/discourse-calendar/test/javascripts/unit/lib/zoom-meeting-session-test.js b/plugins/discourse-calendar/test/javascripts/unit/lib/zoom-meeting-session-test.jsnew file mode 100644index 00000000000..538cd59ccaf--- /dev/null+++ b/plugins/discourse-calendar/test/javascripts/unit/lib/zoom-meeting-session-test.js@@ -0,0 +1,97 @@+import { getOwner } from "@ember/owner";+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import sinon from "sinon";+import ZoomMeetingSession from "../../discourse/lib/zoom-meeting-session";++// `document.hidden` is an inherited accessor, so it can only be faked by+// shadowing it on the document itself.+function stubDocumentHidden(hidden) {+ Object.defineProperty(document, "hidden", {+ configurable: true,+ get: () => hidden,+ });+}++function restoreDocumentHidden() {+ delete document.hidden;+}++// The view sync runs in a frame callback, which `settled` knows nothing about.+function nextFrame() {+ return new Promise((resolve) => requestAnimationFrame(resolve));+}++module("Unit | Lib | ZoomMeetingSession", function (hooks) {+ setupTest(hooks);++ hooks.afterEach(function () {+ restoreDocumentHidden();+ });++ test("defers sizing Zoom's view while the document is hidden", async function (assert) {+ const session = new ZoomMeetingSession(getOwner(this), {+ topicId: 1,+ canJoin: () => true,+ });+ sinon.stub(session, "performJoin").resolves();+ const syncVideoSize = sinon.stub(session, "syncVideoSize");+ session.zoomClient = { updateVideoOptions: () => {} };++ stubDocumentHidden(true);++ await session.join();+ await nextFrame();++ assert.false(+ syncVideoSize.called,+ "frame callbacks do not run in a background tab"+ );++ stubDocumentHidden(false);+ document.dispatchEvent(new Event("visibilitychange"));+ await nextFrame();++ assert.true(+ syncVideoSize.called,+ "the view is sized once the tab is visible again"+ );++ session.teardown();+ });++ test("sizes Zoom's view straight away when the document is visible", async function (assert) {+ const session = new ZoomMeetingSession(getOwner(this), {+ topicId: 1,+ canJoin: () => true,+ });+ sinon.stub(session, "performJoin").resolves();+ const syncVideoSize = sinon.stub(session, "syncVideoSize");++ stubDocumentHidden(false);++ await session.join();+ await nextFrame();++ assert.true(syncVideoSize.called);++ session.teardown();+ });++ test("stops listening for visibility changes once torn down", async function (assert) {+ const session = new ZoomMeetingSession(getOwner(this), {+ topicId: 1,+ canJoin: () => true,+ });+ const syncVideoSize = sinon.stub(session, "syncVideoSize");+ session.zoomClient = { updateVideoOptions: () => {} };++ stubDocumentHidden(false);+ session.teardown();++ document.dispatchEvent(new Event("visibilitychange"));+ await nextFrame();++ assert.false(syncVideoSize.called);+ });+});