Abstract electron settings properly to avoid boilerplate-hell (#22491)

* Remove unused method `BasePlatform::screenCaptureErrorString`

* Extract SeshatIndexManager into its own file

* Improve platform typescripting

* Consolidate IPC call promisification into IPCManager

* Abstract electron settings properly to avoid boilerplate-hell

* i18n

* Iterate PR
This commit is contained in:
Michael Telatynski 2022-06-10 22:38:46 +01:00 committed by GitHub
parent 867fc30ebf
commit 2c0965c240
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 283 additions and 311 deletions

View file

@ -16,7 +16,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { UpdateCheckStatus } from "matrix-react-sdk/src/BasePlatform";
import { UpdateCheckStatus, UpdateStatus } from "matrix-react-sdk/src/BasePlatform";
import request from 'browser-request';
import dis from 'matrix-react-sdk/src/dispatcher/dispatcher';
import { _t } from 'matrix-react-sdk/src/languageHandler';
@ -31,6 +31,15 @@ import { parseQs } from "../url_utils";
const POKE_RATE_MS = 10 * 60 * 1000; // 10 min
function getNormalizedAppVersion(version: string): string {
// if version looks like semver with leading v, strip it (matches scripts/normalize-version.sh)
const semVerRegex = /^v\d+.\d+.\d+(-.+)?$/;
if (semVerRegex.test(version)) {
return version.substring(1);
}
return version;
}
export default class WebPlatform extends VectorBasePlatform {
constructor() {
super();
@ -40,7 +49,7 @@ export default class WebPlatform extends VectorBasePlatform {
}
}
getHumanReadableName(): string {
public getHumanReadableName(): string {
return 'Web Platform'; // no translation required: only used for analytics
}
@ -48,7 +57,7 @@ export default class WebPlatform extends VectorBasePlatform {
* Returns true if the platform supports displaying
* notifications, otherwise false.
*/
supportsNotifications(): boolean {
public supportsNotifications(): boolean {
return Boolean(window.Notification);
}
@ -56,7 +65,7 @@ export default class WebPlatform extends VectorBasePlatform {
* Returns true if the application currently has permission
* to display notifications. Otherwise false.
*/
maySendNotifications(): boolean {
public maySendNotifications(): boolean {
return window.Notification.permission === 'granted';
}
@ -67,7 +76,7 @@ export default class WebPlatform extends VectorBasePlatform {
* that is 'granted' if the user allowed the request or
* 'denied' otherwise.
*/
requestNotificationPermission(): Promise<string> {
public requestNotificationPermission(): Promise<string> {
// annoyingly, the latest spec says this returns a
// promise, but this is only supported in Chrome 46
// and Firefox 47, so adapt the callback API.
@ -99,26 +108,17 @@ export default class WebPlatform extends VectorBasePlatform {
return;
}
resolve(this.getNormalizedAppVersion(body.trim()));
resolve(getNormalizedAppVersion(body.trim()));
},
);
});
}
getNormalizedAppVersion(version: string): string {
// if version looks like semver with leading v, strip it (matches scripts/normalize-version.sh)
const semVerRegex = /^v\d+.\d+.\d+(-.+)?$/;
if (semVerRegex.test(version)) {
return version.substring(1);
}
return version;
public getAppVersion(): Promise<string> {
return Promise.resolve(getNormalizedAppVersion(process.env.VERSION));
}
getAppVersion(): Promise<string> {
return Promise.resolve(this.getNormalizedAppVersion(process.env.VERSION));
}
startUpdater() {
public startUpdater(): void {
// Poll for an update immediately, and reload the page now if we're out of date
// already as we've just initialised an old version of the app somehow.
//
@ -127,7 +127,7 @@ export default class WebPlatform extends VectorBasePlatform {
//
// Ideally, loading an old copy would be impossible with the
// cache-control: nocache HTTP header set, but Firefox doesn't always obey it :/
console.log("startUpdater, current version is " + this.getNormalizedAppVersion(process.env.VERSION));
console.log("startUpdater, current version is " + getNormalizedAppVersion(process.env.VERSION));
this.pollForUpdate((version: string, newVersion: string) => {
const query = parseQs(location);
if (query.updated) {
@ -147,16 +147,16 @@ export default class WebPlatform extends VectorBasePlatform {
setInterval(() => this.pollForUpdate(showUpdateToast, hideUpdateToast), POKE_RATE_MS);
}
async canSelfUpdate(): Promise<boolean> {
public async canSelfUpdate(): Promise<boolean> {
return true;
}
pollForUpdate = (
private pollForUpdate = (
showUpdate: (currentVersion: string, mostRecentVersion: string) => void,
showNoUpdate?: () => void,
) => {
): Promise<UpdateStatus> => {
return this.getMostRecentVersion().then((mostRecentVersion) => {
const currentVersion = this.getNormalizedAppVersion(process.env.VERSION);
const currentVersion = getNormalizedAppVersion(process.env.VERSION);
if (currentVersion !== mostRecentVersion) {
if (this.shouldShowUpdate(mostRecentVersion)) {
@ -181,7 +181,7 @@ export default class WebPlatform extends VectorBasePlatform {
});
};
startUpdateCheck() {
public startUpdateCheck(): void {
super.startUpdateCheck();
this.pollForUpdate(showUpdateToast, hideUpdateToast).then((updateState) => {
dis.dispatch<CheckUpdatesPayload>({
@ -191,11 +191,11 @@ export default class WebPlatform extends VectorBasePlatform {
});
}
installUpdate() {
public installUpdate(): void {
window.location.reload();
}
getDefaultDeviceDisplayName(): string {
public getDefaultDeviceDisplayName(): string {
// strip query-string and fragment from uri
const url = new URL(window.location.href);
@ -217,15 +217,7 @@ export default class WebPlatform extends VectorBasePlatform {
});
}
screenCaptureErrorString(): string | null {
// it won't work at all if you're not on HTTPS so whine whine whine
if (window.location.protocol !== "https:") {
return _t("You need to be using HTTPS to place a screen-sharing call.");
}
return null;
}
reload() {
public reload(): void {
window.location.reload();
}
}