From b4e0869215e95169bdda4f08efc35c372b16c575 Mon Sep 17 00:00:00 2001 From: Core <64542347+coredev-uk@users.noreply.github.com> Date: Wed, 2 Feb 2022 16:17:19 +0000 Subject: [PATCH] Setup filtering for discordrpc --- src/main/plugins/discordrpc.ts | 55 ++++++++++++++++++++++++++-------- src/preload/cider-preload.js | 4 +-- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/main/plugins/discordrpc.ts b/src/main/plugins/discordrpc.ts index 6fae8461..11663b9d 100644 --- a/src/main/plugins/discordrpc.ts +++ b/src/main/plugins/discordrpc.ts @@ -90,6 +90,44 @@ export default class DiscordRichPresence { }).catch((e: any) => console.error(`[DiscordRPC][disconnect] ${e}`)); } + /** + * Filter the Discord activity object + */ + private filterActivity(activity: any, attributes: any): Object { + + // Checks if the name is greater than 128 because some songs can be that long + if (activity.details && activity.details.length > 128) { + activity.details = activity.details.substring(0, 125) + '...' + } + + // Check large image + if (activity.largeImageKey === null || activity.largeImageKey === ""){ + activity.largeImageKey = "cider"; + } + + // Timestamp + if (new Date(attributes.endTime).getTime() < 0) { + delete activity.startTime + delete activity.endTime + } + + // not sure + if (!attributes.artistName) { + delete activity.state; + } + + if (!activity.largeImageText || activity.largeImageText.length < 2) { + delete activity.largeImageText + } + + activity.buttons.forEach((key: {label: string, url: string}, value: Number) => { + if (key.url.endsWith('undefined') || key.url.includes('undefined')) { + activity.buttons.splice(key, 1); + } + }) + return activity + } + /** * Sets the activity of the client * @param {object} attributes @@ -105,27 +143,18 @@ export default class DiscordRichPresence { this._activity = { details: attributes.name, state: `${attributes.artistName ? `by ${attributes.artistName}` : ''}`, - startTimestamp: ((new Date(attributes.endTime).getTime() < 0) ? null : attributes.startTime), - endTimestamp: ((new Date(attributes.endTime).getTime() < 0) ? null : attributes.endTime), - largeImageKey: (attributes.artwork.url.replace('{w}', '1024').replace('{h}', '1024')) ?? 'cider', + startTimestamp: attributes.startTime, + endTimestamp: attributes.endTime, + largeImageKey: attributes.artwork.url.replace('{w}', '1024').replace('{h}', '1024'), largeImageText: attributes.albumName, instance: false, // Whether the activity is in a game session - buttons: [ {label: "Listen on Cider", url: attributes.url.cider}, {label: "View on Apple Music", url: attributes.url.appleMusic}, ] }; - - // Checks if the name is greater than 128 because some songs can be that long - if (this._activity.details && this._activity.details.length > 128) { - this._activity.details = this._activity.details.substring(0, 125) + '...' - } - - if (this._activity.largeImageKey == null || this._activity.largeImageKey == ""){ - this._activity.largeImageKey = "cider"; - } + this._activity = this.filterActivity(this._activity, attributes) // Check if its pausing (false) or playing (true) if (!attributes.status) { diff --git a/src/preload/cider-preload.js b/src/preload/cider-preload.js index 03c9b10a..31b6f662 100644 --- a/src/preload/cider-preload.js +++ b/src/preload/cider-preload.js @@ -51,8 +51,8 @@ const MusicKitInterop = { attributes.playParams = attributes?.playParams ?? {id: 'no-id-found'}; attributes.playParams.id = attributes?.playParams?.id ?? 'no-id-found'; attributes.url = { - cider: "cider://play/s/" + nowPlayingItem?._songId ?? 'no-id-found', - appleMusic: "https://music.apple.com/"+ mk.storefrontId +"/song/" + nowPlayingItem?._songId ?? 'no-id-found' + cider: `cider://play/s/${nowPlayingItem?._songId ?? 'no-id-found'}`, + appleMusic: attributes.websiteUrl ? attributes.websiteUrl : `https://music.apple.com/${mk.storefrontId}/song/${nowPlayingItem?._songId ?? 'no-id-found'}` } if (attributes.playParams.id === 'no-id-found') { attributes.playParams.id = nowPlayingItem?.id ?? 'no-id-found';