When a new entry is added to the user's listening history, we should generate new recommendations PS-213 - #90
When a new entry is added to the user's listening history, we should generate new recommendations PS-213#90DYA13 wants to merge 3 commits into
Conversation
|
Akos I have done initial code , could you please guide me further what to do? |
akosasante
left a comment
There was a problem hiding this comment.
Just a quick note on ordering of tickets. I see in Jira that I marked PS-213 as being blocked by PS-212. So that means we need work from PS-212 first. And it looks like you've assigned yourself that ticket ("When a user starts playing an album, add it to the listening history")
But PS-212 is marked as being blocked by PS-182 ("Backend should store play history"), which is assigned to Natalia. Just a reminder to make sure y'all have talked and are not re-doing same work by implementing the schema for ListeningHistory.js.
If the idea is that in your PR here you're just writing up a barebones version of that schema, to be filled out further by Natalia's work then that's totally fine 👍 . I just wanted to call it out to make sure everyone is on the same page :)
There was a problem hiding this comment.
A little bit of bad planning on my part sorry! It looks like @NatachaKey 's ticket PS-182 also led to her creating a schema for storing recently listened to albums: https://github.com/Code-the-Dream-School/dd-prac-team4-back/pull/91/files
Let's merge those changes in (git merge origin/backendStoresPlayHistory) and then include the post-save callback you've done here but instead in the RecentlyListened schema
| // Access the model to fetch ListeningHistory documents | ||
| const ListeningHistory = this.constructor; | ||
|
|
||
| // Fetch the top 5 listened to artists from the user's listening history |
There was a problem hiding this comment.
| // Fetch the top 5 listened to artists from the user's listening history | |
| // Fetch the 5 most recent listened to albums from the user's listening history |
| // Extract artistIds from listeningHistory | ||
| const artistIds = listeningHistory.map((entry) => entry.artistId); | ||
|
|
||
| // Make a call to Spotify's "Get Recommendations" endpoint |
There was a problem hiding this comment.
Before we can make any calls to Spotify's api we need to authenticate our session. We can follow the same pattern we've done over in the fetchAlbumSpotify file:
dd-prac-team4-back/fetchAlbumSpotify.js
Lines 21 to 23 in 321237c
| } | ||
|
|
||
| // Extract artistIds from listeningHistory | ||
| const artistIds = listeningHistory.map((entry) => entry.artistId); |
There was a problem hiding this comment.
Currently, we aren't storing the spotify artist id in our application. So we will need to first make a call to Spotify to get the artist id.
There is a Get Album by id endpoint: https://developer.spotify.com/documentation/web-api/reference/get-an-album
Our spotify-web-api library that we're using can access it with the getAlbum function: https://github.com/thelinmichael/spotify-web-api-node#more-examples
The album id we need is the spotify album id and not our own Mongo object id. Right now, the only place we're storing that is in the spotifyUrl, we may want to change that later but for now, we can follow a similar pattern as the frontend where we use regex to extract that id from the url field: https://github.com/Code-the-Dream-School/dd-prac-team4-front/blob/40ffb979b0229c5f2485eaecc87e31af55fd3fa4/src/components/album/AlbumPreview.js#L19-L29
An album can have multiple artists, for now just grab the first artist id in the returned list.
| songId: track.id, | ||
| })); | ||
|
|
||
| await AlbumRecommendation.insertMany(recommendations); |
There was a problem hiding this comment.
We will probably want to make AlbumRecommendations have a uniqueIndex so that we don't insert the same recommended song multiple times. And in that case we'll want to add ordered: false so that it will insert all unique entries and return errors for any that were not unique: https://mongoosejs.com/docs/api/model.html#Model.insertMany()
No description provided.