[MM-55152] Add new Desktop API endpoints, improve preload script, some clean-up (#2900)

* Add constants for app info, add to API

* Migrate history button

* Converted calls API over to context bridge, removed some unnecessary logging

* Convert to TS, add types for web app to consume

* Fix tests, prune

* Fix lint

* More changes to support the legacy API

* Force legacy code off, add support for unreads/mentions/expired through the API

* Fix issues with cross-tab login, removed need for log in/log out signalling

* Fixed test, typos

* Change package name for types

* Add some other stuff to the types

* PR feedback

* More feedback

* Use npm package

* Change types and API to provide off listeners

* Version number

* Lock

* Fix typo

* Add sessionID for calls
This commit is contained in:
Devin Binnie
2023-12-13 09:39:46 -05:00
committed by GitHub
parent 675ec6d661
commit 0cab09b7f5
41 changed files with 1071 additions and 1040 deletions

View File

@@ -3,11 +3,11 @@
/* eslint-disable max-lines */
import {BrowserWindow, desktopCapturer, systemPreferences} from 'electron';
import {BrowserWindow, desktopCapturer, systemPreferences, ipcMain} from 'electron';
import ServerViewState from 'app/serverViewState';
import {CALLS_WIDGET_SHARE_SCREEN, CALLS_JOINED_CALL, CALLS_JOIN_REQUEST} from 'common/communication';
import {CALLS_WIDGET_SHARE_SCREEN} from 'common/communication';
import {
MINIMUM_CALLS_WIDGET_WIDTH,
MINIMUM_CALLS_WIDGET_HEIGHT,
@@ -62,6 +62,7 @@ jest.mock('app/serverViewState', () => ({
}));
jest.mock('main/views/viewManager', () => ({
getView: jest.fn(),
getViewByWebContentsId: jest.fn(),
}));
jest.mock('../utils', () => ({
openScreensharePermissionsSettingsMacOS: jest.fn(),
@@ -187,11 +188,7 @@ describe('main/windows/callsWidgetWindow', () => {
it('should resize correctly', () => {
callsWidgetWindow.handleResize({
sender: {id: 'windowID'},
}, 'widget', {
element: 'calls-widget',
width: 300,
height: 100,
});
}, 300, 100);
expect(callsWidgetWindow.setBounds).toHaveBeenCalledWith({
x: 12,
y: 720 - (100 - MINIMUM_CALLS_WIDGET_HEIGHT),
@@ -204,11 +201,7 @@ describe('main/windows/callsWidgetWindow', () => {
callsWidgetWindow.win.webContents.getZoomFactor.mockReturnValue(2.0);
callsWidgetWindow.handleResize({
sender: {id: 'windowID'},
}, 'widget', {
element: 'calls-widget',
width: 300,
height: 100,
});
}, 300, 100);
expect(callsWidgetWindow.setBounds).toHaveBeenCalledWith({
x: 12,
y: 720 - (200 - MINIMUM_CALLS_WIDGET_HEIGHT),
@@ -221,11 +214,7 @@ describe('main/windows/callsWidgetWindow', () => {
callsWidgetWindow.win.webContents.getZoomFactor.mockReturnValue(0.5);
callsWidgetWindow.handleResize({
sender: {id: 'windowID'},
}, 'widget', {
element: 'calls-widget',
width: 300,
height: 100,
});
}, 300, 100);
expect(callsWidgetWindow.setBounds).toHaveBeenCalledWith({
x: 12,
y: 720 - (50 - MINIMUM_CALLS_WIDGET_HEIGHT),
@@ -288,51 +277,26 @@ describe('main/windows/callsWidgetWindow', () => {
it('handleShareScreen', () => {
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.isAllowedEvent = jest.fn();
callsWidgetWindow.mainView = {
webContentsId: 'goodID',
};
callsWidgetWindow.win = {
webContents: {
id: 'goodID',
send: jest.fn(),
},
};
const message = {
callID: 'test-call-id',
};
callsWidgetWindow.isAllowedEvent.mockReturnValue(false);
callsWidgetWindow.handleShareScreen({
sender: {id: 'badID'},
}, message);
}, 'sourceId', true);
expect(callsWidgetWindow.win.webContents.send).not.toHaveBeenCalled();
callsWidgetWindow.isAllowedEvent.mockReturnValue(true);
callsWidgetWindow.handleShareScreen({
sender: {id: 'goodID'},
}, 'widget', message);
expect(callsWidgetWindow.win.webContents.send).toHaveBeenCalledWith(CALLS_WIDGET_SHARE_SCREEN, message);
});
it('handleJoinedCall', () => {
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.isAllowedEvent = jest.fn();
callsWidgetWindow.mainView = {
webContentsId: 'goodID',
sendToRenderer: jest.fn(),
};
const message = {
callID: 'test-call-id',
};
callsWidgetWindow.isAllowedEvent.mockReturnValue(false);
callsWidgetWindow.handleJoinedCall({
sender: {id: 'badID'},
}, 'widget', message);
expect(callsWidgetWindow.mainView.sendToRenderer).not.toHaveBeenCalled();
callsWidgetWindow.isAllowedEvent.mockReturnValue(true);
callsWidgetWindow.handleJoinedCall({
sender: {id: 'goodID'},
}, 'widget', message);
expect(callsWidgetWindow.mainView.sendToRenderer).toHaveBeenCalledWith(CALLS_JOINED_CALL, message);
}, 'sourceId', true);
expect(callsWidgetWindow.win.webContents.send).toHaveBeenCalledWith(CALLS_WIDGET_SHARE_SCREEN, 'sourceId', true);
});
describe('onPopOutOpen', () => {
@@ -451,42 +415,6 @@ describe('main/windows/callsWidgetWindow', () => {
expect(callsWidgetWindow.getViewURL().toString()).toBe('http://localhost:8065/');
});
describe('isAllowedEvent', () => {
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.mainView = {
webContentsId: 'mainViewID',
};
callsWidgetWindow.win = {
webContents: {
id: 'windowID',
},
};
it('should not allow on unknown sender id', () => {
expect(callsWidgetWindow.isAllowedEvent({
sender: {
id: 'senderID',
},
})).toEqual(false);
});
it('should allow on attached browser view', () => {
expect(callsWidgetWindow.isAllowedEvent({
sender: {
id: 'mainViewID',
},
})).toEqual(true);
});
it('should allow on widget window', () => {
expect(callsWidgetWindow.isAllowedEvent({
sender: {
id: 'windowID',
},
})).toEqual(true);
});
});
it('onNavigate', () => {
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.getWidgetURL = () => 'http://localhost:8065';
@@ -510,11 +438,12 @@ describe('main/windows/callsWidgetWindow', () => {
url: new URL('http://server-1.com'),
},
},
webContentsId: 2,
};
const browserWindow = {
on: jest.fn(),
once: jest.fn(),
loadURL: jest.fn().mockReturnValue(Promise.resolve()),
loadURL: jest.fn(),
webContents: {
setWindowOpenHandler: jest.fn(),
on: jest.fn(),
@@ -524,9 +453,18 @@ describe('main/windows/callsWidgetWindow', () => {
};
beforeEach(() => {
let func;
ipcMain.on.mockImplementation((_, callback) => {
func = callback;
});
browserWindow.loadURL.mockImplementation(() => {
func({sender: {id: 1}}, 'test');
return Promise.resolve();
});
BrowserWindow.mockReturnValue(browserWindow);
callsWidgetWindow.close.mockReturnValue(Promise.resolve());
ViewManager.getView.mockReturnValue(view);
callsWidgetWindow.getWidgetURL.mockReturnValue('http://server-1.com/widget');
ViewManager.getViewByWebContentsId.mockReturnValue(view);
});
afterEach(() => {
@@ -539,12 +477,7 @@ describe('main/windows/callsWidgetWindow', () => {
it('should create calls widget window', async () => {
expect(callsWidgetWindow.win).toBeUndefined();
await callsWidgetWindow.handleCreateCallsWidgetWindow('server-1_view-messaging', {callID: 'test'});
expect(callsWidgetWindow.win).toBeDefined();
});
it('should create with correct initial configuration', async () => {
await callsWidgetWindow.handleCreateCallsWidgetWindow('server-1_view-messaging', {callID: 'test'});
await callsWidgetWindow.handleCreateCallsWidgetWindow({sender: {id: 2}}, {callID: 'test'});
expect(BrowserWindow).toHaveBeenCalledWith(expect.objectContaining({
width: MINIMUM_CALLS_WIDGET_WIDTH,
height: MINIMUM_CALLS_WIDGET_HEIGHT,
@@ -556,6 +489,7 @@ describe('main/windows/callsWidgetWindow', () => {
alwaysOnTop: true,
backgroundColor: '#00ffffff',
}));
expect(callsWidgetWindow.win).toBeDefined();
});
it('should catch error when failing to load the URL', async () => {
@@ -570,18 +504,28 @@ describe('main/windows/callsWidgetWindow', () => {
});
it('should not create a new window if call is the same', async () => {
const window = {webContents: {id: 2}};
const window = {webContents: {id: 3}};
callsWidgetWindow.win = window;
callsWidgetWindow.options = {callID: 'test'};
await callsWidgetWindow.handleCreateCallsWidgetWindow('server-1_view-messaging', {callID: 'test'});
await callsWidgetWindow.handleCreateCallsWidgetWindow({sender: {id: 2}}, {callID: 'test'});
expect(callsWidgetWindow.win).toEqual(window);
});
it('should create a new window if switching calls', async () => {
const window = {webContents: {id: 2}};
let func;
ipcMain.on.mockImplementation((_, callback) => {
func = callback;
});
browserWindow.loadURL.mockImplementation(() => {
func({sender: {id: 1}}, 'test2');
return Promise.resolve();
});
BrowserWindow.mockReturnValue(browserWindow);
const window = {webContents: {id: 3}};
callsWidgetWindow.win = window;
callsWidgetWindow.getCallID = jest.fn(() => 'test');
await callsWidgetWindow.handleCreateCallsWidgetWindow('server-1_view-messaging', {callID: 'test2'});
callsWidgetWindow.options = {callID: 'test'};
await callsWidgetWindow.handleCreateCallsWidgetWindow({sender: {id: 2}}, {callID: 'test2'});
expect(callsWidgetWindow.win).not.toEqual(window);
});
});
@@ -627,10 +571,13 @@ describe('main/windows/callsWidgetWindow', () => {
lastActiveView: 2,
},
];
let index = 0;
const map = servers.reduce((arr, item) => {
item.views.forEach((view) => {
index++;
arr.push([`${item.name}_${view.name}`, {
sendToRenderer: jest.fn(),
webContentsId: index,
}]);
});
return arr;
@@ -638,7 +585,8 @@ describe('main/windows/callsWidgetWindow', () => {
const views = new Map(map);
beforeEach(() => {
ViewManager.getView.mockImplementation((viewId) => views.get(viewId));
ViewManager.getViewByWebContentsId.mockImplementation((id) => [...views.values()].find((view) => view.webContentsId === id));
callsWidgetWindow.mainView = views.get('server-1_view-1');
});
afterEach(() => {
@@ -662,9 +610,8 @@ describe('main/windows/callsWidgetWindow', () => {
},
]);
await callsWidgetWindow.handleGetDesktopSources('server-1_view-1', null);
expect(views.get('server-1_view-1').sendToRenderer).toHaveBeenCalledWith('desktop-sources-result', [
const sources = await callsWidgetWindow.handleGetDesktopSources({sender: {id: 1}}, null);
expect(sources).toEqual([
{
id: 'screen0',
},
@@ -676,11 +623,11 @@ describe('main/windows/callsWidgetWindow', () => {
it('should send error with no sources', async () => {
jest.spyOn(desktopCapturer, 'getSources').mockResolvedValue([]);
await callsWidgetWindow.handleGetDesktopSources('server-2_view-1', null);
await callsWidgetWindow.handleGetDesktopSources({sender: {id: 1}}, null);
expect(callsWidgetWindow.win.webContents.send).toHaveBeenCalledWith('calls-error', {
err: 'screen-permissions',
});
expect(views.get('server-2_view-1').sendToRenderer).toHaveBeenCalledWith('calls-error', {
expect(views.get('server-1_view-1').sendToRenderer).toHaveBeenCalledWith('calls-error', {
err: 'screen-permissions',
});
expect(callsWidgetWindow.win.webContents.send).toHaveBeenCalledTimes(1);
@@ -697,7 +644,7 @@ describe('main/windows/callsWidgetWindow', () => {
]);
jest.spyOn(systemPreferences, 'getMediaAccessStatus').mockReturnValue('denied');
await callsWidgetWindow.handleGetDesktopSources('server-1_view-1', null);
await callsWidgetWindow.handleGetDesktopSources({sender: {id: 1}}, null);
expect(systemPreferences.getMediaAccessStatus).toHaveBeenCalledWith('screen');
expect(callsWidgetWindow.win.webContents.send).toHaveBeenCalledWith('calls-error', {
@@ -726,7 +673,7 @@ describe('main/windows/callsWidgetWindow', () => {
]);
jest.spyOn(systemPreferences, 'getMediaAccessStatus').mockReturnValue('denied');
await callsWidgetWindow.handleGetDesktopSources('server-1_view-1', null);
await callsWidgetWindow.handleGetDesktopSources({sender: {id: 1}}, null);
expect(callsWidgetWindow.missingScreensharePermissions).toBe(true);
expect(resetScreensharePermissionsMacOS).toHaveBeenCalledTimes(1);
@@ -738,7 +685,7 @@ describe('main/windows/callsWidgetWindow', () => {
err: 'screen-permissions',
});
await callsWidgetWindow.handleGetDesktopSources('server-1_view-1', null);
await callsWidgetWindow.handleGetDesktopSources({sender: {id: 1}}, null);
expect(resetScreensharePermissionsMacOS).toHaveBeenCalledTimes(2);
expect(openScreensharePermissionsSettingsMacOS).toHaveBeenCalledTimes(1);
@@ -749,74 +696,9 @@ describe('main/windows/callsWidgetWindow', () => {
});
});
describe('handleDesktopSourcesModalRequest', () => {
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.mainView = {
view: {
server: {
id: 'server-1',
},
},
sendToRenderer: jest.fn(),
};
const servers = [
{
name: 'server-1',
order: 1,
views: [
{
name: 'view-1',
order: 0,
isOpen: false,
},
{
name: 'view-2',
order: 2,
isOpen: true,
},
],
}, {
name: 'server-2',
order: 0,
views: [
{
name: 'view-1',
order: 0,
isOpen: false,
},
{
name: 'view-2',
order: 2,
isOpen: true,
},
],
lastActiveView: 2,
},
];
const map = servers.reduce((arr, item) => {
item.views.forEach((view) => {
arr.push([`${item.name}_${view.name}`, {}]);
});
return arr;
}, []);
const views = new Map(map);
beforeEach(() => {
ViewManager.getView.mockImplementation((viewId) => views.get(viewId));
});
afterEach(() => {
jest.resetAllMocks();
});
it('should switch server', () => {
callsWidgetWindow.handleDesktopSourcesModalRequest();
expect(ServerViewState.switchServer).toHaveBeenCalledWith('server-1');
});
});
describe('handleCallsWidgetChannelLinkClick', () => {
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.win = {webContents: {id: 1}};
callsWidgetWindow.mainView = {
view: {
server: {
@@ -877,89 +759,12 @@ describe('main/windows/callsWidgetWindow', () => {
});
it('should switch server', () => {
callsWidgetWindow.handleCallsWidgetChannelLinkClick();
callsWidgetWindow.handleCallsWidgetChannelLinkClick({sender: {id: 1}});
expect(ServerViewState.switchServer).toHaveBeenCalledWith('server-2');
});
});
describe('handleCallsError', () => {
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.mainView = {
view: {
server: {
id: 'server-2',
},
},
sendToRenderer: jest.fn(),
};
const focus = jest.fn();
beforeEach(() => {
MainWindow.get.mockReturnValue({focus});
});
afterEach(() => {
jest.resetAllMocks();
});
it('should focus view and propagate error to main view', () => {
callsWidgetWindow.handleCallsError('', {err: 'client-error'});
expect(ServerViewState.switchServer).toHaveBeenCalledWith('server-2');
expect(focus).toHaveBeenCalled();
expect(callsWidgetWindow.mainView.sendToRenderer).toHaveBeenCalledWith('calls-error', {err: 'client-error'});
});
});
describe('handleCallsLinkClick', () => {
const view = {
view: {
server: {
id: 'server-1',
},
},
sendToRenderer: jest.fn(),
};
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.mainView = view;
beforeEach(() => {
ViewManager.getView.mockReturnValue(view);
});
afterEach(() => {
jest.resetAllMocks();
});
it('should pass through the click link to browser history push', () => {
callsWidgetWindow.handleCallsLinkClick('', {link: '/other/subpath'});
expect(ServerViewState.switchServer).toHaveBeenCalledWith('server-1');
expect(view.sendToRenderer).toBeCalledWith('browser-history-push', '/other/subpath');
});
});
describe('genCallsEventHandler', () => {
const handler = jest.fn();
afterEach(() => {
jest.clearAllMocks();
});
it('should not call handler if source is not allowed', () => {
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.isAllowedEvent = () => false;
callsWidgetWindow.genCallsEventHandler(handler)();
expect(handler).not.toHaveBeenCalled();
});
it('should call handler if source is allowed', () => {
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.isAllowedEvent = () => true;
callsWidgetWindow.genCallsEventHandler(handler)();
expect(handler).toHaveBeenCalledTimes(1);
});
});
describe('handleCallsJoinRequest', () => {
describe('forwardToMainApp', () => {
const view = {
view: {
server: {
@@ -970,6 +775,7 @@ describe('main/windows/callsWidgetWindow', () => {
};
const callsWidgetWindow = new CallsWidgetWindow();
callsWidgetWindow.mainView = view;
callsWidgetWindow.win = {webContents: {id: 1}};
const focus = jest.fn();
@@ -982,11 +788,12 @@ describe('main/windows/callsWidgetWindow', () => {
jest.resetAllMocks();
});
it('should pass through the join call callID to the webapp', () => {
callsWidgetWindow.handleCallsJoinRequest('', {callID: 'thecallchannelid'});
it('should pass through the arguments to the webapp', () => {
const func = callsWidgetWindow.forwardToMainApp('some-channel');
func({sender: {id: 1}}, 'thecallchannelid');
expect(ServerViewState.switchServer).toHaveBeenCalledWith('server-1');
expect(focus).toHaveBeenCalled();
expect(view.sendToRenderer).toBeCalledWith(CALLS_JOIN_REQUEST, {callID: 'thecallchannelid'});
expect(view.sendToRenderer).toBeCalledWith('some-channel', 'thecallchannelid');
});
});
});