2020-10-20 12:10:41 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {ViewModel} from "../../ViewModel.js";
|
2022-01-26 09:51:48 +01:00
|
|
|
import {KeyBackupViewModel} from "./KeyBackupViewModel.js";
|
2020-10-20 12:10:41 +02:00
|
|
|
|
2021-03-18 20:45:01 +01:00
|
|
|
class PushNotificationStatus {
|
|
|
|
constructor() {
|
|
|
|
this.supported = null;
|
|
|
|
this.enabled = false;
|
|
|
|
this.updating = false;
|
2021-04-01 15:01:04 +02:00
|
|
|
this.enabledOnServer = null;
|
|
|
|
this.serverError = null;
|
2021-03-18 20:45:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 12:10:41 +02:00
|
|
|
function formatKey(key) {
|
|
|
|
const partLength = 4;
|
|
|
|
const partCount = Math.ceil(key.length / partLength);
|
|
|
|
let formattedKey = "";
|
|
|
|
for (let i = 0; i < partCount; i += 1) {
|
|
|
|
formattedKey += (formattedKey.length ? " " : "") + key.slice(i * partLength, (i + 1) * partLength);
|
|
|
|
}
|
|
|
|
return formattedKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SettingsViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this._updateService = options.updateService;
|
2021-12-22 17:09:52 +01:00
|
|
|
const {client} = options;
|
|
|
|
this._client = client;
|
2022-01-26 09:51:48 +01:00
|
|
|
this._keyBackupViewModel = this.track(new KeyBackupViewModel(this.childOptions({session: this._session})));
|
2020-10-20 12:10:41 +02:00
|
|
|
this._closeUrl = this.urlCreator.urlUntilSegment("session");
|
2020-10-20 17:50:43 +02:00
|
|
|
this._estimate = null;
|
2020-11-20 15:51:16 +01:00
|
|
|
this.sentImageSizeLimit = null;
|
|
|
|
this.minSentImageSizeLimit = 400;
|
|
|
|
this.maxSentImageSizeLimit = 4000;
|
2021-03-18 20:45:01 +01:00
|
|
|
this.pushNotifications = new PushNotificationStatus();
|
2021-10-26 12:49:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get _session() {
|
2021-12-22 17:09:52 +01:00
|
|
|
return this._client.session;
|
2020-11-20 15:51:16 +01:00
|
|
|
}
|
|
|
|
|
2021-10-28 11:47:31 +02:00
|
|
|
async logout() {
|
2022-01-17 16:30:22 +01:00
|
|
|
this.navigation.push("logout", this._client.sessionId);
|
2021-10-26 12:49:31 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 15:51:16 +01:00
|
|
|
setSentImageSizeLimit(size) {
|
|
|
|
if (size > this.maxSentImageSizeLimit || size < this.minSentImageSizeLimit) {
|
|
|
|
this.sentImageSizeLimit = null;
|
|
|
|
this.platform.settingsStorage.remove("sentImageSizeLimit");
|
|
|
|
} else {
|
|
|
|
this.sentImageSizeLimit = Math.round(size);
|
|
|
|
this.platform.settingsStorage.setInt("sentImageSizeLimit", size);
|
|
|
|
}
|
|
|
|
this.emitChange("sentImageSizeLimit");
|
2020-10-20 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2020-10-26 15:44:11 +01:00
|
|
|
this._estimate = await this.platform.estimateStorageUsage();
|
2020-11-20 15:51:16 +01:00
|
|
|
this.sentImageSizeLimit = await this.platform.settingsStorage.getInt("sentImageSizeLimit");
|
2021-03-18 20:45:01 +01:00
|
|
|
this.pushNotifications.supported = await this.platform.notificationService.supportsPush();
|
|
|
|
this.pushNotifications.enabled = await this._session.arePushNotificationsEnabled();
|
2020-10-20 17:50:43 +02:00
|
|
|
this.emitChange("");
|
2020-10-20 12:10:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get closeUrl() {
|
|
|
|
return this._closeUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
get fingerprintKey() {
|
2021-08-31 18:14:14 +02:00
|
|
|
const key = this._session.fingerprintKey;
|
|
|
|
if (!key) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return formatKey(key);
|
2020-10-20 12:10:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get deviceId() {
|
|
|
|
return this._session.deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get userId() {
|
|
|
|
return this._session.userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get version() {
|
2020-10-26 15:44:11 +01:00
|
|
|
const {updateService} = this.platform;
|
|
|
|
if (updateService) {
|
|
|
|
return `${updateService.version} (${updateService.buildHash})`;
|
2020-10-20 12:10:41 +02:00
|
|
|
}
|
|
|
|
return this.i18n`development version`;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkForUpdate() {
|
2020-10-26 15:44:11 +01:00
|
|
|
this.platform.updateService?.checkForUpdate();
|
2020-10-20 12:10:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get showUpdateButton() {
|
2020-10-26 15:44:11 +01:00
|
|
|
return !!this.platform.updateService;
|
2020-10-20 12:10:41 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:51:48 +01:00
|
|
|
get keyBackupViewModel() {
|
|
|
|
return this._keyBackupViewModel;
|
2020-10-20 12:10:41 +02:00
|
|
|
}
|
2020-10-20 17:50:43 +02:00
|
|
|
|
|
|
|
get storageQuota() {
|
|
|
|
return this._formatBytes(this._estimate?.quota);
|
|
|
|
}
|
|
|
|
|
|
|
|
get storageUsage() {
|
|
|
|
return this._formatBytes(this._estimate?.usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
_formatBytes(n) {
|
|
|
|
if (typeof n === "number") {
|
|
|
|
return Math.round(n / (1024 * 1024)).toFixed(1) + " MB";
|
|
|
|
} else {
|
|
|
|
return this.i18n`unknown`;
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 15:27:24 +01:00
|
|
|
|
|
|
|
async exportLogs() {
|
|
|
|
const logExport = await this.logger.export();
|
2021-02-16 17:46:07 +01:00
|
|
|
this.platform.saveFileAs(logExport.asBlob(), `hydrogen-logs-${this.platform.clock.now()}.json`);
|
2021-02-16 15:27:24 +01:00
|
|
|
}
|
2021-03-18 20:45:01 +01:00
|
|
|
|
|
|
|
async togglePushNotifications() {
|
|
|
|
this.pushNotifications.updating = true;
|
2021-04-01 15:01:04 +02:00
|
|
|
this.pushNotifications.enabledOnServer = null;
|
|
|
|
this.pushNotifications.serverError = null;
|
2021-03-18 20:45:01 +01:00
|
|
|
this.emitChange("pushNotifications.updating");
|
|
|
|
try {
|
|
|
|
if (await this._session.enablePushNotifications(!this.pushNotifications.enabled)) {
|
|
|
|
this.pushNotifications.enabled = !this.pushNotifications.enabled;
|
|
|
|
if (this.pushNotifications.enabled) {
|
|
|
|
this.platform.notificationService.showNotification(this.i18n`Push notifications are now enabled`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this.pushNotifications.updating = false;
|
|
|
|
this.emitChange("pushNotifications.updating");
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 15:01:04 +02:00
|
|
|
|
|
|
|
async checkPushEnabledOnServer() {
|
|
|
|
this.pushNotifications.enabledOnServer = null;
|
|
|
|
this.pushNotifications.serverError = null;
|
|
|
|
try {
|
2021-08-23 19:26:39 +02:00
|
|
|
this.pushNotifications.enabledOnServer = await this._session.checkPusherEnabledOnHomeserver();
|
2021-04-01 15:01:04 +02:00
|
|
|
this.emitChange("pushNotifications.enabledOnServer");
|
|
|
|
} catch (err) {
|
|
|
|
this.pushNotifications.serverError = err;
|
|
|
|
this.emitChange("pushNotifications.serverError");
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 12:10:41 +02:00
|
|
|
}
|
2020-10-20 17:50:43 +02:00
|
|
|
|