diff --git a/i18n/en.json b/i18n/en.json index d5bae7405f9..8f3b61740cc 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -32,6 +32,12 @@ "main.app.app.handleAppCertificateError.certNotTrusted.dialog.title": "Certificate Not Trusted", "main.app.app.handleAppCertificateError.dialog.extraDetail": "Certificate is different from previous one.\n\n", "main.app.initialize.downloadBox.allFiles": "All files", + "main.app.utils.clearAllData.cancel": "Cancel", + "main.app.utils.clearAllData.confirm": "Clear All Data", + "main.app.utils.clearAllData.message": "This action will erase all session, cache, cookie and storage data for all server. Performing this action will restart the application. Are you sure you want to clear all data?", + "main.app.utils.clearDataForServer.cancel": "Cancel", + "main.app.utils.clearDataForServer.confirm": "Clear Data", + "main.app.utils.clearDataForServer.message": "This action will erase all session, cache, cookie and storage data for the server \"{serverName}\". Are you sure you want to clear data for this server?", "main.app.utils.migrateMacAppStore.button.dontImport": "Don't Import", "main.app.utils.migrateMacAppStore.button.selectAndImport": "Select Directory and Import", "main.app.utils.migrateMacAppStore.dialog.detail": "It appears that an existing {appName} configuration exists, would you like to import it? You will be asked to pick the correct configuration directory.", @@ -82,7 +88,9 @@ "main.menus.app.history.forward": "Forward", "main.menus.app.view": "&View", "main.menus.app.view.actualSize": "Actual Size", + "main.menus.app.view.clearAllData": "Clear All Data", "main.menus.app.view.clearCacheAndReload": "Clear Cache and Reload", + "main.menus.app.view.clearDataForServer": "Clear Data for Current Server", "main.menus.app.view.developerModeBrowserOnly": "Browser Only Mode", "main.menus.app.view.developerModeDisableContextMenu": "Disable Context Menu", "main.menus.app.view.developerModeDisableNotificationStorage": "Disable Notification Storage", diff --git a/src/main/app/utils.ts b/src/main/app/utils.ts index 4ce4d5eef19..ed576de8f76 100644 --- a/src/main/app/utils.ts +++ b/src/main/app/utils.ts @@ -258,3 +258,54 @@ export async function updateServerInfos(servers: MattermostServer[]) { })); ServerManager.updateRemoteInfos(map); } + +export async function clearDataForServer(server: MattermostServer) { + const mainWindow = MainWindow.get(); + if (!mainWindow) { + return; + } + + const response = await dialog.showMessageBox(mainWindow, { + type: 'warning', + buttons: [ + localizeMessage('main.app.utils.clearDataForServer.confirm', 'Clear Data'), + localizeMessage('main.app.utils.clearDataForServer.cancel', 'Cancel'), + ], + defaultId: 1, + message: localizeMessage('main.app.utils.clearDataForServer.message', 'This action will erase all session, cache, cookie and storage data for the server "{serverName}". Are you sure you want to clear data for this server?', {serverName: server.name}), + }); + + if (response.response === 0) { + await session.defaultSession.clearData({ + origins: [server.url.origin], + }); + ViewManager.reload(); + } +} + +export async function clearAllData() { + const mainWindow = MainWindow.get(); + if (!mainWindow) { + return; + } + + const response = await dialog.showMessageBox(mainWindow, { + title: app.name, + type: 'warning', + buttons: [ + localizeMessage('main.app.utils.clearAllData.confirm', 'Clear All Data'), + localizeMessage('main.app.utils.clearAllData.cancel', 'Cancel'), + ], + defaultId: 1, + message: localizeMessage('main.app.utils.clearAllData.message', 'This action will erase all session, cache, cookie and storage data for all server. Performing this action will restart the application. Are you sure you want to clear all data?'), + }); + + if (response.response === 0) { + await session.defaultSession.clearAuthCache(); + await session.defaultSession.clearCodeCaches({}); + await session.defaultSession.clearHostResolverCache(); + await session.defaultSession.clearData(); + app.relaunch(); + app.exit(); + } +} diff --git a/src/main/menus/app.test.js b/src/main/menus/app.test.js index e1977fa4636..de187ac655d 100644 --- a/src/main/menus/app.test.js +++ b/src/main/menus/app.test.js @@ -74,6 +74,7 @@ jest.mock('app/serverViewState', () => ({ switchServer: jest.fn(), getCurrentServer: jest.fn(), })); +jest.mock('main/app/utils', () => ({})); jest.mock('main/diagnostics', () => ({})); jest.mock('main/downloadsManager', () => ({ hasDownloads: jest.fn(), diff --git a/src/main/menus/app.ts b/src/main/menus/app.ts index ea8174fe1a6..8215ae85835 100644 --- a/src/main/menus/app.ts +++ b/src/main/menus/app.ts @@ -15,6 +15,7 @@ import ServerManager from 'common/servers/serverManager'; import {t} from 'common/utils/util'; import {getViewDisplayName} from 'common/views/View'; import type {ViewType} from 'common/views/View'; +import {clearAllData, clearDataForServer} from 'main/app/utils'; import type {UpdateManager} from 'main/autoUpdater'; import DeveloperMode from 'main/developerMode'; import Diagnostics from 'main/diagnostics'; @@ -274,6 +275,18 @@ export function createTemplate(config: Config, updateManager: UpdateManager) { click() { return downloadsManager.openDownloadsDropdown(); }, + }, separatorItem, { + id: 'clear-data-for-server', + label: localizeMessage('main.menus.app.view.clearDataForServer', 'Clear Data for Current Server'), + async click() { + return clearDataForServer(ServerViewState.getCurrentServer()); + }, + }, { + id: 'clear-data', + label: localizeMessage('main.menus.app.view.clearAllData', 'Clear All Data'), + async click() { + return clearAllData(); + }, }, separatorItem, { label: localizeMessage('main.menus.app.view.devToolsSubMenu', 'Developer Tools'), submenu: devToolsSubMenu,