commit
Add covers endpoint
diff --git a/apps/desktop/src-tauri/src/discord.rs b/apps/desktop/src-tauri/src/discord.rs
index 701943f..ad14911 100644
--- a/apps/desktop/src-tauri/src/discord.rs
+++ b/apps/desktop/src-tauri/src/discord.rs
@@ -13,7 +13,7 @@ use std::thread;
use std::time::Duration;
use discord_social_sdk::{
- activity::{Activity, ActivityTimestamps},
+ activity::{Activity, ActivityAssets, ActivityTimestamps},
enums::ActivityType,
Client,
};
@@ -24,6 +24,11 @@ pub enum PresenceUpdate {
artist: String,
album: Option<String>,
duration_s: Option<f64>,
+ // Public, absolute URL of the album cover. Discord fetches this from
+ // its own servers to render the large presence image, so it must be
+ // reachable without auth (see the public /cover route). Empty/None
+ // just falls back to the app's default icon.
+ cover_url: Option<String>,
},
Clear,
}
@@ -37,14 +42,16 @@ pub fn spawn(application_id: u64, rx: Receiver<PresenceUpdate>) {
discord_social_sdk::run_callbacks();
match rx.try_recv() {
- Ok(PresenceUpdate::NowPlaying { title, artist, album, duration_s }) => {
+ Ok(PresenceUpdate::NowPlaying { title, artist, album, duration_s, cover_url }) => {
let mut activity = Activity::new();
activity.set_activity_type(ActivityType::Listening);
activity.set_details(Some(&title));
- let state = match album {
+ // Borrow album (as_deref) rather than move it — it's also
+ // used below as the large-image tooltip.
+ let state = match album.as_deref() {
Some(album) => format!("{artist} — {album}"),
- None => artist,
+ None => artist.clone(),
};
activity.set_state(Some(&state));
@@ -61,6 +68,20 @@ pub fn spawn(application_id: u64, rx: Receiver<PresenceUpdate>) {
activity.set_timestamps(Some(×tamps));
}
+ // Album art as the large presence image. large_image takes
+ // an external URL directly (per the SDK docs); large_text
+ // is the hover tooltip (album, or the title as a fallback).
+ if let Some(cover) = cover_url.as_deref().filter(|u| !u.is_empty()) {
+ let mut assets = ActivityAssets::new();
+ assets.set_large_image(Some(cover));
+ let tooltip = album.as_deref().unwrap_or(title.as_str());
+ // The SDK requires large_text to be 2-128 chars.
+ if tooltip.chars().count() >= 2 {
+ assets.set_large_text(Some(tooltip));
+ }
+ activity.set_assets(Some(&assets));
+ }
+
client.update_rich_presence(&mut activity, |_result| {});
}
Ok(PresenceUpdate::Clear) => {
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index 264b41a..838c031 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -42,6 +42,7 @@ fn set_now_playing(
artist: String,
album: Option<String>,
#[allow(non_snake_case)] durationS: Option<f64>,
+ #[allow(non_snake_case)] coverUrl: Option<String>,
handle: State<DiscordHandle>,
) -> Result<(), String> {
handle
@@ -53,6 +54,7 @@ fn set_now_playing(
artist,
album,
duration_s: durationS,
+ cover_url: coverUrl,
})
.map_err(|e| e.to_string())
}
@@ -80,6 +82,7 @@ fn set_now_playing(
artist: String,
album: Option<String>,
#[allow(non_snake_case)] durationS: Option<f64>,
+ #[allow(non_snake_case)] coverUrl: Option<String>,
) -> Result<(), String> {
Ok(())
}
diff --git a/apps/server/src/routes/songs.ts b/apps/server/src/routes/songs.ts
index 8e66b28..21a490b 100644
--- a/apps/server/src/routes/songs.ts
+++ b/apps/server/src/routes/songs.ts
@@ -266,7 +266,11 @@ songRoutes.get("/:id/lyrics", requireAuth, async (c) => {
return c.json(lyrics);
});
-songRoutes.get("/:id/cover", requireAuth, async (c) => {
+// Public on purpose (no requireAuth): Discord Rich Presence shows the album
+// art as the large image, and Discord fetches that URL from its own servers
+// with no session. Covers are unlisted — reachable only via the song's random
+// UUID — and it's only album art; the audio stream stays auth-gated.
+songRoutes.get("/:id/cover", async (c) => {
const song = await getSong(c.req.param("id")!);
if (!song?.cover_path) return c.json({ error: "not_found" }, 404);
diff --git a/apps/web/src/desktop.ts b/apps/web/src/desktop.ts
index a958a24..e7be538 100644
--- a/apps/web/src/desktop.ts
+++ b/apps/web/src/desktop.ts
@@ -9,6 +9,7 @@ import {
startNativeLogin,
completeNativeLogin,
establishSessionCookie,
+ absoluteApiUrl,
type Song,
} from "@musicapp/shared";
@@ -182,11 +183,14 @@ export async function syncDiscordPresence(song: Song | null): Promise<void> {
await invoke("clear_presence");
return;
}
+ // Absolute URL so Discord (which fetches it server-side) can load the art.
+ const coverUrl = song.coverUrl ? absoluteApiUrl(song.coverUrl) : null;
await invoke("set_now_playing", {
title: song.title,
artist: song.artist,
album: song.album,
durationS: song.durationS,
+ coverUrl,
});
} catch {
/* best-effort — never blocks playback */
diff --git a/packages/shared/src/api-client.ts b/packages/shared/src/api-client.ts
index 49b20e0..c464324 100644
--- a/packages/shared/src/api-client.ts
+++ b/packages/shared/src/api-client.ts
@@ -38,6 +38,17 @@ function url(path: string): string {
return `${baseUrl}${path}`;
}
+// Resolve an API path to an ABSOLUTE url (scheme + host). Most calls use
+// same-origin relative urls, but some consumers need a full url that resolves
+// off-device — e.g. an album cover handed to Discord Rich Presence, which
+// Discord fetches from its own servers, not the user's machine. Uses the
+// configured base (desktop) or the current origin (web / remote-loaded shell).
+export function absoluteApiUrl(path: string): string {
+ const base =
+ baseUrl || (typeof location !== "undefined" ? location.origin : "");
+ return `${base}${path}`;
+}
+
// Every request in this module goes through this wrapper (it shadows the
// global `fetch`). It attaches the bearer token when one is set (native
// clients). Web sets no token and keeps fetch's default same-origin