mirror of
https://github.com/samuelthomas2774/nxapi.git
synced 2026-04-18 21:17:29 -05:00
Save active Discord Presence/notifications
This commit is contained in:
parent
c67b326177
commit
816709c3fd
|
|
@ -64,6 +64,8 @@ app.whenReady().then(async () => {
|
|||
store.on('update-nintendo-accounts', () => sendToAllWindows('nxapi:accounts:shouldrefresh'));
|
||||
|
||||
const monitors = new PresenceMonitorManager(store);
|
||||
await store.restoreMonitorState(monitors);
|
||||
|
||||
const menu = new MenuApp(store, monitors);
|
||||
|
||||
app.on('activate', () => {
|
||||
|
|
@ -79,6 +81,19 @@ app.on('window-all-closed', () => {
|
|||
if (process.platform !== 'darwin') app.quit();
|
||||
});
|
||||
|
||||
interface SavedMonitorState {
|
||||
users: {
|
||||
/** Nintendo Account ID */
|
||||
id: string;
|
||||
/** NSA ID */
|
||||
presence_user: string | null;
|
||||
user_notifications: boolean;
|
||||
friend_notifications: boolean;
|
||||
}[];
|
||||
/** Nintendo Account ID */
|
||||
discord_presence_user: string | null;
|
||||
}
|
||||
|
||||
export class Store extends EventEmitter {
|
||||
constructor(
|
||||
public storage: persist.LocalStorage
|
||||
|
|
@ -86,7 +101,51 @@ export class Store extends EventEmitter {
|
|||
super();
|
||||
}
|
||||
|
||||
//
|
||||
async saveMonitorState(monitors: PresenceMonitorManager) {
|
||||
const users = new Set();
|
||||
const state: SavedMonitorState = {
|
||||
users: [],
|
||||
discord_presence_user: null,
|
||||
};
|
||||
|
||||
for (const monitor of monitors.monitors) {
|
||||
if (users.has(monitor.data.user.id)) continue;
|
||||
users.add(monitor.data.user.id);
|
||||
|
||||
state.users.push({
|
||||
id: monitor.data.user.id,
|
||||
presence_user: monitor.presence_user,
|
||||
user_notifications: monitor.user_notifications,
|
||||
friend_notifications: monitor.friend_notifications,
|
||||
});
|
||||
|
||||
if (monitor.presence_user && !state.discord_presence_user) {
|
||||
state.discord_presence_user = monitor.data.user.id;
|
||||
}
|
||||
}
|
||||
|
||||
debug('Saving monitor state', state);
|
||||
await this.storage.setItem('AppMonitors', state);
|
||||
}
|
||||
|
||||
async restoreMonitorState(monitors: PresenceMonitorManager) {
|
||||
const state: SavedMonitorState | undefined = await this.storage.getItem('AppMonitors');
|
||||
debug('Restoring monitor state', state);
|
||||
if (!state) return;
|
||||
|
||||
for (const user of state.users) {
|
||||
if (state.discord_presence_user !== user.id &&
|
||||
!user.user_notifications && !user.friend_notifications
|
||||
) continue;
|
||||
|
||||
await monitors.start(user.id, monitor => {
|
||||
monitor.presence_user = state.discord_presence_user === user.id ?
|
||||
user.presence_user || monitor.data.nsoAccount.user.nsaId : null;
|
||||
monitor.user_notifications = user.user_notifications;
|
||||
monitor.friend_notifications = user.friend_notifications;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class PresenceMonitorManager {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
import { app, dialog, Menu, Tray, nativeImage, MenuItem } from '../electron.js';
|
||||
import createDebug from 'debug';
|
||||
import { addNsoAccount, addPctlAccount } from './na-auth.js';
|
||||
import { PresenceMonitorManager, Store } from './index.js';
|
||||
import { getToken, SavedMoonToken, SavedToken } from '../../util.js';
|
||||
import { WebService } from '../../api/znc-types.js';
|
||||
import openWebService from './webservices.js';
|
||||
|
||||
const debug = createDebug('app:main:menu');
|
||||
|
||||
export default class MenuApp {
|
||||
tray: Tray;
|
||||
|
||||
|
|
@ -206,7 +209,7 @@ export default class MenuApp {
|
|||
monitor.skipIntervalInCurrentLoop();
|
||||
});
|
||||
|
||||
if (monitor || id) this.updateMenu();
|
||||
if (monitor || id) this.saveMonitorStateAndUpdateMenu();
|
||||
}
|
||||
|
||||
async setUserNotificationsActive(id: string, active: boolean) {
|
||||
|
|
@ -220,13 +223,13 @@ export default class MenuApp {
|
|||
}
|
||||
|
||||
monitor.skipIntervalInCurrentLoop();
|
||||
this.updateMenu();
|
||||
this.saveMonitorStateAndUpdateMenu();
|
||||
}
|
||||
|
||||
if (!monitor?.user_notifications && active) await this.monitors.start(id, monitor => {
|
||||
monitor.user_notifications = true;
|
||||
monitor.skipIntervalInCurrentLoop();
|
||||
this.updateMenu();
|
||||
this.saveMonitorStateAndUpdateMenu();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -241,13 +244,26 @@ export default class MenuApp {
|
|||
}
|
||||
|
||||
monitor.skipIntervalInCurrentLoop();
|
||||
this.updateMenu();
|
||||
this.saveMonitorStateAndUpdateMenu();
|
||||
}
|
||||
|
||||
if (!monitor?.friend_notifications && active) await this.monitors.start(id, monitor => {
|
||||
monitor.friend_notifications = true;
|
||||
monitor.skipIntervalInCurrentLoop();
|
||||
this.updateMenu();
|
||||
this.saveMonitorStateAndUpdateMenu();
|
||||
});
|
||||
}
|
||||
|
||||
async saveMonitorState() {
|
||||
try {
|
||||
await this.store.saveMonitorState(this.monitors);
|
||||
} catch (err) {
|
||||
debug('Error saving monitor state', err);
|
||||
}
|
||||
}
|
||||
|
||||
saveMonitorStateAndUpdateMenu() {
|
||||
this.saveMonitorState();
|
||||
this.updateMenu();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user