Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit 91c4ec7

Browse files
authored
✨ Suggest tracks heared by friends at exploring tab (#81)
* Updated dependencies * Suggest tracks heared by friends at exploring tab * Changed chances to get a friends track
1 parent e3c9007 commit 91c4ec7

File tree

3 files changed

+167
-110
lines changed

3 files changed

+167
-110
lines changed

app/Http/Controllers/SpotifyController.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -492,16 +492,51 @@ public function renderMoodMeter(): Renderable {
492492
}
493493

494494
public function renderExplore(): Renderable {
495-
$tracks = auth()->user()->spotifyActivity()->select(['track_id'])->groupBy('track_id');
496-
$alreadyRated = SpotifyTrack::whereIn('id', auth()->user()->spotifyRatedTracks()->select(['track_id']))->select('track_id');
497-
$trackToExplore = SpotifyTrack::whereNotIn('track_id', $tracks)
498-
->whereNotIn('track_id', $alreadyRated)
499-
->where('preview_url', '<>', null)
500-
->orderByDesc('popularity')
501-
->first();
495+
$tracks = auth()->user()->spotifyActivity()->select(['track_id'])->groupBy('track_id');
496+
$alreadyRated = SpotifyTrack::whereIn('id', auth()->user()->spotifyRatedTracks()->select(['track_id']))->select('track_id');
497+
498+
$friends = auth()->user()->friends;
499+
500+
$trackToExplore = null;
501+
502+
if($friends->count() > 0 && rand(1, 100) > 40) {
503+
//Use a popular song from a friend
504+
$friend = $friends->random(1)->first();
505+
$friendTopTracks = $friend->spotifyActivity()
506+
->groupBy('track_id')
507+
->select([
508+
'track_id',
509+
DB::raw('COUNT(*) as minutes')
510+
]);
511+
512+
$trackToExplore = SpotifyTrack::joinSub($friendTopTracks, 'friends_top_tracks', function($join) {
513+
$join->on('spotify_tracks.track_id', '=', 'friends_top_tracks.track_id');
514+
})
515+
->whereIn('spotify_tracks.track_id', $friendTopTracks->select('track_id'))
516+
->whereNotIn('spotify_tracks.track_id', $tracks)
517+
->whereNotIn('spotify_tracks.track_id', $alreadyRated)
518+
->where('preview_url', '<>', null)
519+
->orderByDesc('friends_top_tracks.minutes')
520+
->select(['spotify_tracks.*', 'friends_top_tracks.minutes'])
521+
->first();
522+
523+
}
524+
if($trackToExplore !== null) {
525+
$trackReason = 'Dein Freund "' . $friend->username . '" hat diesen Track schon mehr als ' . round(ceil($trackToExplore->minutes / 60)) . ' Stunden gehört.';
526+
} else {
527+
//Use a popular song from trends
528+
$trackToExplore = SpotifyTrack::whereNotIn('track_id', $tracks)
529+
->whereNotIn('track_id', $alreadyRated)
530+
->where('preview_url', '<>', null)
531+
->orderByDesc('popularity')
532+
->first();
533+
$trackReason = 'Dieser Track ist in den Trends weit oben.';
534+
}
535+
502536

503537
return view('spotify.explore.index', [
504538
'track' => $trackToExplore,
539+
'trackReason' => $trackReason,
505540
'exploredToday' => auth()->user()->spotifyLikedTracks()->whereDate('created_at', Carbon::today()->toDateString())->count(),
506541
'exploredTotal' => auth()->user()->spotifyLikedTracks()->count(),
507542
'ratedTotal' => auth()->user()->spotifyRatedTracks()->count()

0 commit comments

Comments
 (0)