discordrpc now reloads/clears activity with setting changes

This commit is contained in:
Core 2022-08-31 12:19:04 +01:00
parent f8e37029ac
commit a3ca0b09f4
No known key found for this signature in database
GPG key ID: 2AB8327FBA02D1C0
4 changed files with 42 additions and 27 deletions

View file

@ -48,15 +48,21 @@ export default class DiscordRPC {
* Runs on app ready * Runs on app ready
*/ */
onReady(_win: any): void { onReady(_win: any): void {
const self = this;
this.connect(); this.connect();
console.debug(`[Plugin][${this.name}] Ready.`); 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")) { if (!this._utils.getStoreValue("general.privateEnabled")) {
let b64data = ""; let b64data = "";
let postbody = ""; let postbody = "";
if (imageurl.startsWith("/ciderlocalart")) { 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); console.log("http://localhost:" + port + imageurl);
const response = await fetch("http://localhost:" + port + imageurl); const response = await fetch("http://localhost:" + port + imageurl);
b64data = (await response.buffer()).toString("base64"); b64data = (await response.buffer()).toString("base64");
@ -66,7 +72,7 @@ export default class DiscordRPC {
body: postbody, body: postbody,
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"User-Agent": _win.webContents.getUserAgent(), "User-Agent": this._utils.getWindow().webContents.getUserAgent(),
}, },
}) })
.then((res) => res.json()) .then((res) => res.json())
@ -81,7 +87,7 @@ export default class DiscordRPC {
body: postbody, body: postbody,
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"User-Agent": _win.webContents.getUserAgent(), "User-Agent": this._utils.getWindow().webContents.getUserAgent(),
}, },
}) })
.then((res) => res.json()) .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.`); 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 this._client
.endlessLogin({ .endlessLogin({
clientId: this._utils.getStoreValue("connectivity.discord_rpc.client") === "Cider" ? "911790844204437504" : "886578863147192350", clientId: this._utils.getStoreValue("connectivity.discord_rpc.client") === "Cider" ? "911790844204437504" : "886578863147192350",
}) })
.then(() => { .then(() => {
console.log(`[DiscordRPC][reload] DiscordRPC Reloaded.`);
this.ready = true; 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) { if (this._activityCache && this._activityCache.details && this._activityCache.state) {
console.info(`[DiscordRPC][reload] Restoring activity cache.`); console.info(`[DiscordRPC][reload] Restoring activity cache.`);
this._client.setActivity(this._activityCache); this._client.setActivity(this._activityCache);
@ -111,6 +122,13 @@ export default class DiscordRPC {
.catch((e: any) => console.error(`[DiscordRPC][reload] ${e}`)); .catch((e: any) => console.error(`[DiscordRPC][reload] ${e}`));
// this.connect(true) // 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,12 +144,8 @@ export default class DiscordRPC {
*/ */
onPlaybackStateDidChange(attributes: object): void { onPlaybackStateDidChange(attributes: object): void {
this._attributes = attributes; this._attributes = attributes;
if (this._utils.getStoreValue("general.privateEnabled")) {
this._client.clearActivity();
} else {
this.setActivity(attributes); this.setActivity(attributes);
} }
}
/** /**
* Runs on song change * Runs on song change
@ -139,12 +153,8 @@ export default class DiscordRPC {
*/ */
onNowPlayingItemDidChange(attributes: object): void { onNowPlayingItemDidChange(attributes: object): void {
this._attributes = attributes; this._attributes = attributes;
if (this._utils.getStoreValue("general.privateEnabled")) {
this._client.clearActivity();
} else {
this.setActivity(attributes); this.setActivity(attributes);
} }
}
/******************************************************************************************* /*******************************************************************************************
* Private Methods * Private Methods
@ -166,7 +176,7 @@ export default class DiscordRPC {
this._client.once("ready", () => { this._client.once("ready", () => {
console.info(`[DiscordRPC][connect] Successfully Connected to Discord. Authed for user: ${this._client.user.id}.`); 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.`); console.info(`[DiscordRPC][connect] Restoring activity cache.`);
this._client.setActivity(this._activityCache); this._client.setActivity(this._activityCache);
} }
@ -209,12 +219,11 @@ export default class DiscordRPC {
return; return;
} }
// Set the activity
if (this._utils.getStoreValue("general.privateEnabled")) { if (!attributes.status && this._utils.getStoreValue("connectivity.discord_rpc.clear_on_pause")) {
this._client.clearActivity();
} else if (!attributes.status && this._utils.getStoreValue("connectivity.discord_rpc.clear_on_pause")) {
this._client.clearActivity(); this._client.clearActivity();
} else if (activity && this._activityCache !== activity) { } else if (activity && this._activityCache !== activity) {
if (this._utils.getStoreValue("general.privateEnabled")) return
this._client.setActivity(activity); this._client.setActivity(activity);
} }
this._activityCache = activity; this._activityCache = activity;

View file

@ -340,7 +340,7 @@ export default class RAOP {
} }
}); });
electron.ipcMain.on("updateRPCImage", (_event, imageurl) => { electron.ipcMain.on("discordrpc:updateImage", (_event, imageurl) => {
this.uploadImageAirplay(imageurl); this.uploadImageAirplay(imageurl);
}); });
} }

View file

@ -256,6 +256,12 @@ const app = new Vue({
}, },
deep: true, deep: true,
}, },
'cfg.connectivity.discord_rpc.enabled'(newValue) {
ipcRenderer.send("discordrpc:reload", newValue)
},
'mk.privateEnabled'(newValue) {
ipcRenderer.send("onPrivacyModeChange", newValue);
},
page: () => { page: () => {
document.getElementById("app-content").scrollTo(0, 0); document.getElementById("app-content").scrollTo(0, 0);
app.resetState(); app.resetState();
@ -4115,7 +4121,7 @@ const app = new Vue({
if (this.mk.nowPlayingItem._assets[0].artworkURL) { if (this.mk.nowPlayingItem._assets[0].artworkURL) {
this.currentArtUrl = 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 { try {
// document.querySelector('.app-playback-controls .artwork').style.setProperty('--artwork', `url("${this.currentArtUrl}")`); // document.querySelector('.app-playback-controls .artwork').style.setProperty('--artwork', `url("${this.currentArtUrl}")`);
} catch (e) {} } catch (e) {}

View file

@ -1508,7 +1508,7 @@
ipcRenderer.send('cc-logout') ipcRenderer.send('cc-logout')
}, },
reloadDiscordRPC() { reloadDiscordRPC() {
ipcRenderer.send('reloadRPC') ipcRenderer.send('discordrpc:reload')
}, },
lfmDisconnect() { lfmDisconnect() {
this.$root.cfg.connectivity.lastfm.enabled = false; this.$root.cfg.connectivity.lastfm.enabled = false;