diff --git a/src/main/plugins/discordrpc.ts b/src/main/plugins/discordrpc.ts index e0af0b12..b8d94d8c 100644 --- a/src/main/plugins/discordrpc.ts +++ b/src/main/plugins/discordrpc.ts @@ -48,15 +48,21 @@ export default class DiscordRPC { * Runs on app ready */ onReady(_win: any): void { - const self = this; this.connect(); console.debug(`[Plugin][${this.name}] Ready.`); - ipcMain.on("updateRPCImage", async (_event, imageurl) => { + } + + /** + * Set up ipc listeners for the plugin + */ + onRendererReady() { + const self = this; + ipcMain.on("discordrpc:updateImage", async (_event, imageurl) => { if (!this._utils.getStoreValue("general.privateEnabled")) { let b64data = ""; let postbody = ""; if (imageurl.startsWith("/ciderlocalart")) { - let port = await _win.webContents.executeJavaScript(`app.clientPort`); + let port = await this._utils.getWindow().webContents.executeJavaScript(`app.clientPort`); console.log("http://localhost:" + port + imageurl); const response = await fetch("http://localhost:" + port + imageurl); b64data = (await response.buffer()).toString("base64"); @@ -66,7 +72,7 @@ export default class DiscordRPC { body: postbody, headers: { "Content-Type": "application/json", - "User-Agent": _win.webContents.getUserAgent(), + "User-Agent": this._utils.getWindow().webContents.getUserAgent(), }, }) .then((res) => res.json()) @@ -81,7 +87,7 @@ export default class DiscordRPC { body: postbody, headers: { "Content-Type": "application/json", - "User-Agent": _win.webContents.getUserAgent(), + "User-Agent": this._utils.getWindow().webContents.getUserAgent(), }, }) .then((res) => res.json()) @@ -92,17 +98,22 @@ export default class DiscordRPC { } } }); - ipcMain.on("reloadRPC", () => { + ipcMain.on("discordrpc:reload", (_event, configUpdate = null) => { console.log(`[DiscordRPC][reload] Reloading DiscordRPC.`); - this._client.destroy(); + if (this._client) { + this._client.destroy(); + } + + if (!this._utils.getStoreValue("connectivity.discord_rpc.enabled")) return this._client .endlessLogin({ clientId: this._utils.getStoreValue("connectivity.discord_rpc.client") === "Cider" ? "911790844204437504" : "886578863147192350", }) .then(() => { + console.log(`[DiscordRPC][reload] DiscordRPC Reloaded.`); this.ready = true; - this._utils.getWindow().webContents.send("rpcReloaded", this._client.user); + if (configUpdate == null) this._utils.getWindow().webContents.send("rpcReloaded", this._client.user); if (this._activityCache && this._activityCache.details && this._activityCache.state) { console.info(`[DiscordRPC][reload] Restoring activity cache.`); this._client.setActivity(this._activityCache); @@ -111,6 +122,13 @@ export default class DiscordRPC { .catch((e: any) => console.error(`[DiscordRPC][reload] ${e}`)); // this.connect(true) }); + ipcMain.on("onPrivacyModeChange", (_event, enabled) => { + if (enabled && this._client) { + this._client.clearActivity(); + } else if (!enabled && this._activityCache && this._activityCache.details && this._activityCache.state) { + this._client.setActivity(this._activityCache); + } + }); } /** @@ -126,11 +144,7 @@ export default class DiscordRPC { */ onPlaybackStateDidChange(attributes: object): void { this._attributes = attributes; - if (this._utils.getStoreValue("general.privateEnabled")) { - this._client.clearActivity(); - } else { - this.setActivity(attributes); - } + this.setActivity(attributes); } /** @@ -139,11 +153,7 @@ export default class DiscordRPC { */ onNowPlayingItemDidChange(attributes: object): void { this._attributes = attributes; - if (this._utils.getStoreValue("general.privateEnabled")) { - this._client.clearActivity(); - } else { - this.setActivity(attributes); - } + this.setActivity(attributes); } /******************************************************************************************* @@ -166,7 +176,7 @@ export default class DiscordRPC { this._client.once("ready", () => { console.info(`[DiscordRPC][connect] Successfully Connected to Discord. Authed for user: ${this._client.user.id}.`); - if (this._activityCache && this._activityCache.details && this._activityCache.state) { + if (this._activityCache && this._activityCache.details && this._activityCache.state && !this._utils.getStoreValue("general.privateEnabled")) { console.info(`[DiscordRPC][connect] Restoring activity cache.`); this._client.setActivity(this._activityCache); } @@ -209,12 +219,11 @@ export default class DiscordRPC { return; } - // Set the activity - if (this._utils.getStoreValue("general.privateEnabled")) { - this._client.clearActivity(); - } else if (!attributes.status && this._utils.getStoreValue("connectivity.discord_rpc.clear_on_pause")) { + + if (!attributes.status && this._utils.getStoreValue("connectivity.discord_rpc.clear_on_pause")) { this._client.clearActivity(); } else if (activity && this._activityCache !== activity) { + if (this._utils.getStoreValue("general.privateEnabled")) return this._client.setActivity(activity); } this._activityCache = activity; diff --git a/src/main/plugins/raop.ts b/src/main/plugins/raop.ts index 95c17195..0219a012 100644 --- a/src/main/plugins/raop.ts +++ b/src/main/plugins/raop.ts @@ -38,7 +38,7 @@ export default class RAOP { Worker, isMainThread, parentPort, workerData } = require('node:worker_threads'); function getAudioConv (buffers) { - + function interleave16(leftChannel, rightChannel) { var length = leftChannel.length + rightChannel.length; var result = new Int16Array(length); @@ -340,7 +340,7 @@ export default class RAOP { } }); - electron.ipcMain.on("updateRPCImage", (_event, imageurl) => { + electron.ipcMain.on("discordrpc:updateImage", (_event, imageurl) => { this.uploadImageAirplay(imageurl); }); } diff --git a/src/renderer/main/vueapp.js b/src/renderer/main/vueapp.js index ebdd3899..d06b8126 100644 --- a/src/renderer/main/vueapp.js +++ b/src/renderer/main/vueapp.js @@ -256,6 +256,12 @@ const app = new Vue({ }, deep: true, }, + 'cfg.connectivity.discord_rpc.enabled'(newValue) { + ipcRenderer.send("discordrpc:reload", newValue) + }, + 'mk.privateEnabled'(newValue) { + ipcRenderer.send("onPrivacyModeChange", newValue); + }, page: () => { document.getElementById("app-content").scrollTo(0, 0); app.resetState(); @@ -4115,7 +4121,7 @@ const app = new Vue({ if (this.mk.nowPlayingItem._assets[0].artworkURL) { this.currentArtUrl = this.mk.nowPlayingItem._assets[0].artworkURL; } - ipcRenderer.send("updateRPCImage", this.currentArtUrl ?? ""); + ipcRenderer.send("discordrpc:updateImage", this.currentArtUrl ?? ""); try { // document.querySelector('.app-playback-controls .artwork').style.setProperty('--artwork', `url("${this.currentArtUrl}")`); } catch (e) {} diff --git a/src/renderer/views/components/settings-window.ejs b/src/renderer/views/components/settings-window.ejs index 1e0ce44a..dde7df2d 100644 --- a/src/renderer/views/components/settings-window.ejs +++ b/src/renderer/views/components/settings-window.ejs @@ -1508,7 +1508,7 @@ ipcRenderer.send('cc-logout') }, reloadDiscordRPC() { - ipcRenderer.send('reloadRPC') + ipcRenderer.send('discordrpc:reload') }, lfmDisconnect() { this.$root.cfg.connectivity.lastfm.enabled = false;