mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-11-22 04:58:01 +01:00
Popular tracks are now sorted by 'weight'
This commit is contained in:
parent
e5c9ff3e71
commit
568969f7e5
1 changed files with 53 additions and 8 deletions
|
@ -31,6 +31,7 @@ use Poniverse\Ponyfm\Contracts\Commentable;
|
||||||
use Poniverse\Ponyfm\Contracts\Favouritable;
|
use Poniverse\Ponyfm\Contracts\Favouritable;
|
||||||
use Poniverse\Ponyfm\Contracts\Searchable;
|
use Poniverse\Ponyfm\Contracts\Searchable;
|
||||||
use Poniverse\Ponyfm\Exceptions\TrackFileNotFoundException;
|
use Poniverse\Ponyfm\Exceptions\TrackFileNotFoundException;
|
||||||
|
use Poniverse\Ponyfm\Models\ResourceLogItem;
|
||||||
use Poniverse\Ponyfm\Traits\IndexedInElasticsearchTrait;
|
use Poniverse\Ponyfm\Traits\IndexedInElasticsearchTrait;
|
||||||
use Poniverse\Ponyfm\Traits\SlugTrait;
|
use Poniverse\Ponyfm\Traits\SlugTrait;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
@ -289,11 +290,11 @@ class Track extends Model implements Searchable, Commentable, Favouritable
|
||||||
*/
|
*/
|
||||||
public static function popular($count, $allowExplicit = false, $skip = 0)
|
public static function popular($count, $allowExplicit = false, $skip = 0)
|
||||||
{
|
{
|
||||||
$trackIds = Cache::remember(
|
$trackData = Cache::remember(
|
||||||
'popular_tracks'.$count.'-'.($allowExplicit ? 'explicit' : 'safe'),
|
'popular_tracks'.$count.'-'.($allowExplicit ? 'explicit' : 'safe'),
|
||||||
5,
|
5,
|
||||||
function () use ($allowExplicit, $count, $skip) {
|
function () use ($allowExplicit, $count, $skip) {
|
||||||
$query = static
|
/*$query = static
|
||||||
::published()
|
::published()
|
||||||
->listed()
|
->listed()
|
||||||
->join(
|
->join(
|
||||||
|
@ -315,16 +316,56 @@ class Track extends Model implements Searchable, Commentable, Favouritable
|
||||||
$query->where('is_explicit', false);
|
$query->where('is_explicit', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = [];
|
|
||||||
|
|
||||||
foreach ($query->get(['*', DB::raw('count(*) as plays')]) as $track) {
|
foreach ($query->get(['*', DB::raw('count(*) as plays')]) as $track) {
|
||||||
$results[] = $track->id;
|
$results[] = $track->id;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
$explicitFilter = '
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT id, is_explicit FROM tracks
|
||||||
|
WHERE track_id = id AND is_explicit = TRUE
|
||||||
|
)';
|
||||||
|
|
||||||
|
if ($allowExplicit) {
|
||||||
|
$explicitFilter = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$queryText = '
|
||||||
|
SELECT track_id,
|
||||||
|
SUM(CASE WHEN log_type = 1 THEN 0.1
|
||||||
|
WHEN log_type = 3 THEN 1
|
||||||
|
WHEN log_type = 2 THEN 2
|
||||||
|
ELSE 0 END) AS weight
|
||||||
|
FROM "resource_log_items"
|
||||||
|
WHERE track_id IS NOT NULL AND log_type IS NOT NULL AND "created_at" > now() - INTERVAL \'1\' DAY
|
||||||
|
'.$explicitFilter.'
|
||||||
|
GROUP BY track_id
|
||||||
|
ORDER BY weight DESC
|
||||||
|
LIMIT 30;';
|
||||||
|
|
||||||
|
$countQuery = DB::select(DB::raw($queryText));
|
||||||
|
|
||||||
|
$results = [];
|
||||||
|
|
||||||
|
foreach ($countQuery as $track) {
|
||||||
|
$results[] = [
|
||||||
|
'id' => $track->track_id,
|
||||||
|
'weight' => $track->weight
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$trackIds = [];
|
||||||
|
$trackWeights = [];
|
||||||
|
|
||||||
|
foreach ($trackData as $track) {
|
||||||
|
$trackIds[] = $track['id'];
|
||||||
|
$trackWeights[$track['id']] = $track['weight'];
|
||||||
|
}
|
||||||
|
|
||||||
if (!count($trackIds)) {
|
if (!count($trackIds)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
@ -338,12 +379,16 @@ class Track extends Model implements Searchable, Commentable, Favouritable
|
||||||
|
|
||||||
$processed = [];
|
$processed = [];
|
||||||
foreach ($tracks->get() as $track) {
|
foreach ($tracks->get() as $track) {
|
||||||
$processed[] = Track::mapPublicTrackSummary($track);
|
$trackModel = Track::mapPublicTrackSummary($track);
|
||||||
|
$trackModel['weight'] = $trackWeights[$track->id];
|
||||||
|
$processed[] = $trackModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Songs that get played more should drop down
|
usort($processed, function($a, $b) {
|
||||||
// in the list so they don't hog the top spots.
|
return $a['weight'] <=> $b['weight'];
|
||||||
array_reverse($processed);
|
});
|
||||||
|
|
||||||
|
$processed = array_reverse($processed);
|
||||||
|
|
||||||
return $processed;
|
return $processed;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue