127 lines
3.4 KiB
TypeScript
127 lines
3.4 KiB
TypeScript
|
import * as path from "path";
|
||
|
import * as electron from "electron";
|
||
|
import * as fs from "fs-extra";
|
||
|
|
||
|
import {BrowserWindow, ipcMain as ipc} from "electron";
|
||
|
import {Arguments, process_args} from "../../shared/process-arguments";
|
||
|
import UserData = forum.UserData;
|
||
|
import {main_window} from "../main_window";
|
||
|
|
||
|
let current_window: BrowserWindow;
|
||
|
let _current_data: UserData;
|
||
|
|
||
|
function update_data(data?: UserData) {
|
||
|
_current_data = data;
|
||
|
electron.webContents.getAllWebContents().forEach(content => {
|
||
|
content.send('teaforo-update', data);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function config_file_path() {
|
||
|
return path.join(electron.app.getPath('userData'), "forum_data.json");
|
||
|
}
|
||
|
|
||
|
async function load_data() {
|
||
|
try {
|
||
|
const file = config_file_path();
|
||
|
if((await fs.stat(file)).isFile()) {
|
||
|
const raw_data = await fs.readFile(config_file_path());
|
||
|
const data = JSON.parse(raw_data.toString());
|
||
|
update_data(data as UserData);
|
||
|
console.log("Initialized forum account from config!");
|
||
|
} else {
|
||
|
console.log("Missing forum config file. Ignoring forum auth");
|
||
|
}
|
||
|
} catch(error) {
|
||
|
console.error("Failed to load forum account connection: %o", error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function save_data() {
|
||
|
const file = config_file_path();
|
||
|
try {
|
||
|
await fs.ensureFile(file);
|
||
|
} catch(error) {
|
||
|
console.error("Failed to ensure forum config file as file %o", error);
|
||
|
}
|
||
|
try {
|
||
|
await fs.writeJSON(file, _current_data);
|
||
|
} catch(error) {
|
||
|
console.error("Failed to save forum config: %o", error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function open_login(enforce: boolean = false) : Promise<UserData> {
|
||
|
if(_current_data && !enforce) return Promise.resolve(_current_data);
|
||
|
|
||
|
if(current_window) {
|
||
|
current_window.close();
|
||
|
current_window = undefined;
|
||
|
}
|
||
|
current_window = new BrowserWindow({
|
||
|
width: 400,
|
||
|
height: 400,
|
||
|
show: true,
|
||
|
parent: main_window,
|
||
|
webPreferences: {
|
||
|
webSecurity: false
|
||
|
},
|
||
|
});
|
||
|
current_window.setMenu(null);
|
||
|
console.log("Main: " + main_window);
|
||
|
|
||
|
current_window.loadFile(path.join(path.dirname(module.filename), "ui", "index.html"));
|
||
|
if(process_args.has_flag(...Arguments.DEV_TOOLS))
|
||
|
current_window.webContents.openDevTools();
|
||
|
|
||
|
return new Promise<UserData>((resolve, reject) => {
|
||
|
let response = false;
|
||
|
ipc.once("teaforo-callback", (event, data) => {
|
||
|
if(response) return;
|
||
|
response = true;
|
||
|
current_window.close();
|
||
|
current_window = undefined;
|
||
|
|
||
|
update_data(data);
|
||
|
save_data();
|
||
|
if(data)
|
||
|
resolve(data);
|
||
|
else
|
||
|
reject();
|
||
|
});
|
||
|
|
||
|
current_window.on('closed', event => {
|
||
|
if(response) return;
|
||
|
response = true;
|
||
|
|
||
|
current_window = undefined;
|
||
|
reject();
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export function current_data() : UserData | undefined {
|
||
|
return this._current_data;
|
||
|
}
|
||
|
|
||
|
export function logout() {
|
||
|
update_data(undefined);
|
||
|
save_data();
|
||
|
}
|
||
|
|
||
|
export async function initialize() {
|
||
|
await load_data();
|
||
|
}
|
||
|
|
||
|
export function setup() {
|
||
|
ipc.on('teaforo-login', event => {
|
||
|
open_login().catch(error => {}); //TODO may local notify
|
||
|
});
|
||
|
|
||
|
ipc.on('teaforo-logout', event => {
|
||
|
logout();
|
||
|
});
|
||
|
ipc.on('teaforo-update', event => {
|
||
|
update_data(_current_data);
|
||
|
});
|
||
|
}
|