From ca2cb26d8ed3081d2f96a69dca11833a2ce00613 Mon Sep 17 00:00:00 2001
From: Deluan <deluan@deluan.com>
Date: Wed, 2 Nov 2022 11:20:51 -0400
Subject: [PATCH] Add `played` field to Subsonic API responses. Fix #1971

This is not an "official" field in the specification, but I guess it does not hurt to expose this ;)
---
 server/subsonic/browsing.go            | 9 +++++++++
 server/subsonic/helpers.go             | 6 ++++++
 server/subsonic/responses/responses.go | 3 +++
 3 files changed, 18 insertions(+)

diff --git a/server/subsonic/browsing.go b/server/subsonic/browsing.go
index c93ba825d..a5b728272 100644
--- a/server/subsonic/browsing.go
+++ b/server/subsonic/browsing.go
@@ -333,6 +333,9 @@ func (c *BrowsingController) buildArtistDirectory(ctx context.Context, artist *m
 	dir.Id = artist.ID
 	dir.Name = artist.Name
 	dir.PlayCount = artist.PlayCount
+	if artist.PlayCount > 0 {
+		dir.Played = &artist.PlayDate
+	}
 	dir.AlbumCount = artist.AlbumCount
 	dir.UserRating = artist.Rating
 	if artist.Starred {
@@ -361,6 +364,9 @@ func (c *BrowsingController) buildAlbumDirectory(ctx context.Context, album *mod
 	dir.Name = album.Name
 	dir.Parent = album.AlbumArtistID
 	dir.PlayCount = album.PlayCount
+	if album.PlayCount > 0 {
+		dir.Played = &album.PlayDate
+	}
 	dir.UserRating = album.Rating
 	dir.SongCount = album.SongCount
 	dir.CoverArt = album.CoverArtId
@@ -387,6 +393,9 @@ func (c *BrowsingController) buildAlbum(ctx context.Context, album *model.Album,
 	dir.SongCount = album.SongCount
 	dir.Duration = int(album.Duration)
 	dir.PlayCount = album.PlayCount
+	if album.PlayCount > 0 {
+		dir.Played = &album.PlayDate
+	}
 	dir.Year = album.MaxYear
 	dir.Genre = album.Genre
 	dir.UserRating = album.Rating
diff --git a/server/subsonic/helpers.go b/server/subsonic/helpers.go
index 63fd94962..e74b43e86 100644
--- a/server/subsonic/helpers.go
+++ b/server/subsonic/helpers.go
@@ -165,6 +165,9 @@ func childFromMediaFile(ctx context.Context, mf model.MediaFile) responses.Child
 	child.ArtistId = mf.ArtistID
 	child.Type = "music"
 	child.PlayCount = mf.PlayCount
+	if mf.PlayCount > 0 {
+		child.Played = &mf.PlayDate
+	}
 	if mf.Starred {
 		child.Starred = &mf.StarredAt
 	}
@@ -211,6 +214,9 @@ func childFromAlbum(ctx context.Context, al model.Album) responses.Child {
 		child.Starred = &al.StarredAt
 	}
 	child.PlayCount = al.PlayCount
+	if al.PlayCount > 0 {
+		child.Played = &al.PlayDate
+	}
 	child.UserRating = al.Rating
 	return child
 }
diff --git a/server/subsonic/responses/responses.go b/server/subsonic/responses/responses.go
index f67e88a21..9593f6706 100644
--- a/server/subsonic/responses/responses.go
+++ b/server/subsonic/responses/responses.go
@@ -116,6 +116,7 @@ type Child struct {
 	BitRate               int        `xml:"bitRate,attr,omitempty"                  json:"bitRate,omitempty"`
 	Path                  string     `xml:"path,attr,omitempty"                     json:"path,omitempty"`
 	PlayCount             int64      `xml:"playCount,attr,omitempty"                json:"playCount,omitempty"`
+	Played                *time.Time `xml:"played,attr,omitempty"                   json:"played,omitempty"`
 	DiscNumber            int        `xml:"discNumber,attr,omitempty"               json:"discNumber,omitempty"`
 	Created               *time.Time `xml:"created,attr,omitempty"                  json:"created,omitempty"`
 	AlbumId               string     `xml:"albumId,attr,omitempty"                  json:"albumId,omitempty"`
@@ -141,6 +142,7 @@ type Directory struct {
 	Parent     string     `xml:"parent,attr,omitempty"              json:"parent,omitempty"`
 	Starred    *time.Time `xml:"starred,attr,omitempty"             json:"starred,omitempty"`
 	PlayCount  int64      `xml:"playCount,attr,omitempty"           json:"playCount,omitempty"`
+	Played     *time.Time `xml:"played,attr,omitempty"              json:"played,omitempty"`
 	UserRating int        `xml:"userRating,attr,omitempty"          json:"userRating,omitempty"`
 
 	// ID3
@@ -178,6 +180,7 @@ type AlbumID3 struct {
 	SongCount  int        `xml:"songCount,attr,omitempty"           json:"songCount,omitempty"`
 	Duration   int        `xml:"duration,attr,omitempty"            json:"duration,omitempty"`
 	PlayCount  int64      `xml:"playCount,attr,omitempty"           json:"playCount,omitempty"`
+	Played     *time.Time `xml:"played,attr,omitempty"              json:"played,omitempty"`
 	Created    *time.Time `xml:"created,attr,omitempty"             json:"created,omitempty"`
 	Starred    *time.Time `xml:"starred,attr,omitempty"             json:"starred,omitempty"`
 	UserRating int        `xml:"userRating,attr,omitempty"          json:"userRating,omitempty"`