From e26986f66adb1784d5ab62f9c165014296698c84 Mon Sep 17 00:00:00 2001 From: Lumap Date: Tue, 17 Sep 2024 17:29:46 +0200 Subject: [PATCH] AppleMusicRichPresence: fix formatting when listening to radio (#2869) Co-authored-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Co-authored-by: v --- src/plugins/appleMusic.desktop/index.tsx | 30 +++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/plugins/appleMusic.desktop/index.tsx b/src/plugins/appleMusic.desktop/index.tsx index 327fafc3..f3148c36 100644 --- a/src/plugins/appleMusic.desktop/index.tsx +++ b/src/plugins/appleMusic.desktop/index.tsx @@ -24,7 +24,7 @@ interface ActivityButton { } interface Activity { - state: string; + state?: string; details?: string; timestamps?: { start?: number; @@ -52,8 +52,8 @@ const enum ActivityFlag { export interface TrackData { name: string; - album: string; - artist: string; + album?: string; + artist?: string; appleMusicLink?: string; songLink?: string; @@ -61,8 +61,8 @@ export interface TrackData { albumArtwork?: string; artistArtwork?: string; - playerPosition: number; - duration: number; + playerPosition?: number; + duration?: number; } const enum AssetImageType { @@ -155,8 +155,8 @@ const settings = definePluginSettings({ function customFormat(formatStr: string, data: TrackData) { return formatStr .replaceAll("{name}", data.name) - .replaceAll("{album}", data.album) - .replaceAll("{artist}", data.artist); + .replaceAll("{album}", data.album ?? "") + .replaceAll("{artist}", data.artist ?? ""); } function getImageAsset(type: AssetImageType, data: TrackData) { @@ -212,14 +212,16 @@ export default definePlugin({ const assets: ActivityAssets = {}; + const isRadio = Number.isNaN(trackData.duration) && (trackData.playerPosition === 0); + if (settings.store.largeImageType !== AssetImageType.Disabled) { assets.large_image = largeImageAsset; - assets.large_text = customFormat(settings.store.largeTextString, trackData); + if (!isRadio) assets.large_text = customFormat(settings.store.largeTextString, trackData); } if (settings.store.smallImageType !== AssetImageType.Disabled) { assets.small_image = smallImageAsset; - assets.small_text = customFormat(settings.store.smallTextString, trackData); + if (!isRadio) assets.small_text = customFormat(settings.store.smallTextString, trackData); } const buttons: ActivityButton[] = []; @@ -243,17 +245,17 @@ export default definePlugin({ name: customFormat(settings.store.nameString, trackData), details: customFormat(settings.store.detailsString, trackData), - state: customFormat(settings.store.stateString, trackData), + state: isRadio ? undefined : customFormat(settings.store.stateString, trackData), - timestamps: (settings.store.enableTimestamps ? { + timestamps: (trackData.playerPosition && trackData.duration && settings.store.enableTimestamps) ? { start: Date.now() - (trackData.playerPosition * 1000), end: Date.now() - (trackData.playerPosition * 1000) + (trackData.duration * 1000), - } : undefined), + } : undefined, assets, - buttons: buttons.length ? buttons.map(v => v.label) : undefined, - metadata: { button_urls: buttons.map(v => v.url) || undefined, }, + buttons: !isRadio && buttons.length ? buttons.map(v => v.label) : undefined, + metadata: !isRadio && buttons.length ? { button_urls: buttons.map(v => v.url) } : undefined, type: settings.store.activityType, flags: ActivityFlag.INSTANCE,