mirror of
https://github.com/zulip/zulip-desktop.git
synced 2025-10-23 03:31:56 +00:00
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import {app, dialog} from "electron/main";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
|
|
import {JsonDB} from "node-json-db";
|
|
import {DataError} from "node-json-db/dist/lib/Errors.js";
|
|
|
|
import Logger from "../common/logger-util.ts";
|
|
import * as t from "../common/translation-util.ts";
|
|
|
|
const logger = new Logger({
|
|
file: "linux-update-util.log",
|
|
});
|
|
|
|
let database: JsonDB;
|
|
|
|
reloadDatabase();
|
|
|
|
export function getUpdateItem(
|
|
key: string,
|
|
defaultValue: true | null = null,
|
|
): true | null {
|
|
reloadDatabase();
|
|
let value: unknown;
|
|
try {
|
|
value = database.getObject<unknown>(`/${key}`);
|
|
} catch (error: unknown) {
|
|
if (!(error instanceof DataError)) throw error;
|
|
}
|
|
|
|
if (value !== true && value !== null) {
|
|
setUpdateItem(key, defaultValue);
|
|
return defaultValue;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
export function setUpdateItem(key: string, value: true | null): void {
|
|
database.push(`/${key}`, value, true);
|
|
reloadDatabase();
|
|
}
|
|
|
|
export function removeUpdateItem(key: string): void {
|
|
database.delete(`/${key}`);
|
|
reloadDatabase();
|
|
}
|
|
|
|
function reloadDatabase(): void {
|
|
const linuxUpdateJsonPath = path.join(
|
|
app.getPath("userData"),
|
|
"/config/updates.json",
|
|
);
|
|
try {
|
|
const file = fs.readFileSync(linuxUpdateJsonPath, "utf8");
|
|
JSON.parse(file);
|
|
} catch (error: unknown) {
|
|
if (fs.existsSync(linuxUpdateJsonPath)) {
|
|
fs.unlinkSync(linuxUpdateJsonPath);
|
|
dialog.showErrorBox(
|
|
t.__("Error saving update notifications"),
|
|
t.__("We encountered an error while saving the update notifications."),
|
|
);
|
|
logger.error("Error while JSON parsing updates.json: ");
|
|
logger.error(error);
|
|
}
|
|
}
|
|
|
|
database = new JsonDB(linuxUpdateJsonPath, true, true);
|
|
}
|