Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 41cf2971c1 | |||
| 4e0f98aa51 | |||
| e0f3361051 | |||
|
414021f471
|
|||
|
24fb4cf752
|
@@ -2,3 +2,4 @@
|
||||
*.ndp
|
||||
tmp
|
||||
discord-rich-presence
|
||||
.DS_Store
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
[](https://github.com/navidrome/discord-rich-presence-plugin/actions/workflows/build.yml)
|
||||
[](https://github.com/navidrome/discord-rich-presence-plugin/releases/latest/download/discord-rich-presence.ndp)
|
||||
|
||||
**Attention: This plugin requires Navidrome 0.60.2 or later.**
|
||||
**Attention: This plugin requires Navidrome 0.61.0 or later.**
|
||||
|
||||
This plugin integrates Navidrome with Discord Rich Presence, displaying your currently playing track in your Discord status.
|
||||
The goal is to demonstrate the capabilities of Navidrome's plugin system by implementing a real-time presence feature using Discord's Gateway API.
|
||||
|
||||
@@ -25,12 +25,13 @@ import (
|
||||
|
||||
// Configuration keys
|
||||
const (
|
||||
clientIDKey = "clientid"
|
||||
usersKey = "users"
|
||||
activityNameKey = "activityname"
|
||||
spotifyLinksKey = "spotifylinks"
|
||||
caaEnabledKey = "caaenabled"
|
||||
uguuEnabledKey = "uguuenabled"
|
||||
clientIDKey = "clientid"
|
||||
usersKey = "users"
|
||||
activityNameKey = "activityname"
|
||||
activityNameTemplateKey = "activitynametemplate"
|
||||
spotifyLinksKey = "spotifylinks"
|
||||
caaEnabledKey = "caaenabled"
|
||||
uguuEnabledKey = "uguuenabled"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -47,6 +48,7 @@ const (
|
||||
activityNameTrack = "Track"
|
||||
activityNameArtist = "Artist"
|
||||
activityNameAlbum = "Album"
|
||||
activityNameCustom = "Custom"
|
||||
)
|
||||
|
||||
// userToken represents a user-token mapping from the config
|
||||
@@ -170,6 +172,16 @@ func (p *discordPlugin) NowPlaying(input scrobbler.NowPlayingRequest) error {
|
||||
case activityNameArtist:
|
||||
activityName = input.Track.Artist
|
||||
statusDisplayType = statusDisplayName
|
||||
case activityNameCustom:
|
||||
template, _ := pdk.GetConfig(activityNameTemplateKey)
|
||||
if template != "" {
|
||||
r := strings.NewReplacer(
|
||||
"{track}", input.Track.Title,
|
||||
"{artist}", input.Track.Artist,
|
||||
"{album}", input.Track.Album,
|
||||
)
|
||||
activityName = r.Replace(template)
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve Spotify URLs if enabled
|
||||
|
||||
@@ -225,6 +225,62 @@ var _ = Describe("discordPlugin", func() {
|
||||
Entry("uses track album when configured", "Album", true, "Test Album", 0),
|
||||
Entry("uses track artist when configured", "Artist", true, "Test Artist", 0),
|
||||
)
|
||||
|
||||
DescribeTable("custom activity name template",
|
||||
func(template string, templateExists bool, expectedName string) {
|
||||
pdk.PDKMock.On("GetConfig", clientIDKey).Return("test-client-id", true)
|
||||
pdk.PDKMock.On("GetConfig", usersKey).Return(`[{"username":"testuser","token":"test-token"}]`, true)
|
||||
pdk.PDKMock.On("GetConfig", uguuEnabledKey).Return("", false)
|
||||
pdk.PDKMock.On("GetConfig", activityNameKey).Return("Custom", true)
|
||||
pdk.PDKMock.On("GetConfig", activityNameTemplateKey).Return(template, templateExists)
|
||||
pdk.PDKMock.On("GetConfig", spotifyLinksKey).Return("", false)
|
||||
|
||||
// Connect mocks
|
||||
host.CacheMock.On("GetInt", "discord.seq.testuser").Return(int64(0), false, errors.New("not found"))
|
||||
gatewayResp := []byte(`{"url":"wss://gateway.discord.gg"}`)
|
||||
host.HTTPMock.On("Send", mock.MatchedBy(func(req host.HTTPRequest) bool {
|
||||
return req.Method == "GET" && req.URL == "https://discord.com/api/gateway"
|
||||
})).Return(&host.HTTPResponse{StatusCode: 200, Body: gatewayResp}, nil)
|
||||
host.WebSocketMock.On("Connect", mock.MatchedBy(func(url string) bool {
|
||||
return strings.Contains(url, "gateway.discord.gg")
|
||||
}), mock.Anything, "testuser").Return("testuser", nil)
|
||||
|
||||
// Capture the activity payload sent to Discord
|
||||
var sentPayload string
|
||||
host.WebSocketMock.On("SendText", "testuser", mock.Anything).Run(func(args mock.Arguments) {
|
||||
sentPayload = args.Get(1).(string)
|
||||
}).Return(nil)
|
||||
host.SchedulerMock.On("ScheduleRecurring", mock.Anything, payloadHeartbeat, "testuser").Return("testuser", nil)
|
||||
host.SchedulerMock.On("CancelSchedule", "testuser-clear").Return(nil)
|
||||
|
||||
// Image mocks
|
||||
host.CacheMock.On("GetString", discordImageKey).Return("", false, nil)
|
||||
host.CacheMock.On("SetString", discordImageKey, mock.Anything, mock.Anything).Return(nil)
|
||||
host.ArtworkMock.On("GetTrackUrl", "track1", int32(300)).Return("https://example.com/art.jpg", nil)
|
||||
host.HTTPMock.On("Send", externalAssetsReq).Return(&host.HTTPResponse{StatusCode: 200, Body: []byte(`{}`)}, nil)
|
||||
host.SchedulerMock.On("ScheduleOneTime", mock.Anything, payloadClearActivity, "testuser-clear").Return("testuser-clear", nil)
|
||||
|
||||
err := plugin.NowPlaying(scrobbler.NowPlayingRequest{
|
||||
Username: "testuser",
|
||||
Position: 10,
|
||||
Track: scrobbler.TrackInfo{
|
||||
ID: "track1",
|
||||
Title: "Test Song",
|
||||
Artist: "Test Artist",
|
||||
Album: "Test Album",
|
||||
Duration: 180,
|
||||
},
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(sentPayload).To(ContainSubstring(fmt.Sprintf(`"name":"%s"`, expectedName)))
|
||||
},
|
||||
Entry("uses custom template with all placeholders", "{artist} - {track} ({album})", true, "Test Artist - Test Song (Test Album)"),
|
||||
Entry("uses custom template with only track", "{track}", true, "Test Song"),
|
||||
Entry("uses custom template with only artist", "{artist}", true, "Test Artist"),
|
||||
Entry("uses custom template with only album", "{album}", true, "Test Album"),
|
||||
Entry("uses custom template with plain text", "Now Playing", true, "Now Playing"),
|
||||
Entry("falls back to Navidrome when template is empty", "", false, "Navidrome"),
|
||||
)
|
||||
})
|
||||
|
||||
Describe("Scrobble", func() {
|
||||
|
||||
+22
-2
@@ -2,7 +2,7 @@
|
||||
"$schema": "https://raw.githubusercontent.com/navidrome/navidrome/refs/heads/master/plugins/manifest-schema.json",
|
||||
"name": "Discord Rich Presence",
|
||||
"author": "Navidrome Team",
|
||||
"version": "1.0.0-beta-1",
|
||||
"version": "1.0.0",
|
||||
"description": "Discord Rich Presence integration for Navidrome",
|
||||
"website": "https://github.com/navidrome/discord-rich-presence-plugin",
|
||||
"permissions": {
|
||||
@@ -57,10 +57,17 @@
|
||||
"Default",
|
||||
"Track",
|
||||
"Album",
|
||||
"Artist"
|
||||
"Artist",
|
||||
"Custom"
|
||||
],
|
||||
"default": "Default"
|
||||
},
|
||||
"activitynametemplate": {
|
||||
"type": "string",
|
||||
"title": "Custom Activity Name Template",
|
||||
"description": "Template for the activity name. Available placeholders: {track}, {artist}, {album}",
|
||||
"default": "{artist} - {track}"
|
||||
},
|
||||
"caaenabled": {
|
||||
"type": "boolean",
|
||||
"title": "Use artwork from Cover Art Archive (for MusicBrainz-tagged music)",
|
||||
@@ -125,6 +132,19 @@
|
||||
"format": "radio"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Control",
|
||||
"scope": "#/properties/activitynametemplate",
|
||||
"rule": {
|
||||
"effect": "SHOW",
|
||||
"condition": {
|
||||
"scope": "#/properties/activityname",
|
||||
"schema": {
|
||||
"const": "Custom"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Control",
|
||||
"scope": "#/properties/caaenabled"
|
||||
|
||||
Reference in New Issue
Block a user