2015-08-30 14:29:12 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-25 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
|
|
|
* Copyright (C) 2015 Peter Deltchev
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-01 01:12:30 +01:00
|
|
|
namespace Poniverse\Ponyfm\Models;
|
2015-08-31 14:35:47 +02:00
|
|
|
|
|
|
|
use Helpers;
|
2015-08-30 14:29:12 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-11-08 18:46:35 +01:00
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
2015-11-10 05:08:37 +01:00
|
|
|
use Auth;
|
|
|
|
use Cache;
|
2015-12-26 03:30:16 +01:00
|
|
|
use Poniverse\Ponyfm\Exceptions\TrackFileNotFoundException;
|
2015-11-10 06:49:12 +01:00
|
|
|
use Poniverse\Ponyfm\Traits\TrackCollection;
|
2015-10-24 03:22:14 +02:00
|
|
|
use Poniverse\Ponyfm\Traits\SlugTrait;
|
2015-11-24 12:07:43 +01:00
|
|
|
use Venturecraft\Revisionable\RevisionableTrait;
|
2015-08-30 14:29:12 +02:00
|
|
|
|
|
|
|
class Playlist extends Model
|
|
|
|
{
|
2015-11-24 12:07:43 +01:00
|
|
|
use SoftDeletes, SlugTrait, DispatchesJobs, TrackCollection, RevisionableTrait;
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2015-11-08 18:46:35 +01:00
|
|
|
protected $table = 'playlists';
|
2015-08-30 14:29:12 +02:00
|
|
|
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
|
|
|
|
public static function summary()
|
|
|
|
{
|
|
|
|
return self::select('id', 'title', 'user_id', 'slug', 'created_at', 'is_public', 'description', 'comment_count',
|
|
|
|
'download_count', 'view_count', 'favourite_count');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeUserDetails($query)
|
|
|
|
{
|
|
|
|
if (Auth::check()) {
|
|
|
|
$query->with([
|
|
|
|
'users' => function ($query) {
|
|
|
|
$query->whereUserId(Auth::user()->id);
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !$query;
|
|
|
|
}
|
|
|
|
|
2015-11-10 05:08:37 +01:00
|
|
|
public static function mapPublicPlaylistShow(Playlist $playlist)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
$tracks = [];
|
|
|
|
foreach ($playlist->tracks as $track) {
|
2015-11-10 05:08:37 +01:00
|
|
|
/** @var $track Track */
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
$tracks[] = Track::mapPublicTrackSummary($track);
|
|
|
|
}
|
|
|
|
|
|
|
|
$formats = [];
|
|
|
|
foreach (Track::$Formats as $name => $format) {
|
|
|
|
$formats[] = [
|
|
|
|
'name' => $name,
|
|
|
|
'extension' => $format['extension'],
|
|
|
|
'url' => $playlist->getDownloadUrl($name),
|
2015-11-08 18:46:35 +01:00
|
|
|
'size' => Helpers::formatBytes($playlist->getFilesize($name)),
|
|
|
|
'isCacheable' => (in_array($name, Track::$CacheableFormats) ? true : false)
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$comments = [];
|
|
|
|
foreach ($playlist->comments as $comment) {
|
|
|
|
$comments[] = Comment::mapPublic($comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = self::mapPublicPlaylistSummary($playlist);
|
|
|
|
$data['tracks'] = $tracks;
|
|
|
|
$data['comments'] = $comments;
|
|
|
|
$data['formats'] = $formats;
|
|
|
|
$data['share'] = [
|
2015-12-20 16:07:36 +01:00
|
|
|
'url' => action('PlaylistsController@getShortlink', ['id' => $playlist->id]),
|
2015-08-30 14:29:12 +02:00
|
|
|
'tumblrUrl' => 'http://www.tumblr.com/share/link?url=' . urlencode($playlist->url) . '&name=' . urlencode($playlist->title) . '&description=' . urlencode($playlist->description),
|
|
|
|
'twitterUrl' => 'https://platform.twitter.com/widgets/tweet_button.html?text=' . $playlist->title . ' by ' . $playlist->user->display_name . ' on Pony.fm'
|
|
|
|
];
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2015-11-10 05:08:37 +01:00
|
|
|
public static function mapPublicPlaylistSummary(Playlist $playlist)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
|
|
|
'views' => 0,
|
|
|
|
'downloads' => 0
|
|
|
|
],
|
|
|
|
'is_favourited' => false
|
|
|
|
];
|
|
|
|
|
|
|
|
if (Auth::check() && $playlist->users->count()) {
|
|
|
|
$userRow = $playlist->users[0];
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
2015-11-08 18:46:35 +01:00
|
|
|
'views' => (int)$userRow->view_count,
|
|
|
|
'downloads' => (int)$userRow->download_count,
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
2015-11-08 18:46:35 +01:00
|
|
|
'is_favourited' => (bool)$userRow->is_favourited
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2015-11-08 18:46:35 +01:00
|
|
|
'id' => (int)$playlist->id,
|
2015-08-30 14:29:12 +02:00
|
|
|
'track_count' => $playlist->track_count,
|
|
|
|
'title' => $playlist->title,
|
|
|
|
'slug' => $playlist->slug,
|
2015-12-26 16:20:31 +01:00
|
|
|
'created_at' => $playlist->created_at->format('c'),
|
2015-11-08 18:46:35 +01:00
|
|
|
'is_public' => (bool)$playlist->is_public,
|
2015-08-30 14:29:12 +02:00
|
|
|
'stats' => [
|
2015-11-08 18:46:35 +01:00
|
|
|
'views' => (int)$playlist->view_count,
|
|
|
|
'downloads' => (int)$playlist->download_count,
|
|
|
|
'comments' => (int)$playlist->comment_count,
|
|
|
|
'favourites' => (int)$playlist->favourite_count
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'covers' => [
|
|
|
|
'small' => $playlist->getCoverUrl(Image::SMALL),
|
2015-12-13 14:42:37 +01:00
|
|
|
'normal' => $playlist->getCoverUrl(Image::NORMAL),
|
|
|
|
'original' => $playlist->getCoverUrl(Image::ORIGINAL)
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'url' => $playlist->url,
|
|
|
|
'user' => [
|
2015-11-08 18:46:35 +01:00
|
|
|
'id' => (int)$playlist->user->id,
|
2015-08-30 14:29:12 +02:00
|
|
|
'name' => $playlist->user->display_name,
|
|
|
|
'url' => $playlist->user->url,
|
|
|
|
],
|
|
|
|
'user_data' => $userData,
|
|
|
|
'permissions' => [
|
|
|
|
'delete' => Auth::check() && Auth::user()->id == $playlist->user_id,
|
|
|
|
'edit' => Auth::check() && Auth::user()->id == $playlist->user_id
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tracks()
|
|
|
|
{
|
|
|
|
return $this
|
2016-01-01 01:12:30 +01:00
|
|
|
->belongsToMany('Poniverse\Ponyfm\Models\Track')
|
2015-08-30 14:29:12 +02:00
|
|
|
->withPivot('position')
|
|
|
|
->withTimestamps()
|
|
|
|
->orderBy('position', 'asc');
|
|
|
|
}
|
|
|
|
|
2015-11-10 06:49:12 +01:00
|
|
|
public function trackFiles()
|
|
|
|
{
|
|
|
|
$trackIds = $this->tracks->lists('id');
|
|
|
|
return TrackFile::whereIn('track_id', $trackIds);
|
|
|
|
}
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
public function users()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\ResourceUser');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function comments()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\Comment')->orderBy('created_at', 'desc');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function pins()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\PinnedPlaylist');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->belongsTo('Poniverse\Ponyfm\Models\User');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasPinFor($userId)
|
|
|
|
{
|
|
|
|
foreach ($this->pins as $pin) {
|
|
|
|
if ($pin->user_id == $userId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canView($user)
|
|
|
|
{
|
|
|
|
return $this->is_public || ($user != null && $user->id == $this->user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUrlAttribute()
|
|
|
|
{
|
2015-12-20 16:07:36 +01:00
|
|
|
return action('PlaylistsController@getPlaylist', ['id' => $this->id, 'slug' => $this->slug]);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDownloadUrl($format)
|
|
|
|
{
|
2015-12-26 04:00:43 +01:00
|
|
|
return action('PlaylistsController@getDownload', ['id' => $this->id, 'format' => Track::$Formats[$format]['extension']]);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilesize($format)
|
|
|
|
{
|
|
|
|
$tracks = $this->tracks;
|
|
|
|
if (!count($tracks)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Cache::remember($this->getCacheKey('filesize-' . $format), 1440, function () use ($tracks, $format) {
|
|
|
|
$size = 0;
|
|
|
|
foreach ($tracks as $track) {
|
2015-11-10 05:08:37 +01:00
|
|
|
/** @var $track Track */
|
|
|
|
|
2015-11-08 18:46:35 +01:00
|
|
|
// Ensure that only downloadable tracks are added onto the file size
|
|
|
|
if ($track->is_downloadable == 1) {
|
2015-11-10 05:08:37 +01:00
|
|
|
try {
|
|
|
|
$size += $track->getFilesize($format);
|
|
|
|
|
|
|
|
} catch (TrackFileNotFoundException $e) {
|
|
|
|
// do nothing - this track won't be included in the download
|
|
|
|
}
|
2015-11-08 18:46:35 +01:00
|
|
|
}
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $size;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCoverUrl($type = Image::NORMAL)
|
|
|
|
{
|
|
|
|
if ($this->tracks->count() == 0) {
|
|
|
|
return $this->user->getAvatarUrl($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->tracks[0]->getCoverUrl($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function pin($userId)
|
|
|
|
{
|
|
|
|
$pin = new PinnedPlaylist();
|
|
|
|
$pin->playlist_id = $this->id;
|
|
|
|
$pin->user_id = $userId;
|
|
|
|
$pin->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getCacheKey($key)
|
|
|
|
{
|
|
|
|
return 'playlist-' . $this->id . '-' . $key;
|
|
|
|
}
|
2015-10-25 06:17:45 +01:00
|
|
|
}
|