diff --git a/src/common/communication.ts b/src/common/communication.ts index 03049d58..16b1a3e3 100644 --- a/src/common/communication.ts +++ b/src/common/communication.ts @@ -130,6 +130,7 @@ export const CALLS_LINK_CLICK = 'calls-link-click'; export const CALLS_JOINED_CALL = 'calls-joined-call'; export const CALLS_POPOUT_FOCUS = 'calls-popout-focus'; export const CALLS_ERROR = 'calls-error'; +export const CALLS_JOIN_REQUEST = 'calls-join-request'; export const REQUEST_CLEAR_DOWNLOADS_DROPDOWN = 'request-clear-downloads-dropdown'; export const CLOSE_DOWNLOADS_DROPDOWN = 'close-downloads-dropdown'; diff --git a/src/main/preload/callsWidget.js b/src/main/preload/callsWidget.js index a7812144..dd895484 100644 --- a/src/main/preload/callsWidget.js +++ b/src/main/preload/callsWidget.js @@ -16,8 +16,12 @@ import { DESKTOP_SOURCES_RESULT, DESKTOP_SOURCES_MODAL_REQUEST, CALLS_LINK_CLICK, + CALLS_JOIN_REQUEST, } from 'common/communication'; +// +// Handle messages FROM the widget. (i.e., widget's webapp -> widget's window) +// window.addEventListener('message', ({origin, data = {}} = {}) => { const {type, message = {}} = data; @@ -48,13 +52,17 @@ window.addEventListener('message', ({origin, data = {}} = {}) => { case CALLS_JOINED_CALL: case CALLS_POPOUT_FOCUS: case CALLS_ERROR: - case CALLS_LEAVE_CALL: { + case CALLS_LEAVE_CALL: + case CALLS_JOIN_REQUEST: { ipcRenderer.send(type, 'widget', message); break; } } }); +// +// Handle messages TO the widget. +// ipcRenderer.on(DESKTOP_SOURCES_RESULT, (event, sources) => { window.postMessage( { diff --git a/src/main/preload/mattermost.js b/src/main/preload/mattermost.js index b389fba3..b1647096 100644 --- a/src/main/preload/mattermost.js +++ b/src/main/preload/mattermost.js @@ -36,6 +36,7 @@ import { CALLS_WIDGET_SHARE_SCREEN, CLOSE_DOWNLOADS_DROPDOWN, CALLS_ERROR, + CALLS_JOIN_REQUEST, } from 'common/communication'; const UNREAD_COUNT_INTERVAL = 1000; @@ -352,6 +353,16 @@ ipcRenderer.on(CALLS_ERROR, (event, message) => { ); }); +ipcRenderer.on(CALLS_JOIN_REQUEST, (event, message) => { + window.postMessage( + { + type: CALLS_JOIN_REQUEST, + message, + }, + window.location.origin, + ); +}); + /* eslint-enable no-magic-numbers */ window.addEventListener('resize', () => { diff --git a/src/main/windows/callsWidgetWindow.test.js b/src/main/windows/callsWidgetWindow.test.js index 84e585c8..4bc02dd4 100644 --- a/src/main/windows/callsWidgetWindow.test.js +++ b/src/main/windows/callsWidgetWindow.test.js @@ -7,7 +7,7 @@ import {BrowserWindow, desktopCapturer, systemPreferences} from 'electron'; import ServerViewState from 'app/serverViewState'; -import {CALLS_WIDGET_SHARE_SCREEN, CALLS_JOINED_CALL} from 'common/communication'; +import {CALLS_WIDGET_SHARE_SCREEN, CALLS_JOINED_CALL, CALLS_JOIN_REQUEST} from 'common/communication'; import { MINIMUM_CALLS_WIDGET_WIDTH, MINIMUM_CALLS_WIDGET_HEIGHT, @@ -958,4 +958,35 @@ describe('main/windows/callsWidgetWindow', () => { expect(handler).toHaveBeenCalledTimes(1); }); }); + + describe('handleCallsJoinRequest', () => { + const view = { + view: { + server: { + id: 'server-1', + }, + }, + sendToRenderer: jest.fn(), + }; + const callsWidgetWindow = new CallsWidgetWindow(); + callsWidgetWindow.mainView = view; + + const focus = jest.fn(); + + beforeEach(() => { + MainWindow.get.mockReturnValue({focus}); + ViewManager.getView.mockReturnValue(view); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it('should pass through the join call callID to the webapp', () => { + callsWidgetWindow.handleCallsJoinRequest('', {callID: 'thecallchannelid'}); + expect(ServerViewState.switchServer).toHaveBeenCalledWith('server-1'); + expect(focus).toHaveBeenCalled(); + expect(view.sendToRenderer).toBeCalledWith(CALLS_JOIN_REQUEST, {callID: 'thecallchannelid'}); + }); + }); }); diff --git a/src/main/windows/callsWidgetWindow.ts b/src/main/windows/callsWidgetWindow.ts index 157c2521..eb08cc58 100644 --- a/src/main/windows/callsWidgetWindow.ts +++ b/src/main/windows/callsWidgetWindow.ts @@ -8,6 +8,7 @@ import { CallsEventHandler, CallsJoinCallMessage, CallsJoinedCallMessage, + CallsJoinRequestMessage, CallsLinkClickMessage, CallsWidgetResizeMessage, CallsWidgetShareScreenMessage, @@ -24,6 +25,7 @@ import { BROWSER_HISTORY_PUSH, CALLS_ERROR, CALLS_JOIN_CALL, + CALLS_JOIN_REQUEST, CALLS_JOINED_CALL, CALLS_LEAVE_CALL, CALLS_LINK_CLICK, @@ -75,6 +77,7 @@ export class CallsWidgetWindow { ipcMain.on(CALLS_WIDGET_CHANNEL_LINK_CLICK, this.genCallsEventHandler(this.handleCallsWidgetChannelLinkClick)); ipcMain.on(CALLS_ERROR, this.genCallsEventHandler(this.handleCallsError)); ipcMain.on(CALLS_LINK_CLICK, this.genCallsEventHandler(this.handleCallsLinkClick)); + ipcMain.on(CALLS_JOIN_REQUEST, this.genCallsEventHandler(this.handleCallsJoinRequest)); } /** @@ -530,6 +533,17 @@ export class CallsWidgetWindow { MainWindow.get()?.focus(); this.mainView?.sendToRenderer(BROWSER_HISTORY_PUSH, msg.link); } + + private handleCallsJoinRequest = (_: string, msg: CallsJoinRequestMessage) => { + log.debug('handleCallsJoinRequest with callID', msg.callID); + if (!this.serverID) { + return; + } + + ServerViewState.switchServer(this.serverID); + MainWindow.get()?.focus(); + this.mainView?.sendToRenderer(CALLS_JOIN_REQUEST, msg); + } } const callsWidgetWindow = new CallsWidgetWindow(); diff --git a/src/types/calls.ts b/src/types/calls.ts index f8b598da..b956021e 100644 --- a/src/types/calls.ts +++ b/src/types/calls.ts @@ -34,4 +34,8 @@ export type CallsLinkClickMessage = { link: string | URL; } +export type CallsJoinRequestMessage = { + callID: string; +} + export type CallsEventHandler = ((viewName: string, msg: any) => void) | ((viewName: string, opts: Electron.SourcesOptions) => Promise);