2020-10-20 15:40:41 +05:30
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-02-14 22:23:59 +05:30
|
|
|
import {ViewModel} from "../../ViewModel";
|
2022-01-26 14:21:48 +05:30
|
|
|
import {KeyBackupViewModel} from "./KeyBackupViewModel.js";
|
2022-06-14 22:16:02 +05:30
|
|
|
import {submitLogsToRageshakeServer} from "../../../domain/rageshake";
|
2020-10-20 15:40:41 +05:30
|
|
|
|
2021-03-19 01:15:01 +05:30
|
|
|
class PushNotificationStatus {
|
|
|
|
constructor() {
|
|
|
|
this.supported = null;
|
|
|
|
this.enabled = false;
|
|
|
|
this.updating = false;
|
2021-04-01 18:31:04 +05:30
|
|
|
this.enabledOnServer = null;
|
|
|
|
this.serverError = null;
|
2021-03-19 01:15:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:40:41 +05:30
|
|
|
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 21:39:52 +05:30
|
|
|
const {client} = options;
|
|
|
|
this._client = client;
|
2022-01-26 14:21:48 +05:30
|
|
|
this._keyBackupViewModel = this.track(new KeyBackupViewModel(this.childOptions({session: this._session})));
|
2020-10-20 15:40:41 +05:30
|
|
|
this._closeUrl = this.urlCreator.urlUntilSegment("session");
|
2020-10-20 21:20:43 +05:30
|
|
|
this._estimate = null;
|
2020-11-20 20:21:16 +05:30
|
|
|
this.sentImageSizeLimit = null;
|
|
|
|
this.minSentImageSizeLimit = 400;
|
|
|
|
this.maxSentImageSizeLimit = 4000;
|
2021-03-19 01:15:01 +05:30
|
|
|
this.pushNotifications = new PushNotificationStatus();
|
2022-04-25 15:56:45 +05:30
|
|
|
this._activeTheme = undefined;
|
2022-06-15 14:43:46 +05:30
|
|
|
this._logsFeedbackMessage = undefined;
|
2021-10-26 16:19:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get _session() {
|
2021-12-22 21:39:52 +05:30
|
|
|
return this._client.session;
|
2020-11-20 20:21:16 +05:30
|
|
|
}
|
|
|
|
|
2021-10-28 15:17:31 +05:30
|
|
|
async logout() {
|
2022-01-17 21:00:22 +05:30
|
|
|
this.navigation.push("logout", this._client.sessionId);
|
2021-10-26 16:19:31 +05:30
|
|
|
}
|
|
|
|
|
2020-11-20 20:21:16 +05:30
|
|
|
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 21:20:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2020-10-26 20:14:11 +05:30
|
|
|
this._estimate = await this.platform.estimateStorageUsage();
|
2020-11-20 20:21:16 +05:30
|
|
|
this.sentImageSizeLimit = await this.platform.settingsStorage.getInt("sentImageSizeLimit");
|
2021-03-19 01:15:01 +05:30
|
|
|
this.pushNotifications.supported = await this.platform.notificationService.supportsPush();
|
|
|
|
this.pushNotifications.enabled = await this._session.arePushNotificationsEnabled();
|
2022-04-25 17:49:16 +05:30
|
|
|
if (!import.meta.env.DEV) {
|
2022-05-11 14:58:14 +05:30
|
|
|
this._activeTheme = await this.platform.themeLoader.getActiveTheme();
|
2022-04-25 17:49:16 +05:30
|
|
|
}
|
2020-10-20 21:20:43 +05:30
|
|
|
this.emitChange("");
|
2020-10-20 15:40:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get closeUrl() {
|
|
|
|
return this._closeUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
get fingerprintKey() {
|
2021-08-31 21:44:14 +05:30
|
|
|
const key = this._session.fingerprintKey;
|
|
|
|
if (!key) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return formatKey(key);
|
2020-10-20 15:40:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get deviceId() {
|
|
|
|
return this._session.deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get userId() {
|
|
|
|
return this._session.userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get version() {
|
2020-10-26 20:14:11 +05:30
|
|
|
const {updateService} = this.platform;
|
|
|
|
if (updateService) {
|
|
|
|
return `${updateService.version} (${updateService.buildHash})`;
|
2020-10-20 15:40:41 +05:30
|
|
|
}
|
|
|
|
return this.i18n`development version`;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkForUpdate() {
|
2020-10-26 20:14:11 +05:30
|
|
|
this.platform.updateService?.checkForUpdate();
|
2020-10-20 15:40:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get showUpdateButton() {
|
2020-10-26 20:14:11 +05:30
|
|
|
return !!this.platform.updateService;
|
2020-10-20 15:40:41 +05:30
|
|
|
}
|
|
|
|
|
2022-01-26 14:21:48 +05:30
|
|
|
get keyBackupViewModel() {
|
|
|
|
return this._keyBackupViewModel;
|
2020-10-20 15:40:41 +05:30
|
|
|
}
|
2020-10-20 21:20:43 +05:30
|
|
|
|
|
|
|
get storageQuota() {
|
|
|
|
return this._formatBytes(this._estimate?.quota);
|
|
|
|
}
|
|
|
|
|
|
|
|
get storageUsage() {
|
|
|
|
return this._formatBytes(this._estimate?.usage);
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:36:37 +05:30
|
|
|
get themeMapping() {
|
|
|
|
return this.platform.themeLoader.themeMapping;
|
2022-04-25 14:29:31 +05:30
|
|
|
}
|
|
|
|
|
2022-04-25 15:56:45 +05:30
|
|
|
get activeTheme() {
|
|
|
|
return this._activeTheme;
|
|
|
|
}
|
|
|
|
|
2020-10-20 21:20:43 +05:30
|
|
|
_formatBytes(n) {
|
|
|
|
if (typeof n === "number") {
|
|
|
|
return Math.round(n / (1024 * 1024)).toFixed(1) + " MB";
|
|
|
|
} else {
|
|
|
|
return this.i18n`unknown`;
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 19:57:24 +05:30
|
|
|
|
|
|
|
async exportLogs() {
|
|
|
|
const logExport = await this.logger.export();
|
2021-02-16 22:16:07 +05:30
|
|
|
this.platform.saveFileAs(logExport.asBlob(), `hydrogen-logs-${this.platform.clock.now()}.json`);
|
2021-02-16 19:57:24 +05:30
|
|
|
}
|
2021-03-19 01:15:01 +05:30
|
|
|
|
2022-06-15 14:43:46 +05:30
|
|
|
get canSendLogsToServer() {
|
|
|
|
return !!this.platform.config.bugReportEndpointUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
get logsServer() {
|
|
|
|
const {bugReportEndpointUrl} = this.platform.config;
|
|
|
|
try {
|
|
|
|
if (bugReportEndpointUrl) {
|
|
|
|
return new URL(bugReportEndpointUrl).hostname;
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-06-14 22:16:02 +05:30
|
|
|
async sendLogsToServer() {
|
2022-06-15 14:43:46 +05:30
|
|
|
const {bugReportEndpointUrl} = this.platform.config;
|
|
|
|
if (bugReportEndpointUrl) {
|
|
|
|
this._logsFeedbackMessage = this.i18n`Sending logs…`;
|
|
|
|
this.emitChange();
|
|
|
|
try {
|
|
|
|
const logExport = await this.logger.export();
|
|
|
|
await submitLogsToRageshakeServer(
|
|
|
|
{
|
|
|
|
app: "hydrogen",
|
|
|
|
userAgent: this.platform.description,
|
|
|
|
version: DEFINE_VERSION,
|
|
|
|
text: `Submit logs from settings for user ${this._session.userId} on device ${this._session.deviceId}`,
|
|
|
|
},
|
|
|
|
logExport.asBlob(),
|
|
|
|
bugReportEndpointUrl,
|
|
|
|
this.platform.request
|
|
|
|
);
|
|
|
|
this._logsFeedbackMessage = this.i18n`Logs sent succesfully!`;
|
|
|
|
this.emitChange();
|
|
|
|
} catch (err) {
|
|
|
|
this._logsFeedbackMessage = err.message;
|
|
|
|
this.emitChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get logsFeedbackMessage() {
|
|
|
|
return this._logsFeedbackMessage;
|
2022-06-14 22:16:02 +05:30
|
|
|
}
|
|
|
|
|
2021-03-19 01:15:01 +05:30
|
|
|
async togglePushNotifications() {
|
|
|
|
this.pushNotifications.updating = true;
|
2021-04-01 18:31:04 +05:30
|
|
|
this.pushNotifications.enabledOnServer = null;
|
|
|
|
this.pushNotifications.serverError = null;
|
2021-03-19 01:15:01 +05:30
|
|
|
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 18:31:04 +05:30
|
|
|
|
|
|
|
async checkPushEnabledOnServer() {
|
|
|
|
this.pushNotifications.enabledOnServer = null;
|
|
|
|
this.pushNotifications.serverError = null;
|
|
|
|
try {
|
2021-08-23 22:56:39 +05:30
|
|
|
this.pushNotifications.enabledOnServer = await this._session.checkPusherEnabledOnHomeserver();
|
2021-04-01 18:31:04 +05:30
|
|
|
this.emitChange("pushNotifications.enabledOnServer");
|
|
|
|
} catch (err) {
|
|
|
|
this.pushNotifications.serverError = err;
|
|
|
|
this.emitChange("pushNotifications.serverError");
|
|
|
|
}
|
|
|
|
}
|
2022-05-26 23:36:37 +05:30
|
|
|
|
2022-06-05 20:49:05 +05:30
|
|
|
changeThemeOption(themeName, themeVariant) {
|
2022-06-06 17:20:36 +05:30
|
|
|
this.platform.themeLoader.setTheme(themeName, themeVariant);
|
2022-05-26 23:36:37 +05:30
|
|
|
// emit so that radio-buttons become displayed/hidden
|
|
|
|
this.emitChange("themeOption");
|
|
|
|
}
|
2020-10-20 15:40:41 +05:30
|
|
|
}
|
2020-10-20 21:20:43 +05:30
|
|
|
|